diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesGraphTask.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesGraphTask.java index c1c4dff63556c..3ffaad5cb5a1b 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesGraphTask.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/DependenciesGraphTask.java @@ -85,55 +85,46 @@ void generateDependenciesGraph() { for (final Dependency dependency : runtimeDependencies) { final String id = dependency.getGroup() + ":" + dependency.getName(); final String versionedId = id + "@" + dependency.getVersion(); - final StringBuilder packageString = new StringBuilder(); final StringBuilder nodeString = new StringBuilder(); if (dependency instanceof ProjectDependency) { continue; } - packageString.append("{\"id\": \"") - .append(versionedId) - .append("\",\"info\": {\"name\": \"") - .append(id) - .append("\",\"version\": \"") - .append(dependency.getVersion()) - .append("\"}}"); - packages.add(packageString.toString()); - nodeString.append("{\"nodeId\": \"") - .append(versionedId) - .append("\",\"pkgId\": \"") - .append(versionedId) - .append("\",\"deps\": []}"); + packages.add(""" + {"id": "%s","info": {"name": "%s","version": "%s"}}\ + """.formatted(versionedId, id, dependency.getVersion())); + nodeString.append(""" + {"nodeId": "%s","pkgId": "%s","deps": []}\ + """.formatted(versionedId, versionedId)); nodes.add(nodeString.toString()); - nodeIds.add("{\"nodeId\": \"" + versionedId + "\"}"); + nodeIds.add(""" + {"nodeId": "%s"}\ + """.formatted(versionedId)); } // We add one package and one node for each dependency, it suffices to check packages. if (packages.size() > 0) { final String projectName = "elastic/elasticsearch" + getProject().getPath(); - final StringBuilder output = new StringBuilder(); - output.append("{\"depGraph\": {\"schemaVersion\": \"1.2.0\",\"pkgManager\": {\"name\": \"gradle\"},\"pkgs\": [") - .append("{\"id\": \"") - .append(projectName) - .append("@0.0.0") - .append("\", \"info\": {\"name\": \"") - .append(projectName) - .append("\", \"version\": \"0.0.0\"}},") - .append(String.join(",", packages)) - .append("],\"graph\": {\"rootNodeId\": \"") - .append(projectName) - .append("@0.0.0") - .append("\",\"nodes\": [") - .append("{\"nodeId\": \"") - .append(projectName) - .append("@0.0.0") - .append("\",\"pkgId\": \"") - .append(projectName) - .append("@0.0.0") - .append("\",\"deps\": [") - .append(String.join(",", nodeIds)) - .append("]},") - .append(String.join(",", nodes)) - .append("]}}}"); - getLogger().debug("Dependency Graph: " + output.toString()); + final String output = """ + { + "depGraph": { + "schemaVersion": "1.2.0", + "pkgManager": {"name": "gradle"}, + "pkgs": [ + { + "id": "%s@0.0.0", + "info": {"name": "%1$s", "version": "0.0.0"} + }, + %s + ], + "graph": { + "rootNodeId": "%1$s@0.0.0", + "nodes": [ + { "nodeId": "%1$s@0.0.0","pkgId": "%1$s@0.0.0","deps": [%s] }, + %s + ] + } + } + }""".formatted(projectName, String.join(",", packages), String.join(",", nodeIds), String.join(",", nodes)); + getLogger().debug("Dependency Graph: " + output); try (CloseableHttpClient client = HttpClients.createDefault()) { HttpPost postRequest = new HttpPost(url); postRequest.addHeader("Authorization", "token " + token); diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docker/DockerSupportService.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docker/DockerSupportService.java index 552b3bec8674c..4a50f8dd91960 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docker/DockerSupportService.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/docker/DockerSupportService.java @@ -154,8 +154,11 @@ void failIfDockerUnavailable(List tasks) { // Some other problem, print the error final String message = String.format( Locale.ROOT, - "a problem occurred while using Docker from [%s]%s yet it is required to run the following task%s: \n%s\n" - + "the problem is that Docker exited with exit code [%d] with standard error output:\n%s", + """ + a problem occurred while using Docker from [%s]%s yet it is required to run the following task%s: + %s + the problem is that Docker exited with exit code [%d] with standard error output: + %s""", availability.path, availability.version == null ? "" : " v" + availability.version, tasks.size() > 1 ? "s" : "", diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java index f338f0b55d5e5..7b1bb283290db 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/DependencyLicensesTask.java @@ -318,16 +318,11 @@ private void checkSha(File jar, String jarName, Set shaFiles) throws NoSuc String sha = getSha1(jar); if (expectedSha.equals(sha) == false) { - final String exceptionMessage = String.format( - Locale.ROOT, - "SHA has changed! Expected %s for %s but got %s." - + "\nThis usually indicates a corrupt dependency cache or artifacts changed upstream." - + "\nEither wipe your cache, fix the upstream artifact, or delete %s and run updateShas", - expectedSha, - jarName, - sha, - shaFile - ); + final String exceptionMessage = String.format(Locale.ROOT, """ + SHA has changed! Expected %s for %s but got %s. + This usually indicates a corrupt dependency cache or artifacts changed upstream. + Either wipe your cache, fix the upstream artifact, or delete %s and run updateShas + """, expectedSha, jarName, sha, shaFile); throw new GradleException(exceptionMessage); } diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/LicenseAnalyzer.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/LicenseAnalyzer.java index df61f2f84a7fa..0e999484d16e6 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/LicenseAnalyzer.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/LicenseAnalyzer.java @@ -22,68 +22,55 @@ public class LicenseAnalyzer { */ private static final LicenseMatcher[] matchers = new LicenseMatcher[] { new LicenseMatcher("Apache-2.0", true, false, Pattern.compile("Apache.*License.*[vV]ersion.*2\\.0", Pattern.DOTALL)), - new LicenseMatcher( - "BSD-2-Clause", - true, - false, - Pattern.compile( - ("Redistribution and use in source and binary forms, with or without\n" - + "modification, are permitted provided that the following conditions\n" - + "are met:\n" - + "\n" - + " 1\\. Redistributions of source code must retain the above copyright\n" - + " notice, this list of conditions and the following disclaimer\\.\n" - + " 2\\. Redistributions in binary form must reproduce the above copyright\n" - + " notice, this list of conditions and the following disclaimer in the\n" - + " documentation and/or other materials provided with the distribution\\.\n" - + "\n" - + "THIS SOFTWARE IS PROVIDED BY .+ (``|''|\")AS IS(''|\") AND ANY EXPRESS OR\n" - + "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n" - + "OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\.\n" - + "IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT,\n" - + "INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\(INCLUDING, BUT\n" - + "NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" - + "DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\) HOWEVER CAUSED AND ON ANY\n" - + "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" - + "\\(INCLUDING NEGLIGENCE OR OTHERWISE\\) ARISING IN ANY WAY OUT OF THE USE OF\n" - + "THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\.").replaceAll("\\s+", "\\\\s*"), - Pattern.DOTALL - ) - ), - new LicenseMatcher( - "BSD-3-Clause", - true, - false, - Pattern.compile( - ("\n" - + "Redistribution and use in source and binary forms, with or without\n" - + "modification, are permitted provided that the following conditions\n" - + "are met:\n" - + "\n" - + " (1\\.)? Redistributions of source code must retain the above copyright\n" - + " notice, this list of conditions and the following disclaimer\\.\n" - + " (2\\.)? Redistributions in binary form must reproduce the above copyright\n" - + " notice, this list of conditions and the following disclaimer in the\n" - + " documentation and/or other materials provided with the distribution\\.\n" - + " ((3\\.)? The name of .+ may not be used to endorse or promote products\n" - + " derived from this software without specific prior written permission\\.|\n" - + " (3\\.)? Neither the name of .+ nor the names of its\n" - + " contributors may be used to endorse or promote products derived from\n" - + " this software without specific prior written permission\\.)\n" - + "\n" - + "THIS SOFTWARE IS PROVIDED BY .+ (``|''|\")AS IS(''|\") AND ANY EXPRESS OR\n" - + "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n" - + "OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\.\n" - + "IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT,\n" - + "INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\(INCLUDING, BUT\n" - + "NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" - + "DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\) HOWEVER CAUSED AND ON ANY\n" - + "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" - + "\\(INCLUDING NEGLIGENCE OR OTHERWISE\\) ARISING IN ANY WAY OUT OF THE USE OF\n" - + "THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\.\n").replaceAll("\\s+", "\\\\s*"), - Pattern.DOTALL - ) - ), + new LicenseMatcher("BSD-2-Clause", true, false, Pattern.compile((""" + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1\\. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer\\. + 2\\. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution\\. + + THIS SOFTWARE IS PROVIDED BY .+ (``|''|")AS IS(''|") AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\. + IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\(INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + \\(INCLUDING NEGLIGENCE OR OTHERWISE\\) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\.""").replaceAll("\\s+", "\\\\s*"), Pattern.DOTALL)), + new LicenseMatcher("BSD-3-Clause", true, false, Pattern.compile((""" + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + (1\\.)? Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer\\. + (2\\.)? Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution\\. + ((3\\.)? The name of .+ may not be used to endorse or promote products + derived from this software without specific prior written permission\\.| + (3\\.)? Neither the name of .+ nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission\\.) + + THIS SOFTWARE IS PROVIDED BY .+ (``|''|")AS IS(''|") AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED\\. + IN NO EVENT SHALL .+ BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \\(INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION\\) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + \\(INCLUDING NEGLIGENCE OR OTHERWISE\\) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\\. + """).replaceAll("\\s+", "\\\\s*"), Pattern.DOTALL)), new LicenseMatcher( "CDDL-1.0", true, @@ -97,51 +84,40 @@ public class LicenseAnalyzer { Pattern.compile("COMMON DEVELOPMENT AND DISTRIBUTION LICENSE.*Version 1.1", Pattern.DOTALL) ), new LicenseMatcher("ICU", true, false, Pattern.compile("ICU License - ICU 1.8.1 and later", Pattern.DOTALL)), - new LicenseMatcher( - "MIT", - true, - false, - Pattern.compile( - ("\n" - + "Permission is hereby granted, free of charge, to any person obtaining a copy of\n" - + "this software and associated documentation files \\(the \"Software\"\\), to deal in\n" - + "the Software without restriction, including without limitation the rights to\n" - + "use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\n" - + "of the Software, and to permit persons to whom the Software is furnished to do\n" - + "so, subject to the following conditions:\n" - + "\n" - + "The above copyright notice and this permission notice shall be included in all\n" - + "copies or substantial portions of the Software\\.\n" - + "\n" - + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n" - + "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n" - + "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\\. IN NO EVENT SHALL THE\n" - + "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n" - + "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n" - + "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" - + "SOFTWARE\\.\n").replaceAll("\\s+", "\\\\s*"), - Pattern.DOTALL - ) - ), + new LicenseMatcher("MIT", true, false, Pattern.compile((""" + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files \\(the "Software"\\), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do + so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software\\. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\\. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE\\. + """).replaceAll("\\s+", "\\\\s*"), Pattern.DOTALL)), new LicenseMatcher( "MIT-0", true, false, Pattern.compile( - ("MIT No Attribution\n" - + "Copyright .+\n" - + "\n" - + "Permission is hereby granted, free of charge, to any person obtaining a copy of " - + "this software and associated documentation files \\(the \"Software\"\\), to deal in the Software without " - + "restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, " - + "and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.\n" - + "\n" - + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, " - + "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND " - + "NONINFRINGEMENT\\. IN NO EVENT SHALL THE AUTHORS OR " - + "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR " - + "OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n") - .replaceAll("\\s+", "\\\\s*"), + (""" + MIT No Attribution + Copyright .+ + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files \\(the "Software"\\), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\\. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + """) + .replaceAll("\\s+", "\\\\s*"), Pattern.DOTALL ) ), @@ -152,17 +128,10 @@ public class LicenseAnalyzer { new LicenseMatcher("EDL-1.0", true, false, Pattern.compile("Eclipse Distribution License - v 1.0", Pattern.DOTALL)), new LicenseMatcher("LGPL-2.1", true, true, Pattern.compile("GNU LESSER GENERAL PUBLIC LICENSE.*Version 2.1", Pattern.DOTALL)), new LicenseMatcher("LGPL-3.0", true, true, Pattern.compile("GNU LESSER GENERAL PUBLIC LICENSE.*Version 3", Pattern.DOTALL)), - new LicenseMatcher( - "GeoLite", - false, - false, - Pattern.compile( - ("The Elastic GeoIP Database Service uses the GeoLite2 Data created " - + "and licensed by MaxMind,\nwhich is governed by MaxMind’s GeoLite2 End User License Agreement, " - + "available at https://www.maxmind.com/en/geolite2/eula.\n").replaceAll("\\s+", "\\\\s*"), - Pattern.DOTALL - ) - ), + new LicenseMatcher("GeoLite", false, false, Pattern.compile((""" + The Elastic GeoIP Database Service uses the GeoLite2 Data created and licensed by MaxMind, + which is governed by MaxMind’s GeoLite2 End User License Agreement, available at https://www.maxmind.com/en/geolite2/eula. + """).replaceAll("\\s+", "\\\\s*"), Pattern.DOTALL)), new LicenseMatcher( "GeoIp-Database-Service", false, diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java index 6aa9be9e378b3..cd3f0b9c03cb2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/BulkProcessorIT.java @@ -212,16 +212,14 @@ public void testBulkProcessorWaitOnClose() throws Exception { public void testBulkProcessorConcurrentRequestsReadOnlyIndex() throws Exception { Request request = new Request("PUT", "/test-ro"); - request.setJsonEntity( - "{\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"blocks.write\" : true\n" - + " }\n" - + " }\n" - + " \n" - + "}" - ); + request.setJsonEntity(""" + { + "settings": { + "index": { + "blocks.write": true + } + } + }"""); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java index c391fb485e1ca..8e2f584a897eb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ClusterClientIT.java @@ -380,7 +380,8 @@ public void testRemoteInfo() throws Exception { public void testComponentTemplates() throws Exception { String templateName = "my-template"; Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); - CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}"); + CompressedXContent mappings = new CompressedXContent(""" + {"properties":{"host_name":{"type":"keyword"}}}"""); AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); Template template = new Template(settings, mappings, Map.of("alias", alias)); ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java index cbda7f0aaf36c..3e375f01d2217 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java @@ -180,7 +180,8 @@ public void testExists() throws IOException { assertFalse(execute(getRequest, highLevelClient()::exists, highLevelClient()::existsAsync)); } IndexRequest index = new IndexRequest("index").id("id"); - index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON); + index.source(""" + {"field1":"value1","field2":"value2"}""", XContentType.JSON); index.setRefreshPolicy(RefreshPolicy.IMMEDIATE); highLevelClient().index(index, RequestOptions.DEFAULT); { @@ -205,7 +206,8 @@ public void testDeprecatedSourceExists() throws IOException { assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); } IndexRequest index = new IndexRequest("index").id("id"); - index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON); + index.source(""" + {"field1":"value1","field2":"value2"}""", XContentType.JSON); index.setRefreshPolicy(RefreshPolicy.IMMEDIATE); highLevelClient().index(index, RequestOptions.DEFAULT); { @@ -228,7 +230,8 @@ public void testSourceExists() throws IOException { assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync)); } IndexRequest index = new IndexRequest("index").id("id"); - index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON); + index.source(""" + {"field1":"value1","field2":"value2"}""", XContentType.JSON); index.setRefreshPolicy(RefreshPolicy.IMMEDIATE); highLevelClient().index(index, RequestOptions.DEFAULT); { @@ -283,7 +286,8 @@ public void testGet() throws IOException { assertEquals("index", exception.getMetadata("es.index").get(0)); } IndexRequest index = new IndexRequest("index").id("id"); - String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}"; + String document = """ + {"field1":"value1","field2":"value2"}"""; index.source(document, XContentType.JSON); index.setRefreshPolicy(RefreshPolicy.IMMEDIATE); highLevelClient().index(index, RequestOptions.DEFAULT); @@ -423,7 +427,8 @@ public void testGetSource() throws IOException { assertEquals("index", exception.getMetadata("es.index").get(0)); } IndexRequest index = new IndexRequest("index").id("id"); - String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}"; + String document = """ + {"field1":"value1","field2":"value2"}"""; index.source(document, XContentType.JSON); index.setRefreshPolicy(RefreshPolicy.IMMEDIATE); highLevelClient().index(index, RequestOptions.DEFAULT); @@ -1073,7 +1078,8 @@ public void testTermvectors() throws IOException { { // prepare : index docs Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); - String mappings = "\"properties\":{\"field\":{\"type\":\"text\"}}"; + String mappings = """ + "properties":{"field":{"type":"text"}}"""; createIndex(sourceIndex, settings, mappings); assertEquals( RestStatus.OK, @@ -1163,7 +1169,9 @@ public void testMultiTermvectors() throws IOException { { // prepare : index docs Settings settings = Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0).build(); - String mappings = "\"properties\":{\"field\":{\"type\":\"text\"}, \"field2\":{\"type\":\"text\"}}"; + String mappings = """ + "properties":{"field":{"type":"text"}, "field2":{"type":"text"}} + """; createIndex(sourceIndex, settings, mappings); assertEquals( RestStatus.OK, diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java index 79bef5f9a86df..17e0944c9cab0 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java @@ -92,27 +92,28 @@ protected void assertEqualInstances(GetAliasesResponse expectedInstance, GetAlia } public void testFromXContentWithElasticsearchException() throws IOException { - String xContent = "{" - + " \"error\": {" - + " \"root_cause\": [" - + " {" - + " \"type\": \"index_not_found_exception\"," - + " \"reason\": \"no such index [index]\"," - + " \"resource.type\": \"index_or_alias\"," - + " \"resource.id\": \"index\"," - + " \"index_uuid\": \"_na_\"," - + " \"index\": \"index\"" - + " }" - + " ]," - + " \"type\": \"index_not_found_exception\"," - + " \"reason\": \"no such index [index]\"," - + " \"resource.type\": \"index_or_alias\"," - + " \"resource.id\": \"index\"," - + " \"index_uuid\": \"_na_\"," - + " \"index\": \"index\"" - + " }," - + " \"status\": 404" - + "}"; + String xContent = """ + { + "error": { + "root_cause": [ + { + "type": "index_not_found_exception", + "reason": "no such index [index]", + "resource.type": "index_or_alias", + "resource.id": "index", + "index_uuid": "_na_", + "index": "index" + } + ], + "type": "index_not_found_exception", + "reason": "no such index [index]", + "resource.type": "index_or_alias", + "resource.id": "index", + "index_uuid": "_na_", + "index": "index" + }, + "status": 404 + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { GetAliasesResponse getAliasesResponse = GetAliasesResponse.fromXContent(parser); @@ -126,7 +127,11 @@ public void testFromXContentWithElasticsearchException() throws IOException { } public void testFromXContentWithNoAliasFound() throws IOException { - String xContent = "{" + " \"error\": \"alias [aa] missing\"," + " \"status\": 404" + "}"; + String xContent = """ + { + "error": "alias [aa] missing", + "status": 404 + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { GetAliasesResponse getAliasesResponse = GetAliasesResponse.fromXContent(parser); assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND)); @@ -136,15 +141,16 @@ public void testFromXContentWithNoAliasFound() throws IOException { } public void testFromXContentWithMissingAndFoundAlias() throws IOException { - String xContent = "{" - + " \"error\": \"alias [something] missing\"," - + " \"status\": 404," - + " \"index\": {" - + " \"aliases\": {" - + " \"alias\": {}" - + " }" - + " }" - + "}"; + String xContent = """ + { + "error": "alias [something] missing", + "status": 404, + "index": { + "aliases": { + "alias": {} + } + } + }"""; final String index = "index"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) { GetAliasesResponse response = GetAliasesResponse.fromXContent(parser); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java index 5f589030ae92d..7737387503743 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/GraphIT.java @@ -31,23 +31,28 @@ public class GraphIT extends ESRestHighLevelClientTestCase { public void indexDocuments() throws IOException { // Create chain of doc IDs across indices 1->2->3 Request doc1 = new Request(HttpPut.METHOD_NAME, "/index1/_doc/1"); - doc1.setJsonEntity("{ \"num\":[1], \"const\":\"start\"}"); + doc1.setJsonEntity(""" + { "num":[1], "const":"start"}"""); client().performRequest(doc1); Request doc2 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/1"); - doc2.setJsonEntity("{\"num\":[1,2], \"const\":\"foo\"}"); + doc2.setJsonEntity(""" + {"num":[1,2], "const":"foo"}"""); client().performRequest(doc2); Request doc3 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/2"); - doc3.setJsonEntity("{\"num\":[2,3], \"const\":\"foo\"}"); + doc3.setJsonEntity(""" + {"num":[2,3], "const":"foo"}"""); client().performRequest(doc3); Request doc4 = new Request(HttpPut.METHOD_NAME, "/index_no_field_data/_doc/2"); - doc4.setJsonEntity("{\"num\":\"string\", \"const\":\"foo\"}"); + doc4.setJsonEntity(""" + {"num":"string", "const":"foo"}"""); client().performRequest(doc4); Request doc5 = new Request(HttpPut.METHOD_NAME, "/index_no_field_data/_doc/2"); - doc5.setJsonEntity("{\"num\":[2,4], \"const\":\"foo\"}"); + doc5.setJsonEntity(""" + {"num":[2,4], "const":"foo"}"""); client().performRequest(doc5); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java index e9657bf7d0d14..e762bd585ff15 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientCompressionIT.java @@ -20,7 +20,8 @@ public class HighLevelRestClientCompressionIT extends ESRestHighLevelClientTestCase { private static final String GZIP_ENCODING = "gzip"; - private static final String SAMPLE_DOCUMENT = "{\"name\":{\"first name\":\"Steve\",\"last name\":\"Jobs\"}}"; + private static final String SAMPLE_DOCUMENT = """ + {"name":{"first name":"Steve","last name":"Jobs"}}"""; public void testCompressesResponseIfRequested() throws IOException { Request doc = new Request(HttpPut.METHOD_NAME, "/company/_doc/1"); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java index 01eaa6aee7e70..9b4a57e30abe1 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/HighLevelRestClientFilterPathIT.java @@ -18,7 +18,8 @@ public class HighLevelRestClientFilterPathIT extends ESRestHighLevelClientTestCase { - private static final String SAMPLE_DOCUMENT = "{\"name\":{\"first name\":\"Steve\",\"last name\":\"Jobs\"}}"; + private static final String SAMPLE_DOCUMENT = """ + {"name":{"first name":"Steve","last name":"Jobs"}}"""; private static final String FILTER_PATH_PARAM = "filter_path"; private static final String FILTER_PATH_PARAM_VALUE = "-hits.hits._index,-hits.hits._type,-hits.hits.matched_queries"; 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 0201c813b0bb3..c171a36a7c63e 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 @@ -357,7 +357,9 @@ public void testGetSettingsWithDefaultsFiltered() throws IOException { public void testGetIndex() throws IOException { String indexName = "get_index_test"; Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build(); - String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}"; + String mappings = """ + "properties":{"field-1":{"type":"integer"}} + """; createIndex(indexName, basicSettings, mappings); GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(false); @@ -376,7 +378,8 @@ public void testGetIndex() throws IOException { MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName); assertNotNull(mappingMetadata); assertEquals("_doc", mappingMetadata.type()); - assertEquals("{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}", mappingMetadata.source().string()); + assertEquals(""" + {"properties":{"field-1":{"type":"integer"}}}""", mappingMetadata.source().string()); Object o = mappingMetadata.getSourceAsMap().get("properties"); assertThat(o, instanceOf(Map.class)); // noinspection unchecked @@ -390,7 +393,9 @@ public void testGetIndex() throws IOException { public void testGetIndexWithDefaults() throws IOException { String indexName = "get_index_test"; Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build(); - String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}"; + String mappings = """ + "properties":{"field-1":{"type":"integer"}} + """; createIndex(indexName, basicSettings, mappings); GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(true); @@ -520,7 +525,8 @@ public void testGetFieldMapping() throws IOException { final GetFieldMappingsResponse.FieldMappingMetadata metadata = new GetFieldMappingsResponse.FieldMappingMetadata( "field", - new BytesArray("{\"field\":{\"type\":\"text\"}}") + new BytesArray(""" + {"field":{"type":"text"}}""") ); assertThat(fieldMappingMap, equalTo(Collections.singletonMap("field", metadata))); } @@ -1067,7 +1073,9 @@ public void testRollover() throws IOException { assertEquals("test_new", rolloverResponse.getNewIndex()); } { - String mappings = "{\"properties\":{\"field2\":{\"type\":\"keyword\"}}}"; + String mappings = """ + {"properties":{"field2":{"type":"keyword"}}} + """; rolloverRequest.getCreateIndexRequest().mapping(mappings, XContentType.JSON); rolloverRequest.dryRun(false); rolloverRequest.addMaxIndexSizeCondition(new ByteSizeValue(1, ByteSizeUnit.MB)); @@ -1514,18 +1522,16 @@ public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception { PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "name-*")).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")); @@ -1607,17 +1613,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\"" - + "}" - ); + postDoc.setJsonEntity(""" + { + "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.queryStringQuery("line_id:foo").lenient(false); @@ -1860,7 +1865,8 @@ public void testDeleteAlias() throws IOException { public void testDataStreams() throws Exception { String dataStreamName = "data-stream"; - CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"@timestamp\":{\"type\":\"date\"}}}"); + CompressedXContent mappings = new CompressedXContent(""" + {"properties":{"@timestamp":{"type":"date"}}}"""); Template template = new Template(null, mappings, null); ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate( Collections.singletonList(dataStreamName), @@ -1952,7 +1958,8 @@ public void testDataStreams() throws Exception { public void testIndexTemplates() throws Exception { String templateName = "my-template"; Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); - CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}"); + CompressedXContent mappings = new CompressedXContent(""" + {"properties":{"host_name":{"type":"keyword"}}}"""); AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); Template template = new Template(settings, mappings, Map.of("alias", alias)); List pattern = List.of("pattern"); @@ -2028,7 +2035,8 @@ public void testIndexTemplates() throws Exception { public void testSimulateIndexTemplate() throws Exception { String templateName = "my-template"; Settings settings = Settings.builder().put("index.number_of_shards", 1).build(); - CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}"); + CompressedXContent mappings = new CompressedXContent(""" + {"properties":{"host_name":{"type":"keyword"}}}"""); AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build(); Template template = new Template(settings, mappings, Map.of("alias", alias)); List pattern = List.of("pattern"); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java index 9c3357c8fa10d..b000df421cc7e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesRequestConvertersTests.java @@ -826,14 +826,9 @@ public void testPutTemplateRequest() throws Exception { } Map expectedParams = new HashMap<>(); if (ESTestCase.randomBoolean()) { - putTemplateRequest.mapping( - "{ \"properties\": { \"field-" - + ESTestCase.randomInt() - + "\" : { \"type\" : \"" - + ESTestCase.randomFrom("text", "keyword") - + "\" }}}", - XContentType.JSON - ); + putTemplateRequest.mapping(""" + { "properties": { "field-%s" : { "type" : "%s" }}} + """.formatted(ESTestCase.randomInt(), ESTestCase.randomFrom("text", "keyword")), XContentType.JSON); } if (ESTestCase.randomBoolean()) { putTemplateRequest.alias(new Alias("alias-" + ESTestCase.randomInt())); 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 e087eb13a4219..7441dcfa3d9bd 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 @@ -82,29 +82,30 @@ 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/MLRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java index 68339e482ce40..6d35f60107bdb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java @@ -98,6 +98,7 @@ import org.elasticsearch.client.ml.job.config.MlFilterTests; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.SearchModule; import org.elasticsearch.test.ESTestCase; @@ -204,10 +205,8 @@ public void testCloseJob() throws Exception { request = MLRequestConverters.closeJob(closeJobRequest); assertEquals("/_ml/anomaly_detectors/" + jobId + ",otherjobs*/_close", request.getEndpoint()); - assertEquals( - "{\"job_id\":\"somejobid,otherjobs*\",\"timeout\":\"10m\",\"force\":true,\"allow_no_match\":false}", - requestEntityToString(request) - ); + assertEquals(""" + {"job_id":"somejobid,otherjobs*","timeout":"10m","force":true,"allow_no_match":false}""", requestEntityToString(request)); } public void testDeleteExpiredData() throws Exception { @@ -225,12 +224,13 @@ public void testDeleteExpiredData() throws Exception { String expectedPath = jobId == null ? "/_ml/_delete_expired_data" : "/_ml/_delete_expired_data/" + jobId; assertEquals(expectedPath, request.getEndpoint()); if (jobId == null) { - assertEquals("{\"requests_per_second\":" + requestsPerSec + ",\"timeout\":\"1h\"}", requestEntityToString(request)); + assertEquals(""" + {"requests_per_second":%s,"timeout":"1h"}\ + """.formatted(requestsPerSec), requestEntityToString(request)); } else { - assertEquals( - "{\"job_id\":\"" + jobId + "\",\"requests_per_second\":" + requestsPerSec + ",\"timeout\":\"1h\"}", - requestEntityToString(request) - ); + assertEquals(""" + {"job_id":"%s","requests_per_second":%s,"timeout":"1h"}\ + """.formatted(jobId, requestsPerSec), requestEntityToString(request)); } } @@ -270,13 +270,15 @@ public void testFlushJob() throws Exception { flushJobRequest.setAdvanceTime("100"); flushJobRequest.setCalcInterim(true); request = MLRequestConverters.flushJob(flushJobRequest); - assertEquals( - "{\"job_id\":\"" - + jobId - + "\",\"calc_interim\":true,\"start\":\"105\"," - + "\"end\":\"200\",\"advance_time\":\"100\",\"skip_time\":\"1000\"}", - requestEntityToString(request) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "job_id": "%s", + "calc_interim": true, + "start": "105", + "end": "200", + "advance_time": "100", + "skip_time": "1000" + }""".formatted(jobId)), requestEntityToString(request)); } public void testForecastJob() throws Exception { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java index 0ff75f9aa762f..98b55032e956e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningGetResultsIT.java @@ -96,27 +96,25 @@ private void addBucketIndexRequest(long timestamp, boolean isInterim, BulkReques IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); double bucketScore = randomDoubleBetween(0.0, 100.0, true); bucketStats.report(bucketScore); - indexRequest.source( - "{\"job_id\":\"" - + JOB_ID - + "\", \"result_type\":\"bucket\", \"timestamp\": " - + timestamp - + "," - + "\"bucket_span\": 3600,\"is_interim\": " - + isInterim - + ", \"anomaly_score\": " - + bucketScore - + ", \"bucket_influencers\":[{\"job_id\": \"" - + JOB_ID - + "\", \"result_type\":\"bucket_influencer\", " - + "\"influencer_field_name\": \"bucket_time\", \"timestamp\": " - + timestamp - + ", \"bucket_span\": 3600, " - + "\"is_interim\": " - + isInterim - + "}]}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "result_type": "bucket", + "timestamp": %s, + "bucket_span": 3600, + "is_interim": %s, + "anomaly_score": %s, + "bucket_influencers": [ + { + "job_id": "%s", + "result_type": "bucket_influencer", + "influencer_field_name": "bucket_time", + "timestamp": %s, + "bucket_span": 3600, + "is_interim": %s + } + ] + }""".formatted(JOB_ID, timestamp, isInterim, bucketScore, JOB_ID, timestamp, isInterim), XContentType.JSON); bulkRequest.add(indexRequest); } @@ -135,40 +133,32 @@ private void addRecordIndexRequest(long timestamp, boolean isInterim, BulkReques double recordScore = randomDoubleBetween(0.0, 100.0, true); recordStats.report(recordScore); double p = randomDoubleBetween(0.0, 0.05, false); - indexRequest.source( - "{\"job_id\":\"" - + JOB_ID - + "\", \"result_type\":\"record\", \"timestamp\": " - + timestamp - + "," - + "\"bucket_span\": 3600,\"is_interim\": " - + isInterim - + ", \"record_score\": " - + recordScore - + ", \"probability\": " - + p - + "}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "result_type": "record", + "timestamp": %s, + "bucket_span": 3600, + "is_interim": %s, + "record_score": %s, + "probability": %s + }""".formatted(JOB_ID, timestamp, isInterim, recordScore, p), XContentType.JSON); bulkRequest.add(indexRequest); } private void addCategoryIndexRequest(long categoryId, String categoryName, BulkRequest bulkRequest) { IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); - indexRequest.source( - "{\"job_id\":\"" - + JOB_ID - + "\", \"category_id\": " - + categoryId - + ", \"terms\": \"" - + categoryName - + "\", \"regex\": \".*?" - + categoryName - + ".*\", \"max_matching_length\": 3, \"examples\": [\"" - + categoryName - + "\"]}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "category_id": %s, + "terms": "%s", + "regex": ".*?%s.*", + "max_matching_length": 3, + "examples": [ + "%s" + ] + }""".formatted(JOB_ID, categoryId, categoryName, categoryName, categoryName), XContentType.JSON); bulkRequest.add(indexRequest); } @@ -1114,20 +1104,17 @@ public void testGetOverallBuckets() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (Bucket bucket : firstBuckets) { + String anomalyScore = String.valueOf(bucket.getAnomalyScore() + 10.0); IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); - indexRequest.source( - "{\"job_id\":\"" - + anotherJobId - + "\", \"result_type\":\"bucket\", \"timestamp\": " - + bucket.getTimestamp().getTime() - + "," - + "\"bucket_span\": 3600,\"is_interim\": " - + bucket.isInterim() - + ", \"anomaly_score\": " - + String.valueOf(bucket.getAnomalyScore() + 10.0) - + "}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "result_type": "bucket", + "timestamp": %s, + "bucket_span": 3600, + "is_interim": %s, + "anomaly_score": %s + }""".formatted(anotherJobId, bucket.getTimestamp().getTime(), bucket.isInterim(), anomalyScore), XContentType.JSON); bulkRequest.add(indexRequest); } highLevelClient().bulk(bulkRequest, RequestOptions.DEFAULT); @@ -1317,22 +1304,18 @@ public void testGetInfluencers() throws IOException { double score = isLast ? 90.0 : 42.0; IndexRequest indexRequest = new IndexRequest(RESULTS_INDEX); - indexRequest.source( - "{\"job_id\":\"" - + JOB_ID - + "\", \"result_type\":\"influencer\", \"timestamp\": " - + timestamp - + "," - + "\"bucket_span\": 3600,\"is_interim\": " - + isInterim - + ", \"influencer_score\": " - + score - + ", " - + "\"influencer_field_name\":\"my_influencer\", \"influencer_field_value\": \"inf_1\", \"probability\":" - + randomDouble() - + "}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "result_type": "influencer", + "timestamp": %s, + "bucket_span": 3600, + "is_interim": %s, + "influencer_score": %s, + "influencer_field_name": "my_influencer", + "influencer_field_value": "inf_1", + "probability": %s + }""".formatted(JOB_ID, timestamp, isInterim, score, randomDouble()), XContentType.JSON); bulkRequest.add(indexRequest); timestamp += 3600000L; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java index c28e0cdf08e72..8ea7109edcb39 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java @@ -2728,24 +2728,26 @@ public void testGetTrainedModelsStats() throws Exception { putTrainedModel(modelId); } - String regressionPipeline = "{" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"target_field\": \"regression_value\",\n" - + " \"model_id\": \"" - + modelIdPrefix - + 0 - + "\",\n" - + " \"inference_config\": {\"regression\": {}},\n" - + " \"field_map\": {\n" - + " \"col1\": \"col1\",\n" - + " \"col2\": \"col2\",\n" - + " \"col3\": \"col3\",\n" - + " \"col4\": \"col4\"\n" - + " }\n" - + " }\n" - + " }]}\n"; + String regressionPipeline = """ + { + "processors": [ + { + "inference": { + "target_field": "regression_value", + "model_id": "%s%s", + "inference_config": { + "regression": {} + }, + "field_map": { + "col1": "col1", + "col2": "col2", + "col3": "col3", + "col4": "col4" + } + } + } + ] + }""".formatted(modelIdPrefix, "0"); String pipelineId = "regression-stats-pipeline"; highLevelClient().ingest() @@ -3116,25 +3118,30 @@ public void createModelSnapshot(String jobId, String snapshotId) throws IOExcept IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"" - + jobId - + "\", \"timestamp\":1541587919000, " - + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " - + "\"snapshot_id\":\"" - + snapshotId - + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" - + "\"job_id\":\"" - + jobId - + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " - + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," - + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " - + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," - + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, \"min_version\":\"" - + Version.CURRENT.toString() - + "\"}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "timestamp": 1541587919000, + "description": "State persisted due to job close at 2018-11-07T10:51:59+0000", + "snapshot_id": "%s", + "snapshot_doc_count": 1, + "model_size_stats": { + "job_id": "%s", + "result_type": "model_size_stats", + "model_bytes": 51722, + "total_by_field_count": 3, + "total_over_field_count": 0, + "total_partition_field_count": 2, + "bucket_allocation_failures_count": 0, + "memory_status": "ok", + "log_time": 1541587919000, + "timestamp": 1519930800000 + }, + "latest_record_time_stamp": 1519931700000, + "latest_result_time_stamp": 1519930800000, + "retain": false, + "min_version": "%s" + }""".formatted(jobId, snapshotId, jobId, Version.CURRENT.toString()), XContentType.JSON); highLevelClient().index(indexRequest, RequestOptions.DEFAULT); } @@ -3147,27 +3154,34 @@ public void createModelSnapshots(String jobId, List snapshotIds) throws String documentId = jobId + "_model_snapshot_" + snapshotId; IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"" - + jobId - + "\", \"timestamp\":1541587919000, " - + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " - + "\"snapshot_id\":\"" - + snapshotId - + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" - + "\"job_id\":\"" - + jobId - + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " - + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," - + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " - + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," - + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " - + "\"quantiles\":{\"job_id\":\"" - + jobId - + "\", \"timestamp\":1541587919000, " - + "\"quantile_state\":\"state\"}}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "timestamp": 1541587919000, + "description": "State persisted due to job close at 2018-11-07T10:51:59+0000", + "snapshot_id": "%s", + "snapshot_doc_count": 1, + "model_size_stats": { + "job_id": "%s", + "result_type": "model_size_stats", + "model_bytes": 51722, + "total_by_field_count": 3, + "total_over_field_count": 0, + "total_partition_field_count": 2, + "bucket_allocation_failures_count": 0, + "memory_status": "ok", + "log_time": 1541587919000, + "timestamp": 1519930800000 + }, + "latest_record_time_stamp": 1519931700000, + "latest_result_time_stamp": 1519930800000, + "retain": false, + "quantiles": { + "job_id": "%s", + "timestamp": 1541587919000, + "quantile_state": "state" + } + }""".formatted(jobId, snapshotId, jobId, jobId), XContentType.JSON); highLevelClient().index(indexRequest, RequestOptions.DEFAULT); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java index 1343e9952ae43..6450865aea0a4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RestHighLevelClientExtTests.java @@ -41,14 +41,16 @@ public void initClient() { public void testParseEntityCustomResponseSection() throws IOException { { - HttpEntity jsonEntity = new NStringEntity("{\"custom1\":{ \"field\":\"value\"}}", ContentType.APPLICATION_JSON); + HttpEntity jsonEntity = new NStringEntity(""" + {"custom1":{ "field":"value"}}""", ContentType.APPLICATION_JSON); BaseCustomResponseSection customSection = restHighLevelClient.parseEntity(jsonEntity, BaseCustomResponseSection::fromXContent); assertThat(customSection, instanceOf(CustomResponseSection1.class)); CustomResponseSection1 customResponseSection1 = (CustomResponseSection1) customSection; assertEquals("value", customResponseSection1.value); } { - HttpEntity jsonEntity = new NStringEntity("{\"custom2\":{ \"array\": [\"item1\", \"item2\"]}}", ContentType.APPLICATION_JSON); + HttpEntity jsonEntity = new NStringEntity(""" + {"custom2":{ "array": ["item1", "item2"]}}""", ContentType.APPLICATION_JSON); BaseCustomResponseSection customSection = restHighLevelClient.parseEntity(jsonEntity, BaseCustomResponseSection::fromXContent); assertThat(customSection, instanceOf(CustomResponseSection2.class)); CustomResponseSection2 customResponseSection2 = (CustomResponseSection2) customSection; 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 2fce3daffbdc5..e316c971b7738 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 @@ -112,99 +112,112 @@ public class SearchIT extends ESRestHighLevelClientTestCase { public void indexDocuments() throws IOException { { Request doc1 = new Request(HttpPut.METHOD_NAME, "/index/_doc/1"); - doc1.setJsonEntity("{\"type\":\"type1\", \"id\":1, \"num\":10, \"num2\":50}"); + doc1.setJsonEntity(""" + {"type":"type1", "id":1, "num":10, "num2":50}"""); client().performRequest(doc1); Request doc2 = new Request(HttpPut.METHOD_NAME, "/index/_doc/2"); - doc2.setJsonEntity("{\"type\":\"type1\", \"id\":2, \"num\":20, \"num2\":40}"); + doc2.setJsonEntity(""" + {"type":"type1", "id":2, "num":20, "num2":40}"""); client().performRequest(doc2); Request doc3 = new Request(HttpPut.METHOD_NAME, "/index/_doc/3"); - doc3.setJsonEntity("{\"type\":\"type1\", \"id\":3, \"num\":50, \"num2\":35}"); + doc3.setJsonEntity(""" + {"type":"type1", "id":3, "num":50, "num2":35}"""); client().performRequest(doc3); Request doc4 = new Request(HttpPut.METHOD_NAME, "/index/_doc/4"); - doc4.setJsonEntity("{\"type\":\"type2\", \"id\":4, \"num\":100, \"num2\":10}"); + doc4.setJsonEntity(""" + {"type":"type2", "id":4, "num":100, "num2":10}"""); client().performRequest(doc4); Request doc5 = new Request(HttpPut.METHOD_NAME, "/index/_doc/5"); - doc5.setJsonEntity("{\"type\":\"type2\", \"id\":5, \"num\":100, \"num2\":10}"); + doc5.setJsonEntity(""" + {"type":"type2", "id":5, "num":100, "num2":10}"""); client().performRequest(doc5); } { Request doc1 = new Request(HttpPut.METHOD_NAME, "/index1/_doc/1"); - doc1.setJsonEntity("{\"id\":1, \"field\":\"value1\", \"rating\": 7}"); + doc1.setJsonEntity(""" + {"id":1, "field":"value1", "rating": 7}"""); client().performRequest(doc1); Request doc2 = new Request(HttpPut.METHOD_NAME, "/index1/_doc/2"); - doc2.setJsonEntity("{\"id\":2, \"field\":\"value2\"}"); + doc2.setJsonEntity(""" + {"id":2, "field":"value2"}"""); client().performRequest(doc2); } { Request create = new Request("PUT", "/index2"); - create.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"rating\": {" - + " \"type\": \"keyword\"" - + " }" - + " }" - + " }" - + "}" - ); + create.setJsonEntity(""" + { + "mappings": { + "properties": { + "rating": { + "type": "keyword" + } + } + } + }"""); client().performRequest(create); Request doc3 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/3"); - doc3.setJsonEntity("{\"id\":3, \"field\":\"value1\", \"rating\": \"good\"}"); + doc3.setJsonEntity(""" + {"id":3, "field":"value1", "rating": "good"}"""); client().performRequest(doc3); Request doc4 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/4"); - doc4.setJsonEntity("{\"id\":4, \"field\":\"value2\"}"); + doc4.setJsonEntity(""" + {"id":4, "field":"value2"}"""); client().performRequest(doc4); } { Request doc5 = new Request(HttpPut.METHOD_NAME, "/index3/_doc/5"); - doc5.setJsonEntity("{\"id\":5, \"field\":\"value1\"}"); + doc5.setJsonEntity(""" + {"id":5, "field":"value1"}"""); client().performRequest(doc5); Request doc6 = new Request(HttpPut.METHOD_NAME, "/index3/_doc/6"); - doc6.setJsonEntity("{\"id\":6, \"field\":\"value2\"}"); + doc6.setJsonEntity(""" + {"id":6, "field":"value2"}"""); client().performRequest(doc6); } { Request create = new Request(HttpPut.METHOD_NAME, "/index4"); - create.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"field1\": {" - + " \"type\": \"keyword\"," - + " \"store\": true" - + " }," - + " \"field2\": {" - + " \"type\": \"keyword\"," - + " \"store\": true" - + " }" - + " }" - + " }" - + "}" - ); + create.setJsonEntity(""" + { + "mappings": { + "properties": { + "field1": { + "type": "keyword", + "store": true + }, + "field2": { + "type": "keyword", + "store": true + } + } + } + }"""); client().performRequest(create); Request doc1 = new Request(HttpPut.METHOD_NAME, "/index4/_doc/1"); - doc1.setJsonEntity("{\"id\":1, \"field1\":\"value1\", \"field2\":\"value2\"}"); + doc1.setJsonEntity(""" + {"id":1, "field1":"value1", "field2":"value2"}"""); client().performRequest(doc1); Request createFilteredAlias = new Request(HttpPost.METHOD_NAME, "/_aliases"); - createFilteredAlias.setJsonEntity( - "{" - + " \"actions\" : [" - + " {" - + " \"add\" : {" - + " \"index\" : \"index4\"," - + " \"alias\" : \"alias4\"," - + " \"filter\" : { \"term\" : { \"field2\" : \"value1\" } }" - + " }" - + " }" - + " ]" - + "}" - ); + createFilteredAlias.setJsonEntity(""" + { + "actions": [ + { + "add": { + "index": "index4", + "alias": "alias4", + "filter": { + "term": { + "field2": "value1" + } + } + } + } + ] + }"""); client().performRequest(createFilteredAlias); } @@ -493,68 +506,62 @@ public void testSearchWithMatrixStats() throws IOException { public void testSearchWithParentJoin() throws IOException { final String indexName = "child_example"; Request createIndex = new Request(HttpPut.METHOD_NAME, "/" + indexName); - createIndex.setJsonEntity( - "{\n" - + " \"mappings\": {\n" - + " \"properties\" : {\n" - + " \"qa_join_field\" : {\n" - + " \"type\" : \"join\",\n" - + " \"relations\" : { \"question\" : \"answer\" }\n" - + " }\n" - + " }\n" - + " }" - + "}" - ); + createIndex.setJsonEntity(""" + { + "mappings": { + "properties": { + "qa_join_field": { + "type": "join", + "relations": { + "question": "answer" + } + } + } + } + }"""); client().performRequest(createIndex); Request questionDoc = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/1"); - questionDoc.setJsonEntity( - "{\n" - + " \"body\": \"

I have Windows 2003 server and i bought a new Windows 2008 server...\",\n" - + " \"title\": \"Whats the best way to file transfer my site from server to a newer one?\",\n" - + " \"tags\": [\n" - + " \"windows-server-2003\",\n" - + " \"windows-server-2008\",\n" - + " \"file-transfer\"\n" - + " ],\n" - + " \"qa_join_field\" : \"question\"\n" - + "}" - ); + questionDoc.setJsonEntity(""" + { + "body": "

I have Windows 2003 server and i bought a new Windows 2008 server...", + "title": "Whats the best way to file transfer my site from server to a newer one?", + "tags": [ "windows-server-2003", "windows-server-2008", "file-transfer" ], + "qa_join_field": "question" + }"""); client().performRequest(questionDoc); Request answerDoc1 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/2"); answerDoc1.addParameter("routing", "1"); - answerDoc1.setJsonEntity( - "{\n" - + " \"owner\": {\n" - + " \"location\": \"Norfolk, United Kingdom\",\n" - + " \"display_name\": \"Sam\",\n" - + " \"id\": 48\n" - + " },\n" - + " \"body\": \"

Unfortunately you're pretty much limited to FTP...\",\n" - + " \"qa_join_field\" : {\n" - + " \"name\" : \"answer\",\n" - + " \"parent\" : \"1\"\n" - + " },\n" - + " \"creation_date\": \"2009-05-04T13:45:37.030\"\n" - + "}" - ); + answerDoc1.setJsonEntity(""" + { + "owner": { + "location": "Norfolk, United Kingdom", + "display_name": "Sam", + "id": 48 + }, + "body": "

Unfortunately you're pretty much limited to FTP...", + "qa_join_field": { + "name": "answer", + "parent": "1" + }, + "creation_date": "2009-05-04T13:45:37.030" + }"""); client().performRequest(answerDoc1); Request answerDoc2 = new Request(HttpPut.METHOD_NAME, "/" + indexName + "/_doc/3"); answerDoc2.addParameter("routing", "1"); - answerDoc2.setJsonEntity( - "{\n" - + " \"owner\": {\n" - + " \"location\": \"Norfolk, United Kingdom\",\n" - + " \"display_name\": \"Troll\",\n" - + " \"id\": 49\n" - + " },\n" - + " \"body\": \"

Use Linux...\",\n" - + " \"qa_join_field\" : {\n" - + " \"name\" : \"answer\",\n" - + " \"parent\" : \"1\"\n" - + " },\n" - + " \"creation_date\": \"2009-05-05T13:45:37.030\"\n" - + "}" - ); + answerDoc2.setJsonEntity(""" + { + "owner": { + "location": "Norfolk, United Kingdom", + "display_name": "Troll", + "id": 49 + }, + "body": "

Use Linux...", + "qa_join_field": { + "name": "answer", + "parent": "1" + }, + "creation_date": "2009-05-05T13:45:37.030" + }"""); client().performRequest(answerDoc2); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); @@ -948,7 +955,8 @@ 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); @@ -1037,7 +1045,8 @@ 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); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java index de7bb7253159f..657a81e3a19b2 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TextStructureIT.java @@ -20,13 +20,12 @@ public class TextStructureIT extends ESRestHighLevelClientTestCase { public void testFindFileStructure() throws IOException { - - String sample = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151445," - + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + String sample = """ + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 1",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 2",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + """; TextStructureClient textStructureClient = highLevelClient().textStructure(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java index 3f35abe0eab76..5ab7a02e696ca 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java @@ -73,11 +73,12 @@ public void testPutWatch() throws Exception { assertThat(putWatchResponse.getVersion(), is(1L)); } - private static final String WATCH_JSON = "{ \n" - + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" - + " \"input\": { \"none\": {} },\n" - + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" - + "}"; + private static final String WATCH_JSON = """ + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "none": {} }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""; private PutWatchResponse createWatch(String watchId) throws Exception { BytesReference bytesReference = new BytesArray(WATCH_JSON); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java index 2a13be0cb7a4e..3f862fd51f240 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java @@ -247,11 +247,12 @@ public void testExecuteWatchByIdRequest() throws IOException { } - private static final String WATCH_JSON = "{ \n" - + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" - + " \"input\": { \"none\": {} },\n" - + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" - + "}"; + private static final String WATCH_JSON = """ + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "none": {} }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""; public void testExecuteInlineWatchRequest() throws IOException { boolean ignoreCondition = randomBoolean(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java index ae982a2e927cd..5cd10e5e353c9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/analytics/InferenceAggIT.java @@ -104,7 +104,8 @@ private void putTrainedModel(String modelId, List inputFields, Tree tree private void indexData(String index) throws IOException { CreateIndexRequest create = new CreateIndexRequest(index); - create.mapping("{\"properties\": {\"fruit\": {\"type\": \"keyword\"}," + "\"cost\": {\"type\": \"double\"}}}", XContentType.JSON); + create.mapping(""" + {"properties": {"fruit": {"type": "keyword"}, "cost": {"type": "double"}}}""", XContentType.JSON); highLevelClient().indices().create(create, RequestOptions.DEFAULT); BulkRequest bulk = new BulkRequest(index).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); bulk.add(new IndexRequest().source(XContentType.JSON, "fruit", "apple", "cost", "1.2")); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java index d6bf4c04eee0e..9bcdcc72a02c8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java @@ -145,11 +145,13 @@ public void testIndex() throws Exception { //tag::index-request-string IndexRequest request = new IndexRequest("posts"); // <1> request.id("1"); // <2> - String jsonString = "{" + - "\"user\":\"kimchy\"," + - "\"postDate\":\"2013-01-30\"," + - "\"message\":\"trying out Elasticsearch\"" + - "}"; + String jsonString = """ + { + "user": "kimchy", + "postDate": "2013-01-30", + "message": "trying out Elasticsearch" + } + """; request.source(jsonString, XContentType.JSON); // <3> //end::index-request-string @@ -787,17 +789,19 @@ public void onFailure(Exception e) { public void testReindex() throws Exception { RestHighLevelClient client = highLevelClient(); { - String mapping = " \"properties\": {\n" - + " \"user\": {\n" - + " \"type\": \"text\"\n" - + " },\n" - + " \"field1\": {\n" - + " \"type\": \"integer\"\n" - + " },\n" - + " \"field2\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }"; + String mapping = """ + "properties": { + "user": { + "type": "text" + }, + "field1": { + "type": "integer" + }, + "field2": { + "type": "integer" + } + } + """; createIndex("source1", Settings.EMPTY, mapping); createIndex("source2", Settings.EMPTY, mapping); createPipeline("my_pipeline"); @@ -987,17 +991,19 @@ public void onFailure(Exception e) { public void testUpdateByQuery() throws Exception { RestHighLevelClient client = highLevelClient(); { - String mapping = " \"properties\": {\n" - + " \"user\": {\n" - + " \"type\": \"text\"\n" - + " },\n" - + " \"field1\": {\n" - + " \"type\": \"integer\"\n" - + " },\n" - + " \"field2\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }"; + String mapping = """ + "properties": { + "user": { + "type": "text" + }, + "field1": { + "type": "integer" + }, + "field2": { + "type": "integer" + } + } + """; createIndex("source1", Settings.EMPTY, mapping); createIndex("source2", Settings.EMPTY, mapping); createPipeline("my_pipeline"); @@ -1110,17 +1116,18 @@ public void onFailure(Exception e) { public void testDeleteByQuery() throws Exception { RestHighLevelClient client = highLevelClient(); { - String mapping = " \"properties\": {\n" - + " \"user\": {\n" - + " \"type\": \"text\"\n" - + " },\n" - + " \"field1\": {\n" - + " \"type\": \"integer\"\n" - + " },\n" - + " \"field2\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }"; + String mapping = """ + "properties": { + "user": { + "type": "text" + }, + "field1": { + "type": "integer" + }, + "field2": { + "type": "integer" + } + }"""; createIndex("source1", Settings.EMPTY, mapping); createIndex("source2", Settings.EMPTY, mapping); } @@ -1222,18 +1229,17 @@ public void testGet() throws Exception { RestHighLevelClient client = highLevelClient(); { Request createIndex = new Request("PUT", "/posts"); - createIndex.setJsonEntity( - "{\n" - + " \"mappings\" : {\n" - + " \"properties\" : {\n" - + " \"message\" : {\n" - + " \"type\": \"text\",\n" - + " \"store\": true\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + createIndex.setJsonEntity(""" + { + "mappings": { + "properties": { + "message": { + "type": "text", + "store": true + } + } + } + }"""); Response response = client().performRequest(createIndex); assertEquals(200, response.getStatusLine().getStatusCode()); @@ -1394,18 +1400,17 @@ public void testGetSource() throws Exception { RestHighLevelClient client = highLevelClient(); { Request createIndex = new Request("PUT", "/posts"); - createIndex.setJsonEntity( - "{\n" - + " \"mappings\" : {\n" - + " \"properties\" : {\n" - + " \"message\" : {\n" - + " \"type\": \"text\",\n" - + " \"store\": true\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + createIndex.setJsonEntity(""" + { + "mappings": { + "properties": { + "message": { + "type": "text", + "store": true + } + } + } + }"""); Response response = client().performRequest(createIndex); assertEquals(200, response.getStatusLine().getStatusCode()); @@ -1845,18 +1850,17 @@ public void testMultiGet() throws Exception { { Request createIndex = new Request("PUT", "/index"); - createIndex.setJsonEntity( - "{\n" - + " \"mappings\" : {\n" - + " \"properties\" : {\n" - + " \"foo\" : {\n" - + " \"type\": \"text\",\n" - + " \"store\": true\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + createIndex.setJsonEntity(""" + { + "mappings": { + "properties": { + "foo": { + "type": "text", + "store": true + } + } + } + }"""); Response response = client().performRequest(createIndex); assertEquals(200, response.getStatusLine().getStatusCode()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java index 44b8e272804dd..6d284fecec74b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/ClusterClientDocumentationIT.java @@ -547,13 +547,14 @@ public void testPutComponentTemplate() throws Exception { .put("index.number_of_shards", 3) .put("index.number_of_replicas", 1) .build(); - String mappingJson = "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}"; + String mappingJson = """ + { + "properties": { + "message": { + "type": "text" + } + } + }"""; AliasMetadata twitterAlias = AliasMetadata.builder("twitter_alias").build(); Template template = new Template(settings, new CompressedXContent(mappingJson), Map.of("twitter_alias", twitterAlias)); // <2> @@ -626,13 +627,14 @@ public void testDeleteComponentTemplate() throws Exception { PutComponentTemplateRequest request = new PutComponentTemplateRequest().name("ct1"); Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build(); - String mappingJson = "{\n" - + " \"properties\": {\n" - + " \"message\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + "}"; + String mappingJson = """ + { + "properties": { + "message": { + "type": "text" + } + } + }"""; AliasMetadata twitterAlias = AliasMetadata.builder("twitter_alias").build(); Template template = new Template(settings, new CompressedXContent(mappingJson), Map.of("twitter_alias", twitterAlias)); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java index 771ec47fbe5ff..64506d6d26427 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/GraphDocumentationIT.java @@ -33,11 +33,13 @@ public class GraphDocumentationIT extends ESRestHighLevelClientTestCase { public void indexDocuments() throws IOException { // Create chain of doc IDs across indices 1->2->3 Request doc1 = new Request(HttpPut.METHOD_NAME, "/index1/_doc/1"); - doc1.setJsonEntity("{ \"participants\":[1,2], \"text\":\"let's start projectx\", \"attachment_md5\":\"324FHDGHFDG4564\"}"); + doc1.setJsonEntity(""" + {"participants":[1,2], "text":"let's start projectx", "attachment_md5":"324FHDGHFDG4564"}"""); client().performRequest(doc1); Request doc2 = new Request(HttpPut.METHOD_NAME, "/index2/_doc/2"); - doc2.setJsonEntity("{\"participants\":[2,3,4], \"text\":\"got something you both may be interested in\"}"); + doc2.setJsonEntity(""" + {"participants":[2,3,4], "text":"got something you both may be interested in"}"""); client().performRequest(doc2); client().performRequest(new Request(HttpPost.METHOD_NAME, "/_refresh")); 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 b321e92e71196..83d7240ee5772 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 @@ -308,13 +308,14 @@ public void testCreateIndex() throws IOException { { // tag::create-index-request-mappings request.mapping(// <1> - "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}", // <2> + """ + { + "properties": { + "message": { + "type": "text" + } + } + }""", // <2> XContentType.JSON); // end::create-index-request-mappings CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); @@ -380,20 +381,21 @@ public void testCreateIndex() throws IOException { request = new CreateIndexRequest("twitter6"); // tag::create-index-whole-source - request.source("{\n" + - " \"settings\" : {\n" + - " \"number_of_shards\" : 1,\n" + - " \"number_of_replicas\" : 0\n" + - " },\n" + - " \"mappings\" : {\n" + - " \"properties\" : {\n" + - " \"message\" : { \"type\" : \"text\" }\n" + - " }\n" + - " },\n" + - " \"aliases\" : {\n" + - " \"twitter_alias\" : {}\n" + - " }\n" + - "}", XContentType.JSON); // <1> + request.source(""" + { + "settings" : { + "number_of_shards" : 1, + "number_of_replicas" : 0 + }, + "mappings" : { + "properties" : { + "message" : { "type" : "text" } + } + }, + "aliases" : { + "twitter_alias" : {} + } + }""", XContentType.JSON); // <1> // end::create-index-whole-source // tag::create-index-execute @@ -459,13 +461,14 @@ public void testPutMapping() throws IOException { { // tag::put-mapping-request-source request.source( - "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}", // <1> + """ + { + "properties": { + "message": { + "type": "text" + } + } + }""", // <1> XContentType.JSON); // end::put-mapping-request-source AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); @@ -680,16 +683,17 @@ public void testGetFieldMapping() throws IOException, InterruptedException { assertTrue(createIndexResponse.isAcknowledged()); PutMappingRequest request = new PutMappingRequest("twitter"); request.source( - "{\n" - + " \"properties\": {\n" - + " \"message\": {\n" - + " \"type\": \"text\"\n" - + " },\n" - + " \"timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + "}", // <1> + """ + { + "properties": { + "message": { + "type": "text" + }, + "timestamp": { + "type": "date" + } + } + }""", // <1> XContentType.JSON ); AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); @@ -1112,7 +1116,8 @@ public void testGetIndex() throws Exception { { Settings settings = Settings.builder().put("number_of_shards", 3).build(); - String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}"; + String mappings = """ + {"properties":{"field-1":{"type":"integer"}}}"""; CreateIndexRequest createIndexRequest = new CreateIndexRequest("index").settings(settings).mapping(mappings, XContentType.JSON); CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); @@ -1828,7 +1833,9 @@ public void testRolloverIndex() throws Exception { .put("index.number_of_shards", 4)); // <1> // end::rollover-index-request-settings // tag::rollover-index-request-mapping - String mappings = "{\"properties\":{\"field-1\":{\"type\":\"keyword\"}}}"; + String mappings = """ + {"properties":{"field-1":{"type":"keyword"}}} + """; request.getCreateIndexRequest().mapping(mappings, XContentType.JSON); // <1> // end::rollover-index-request-mapping // tag::rollover-index-request-alias @@ -2007,9 +2014,9 @@ public void testIndexPutSettings() throws Exception { } { // tag::indices-put-settings-settings-source - request.settings( - "{\"index.number_of_replicas\": \"2\"}" - , XContentType.JSON); // <1> + request.settings(""" + {"index.number_of_replicas": "2"} + """, XContentType.JSON); // <1> // end::indices-put-settings-settings-source } @@ -2086,13 +2093,14 @@ public void testPutTemplate() throws Exception { { // tag::put-template-request-mappings-json request.mapping(// <1> - "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}", + """ + { + "properties": { + "message": { + "type": "text" + } + } + }""", XContentType.JSON); // end::put-template-request-mappings-json assertTrue(client.indices().putTemplate(request, LEGACY_TEMPLATE_OPTIONS).isAcknowledged()); @@ -2148,27 +2156,28 @@ public void testPutTemplate() throws Exception { // end::put-template-request-version // tag::put-template-whole-source - request.source("{\n" + - " \"index_patterns\": [\n" + - " \"log-*\",\n" + - " \"pattern-1\"\n" + - " ],\n" + - " \"order\": 1,\n" + - " \"settings\": {\n" + - " \"number_of_shards\": 1\n" + - " },\n" + - " \"mappings\": {\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - " },\n" + - " \"aliases\": {\n" + - " \"alias-1\": {},\n" + - " \"{index}-alias\": {}\n" + - " }\n" + - "}", XContentType.JSON); // <1> + request.source(""" + { + "index_patterns": [ + "log-*", + "pattern-1" + ], + "order": 1, + "settings": { + "number_of_shards": 1 + }, + "mappings": { + "properties": { + "message": { + "type": "text" + } + } + }, + "aliases": { + "alias-1": {}, + "{index}-alias": {} + } + }""", XContentType.JSON); // <1> // end::put-template-whole-source // tag::put-template-request-create @@ -2367,13 +2376,14 @@ public void testPutIndexTemplateV2() throws Exception { { // tag::put-index-template-v2-request-mappings-json - String mappingJson = "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}"; // <1> + String mappingJson = """ + { + "properties": { + "message": { + "type": "text" + } + } + }"""; // <1> PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest() .name("my-template"); Template template = new Template(null, new CompressedXContent(mappingJson), null); // <2> diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java index d252c88a6677c..51d8ef58db4b8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IngestClientDocumentationIT.java @@ -62,9 +62,19 @@ public void testPutPipeline() throws IOException { { // tag::put-pipeline-request - String source = - "{\"description\":\"my set of processors\"," + - "\"processors\":[{\"set\":{\"field\":\"foo\",\"value\":\"bar\"}}]}"; + String source = """ + { + "description": "my set of processors", + "processors": [ + { + "set": { + "field": "foo", + "value": "bar" + } + } + ] + } + """; PutPipelineRequest request = new PutPipelineRequest( "my-pipeline-id", // <1> new BytesArray(source.getBytes(StandardCharsets.UTF_8)), // <2> @@ -97,8 +107,11 @@ public void testPutPipelineAsync() throws Exception { RestHighLevelClient client = highLevelClient(); { - String source = "{\"description\":\"my set of processors\"," - + "\"processors\":[{\"set\":{\"field\":\"foo\",\"value\":\"bar\"}}]}"; + String source = """ + { + "description": "my set of processors", + "processors": [ { "set": { "field": "foo", "value": "bar" } } ] + }"""; PutPipelineRequest request = new PutPipelineRequest( "my-pipeline-id", new BytesArray(source.getBytes(StandardCharsets.UTF_8)), @@ -278,17 +291,29 @@ public void testSimulatePipeline() throws IOException { { // tag::simulate-pipeline-request - String source = - "{\"" + - "pipeline\":{" + - "\"description\":\"_description\"," + - "\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]" + - "}," + - "\"docs\":[" + - "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"bar\"}}," + - "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}" + - "]" + - "}"; + String source = """ + { + "pipeline": { + "description": "_description", + "processors": [ { "set": { "field": "field2", "value": "_value" } } ] + }, + "docs": [ + { + "_index": "index", + "_id": "id", + "_source": { + "foo": "bar" + } + }, + { + "_index": "index", + "_id": "id", + "_source": { + "foo": "rab" + } + } + ] + }"""; SimulatePipelineRequest request = new SimulatePipelineRequest( new BytesArray(source.getBytes(StandardCharsets.UTF_8)), // <1> XContentType.JSON // <2> @@ -335,16 +360,29 @@ public void testSimulatePipelineAsync() throws Exception { RestHighLevelClient client = highLevelClient(); { - String source = "{\"" - + "pipeline\":{" - + "\"description\":\"_description\"," - + "\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]" - + "}," - + "\"docs\":[" - + "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"bar\"}}," - + "{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}" - + "]" - + "}"; + String source = """ + { + "pipeline": { + "description": "_description", + "processors": [ { "set": { "field": "field2", "value": "_value" } } ] + }, + "docs": [ + { + "_index": "index", + "_id": "id", + "_source": { + "foo": "bar" + } + }, + { + "_index": "index", + "_id": "id", + "_source": { + "foo": "rab" + } + } + ] + }"""; SimulatePipelineRequest request = new SimulatePipelineRequest( new BytesArray(source.getBytes(StandardCharsets.UTF_8)), XContentType.JSON diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java index 9b1c930d30744..14f9c97b8db68 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/LicensingDocumentationIT.java @@ -64,16 +64,26 @@ public void rollbackToTrial() throws IOException { public void testLicense() throws Exception { RestHighLevelClient client = highLevelClient(); - String license = "{\"license\": {\"uid\":\"893361dc-9749-4997-93cb-802e3d7fa4a8\",\"type\":\"gold\"," - + "\"issue_date_in_millis\":1411948800000,\"expiry_date_in_millis\":1914278399999,\"max_nodes\":1,\"issued_to\":\"issued_to\"," - + "\"issuer\":\"issuer\",\"signature\":\"AAAAAgAAAA3U8+YmnvwC+CWsV/mRAAABmC9ZN0hjZDBGYnVyRXpCOW5Bb3FjZDAxOWpSbTVoMVZwUzRxVk1PSm" - + "kxakxZdW5IMlhlTHNoN1N2MXMvRFk4d3JTZEx3R3RRZ0pzU3lobWJKZnQvSEFva0ppTHBkWkprZWZSQi9iNmRQNkw1SlpLN0lDalZCS095MXRGN1lIZlpYcVVTTn" - + "FrcTE2dzhJZmZrdFQrN3JQeGwxb0U0MXZ0dDJHSERiZTVLOHNzSDByWnpoZEphZHBEZjUrTVBxRENNSXNsWWJjZllaODdzVmEzUjNiWktNWGM5TUhQV2plaUo4Q1" - + "JOUml4MXNuL0pSOEhQaVB2azhmUk9QVzhFeTFoM1Q0RnJXSG53MWk2K055c28zSmRnVkF1b2JSQkFLV2VXUmVHNDZ2R3o2VE1qbVNQS2lxOHN5bUErZlNIWkZSVm" - + "ZIWEtaSU9wTTJENDVvT1NCYklacUYyK2FwRW9xa0t6dldMbmMzSGtQc3FWOTgzZ3ZUcXMvQkt2RUZwMFJnZzlvL2d2bDRWUzh6UG5pdENGWFRreXNKNkE9PQAAAQ" - + "Be8GfzDm6T537Iuuvjetb3xK5dvg0K5NQapv+rczWcQFxgCuzbF8plkgetP1aAGZP4uRESDQPMlOCsx4d0UqqAm9f7GbBQ3l93P+PogInPFeEH9NvOmaAQovmxVM" - + "9SE6DsDqlX4cXSO+bgWpXPTd2LmpoQc1fXd6BZ8GeuyYpVHVKp9hVU0tAYjw6HzYOE7+zuO1oJYOxElqy66AnIfkvHrvni+flym3tE7tDTgsDRaz7W3iBhaqiSnt" - + "EqabEkvHdPHQdSR99XGaEvnHO1paK01/35iZF6OXHsF7CCj+558GRXiVxzueOe7TsGSSt8g7YjZwV9bRCyU7oB4B/nidgI\"}}"; + String license = """ + { + "license": { + "uid": "893361dc-9749-4997-93cb-802e3d7fa4a8", + "type": "gold", + "issue_date_in_millis": 1411948800000, + "expiry_date_in_millis": 1914278399999, + "max_nodes": 1, + "issued_to": "issued_to", + "issuer": "issuer", + "signature": "AAAAAgAAAA3U8+YmnvwC+CWsV/mRAAABmC9ZN0hjZDBGYnVyRXpCOW5Bb3FjZDAxOWpSbTVoMVZwUzRxVk1PSmkxakxZdW5IMlhlTHNoN1N2\ + MXMvRFk4d3JTZEx3R3RRZ0pzU3lobWJKZnQvSEFva0ppTHBkWkprZWZSQi9iNmRQNkw1SlpLN0lDalZCS095MXRGN1lIZlpYcVVTTnFrcTE2dzhJZmZrdFQrN3JQeG\ + wxb0U0MXZ0dDJHSERiZTVLOHNzSDByWnpoZEphZHBEZjUrTVBxRENNSXNsWWJjZllaODdzVmEzUjNiWktNWGM5TUhQV2plaUo4Q1JOUml4MXNuL0pSOEhQaVB2azhm\ + Uk9QVzhFeTFoM1Q0RnJXSG53MWk2K055c28zSmRnVkF1b2JSQkFLV2VXUmVHNDZ2R3o2VE1qbVNQS2lxOHN5bUErZlNIWkZSVmZIWEtaSU9wTTJENDVvT1NCYklacU\ + YyK2FwRW9xa0t6dldMbmMzSGtQc3FWOTgzZ3ZUcXMvQkt2RUZwMFJnZzlvL2d2bDRWUzh6UG5pdENGWFRreXNKNkE9PQAAAQBe8GfzDm6T537Iuuvjetb3xK5dvg0K\ + 5NQapv+rczWcQFxgCuzbF8plkgetP1aAGZP4uRESDQPMlOCsx4d0UqqAm9f7GbBQ3l93P+PogInPFeEH9NvOmaAQovmxVM9SE6DsDqlX4cXSO+bgWpXPTd2LmpoQc1\ + fXd6BZ8GeuyYpVHVKp9hVU0tAYjw6HzYOE7+zuO1oJYOxElqy66AnIfkvHrvni+flym3tE7tDTgsDRaz7W3iBhaqiSntEqabEkvHdPHQdSR99XGaEvnHO1paK01/35\ + iZF6OXHsF7CCj+558GRXiVxzueOe7TsGSSt8g7YjZwV9bRCyU7oB4B/nidgI" + } + }"""; { //tag::put-license-execute PutLicenseRequest request = new PutLicenseRequest(); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java index 98b1001a5b2d8..eea8cd2223e7e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java @@ -1207,11 +1207,15 @@ public void testGetBuckets() throws IOException, InterruptedException { // Let us index a bucket IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"test-get-buckets\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," - + "\"bucket_span\": 600,\"is_interim\": false, \"anomaly_score\": 80.0}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-get-buckets", + "result_type": "bucket", + "timestamp": 1533081600000, + "bucket_span": 600, + "is_interim": false, + "anomaly_score": 80 + }""", XContentType.JSON); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -1588,20 +1592,28 @@ public void testGetOverallBuckets() throws IOException, InterruptedException { { IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); - indexRequest.source( - "{\"job_id\":\"test-get-overall-buckets-1\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," - + "\"bucket_span\": 600,\"is_interim\": false, \"anomaly_score\": 60.0}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-get-overall-buckets-1", + "result_type": "bucket", + "timestamp": 1533081600000, + "bucket_span": 600, + "is_interim": false, + "anomaly_score": 60 + }""", XContentType.JSON); bulkRequest.add(indexRequest); } { IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); - indexRequest.source( - "{\"job_id\":\"test-get-overall-buckets-2\", \"result_type\":\"bucket\", \"timestamp\": 1533081600000," - + "\"bucket_span\": 3600,\"is_interim\": false, \"anomaly_score\": 100.0}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-get-overall-buckets-2", + "result_type": "bucket", + "timestamp": 1533081600000, + "bucket_span": 3600, + "is_interim": false, + "anomaly_score": 100 + }""", XContentType.JSON); bulkRequest.add(indexRequest); } @@ -1689,11 +1701,15 @@ public void testGetRecords() throws IOException, InterruptedException { // Let us index a record IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"test-get-records\", \"result_type\":\"record\", \"timestamp\": 1533081600000," - + "\"bucket_span\": 600,\"is_interim\": false, \"record_score\": 80.0}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-get-records", + "result_type": "record", + "timestamp": 1533081600000, + "bucket_span": 600, + "is_interim": false, + "record_score": 80 + }""", XContentType.JSON); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -1858,12 +1874,17 @@ public void testGetInfluencers() throws IOException, InterruptedException { // Let us index a record IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"test-get-influencers\", \"result_type\":\"influencer\", \"timestamp\": 1533081600000," - + "\"bucket_span\": 600,\"is_interim\": false, \"influencer_score\": 80.0, \"influencer_field_name\": \"my_influencer\"," - + "\"influencer_field_value\":\"foo\"}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-get-influencers", + "result_type": "influencer", + "timestamp": 1533081600000, + "bucket_span": 600, + "is_interim": false, + "influencer_score": 80, + "influencer_field_name": "my_influencer", + "influencer_field_value": "foo" + }""", XContentType.JSON); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2080,23 +2101,29 @@ public void testDeleteModelSnapshot() throws IOException, InterruptedException { // Let us index a snapshot IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"" - + jobId - + "\", \"timestamp\":1541587919000, " - + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " - + "\"snapshot_id\":\"" - + snapshotId - + "\", \"snapshot_doc_count\":1, \"model_size_stats\":{" - + "\"job_id\":\"" - + jobId - + "\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " - + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," - + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " - + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," - + "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "%s", + "timestamp": 1541587919000, + "description": "State persisted due to job close at 2018-11-07T10:51:59+0000", + "snapshot_id": "%s", + "snapshot_doc_count": 1, + "model_size_stats": { + "job_id": "%s", + "result_type": "model_size_stats", + "model_bytes": 51722, + "total_by_field_count": 3, + "total_over_field_count": 0, + "total_partition_field_count": 2, + "bucket_allocation_failures_count": 0, + "memory_status": "ok", + "log_time": 1541587919000, + "timestamp": 1519930800000 + }, + "latest_record_time_stamp": 1519931700000, + "latest_result_time_stamp": 1519930800000, + "retain": false + }""".formatted(jobId, snapshotId, jobId), XContentType.JSON); { client.index(indexRequest, RequestOptions.DEFAULT); @@ -2155,17 +2182,29 @@ public void testGetModelSnapshots() throws IOException, InterruptedException { // Let us index a snapshot IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared"); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"test-get-model-snapshots\", \"timestamp\":1541587919000, " - + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " - + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" - + "\"job_id\":\"test-get-model-snapshots\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " - + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," - + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " - + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," - + "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-get-model-snapshots", + "timestamp": 1541587919000, + "description": "State persisted due to job close at 2018-11-07T10:51:59+0000", + "snapshot_id": "1541587919", + "snapshot_doc_count": 1, + "model_size_stats": { + "job_id": "test-get-model-snapshots", + "result_type": "model_size_stats", + "model_bytes": 51722, + "total_by_field_count": 3, + "total_over_field_count": 0, + "total_partition_field_count": 2, + "bucket_allocation_failures_count": 0, + "memory_status": "ok", + "log_time": 1541587919000, + "timestamp": 1519930800000 + }, + "latest_record_time_stamp": 1519931700000, + "latest_result_time_stamp": 1519930800000, + "retain": false + }""", XContentType.JSON); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2256,19 +2295,34 @@ public void testRevertModelSnapshot() throws IOException, InterruptedException { String documentId = jobId + "_model_snapshot_" + snapshotId; IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " - + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " - + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" - + "\"job_id\":\"test-revert-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " - + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," - + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " - + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," - + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " - + "\"quantiles\":{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " - + "\"quantile_state\":\"state\"}}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-revert-model-snapshot", + "timestamp": 1541587919000, + "description": "State persisted due to job close at 2018-11-07T10:51:59+0000", + "snapshot_id": "1541587919", + "snapshot_doc_count": 1, + "model_size_stats": { + "job_id": "test-revert-model-snapshot", + "result_type": "model_size_stats", + "model_bytes": 51722, + "total_by_field_count": 3, + "total_over_field_count": 0, + "total_partition_field_count": 2, + "bucket_allocation_failures_count": 0, + "memory_status": "ok", + "log_time": 1541587919000, + "timestamp": 1519930800000 + }, + "latest_record_time_stamp": 1519931700000, + "latest_result_time_stamp": 1519930800000, + "retain": false, + "quantiles": { + "job_id": "test-revert-model-snapshot", + "timestamp": 1541587919000, + "quantile_state": "state" + } + }""", XContentType.JSON); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2334,19 +2388,34 @@ public void testUpgradeJobSnapshot() throws IOException, InterruptedException { String documentId = jobId + "_model_snapshot_" + snapshotId; IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"test-upgrade-job-model-snapshot\", \"timestamp\":1541587919000, " - + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " - + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" - + "\"job_id\":\"test-revert-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " - + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," - + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " - + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," - + "\"latest_result_time_stamp\":1519930800000, \"retain\":false, " - + "\"quantiles\":{\"job_id\":\"test-revert-model-snapshot\", \"timestamp\":1541587919000, " - + "\"quantile_state\":\"state\"}}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-upgrade-job-model-snapshot", + "timestamp": 1541587919000, + "description": "State persisted due to job close at 2018-11-07T10:51:59+0000", + "snapshot_id": "1541587919", + "snapshot_doc_count": 1, + "model_size_stats": { + "job_id": "test-revert-model-snapshot", + "result_type": "model_size_stats", + "model_bytes": 51722, + "total_by_field_count": 3, + "total_over_field_count": 0, + "total_partition_field_count": 2, + "bucket_allocation_failures_count": 0, + "memory_status": "ok", + "log_time": 1541587919000, + "timestamp": 1519930800000 + }, + "latest_record_time_stamp": 1519931700000, + "latest_result_time_stamp": 1519930800000, + "retain": false, + "quantiles": { + "job_id": "test-revert-model-snapshot", + "timestamp": 1541587919000, + "quantile_state": "state" + } + }""", XContentType.JSON); client.index(indexRequest, RequestOptions.DEFAULT); { @@ -2415,17 +2484,29 @@ public void testUpdateModelSnapshot() throws IOException, InterruptedException { // Let us index a snapshot IndexRequest indexRequest = new IndexRequest(".ml-anomalies-shared").id(documentId); indexRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); - indexRequest.source( - "{\"job_id\":\"test-update-model-snapshot\", \"timestamp\":1541587919000, " - + "\"description\":\"State persisted due to job close at 2018-11-07T10:51:59+0000\", " - + "\"snapshot_id\":\"1541587919\", \"snapshot_doc_count\":1, \"model_size_stats\":{" - + "\"job_id\":\"test-update-model-snapshot\", \"result_type\":\"model_size_stats\",\"model_bytes\":51722, " - + "\"total_by_field_count\":3, \"total_over_field_count\":0, \"total_partition_field_count\":2," - + "\"bucket_allocation_failures_count\":0, \"memory_status\":\"ok\", \"log_time\":1541587919000, " - + "\"timestamp\":1519930800000}, \"latest_record_time_stamp\":1519931700000," - + "\"latest_result_time_stamp\":1519930800000, \"retain\":false}", - XContentType.JSON - ); + indexRequest.source(""" + { + "job_id": "test-update-model-snapshot", + "timestamp": 1541587919000, + "description": "State persisted due to job close at 2018-11-07T10:51:59+0000", + "snapshot_id": "1541587919", + "snapshot_doc_count": 1, + "model_size_stats": { + "job_id": "test-update-model-snapshot", + "result_type": "model_size_stats", + "model_bytes": 51722, + "total_by_field_count": 3, + "total_over_field_count": 0, + "total_partition_field_count": 2, + "bucket_allocation_failures_count": 0, + "memory_status": "ok", + "log_time": 1541587919000, + "timestamp": 1519930800000 + }, + "latest_record_time_stamp": 1519931700000, + "latest_result_time_stamp": 1519930800000, + "retain": false + }""", XContentType.JSON); client.index(indexRequest, RequestOptions.DEFAULT); { 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 4c72e37e9a095..eff8060f78819 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 @@ -821,10 +821,11 @@ public void testSearchTemplateWithInlineScript() throws Exception { request.setScriptType(ScriptType.INLINE); request.setScript( // <2> - "{" + - " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + - " \"size\" : \"{{size}}\"" + - "}"); + """ + { + "query": { "match": { "{{field}}": "{{value}}" } }, + "size": "{{size}}" + }"""); Map scriptParams = new HashMap<>(); scriptParams.put("field", "title"); @@ -931,10 +932,11 @@ public void testMultiSearchTemplateWithInlineScript() throws Exception { request.setScriptType(ScriptType.INLINE); request.setScript( - "{" + - " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + - " \"size\" : \"{{size}}\"" + - "}"); + """ + { + "query": { "match" : { "{{field}}" : "{{value}}" } }, + "size" : "{{size}}" + }"""); Map scriptParams = new HashMap<>(); scriptParams.put("field", "title"); @@ -1036,16 +1038,16 @@ public void onFailure(Exception e) { protected void registerQueryScript(RestClient restClient) throws IOException { // tag::register-script Request scriptRequest = new Request("POST", "_scripts/title_search"); - scriptRequest.setJsonEntity( - "{" + - " \"script\": {" + - " \"lang\": \"mustache\"," + - " \"source\": {" + - " \"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" } }," + - " \"size\" : \"{{size}}\"" + - " }" + - " }" + - "}"); + scriptRequest.setJsonEntity(""" + { + "script": { + "lang": "mustache", + "source": { + "query": { "match": { "{{field}}": "{{value}}" } }, + "size": "{{size}}" + } + } + }"""); Response scriptResponse = restClient.performRequest(scriptRequest); // end::register-script assertEquals(RestStatus.OK.getStatus(), scriptResponse.getStatusLine().getStatusCode()); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java index e47a46b5936aa..dabf3d97a9888 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java @@ -133,8 +133,9 @@ public void testSnapshotCreateRepository() throws IOException { } { // tag::create-repository-settings-source - request.settings("{\"location\": \".\", \"compress\": \"true\"}", - XContentType.JSON); // <1> + request.settings(""" + {"location": ".", "compress": "true"} + """, XContentType.JSON); // <1> // end::create-repository-settings-source } 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 c16359e0cdccb..0b93a5bbf11f3 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 @@ -191,13 +191,14 @@ public void testPutScript() throws Exception { // tag::put-stored-script-request PutStoredScriptRequest request = new PutStoredScriptRequest(); request.id("id"); // <1> - request.content(new BytesArray( - "{\n" + - "\"script\": {\n" + - "\"lang\": \"painless\",\n" + - "\"source\": \"Math.log(_score * 2) + params.multiplier\"" + - "}\n" + - "}\n" + request.content(new BytesArray(""" + { + "script": { + "lang": "painless", + "source": "Math.log(_score * 2) + params.multiplier" + } + } + """ ), XContentType.JSON); // <2> // end::put-stored-script-request @@ -282,7 +283,8 @@ public void onFailure(Exception e) { builder.startObject("script"); { builder.field("lang", "mustache"); - builder.field("source", "{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}"); + builder.field("source", """ + {"query":{"match":{"title":"{{query_string}}"}}}"""); } builder.endObject(); } @@ -294,7 +296,8 @@ public void onFailure(Exception e) { Map script = getAsMap("/_scripts/id"); assertThat(extractValue("script.lang", script), equalTo("mustache")); - assertThat(extractValue("script.source", script), equalTo("{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}")); + assertThat(extractValue("script.source", script), equalTo(""" + {"query":{"match":{"title":"{{query_string}}"}}}""")); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java index def624c9b3495..bcac9ff5e520b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/TextStructureClientDocumentationIT.java @@ -30,12 +30,12 @@ public void testFindStructure() throws Exception { RestHighLevelClient client = highLevelClient(); Path anInterestingFile = createTempFile(); - String contents = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151445," - + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + String contents = """ + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 1",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 2",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + """; Files.write(anInterestingFile, Collections.singleton(contents), StandardCharsets.UTF_8); { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java index 002d79017fccd..abf1628119b69 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java @@ -144,11 +144,12 @@ public void testWatcher() throws Exception { { //tag::x-pack-put-watch-execute // you can also use the WatchSourceBuilder from org.elasticsearch.plugin:x-pack-core to create a watch programmatically - BytesReference watch = new BytesArray("{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"); + BytesReference watch = new BytesArray(""" + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "simple": { "foo" : "bar" } }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""); PutWatchRequest request = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); request.setActive(false); // <1> PutWatchResponse response = client.watcher().putWatch(request, RequestOptions.DEFAULT); @@ -162,13 +163,12 @@ public void testWatcher() throws Exception { } { - BytesReference watch = new BytesArray( - "{ \n" - + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" - + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" - + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" - + "}" - ); + BytesReference watch = new BytesArray(""" + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "simple": { "foo" : "bar" } }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""); PutWatchRequest request = new PutWatchRequest("my_other_watch_id", watch, XContentType.JSON); // tag::x-pack-put-watch-execute-listener ActionListener listener = new ActionListener() { @@ -332,11 +332,12 @@ public void testExecuteInlineWatch() throws Exception { { // tag::x-pack-execute-watch-inline - String watchJson = "{ \n" + - " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + - " \"input\": { \"none\": {} },\n" + - " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + - "}"; + String watchJson = """ + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "none": {} }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""; ExecuteWatchRequest request = ExecuteWatchRequest.inline(watchJson); request.setAlternativeInput("{ \"foo\" : \"bar\" }"); // <1> request.setActionMode("action1", ExecuteWatchRequest.ActionExecutionMode.SIMULATE); // <2> @@ -354,11 +355,12 @@ public void testExecuteInlineWatch() throws Exception { } { - String watchJson = "{ \n" - + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" - + " \"input\": { \"none\": {} },\n" - + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" - + "}"; + String watchJson = """ + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "none": {} }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""; ExecuteWatchRequest request = ExecuteWatchRequest.inline(watchJson); // tag::x-pack-execute-watch-inline-execute-listener ActionListener listener = new ActionListener() { @@ -390,13 +392,12 @@ public void testAckWatch() throws Exception { RestHighLevelClient client = highLevelClient(); { - BytesReference watch = new BytesArray( - "{ \n" - + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" - + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" - + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" - + "}" - ); + BytesReference watch = new BytesArray(""" + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "simple": { "foo" : "bar" } }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""); PutWatchRequest putWatchRequest = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); client.watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); @@ -458,13 +459,12 @@ public void testDeactivateWatch() throws Exception { RestHighLevelClient client = highLevelClient(); { - BytesReference watch = new BytesArray( - "{ \n" - + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" - + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" - + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" - + "}" - ); + BytesReference watch = new BytesArray(""" + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "simple": { "foo" : "bar" } }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""); PutWatchRequest putWatchRequest = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); client.watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); } @@ -510,13 +510,12 @@ public void testActivateWatch() throws Exception { RestHighLevelClient client = highLevelClient(); { - BytesReference watch = new BytesArray( - "{ \n" - + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" - + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" - + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" - + "}" - ); + BytesReference watch = new BytesArray(""" + { + "trigger": { "schedule": { "interval": "10h" } }, + "input": { "simple": { "foo" : "bar" } }, + "actions": { "logme": { "logging": { "text": "{{ctx.payload}}" } } } + }"""); PutWatchRequest request = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); request.setActive(false); // <1> PutWatchResponse response = client.watcher().putWatch(request, RequestOptions.DEFAULT); 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 7024fe126fd9b..9a7e4d422ce3e 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 @@ -43,7 +43,8 @@ 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( diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java index 25cffd590913d..4b2db5ca2f3b7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PostDataRequestTests.java @@ -59,10 +59,8 @@ public void testJsonBuilder() throws IOException { PostDataRequest request = new PostDataRequest(jobId, builder); - assertEquals( - "{\"entry1\":\"value1\",\"entry2\":\"value2\"}{\"entry3\":\"value3\"}{\"entry4\":\"value4\"}", - request.getContent().utf8ToString() - ); + assertEquals(""" + {"entry1":"value1","entry2":"value2"}{"entry3":"value3"}{"entry4":"value4"}""", request.getContent().utf8ToString()); assertEquals(XContentType.JSON, request.getXContentType()); assertEquals(jobId, request.getJobId()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedResponseTests.java index e643d59c46dc7..04b42bbf117ee 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/PreviewDatafeedResponseTests.java @@ -32,26 +32,27 @@ protected PreviewDatafeedResponse createTestInstance() throws IOException { } public void testGetDataList() throws IOException { - String rawData = "[\n" - + " {\n" - + " \"time\": 1454803200000,\n" - + " \"airline\": \"JZA\",\n" - + " \"doc_count\": 5,\n" - + " \"responsetime\": 990.4628295898438\n" - + " },\n" - + " {\n" - + " \"time\": 1454803200000,\n" - + " \"airline\": \"JBU\",\n" - + " \"doc_count\": 23,\n" - + " \"responsetime\": 877.5927124023438\n" - + " },\n" - + " {\n" - + " \"time\": 1454803200000,\n" - + " \"airline\": \"KLM\",\n" - + " \"doc_count\": 42,\n" - + " \"responsetime\": 1355.481201171875\n" - + " }\n" - + "]"; + String rawData = """ + [ + { + "time": 1454803200000, + "airline": "JZA", + "doc_count": 5, + "responsetime": 990.4628295898438 + }, + { + "time": 1454803200000, + "airline": "JBU", + "doc_count": 23, + "responsetime": 877.5927124023438 + }, + { + "time": 1454803200000, + "airline": "KLM", + "doc_count": 42, + "responsetime": 1355.481201171875 + } + ]"""; BytesReference bytes = new BytesArray(rawData); PreviewDatafeedResponse response = new PreviewDatafeedResponse(bytes); assertThat( diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedConfigTests.java index 5689d8a6df678..105b104b1583f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/datafeed/DatafeedConfigTests.java @@ -140,14 +140,15 @@ protected boolean supportsUnknownFields() { return false; } - private static final String FUTURE_DATAFEED = "{\n" - + " \"datafeed_id\": \"farequote-datafeed\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + " \"tomorrows_technology_today\": \"amazing\",\n" - + " \"scroll_size\": 1234\n" - + "}"; + private static final String FUTURE_DATAFEED = """ + { + "datafeed_id": "farequote-datafeed", + "job_id": "farequote", + "frequency": "1h", + "indices": ["farequote1", "farequote2"], + "tomorrows_technology_today": "amazing", + "scroll_size": 1234 + }"""; public void testFutureMetadataParse() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java index 040988ef3c356..01773c7f272b9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/inference/NamedXContentObjectHelperTests.java @@ -65,8 +65,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } public void testSerializeInOrder() throws IOException { - String expected = - "{\"my_objects\":[{\"my_named_object\":{\"my_field\":\"value1\"}},{\"my_named_object\":{\"my_field\":\"value2\"}}]}"; + String expected = """ + {"my_objects":[{"my_named_object":{"my_field":"value1"}},{"my_named_object":{"my_field":"value2"}}]}"""; try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.startObject(); List objects = Arrays.asList(new NamedTestObject("value1"), new NamedTestObject("value2")); @@ -77,7 +77,8 @@ public void testSerializeInOrder() throws IOException { } public void testSerialize() throws IOException { - String expected = "{\"my_objects\":{\"my_named_object\":{\"my_field\":\"value1\"},\"my_named_object\":{\"my_field\":\"value2\"}}}"; + String expected = """ + {"my_objects":{"my_named_object":{"my_field":"value1"},"my_named_object":{"my_field":"value2"}}}"""; try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.startObject(); List objects = Arrays.asList(new NamedTestObject("value1"), new NamedTestObject("value2")); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java index 0e7f79ace5a24..0491c9c5a6546 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/config/JobTests.java @@ -28,20 +28,21 @@ public class JobTests extends AbstractXContentTestCase { - private static final String FUTURE_JOB = "{\n" - + " \"job_id\": \"farequote\",\n" - + " \"create_time\": 1234567890000,\n" - + " \"tomorrows_technology_today\": \"wow\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"something_new\": \"gasp\",\n" - + " \"detectors\": [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\": \"airline\"}]\n" - + " },\n" - + " \"data_description\": {\n" - + " \"time_field\": \"time\",\n" - + " \"the_future\": 123\n" - + " }\n" - + "}"; + private static final String FUTURE_JOB = """ + { + "job_id": "farequote", + "create_time": 1234567890000, + "tomorrows_technology_today": "wow", + "analysis_config": { + "bucket_span": "1h", + "something_new": "gasp", + "detectors": [{"function": "metric", "field_name": "responsetime", "by_field_name": "airline"}] + }, + "data_description": { + "time_field": "time", + "the_future": 123 + } + }"""; @Override protected Job createTestInstance() { 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 32b60d29a7cf0..4e8b31d016504 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 @@ -8,8 +8,6 @@ package org.elasticsearch.client.security; -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.common.Strings; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.DeprecationHandler; import org.elasticsearch.xcontent.NamedXContentRegistry; @@ -26,16 +24,16 @@ public class ClearRealmCacheResponseTests extends ESTestCase { public void testParseFromXContent() throws IOException { - final ElasticsearchException exception = new ElasticsearchException("test"); - final String nodesHeader = "\"_nodes\": { \"total\": 2, \"successful\": 1, \"failed\": 1, \"failures\": [ " - + Strings.toString(exception) - + "] },"; - final String clusterName = "\"cluster_name\": \"cn\","; try ( XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - "{" + nodesHeader + clusterName + "\"nodes\" : {} }" + """ + { + "_nodes": { "total": 2, "successful": 1, "failed": 1, "failures": [ {"type":"exception","reason":"test"}] }, + "cluster_name": "cn", + "nodes" : {} + }""" ) ) { @@ -54,7 +52,12 @@ public void testParseFromXContent() throws IOException { XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - "{" + nodesHeader + clusterName + "\"nodes\" : { \"id1\": { \"name\": \"a\"}, \"id2\": { \"name\": \"b\"}}}" + """ + { + "_nodes": { "total": 2, "successful": 1, "failed": 1, "failures": [ {"type":"exception","reason":"test"}] }, + "cluster_name": "cn", + "nodes" : { "id1": { "name": "a"}, "id2": { "name": "b"}} + }""" ) ) { 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 2aa19a0a77821..73259683abee8 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 @@ -27,10 +27,10 @@ public class ClearRolesCacheResponseTests extends ESTestCase { public void testParseFromXContent() throws IOException { final ElasticsearchException exception = new ElasticsearchException("test"); - final String nodesHeader = "\"_nodes\": { \"total\": 2, \"successful\": 1, \"failed\": 1, \"failures\": [ " - + Strings.toString(exception) - + "] },"; - final String clusterName = "\"cluster_name\": \"cn\","; + final String nodesHeader = """ + "_nodes": { "total": 2, "successful": 1, "failed": 1, "failures": [ %s] },""".formatted(Strings.toString(exception)); + final String clusterName = """ + "cluster_name": "cn","""; try ( XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, @@ -54,7 +54,9 @@ public void testParseFromXContent() throws IOException { XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - "{" + nodesHeader + clusterName + "\"nodes\" : { \"id1\": { \"name\": \"a\"}, \"id2\": { \"name\": \"b\"}}}" + """ + {%s%s"nodes" : { "id1": { "name": "a"}, "id2": { "name": "b"}}} + """.formatted(nodesHeader, clusterName) ) ) { 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 55d19ecb7a706..d4090bb787f89 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 @@ -23,10 +23,8 @@ 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() { @@ -36,10 +34,8 @@ 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() { @@ -60,7 +56,8 @@ 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/ExpressionRoleMappingTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ExpressionRoleMappingTests.java index 3d40c08b96d08..92a5762a2db1a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ExpressionRoleMappingTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ExpressionRoleMappingTests.java @@ -26,18 +26,19 @@ public class ExpressionRoleMappingTests extends ESTestCase { public void testExpressionRoleMappingParser() throws IOException { - final String json = "{\n" - + " \"enabled\" : true,\n" - + " \"roles\" : [\n" - + " \"superuser\"\n" - + " ],\n" - + " \"rules\" : {\n" - + " \"field\" : {\n" - + " \"realm.name\" : \"kerb1\"\n" - + " }\n" - + " },\n" - + " \"metadata\" : { }\n" - + " }"; + final String json = """ + { + "enabled" : true, + "roles" : [ + "superuser" + ], + "rules" : { + "field" : { + "realm.name" : "kerb1" + } + }, + "metadata" : { } + }"""; final ExpressionRoleMapping expressionRoleMapping = ExpressionRoleMapping.PARSER.parse( XContentType.JSON.xContent() .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json), diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java index 78b8cdb5c3997..82582f40e296c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetPrivilegesResponseTests.java @@ -30,44 +30,49 @@ public class GetPrivilegesResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - final String json = "{" - + " \"testapp\": {" - + " \"read\": {" - + " \"application\": \"testapp\"," - + " \"name\": \"read\"," - + " \"actions\": [ \"action:login\", \"data:read/*\" ]" - + " }," - + " \"write\": {" - + " \"application\": \"testapp\"," - + " \"name\": \"write\"," - + " \"actions\": [ \"action:login\", \"data:write/*\" ]," - + " \"metadata\": { \"key1\": \"value1\" }" - + " }," - + " \"all\": {" - + " \"application\": \"testapp\"," - + " \"name\": \"all\"," - + " \"actions\": [ \"action:login\", \"data:write/*\" , \"manage:*\"]" - + " }" - + " }," - + " \"testapp2\": {" - + " \"read\": {" - + " \"application\": \"testapp2\"," - + " \"name\": \"read\"," - + " \"actions\": [ \"action:login\", \"data:read/*\" ]," - + " \"metadata\": { \"key2\": \"value2\" }" - + " }," - + " \"write\": {" - + " \"application\": \"testapp2\"," - + " \"name\": \"write\"," - + " \"actions\": [ \"action:login\", \"data:write/*\" ]" - + " }," - + " \"all\": {" - + " \"application\": \"testapp2\"," - + " \"name\": \"all\"," - + " \"actions\": [ \"action:login\", \"data:write/*\" , \"manage:*\"]" - + " }" - + " }" - + "}"; + final String json = """ + { + "testapp": { + "read": { + "application": "testapp", + "name": "read", + "actions": [ "action:login", "data:read/*" ] + }, + "write": { + "application": "testapp", + "name": "write", + "actions": [ "action:login", "data:write/*" ], + "metadata": { + "key1": "value1" + } + }, + "all": { + "application": "testapp", + "name": "all", + "actions": [ "action:login", "data:write/*", "manage:*" ] + } + }, + "testapp2": { + "read": { + "application": "testapp2", + "name": "read", + "actions": [ "action:login", "data:read/*" ], + "metadata": { + "key2": "value2" + } + }, + "write": { + "application": "testapp2", + "name": "write", + "actions": [ "action:login", "data:write/*" ] + }, + "all": { + "application": "testapp2", + "name": "all", + "actions": [ "action:login", "data:write/*", "manage:*" ] + } + } + }"""; final GetPrivilegesResponse response = GetPrivilegesResponse.fromXContent( XContentType.JSON.xContent() diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java index ec5730356a12f..14394e3f2c6ad 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRoleMappingsResponseTests.java @@ -25,32 +25,33 @@ public class GetRoleMappingsResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - final String json = "{\n" - + " \"kerberosmapping\" : {\n" - + " \"enabled\" : true,\n" - + " \"roles\" : [\n" - + " \"superuser\"\n" - + " ],\n" - + " \"rules\" : {\n" - + " \"field\" : {\n" - + " \"realm.name\" : \"kerb1\"\n" - + " }\n" - + " },\n" - + " \"metadata\" : { }\n" - + " },\n" - + " \"ldapmapping\" : {\n" - + " \"enabled\" : false,\n" - + " \"roles\" : [\n" - + " \"monitoring\"\n" - + " ],\n" - + " \"rules\" : {\n" - + " \"field\" : {\n" - + " \"groups\" : \"cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local\"\n" - + " }\n" - + " },\n" - + " \"metadata\" : { }\n" - + " }\n" - + "}"; + final String json = """ + { + "kerberosmapping" : { + "enabled" : true, + "roles" : [ + "superuser" + ], + "rules" : { + "field" : { + "realm.name" : "kerb1" + } + }, + "metadata" : { } + }, + "ldapmapping" : { + "enabled" : false, + "roles" : [ + "monitoring" + ], + "rules" : { + "field" : { + "groups" : "cn=ipausers,cn=groups,cn=accounts,dc=ipademo,dc=local" + } + }, + "metadata" : { } + } + }"""; final GetRoleMappingsResponse response = GetRoleMappingsResponse.fromXContent( XContentType.JSON.xContent() .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java index 768b26f720d25..87f4822850cc8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetRolesResponseTests.java @@ -29,28 +29,29 @@ public class GetRolesResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - String json = "{\n" - + " \"my_admin_role\": {\n" - + " \"cluster\" : [ \"all\" ],\n" - + " \"indices\" : [\n" - + " {\n" - + " \"names\" : [ \"index1\", \"index2\" ],\n" - + " \"privileges\" : [ \"all\" ],\n" - + " \"allow_restricted_indices\" : true,\n" - + " \"field_security\" : {\n" - + " \"grant\" : [ \"title\", \"body\" ]}\n" - + " }\n" - + " ],\n" - + " \"applications\" : [ ],\n" - + " \"run_as\" : [ \"other_user\" ],\n" - + " \"metadata\" : {\n" - + " \"version\" : 1\n" - + " },\n" - + " \"transient_metadata\" : {\n" - + " \"enabled\" : true\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "my_admin_role": { + "cluster" : [ "all" ], + "indices" : [ + { + "names" : [ "index1", "index2" ], + "privileges" : [ "all" ], + "allow_restricted_indices" : true, + "field_security" : { + "grant" : [ "title", "body" ]} + } + ], + "applications" : [ ], + "run_as" : [ "other_user" ], + "metadata" : { + "version" : 1 + }, + "transient_metadata" : { + "enabled" : true + } + } + }"""; final GetRolesResponse response = GetRolesResponse.fromXContent( (XContentType.JSON.xContent() .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json)) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java index 3f0fbb2c876fb..b7829324f7838 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GetUserPrivilegesResponseTests.java @@ -30,32 +30,103 @@ public class GetUserPrivilegesResponseTests extends ESTestCase { public void testParse() throws Exception { - String json = "{" - + "\"cluster\":[\"manage\",\"manage_security\",\"monitor\"]," - + "\"global\":[" - + " {\"application\":{\"manage\":{\"applications\":[\"test-*\"]}}}," - + " {\"application\":{\"manage\":{\"applications\":[\"apps-*\"]}}}" - + "]," - + "\"indices\":[" - + " {\"names\":[\"test-1-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false}," - + " {\"names\":[\"test-4-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": true," - + " \"field_security\":[{\"grant\":[\"*\"],\"except\":[\"private-*\"]}]}," - + " {\"names\":[\"test-6-*\",\"test-7-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": true," - + " \"query\":[\"{\\\"term\\\":{\\\"test\\\":true}}\"]}," - + " {\"names\":[\"test-2-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false," - + " \"field_security\":[{\"grant\":[\"*\"],\"except\":[\"secret-*\",\"private-*\"]},{\"grant\":[\"apps-*\"]}]," - + " \"query\":[\"{\\\"term\\\":{\\\"test\\\":true}}\",\"{\\\"term\\\":{\\\"apps\\\":true}}\"]}," - + " {\"names\":[\"test-3-*\",\"test-6-*\"],\"privileges\":[\"read\",\"write\"],\"allow_restricted_indices\": true}," - + " {\"names\":[\"test-3-*\",\"test-4-*\",\"test-5-*\"],\"privileges\":[\"read\"],\"allow_restricted_indices\": false," - + " \"field_security\":[{\"grant\":[\"test-*\"]}]}," - + " {\"names\":[\"test-1-*\",\"test-9-*\"],\"privileges\":[\"all\"],\"allow_restricted_indices\": true}" - + "]," - + "\"applications\":[" - + " {\"application\":\"app-dne\",\"privileges\":[\"all\"],\"resources\":[\"*\"]}," - + " {\"application\":\"test-app\",\"privileges\":[\"read\"],\"resources\":[\"object/1\",\"object/2\"]}," - + " {\"application\":\"test-app\",\"privileges\":[\"user\",\"dne\"],\"resources\":[\"*\"]}" - + "]," - + "\"run_as\":[\"app-*\",\"test-*\"]}"; + String json = """ + { + "cluster": [ "manage", "manage_security", "monitor" ], + "global": [ + { + "application": { + "manage": { + "applications": [ "test-*" ] + } + } + }, + { + "application": { + "manage": { + "applications": [ "apps-*" ] + } + } + } + ], + "indices": [ + { + "names": [ "test-1-*" ], + "privileges": [ "read" ], + "allow_restricted_indices": false + }, + { + "names": [ "test-4-*" ], + "privileges": [ "read" ], + "allow_restricted_indices": true, + "field_security": [ + { + "grant": [ "*" ], + "except": [ "private-*" ] + } + ] + }, + { + "names": [ "test-6-*", "test-7-*" ], + "privileges": [ "read" ], + "allow_restricted_indices": true, + "query": [ "{\\"term\\":{\\"test\\":true}}" ] + }, + { + "names": [ "test-2-*" ], + "privileges": [ "read" ], + "allow_restricted_indices": false, + "field_security": [ + { + "grant": [ "*" ], + "except": [ "secret-*", "private-*" ] + }, + { + "grant": [ "apps-*" ] + } + ], + "query": [ "{\\"term\\":{\\"test\\":true}}", "{\\"term\\":{\\"apps\\":true}}" ] + }, + { + "names": [ "test-3-*", "test-6-*" ], + "privileges": [ "read", "write" ], + "allow_restricted_indices": true + }, + { + "names": [ "test-3-*", "test-4-*", "test-5-*" ], + "privileges": [ "read" ], + "allow_restricted_indices": false, + "field_security": [ + { + "grant": [ "test-*" ] + } + ] + }, + { + "names": [ "test-1-*", "test-9-*" ], + "privileges": [ "all" ], + "allow_restricted_indices": true + } + ], + "applications": [ + { + "application": "app-dne", + "privileges": [ "all" ], + "resources": [ "*" ] + }, + { + "application": "test-app", + "privileges": [ "read" ], + "resources": [ "object/1", "object/2" ] + }, + { + "application": "test-app", + "privileges": [ "user", "dne" ], + "resources": [ "*" ] + } + ], + "run_as": [ "app-*", "test-*" ] + }"""; final XContentParser parser = createParser(XContentType.JSON.xContent(), json); final GetUserPrivilegesResponse response = GetUserPrivilegesResponse.fromXContent(parser); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java index 3fc73a71f50bf..653b97a81418c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/GrantApiKeyRequestTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.client.security.user.privileges.Role.ClusterPrivilegeName; import org.elasticsearch.client.security.user.privileges.Role.IndexPrivilegeName; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.EqualsHashCodeTestUtils; @@ -42,19 +43,18 @@ public void testToXContent() throws IOException { final String apiKeyMetadataString = apiKeyMetadata == null ? "" : ",\"metadata\":" + XContentTestUtils.convertToXContent(apiKeyMetadata, XContentType.JSON).utf8ToString(); - assertThat( - output, - equalTo( - "{" - + "\"grant_type\":\"password\"," - + "\"username\":\"kamala.khan\"," - + "\"password\":\"JerseyGirl!\"," - + "\"api_key\":{\"name\":\"api-key\",\"role_descriptors\":{}" - + apiKeyMetadataString - + "}" - + "}" - ) - ); + assertThat(output, equalTo(XContentHelper.stripWhitespace(""" + { + "grant_type": "password", + "username": "kamala.khan", + "password": "JerseyGirl!", + "api_key": { + "name": "api-key", + "role_descriptors": {} + %s + } + } + """.formatted(apiKeyMetadataString)))); } public void testEqualsHashCode() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java index 28fb340c64ae7..23516dd8e00b4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesRequestTests.java @@ -47,31 +47,34 @@ public void testToXContent() throws IOException { String json = Strings.toString(request); final Map parsed = XContentHelper.convertToMap(XContentType.JSON.xContent(), json, false); - final Map expected = XContentHelper.convertToMap( - XContentType.JSON.xContent(), - "{" - + " \"cluster\":[\"monitor\",\"manage_watcher\",\"manage_ml\"]," - + " \"index\":[{" - + " \"names\":[\"index-001\",\"index-002\"]," - + " \"privileges\":[\"all\"]," - + " \"allow_restricted_indices\":true" - + " },{" - + " \"names\":[\"index-003\"]," - + " \"privileges\":[\"read\"]," - + " \"allow_restricted_indices\":false" - + " }]," - + " \"application\":[{" - + " \"application\":\"myapp\"," - + " \"privileges\":[\"read\",\"write\"]," - + " \"resources\":[\"*\"]" - + " },{" - + " \"application\":\"myapp\"," - + " \"privileges\":[\"admin\"]," - + " \"resources\":[\"/data/*\"]" - + " }]" - + "}", - false - ); + final Map expected = XContentHelper.convertToMap(XContentType.JSON.xContent(), """ + { + "cluster": [ "monitor", "manage_watcher", "manage_ml" ], + "index": [ + { + "names": [ "index-001", "index-002" ], + "privileges": [ "all" ], + "allow_restricted_indices": true + }, + { + "names": [ "index-003" ], + "privileges": [ "read" ], + "allow_restricted_indices": false + } + ], + "application": [ + { + "application": "myapp", + "privileges": [ "read", "write" ], + "resources": [ "*" ] + }, + { + "application": "myapp", + "privileges": [ "admin" ], + "resources": [ "/data/*" ] + } + ] + }""", false); assertThat(XContentTestUtils.differenceBetweenMapsIgnoringArrayOrder(parsed, expected), Matchers.nullValue()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java index 48bcf4752a31f..eaa08d8907391 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/HasPrivilegesResponseTests.java @@ -25,50 +25,51 @@ public class HasPrivilegesResponseTests extends ESTestCase { public void testParseValidResponse() throws IOException { - String json = "{" - + " \"username\": \"namor\"," - + " \"has_all_requested\": false," - + " \"cluster\" : {" - + " \"manage\" : false," - + " \"monitor\" : true" - + " }," - + " \"index\" : {" - + " \"index-01\": {" - + " \"read\" : true," - + " \"write\" : false" - + " }," - + " \"index-02\": {" - + " \"read\" : true," - + " \"write\" : true" - + " }," - + " \"index-03\": {" - + " \"read\" : false," - + " \"write\" : false" - + " }" - + " }," - + " \"application\" : {" - + " \"app01\" : {" - + " \"/object/1\" : {" - + " \"read\" : true," - + " \"write\" : false" - + " }," - + " \"/object/2\" : {" - + " \"read\" : true," - + " \"write\" : true" - + " }" - + " }," - + " \"app02\" : {" - + " \"/object/1\" : {" - + " \"read\" : false," - + " \"write\" : false" - + " }," - + " \"/object/3\" : {" - + " \"read\" : false," - + " \"write\" : true" - + " }" - + " }" - + " }" - + "}"; + String json = """ + { + "username": "namor", + "has_all_requested": false, + "cluster": { + "manage": false, + "monitor": true + }, + "index": { + "index-01": { + "read": true, + "write": false + }, + "index-02": { + "read": true, + "write": true + }, + "index-03": { + "read": false, + "write": false + } + }, + "application": { + "app01": { + "/object/1": { + "read": true, + "write": false + }, + "/object/2": { + "read": true, + "write": true + } + }, + "app02": { + "/object/1": { + "read": false, + "write": false + }, + "/object/3": { + "read": false, + "write": true + } + } + } + }"""; final XContentParser parser = createParser(XContentType.JSON.xContent(), json); HasPrivilegesResponse response = HasPrivilegesResponse.fromXContent(parser); 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 b0401cc007c62..cfe176af77ba9 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 @@ -60,7 +60,8 @@ 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/PutPrivilegesRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesRequestTests.java index 21acffe6fe06e..727617b87cabe 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesRequestTests.java @@ -62,41 +62,42 @@ public void testConstructor() { } public void testToXContent() throws IOException { - final String expected = "{\n" - + " \"app01\" : {\n" - + " \"all\" : {\n" - + " \"application\" : \"app01\",\n" - + " \"name\" : \"all\",\n" - + " \"actions\" : [\n" - + " \"action:login\",\n" - + " \"action:logout\"\n" - + " ],\n" - + " \"metadata\" : {\n" - + " \"k1\" : \"v1\"\n" - + " }\n" - + " },\n" - + " \"read\" : {\n" - + " \"application\" : \"app01\",\n" - + " \"name\" : \"read\",\n" - + " \"actions\" : [\n" - + " \"data:read\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"app02\" : {\n" - + " \"all\" : {\n" - + " \"application\" : \"app02\",\n" - + " \"name\" : \"all\",\n" - + " \"actions\" : [\n" - + " \"action:login\",\n" - + " \"action:logout\"\n" - + " ],\n" - + " \"metadata\" : {\n" - + " \"k2\" : \"v2\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + final String expected = """ + { + "app01" : { + "all" : { + "application" : "app01", + "name" : "all", + "actions" : [ + "action:login", + "action:logout" + ], + "metadata" : { + "k1" : "v1" + } + }, + "read" : { + "application" : "app01", + "name" : "read", + "actions" : [ + "data:read" + ] + } + }, + "app02" : { + "all" : { + "application" : "app02", + "name" : "all", + "actions" : [ + "action:login", + "action:logout" + ], + "metadata" : { + "k2" : "v2" + } + } + } + }"""; List privileges = new ArrayList<>(); privileges.add( ApplicationPrivilege.builder() diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java index 3040979e4113d..1adb5896d5170 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutPrivilegesResponseTests.java @@ -21,21 +21,22 @@ public class PutPrivilegesResponseTests extends ESTestCase { public void testFromXContent() throws IOException { - final String json = "{\n" - + " \"app02\": {\n" - + " \"all\": {\n" - + " \"created\": true\n" - + " }\n" - + " },\n" - + " \"app01\": {\n" - + " \"read\": {\n" - + " \"created\": false\n" - + " },\n" - + " \"write\": {\n" - + " \"created\": true\n" - + " }\n" - + " }\n" - + "}"; + final String json = """ + { + "app02": { + "all": { + "created": true + } + }, + "app01": { + "read": { + "created": false + }, + "write": { + "created": true + } + } + }"""; final PutPrivilegesResponse putPrivilegesResponse = PutPrivilegesResponse.fromXContent( createParser(XContentType.JSON.xContent(), json) 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 bf979dcc86553..4f5e8ef6caf35 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 @@ -144,29 +144,22 @@ 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 = String.format( - Locale.ROOT, - "{" - + " \"enabled\": %s," - + " \"roles\": [" - + " \"superuser\"" - + " ]," - + "\"role_templates\":[]," - + "\"rules\":{" - + " \"field\": {" - + " \"username\": [" - + " \"user\"" - + " ]" - + " }" - + "}," - + " \"metadata\": {" - + " \"k1\": \"v1\"" - + " }" - + "}", - enabled - ).replaceAll("\\s+", ""); + final String expected = String.format(Locale.ROOT, """ + { + "enabled": %s, + "roles": [ "superuser" ], + "role_templates": [], + "rules": { + "field": { + "username": [ "user" ] + } + }, + "metadata": { + "k1": "v1" + } + }""", enabled); - assertThat(output, equalTo(expected)); + assertThat(output.replace("\\s", ""), equalTo(expected.replaceAll("\\s+", ""))); } public void testPutRoleMappingRequestWithTemplateToXContent() throws IOException { @@ -194,34 +187,29 @@ 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 = String.format( - Locale.ROOT, - "{" - + " \"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+", ""); + final String expected = String.format(Locale.ROOT, """ + { + "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/security/hlrc/HasPrivilegesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/hlrc/HasPrivilegesResponseTests.java index 2ff8ed581e5b0..43829edfd75bf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/hlrc/HasPrivilegesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/hlrc/HasPrivilegesResponseTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; @@ -74,21 +75,29 @@ public void testToXContent() throws Exception { BytesReference bytes = BytesReference.bytes(builder); final String json = bytes.utf8ToString(); - Assert.assertThat( - json, - equalTo( - "{" - + "\"username\":\"daredevil\"," - + "\"has_all_requested\":false," - + "\"cluster\":{\"manage\":true}," - + "\"index\":{" - + "\"customers\":{\"read\":true,\"index\":true,\"delete\":true,\"manage\":false}," - + "\"staff\":{\"read\":true,\"index\":true,\"delete\":false,\"manage\":false}" - + "}," - + "\"application\":{}" - + "}" - ) - ); + Assert.assertThat(json, equalTo(XContentHelper.stripWhitespace(""" + { + "username": "daredevil", + "has_all_requested": false, + "cluster": { + "manage": true + }, + "index": { + "customers": { + "read": true, + "index": true, + "delete": true, + "manage": false + }, + "staff": { + "read": true, + "index": true, + "delete": false, + "manage": false + } + }, + "application": {} + }"""))); } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java index 93cfa21d4418c..422ef1ce8064c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/support/expressiondsl/RoleMapperExpressionDslTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.client.security.support.expressiondsl.expressions.ExceptRoleMapperExpression; import org.elasticsearch.client.security.support.expressiondsl.fields.FieldRoleMapperExpression; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -43,41 +44,39 @@ public void testRoleMapperExpressionToXContentType() throws IOException { final XContentBuilder builder = XContentFactory.jsonBuilder(); allExpression.toXContent(builder, ToXContent.EMPTY_PARAMS); final String output = Strings.toString(builder); - final String expected = "{" - + "\"all\":[" - + "{" - + "\"any\":[" - + "{" - + "\"field\":{" - + "\"dn\":[\"*,ou=admin,dc=example,dc=com\"]" - + "}" - + "}," - + "{" - + "\"field\":{" - + "\"username\":[" - + "\"es-admin\"," - + "\"es-system\"" - + "]" - + "}" - + "}" - + "]" - + "}," - + "{" - + "\"field\":{" - + "\"groups\":[\"cn=people,dc=example,dc=com\"]" - + "}" - + "}," - + "{" - + "\"except\":{" - + "\"field\":{" - + "\"metadata.terminated_date\":[\"2018-09-17T00:50:01.027Z\"]" - + "}" - + "}" - + "}" - + "]" - + "}"; + final String expected = """ + { + "all": [ + { + "any": [ + { + "field": { + "dn": [ "*,ou=admin,dc=example,dc=com" ] + } + }, + { + "field": { + "username": [ "es-admin", "es-system" ] + } + } + ] + }, + { + "field": { + "groups": [ "cn=people,dc=example,dc=com" ] + } + }, + { + "except": { + "field": { + "metadata.terminated_date": [ "2018-09-17T00:50:01.027Z" ] + } + } + } + ] + }"""; - assertThat(output, equalTo(expected)); + assertThat(output, equalTo(XContentHelper.stripWhitespace(expected))); } public void testFieldRoleMapperExpressionThrowsExceptionForMissingMetadataPrefix() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java index a1053f4dc8a5c..c843129f27b0b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/ApplicationPrivilegeTests.java @@ -30,17 +30,18 @@ public class ApplicationPrivilegeTests extends ESTestCase { public void testFromXContentAndToXContent() throws IOException { - String json = "{\n" - + " \"application\" : \"myapp\",\n" - + " \"name\" : \"read\",\n" - + " \"actions\" : [\n" - + " \"data:read/*\",\n" - + " \"action:login\"\n" - + " ],\n" - + " \"metadata\" : {\n" - + " \"description\" : \"Read access to myapp\"\n" - + " }\n" - + "}"; + String json = """ + { + "application" : "myapp", + "name" : "read", + "actions" : [ + "data:read/*", + "action:login" + ], + "metadata" : { + "description" : "Read access to myapp" + } + }"""; final ApplicationPrivilege privilege = ApplicationPrivilege.fromXContent( XContentType.JSON.xContent() .createParser(new NamedXContentRegistry(Collections.emptyList()), DeprecationHandler.IGNORE_DEPRECATIONS, json) 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 4b604f35cee8d..564c8725841ce 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 @@ -80,26 +80,22 @@ 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," - + " 2" - + " ]" - + "}" - ); + 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, 2 ] + }"""); XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, @@ -116,22 +112,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\"" - + " }" - + " }" - + "}" - ); + BytesArray json = new BytesArray(""" + { + "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, diff --git a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java index 718a221559c4f..c1a93c983a68f 100644 --- a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java +++ b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java @@ -36,7 +36,9 @@ public void testIndexWithoutId() throws IOException { public void testUpsert() throws IOException { Request request = new Request("POST", "test/_update/1"); - request.setJsonEntity("{" + "\"doc\": {\"test\": \"test\"}," + "\"doc_as_upsert\": true}"); + request.setJsonEntity(""" + {"doc": {"test": "test"},"doc_as_upsert": true} + """); locationTestCase(client().performRequest(request)); } diff --git a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java index de8bcdd9d6b4f..5bcea2a7aef8e 100644 --- a/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java +++ b/distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java @@ -121,13 +121,9 @@ public void testMetricsWithAll() throws IOException { () -> client().performRequest(new Request("GET", "_nodes/usage/_all,rest_actions")) ); assertNotNull(exception); - assertThat( - exception.getMessage(), - containsString( - "\"type\":\"illegal_argument_exception\"," - + "\"reason\":\"request [_nodes/usage/_all,rest_actions] contains _all and individual metrics [_all,rest_actions]\"" - ) - ); + assertThat(exception.getMessage(), containsString(""" + "type":"illegal_argument_exception",\ + "reason":"request [_nodes/usage/_all,rest_actions] contains _all and individual metrics [_all,rest_actions]\"""")); } @SuppressWarnings("unchecked") @@ -146,10 +142,25 @@ public void testAggregationUsage() throws IOException { Map> beforeCombinedAggsUsage = getTotalUsage(beforeNodesMap); // Do some requests to get some rest usage stats Request create = new Request("PUT", "/test"); - create.setJsonEntity( - "{\"mappings\": {\"properties\": { \"str\": {\"type\": \"keyword\"}, " - + "\"foo\": {\"type\": \"keyword\"}, \"num\": {\"type\": \"long\"}, \"start\": {\"type\": \"date\"} } }}" - ); + create.setJsonEntity(""" + { + "mappings": { + "properties": { + "str": { + "type": "keyword" + }, + "foo": { + "type": "keyword" + }, + "num": { + "type": "long" + }, + "start": { + "type": "date" + } + } + } + }"""); client().performRequest(create); Request searchRequest = new Request("GET", "/test/_search"); diff --git a/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/NodeRoleParserTests.java b/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/NodeRoleParserTests.java index 9ac24537c1702..8e1950fb92f29 100644 --- a/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/NodeRoleParserTests.java +++ b/distribution/tools/launchers/src/test/java/org/elasticsearch/tools/launchers/NodeRoleParserTests.java @@ -81,17 +81,15 @@ public void testLegacySettings() throws IOException { } public void testYamlSyntax() throws IOException { - MachineDependentHeap.MachineNodeRole nodeRole = parseConfig(sb -> { - sb.append("node:\n"); - sb.append(" roles:\n"); - sb.append(" - master"); - }); + MachineDependentHeap.MachineNodeRole nodeRole = parseConfig(sb -> sb.append(""" + node: + roles: + - master""")); assertThat(nodeRole, equalTo(MASTER_ONLY)); - nodeRole = parseConfig(sb -> { - sb.append("node:\n"); - sb.append(" roles: [ml]"); - }); + nodeRole = parseConfig(sb -> sb.append(""" + node: + roles: [ml]""")); assertThat(nodeRole, equalTo(ML_ONLY)); } diff --git a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/cli/ListPluginsCommandTests.java b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/cli/ListPluginsCommandTests.java index 0977a4d00b976..71844ff524bc6 100644 --- a/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/cli/ListPluginsCommandTests.java +++ b/distribution/tools/plugin-cli/src/test/java/org/elasticsearch/plugins/cli/ListPluginsCommandTests.java @@ -258,11 +258,17 @@ public void testExistingIncompatiblePlugin() throws Exception { MockTerminal terminal = listPlugins(home); String message = "plugin [fake_plugin1] was built for Elasticsearch version 1.0.0 but version " + Version.CURRENT + " is required"; - assertEquals("fake_plugin1\nfake_plugin2\n", terminal.getOutput()); + assertEquals(""" + fake_plugin1 + fake_plugin2 + """, terminal.getOutput()); assertEquals("WARNING: " + message + "\n", terminal.getErrorOutput()); String[] params = { "-s" }; terminal = listPlugins(home, params); - assertEquals("fake_plugin1\nfake_plugin2\n", terminal.getOutput()); + assertEquals(""" + fake_plugin1 + fake_plugin2 + """, terminal.getOutput()); } } diff --git a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java index d4ed09b7662ba..c7502037b1a15 100644 --- a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java +++ b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java @@ -315,26 +315,10 @@ public void execute(ClientYamlTestExecutionContext executionContext) throws IOEx Object previousSecond = null; while (firstTokens.hasNext()) { if (false == secondTokens.hasNext()) { - fail( - second - + " has fewer tokens than " - + first - + ". " - + first - + " has [" - + firstTokens.next() - + "] but " - + second - + " is out of tokens. " - + first - + "'s last token was [" - + previousFirst - + "] and " - + second - + "'s last token was' [" - + previousSecond - + "]" - ); + fail(""" + %s has fewer tokens than %s. %s has [%s] but %s is out of tokens. \ + %s's last token was [%s] and %s's last token was' [%s] + """.formatted(second, first, first, firstTokens.next(), second, first, previousFirst, second, previousSecond)); } Map firstToken = (Map) firstTokens.next(); Map secondToken = (Map) secondTokens.next(); @@ -342,20 +326,11 @@ public void execute(ClientYamlTestExecutionContext executionContext) throws IOEx String secondText = (String) secondToken.get("token"); // Check the text and produce an error message with the utf8 sequence if they don't match. if (false == secondText.equals(firstText)) { - fail( - "text differs: " - + first - + " was [" - + firstText - + "] but " - + second - + " was [" - + secondText - + "]. In utf8 those are\n" - + new BytesRef(firstText) - + " and\n" - + new BytesRef(secondText) - ); + fail(""" + text differs: %s was [%s] but %s was [%s]. In utf8 those are + %s and + %s + """.formatted(first, firstText, second, secondText, new BytesRef(firstText), new BytesRef(secondText))); } // Now check the whole map just in case the text matches but something else differs assertEquals(firstToken, secondToken); @@ -363,26 +338,10 @@ public void execute(ClientYamlTestExecutionContext executionContext) throws IOEx previousSecond = secondToken; } if (secondTokens.hasNext()) { - fail( - second - + " has more tokens than " - + first - + ". " - + second - + " has [" - + secondTokens.next() - + "] but " - + first - + " is out of tokens. " - + first - + "'s last token was [" - + previousFirst - + "] and " - + second - + "'s last token was' [" - + previousSecond - + "]" - ); + fail(""" + %s has more tokens than %s. %s has [%s] but %s is out of tokens. \ + %s's last token was [%s] and %s's last token was [%s] + """.formatted(second, first, second, secondTokens.next(), first, first, previousFirst, second, previousSecond)); } } } diff --git a/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java b/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java index 89b1c8390ce25..b668b73443e69 100644 --- a/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java +++ b/libs/grok/src/test/java/org/elasticsearch/grok/GrokTests.java @@ -654,9 +654,10 @@ public void testApacheLog( Tuple, Object> verb, List, Object>> additionalFields ) { - String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " - + "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " - + "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\""; + String logLine = """ + 31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] "GET /logs/access.log HTTP/1.0" 200 69849 "http://8rursodiol.enjin.com" \ + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 \ + Safari/537.36" "www.dlwindianrailways.com\""""; Grok grok = new Grok(Grok.getBuiltinPatterns(ecsCompatibility), "%{COMBINEDAPACHELOG}", logger::warn); Map captureTypes = new HashMap<>(); @@ -739,12 +740,13 @@ public void testComplete() { bank.put("DATA", ".*?"); bank.put("QS", "(?>(?\"(?>\\\\.|[^\\\\\"]+)+\"|\"\"|(?>'(?>\\\\.|[^\\\\']+)+')|''|(?>`(?>\\\\.|[^\\\\`]+)+`)|``))"); - String text = "83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] \"GET /presentations/logstash-monitorama-2013/images/" - + "kibana-dashboard3.png HTTP/1.1\" 200 171717 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" " - + "\"Mozilla" - + "/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""; - String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " - + "HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"; + String text = """ + 83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] "GET /presentations/logstash-monitorama-2013/images/kibana-dashboard3.png \ + HTTP/1.1" 200 171717 "http://semicomplete.com/presentations/logstash-monitorama-2013/" "Mozilla/5.0 (Macintosh; Intel Mac \ + OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""""; + String pattern = """ + %{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] "%{WORD:verb} %{DATA:request} \ + HTTP/%{NUMBER:httpversion}" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"""; Grok grok = new Grok(bank, pattern, logger::warn); assertCaptureConfig( diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java index 3fc353ad0f103..446fb21471961 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ConstructingObjectParserTests.java @@ -159,7 +159,13 @@ public void testBadParam() throws IOException { JsonXContent.jsonXContent, // 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" + "}" + """ + { + "animal": "cat", + "vegetable": 2, + "a": "supercalifragilisticexpialidocious" + } + """ ); XContentParseException e = expectThrows( XContentParseException.class, @@ -178,7 +184,13 @@ public void testBadParamBeforeObjectBuilt() throws IOException { JsonXContent.jsonXContent, // 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" + "}" + """ + { + "a": "supercalifragilisticexpialidocious", + "animal": "cat", + "vegetable": 2 + } + """ ); XContentParseException e = expectThrows( XContentParseException.class, @@ -264,7 +276,8 @@ void setFoo(String foo) { } public void testIgnoreUnknownFields() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : \"foo\", \"junk\" : 2 }"); + XContentParser parser = createParser(JsonXContent.jsonXContent, """ + { "test" : "foo", "junk" : 2 }"""); class TestStruct { public final String test; @@ -283,7 +296,8 @@ class TestStruct { } public void testConstructObjectUsingContext() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"animal\": \"dropbear\", \"mineral\": -8 }"); + 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); @@ -423,10 +437,8 @@ public void testParseNamedObject() throws IOException { } public void testParseNamedObjectInOrder() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\"named\": [ {\"a\": {}} ], \"named_in_constructor\": [ {\"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); @@ -436,10 +448,8 @@ public void testParseNamedObjectInOrder() throws IOException { } public void testParseNamedObjectTwoFieldsInArray() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\"named\": [ {\"a\": {}, \"b\": {}}], \"named_in_constructor\": [ {\"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( @@ -452,10 +462,8 @@ public void testParseNamedObjectTwoFieldsInArray() throws IOException { } public void testParseNamedObjectTwoFieldsInArrayConstructorArg() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\"named\": [ {\"a\": {}}], \"named_in_constructor\": [ {\"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( @@ -494,10 +502,8 @@ public void testParseNamedObjectNoFieldsInArrayConstructorArg() throws IOExcepti } public void testParseNamedObjectJunkInArray() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\"named\": [ \"junk\" ], \"named_in_constructor\": [ {\"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( @@ -510,10 +516,8 @@ public void testParseNamedObjectJunkInArray() throws IOException { } public void testParseNamedObjectJunkInArrayConstructorArg() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\"named\": [ {\"a\": {}} ], \"named_in_constructor\": [ \"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( @@ -526,10 +530,13 @@ 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": [ { "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") @@ -551,10 +558,13 @@ public void testParseNamedObjectInOrderNotSupported() throws IOException { } public void testParseNamedObjectInOrderNotSupportedConstructorArg() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\"named\": {\"a\": {}}, \"named_in_constructor\": [ {\"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") @@ -800,21 +810,15 @@ private static void failWithException() { public void testRemovalOfField() throws IOException { { // old_name with NO compatibility is resulting in an exception - XContentParser parser = createParserWithCompatibilityFor( - JsonXContent.jsonXContent, - "{\"old_name\": 1, \"second_field\": \"someString\"}", - RestApiVersion.current() - ); + XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, """ + {"old_name": 1, "second_field": "someString"}""", RestApiVersion.current()); expectThrows(XContentParseException.class, () -> StructRemovalField.PARSER.parse(parser, null)); } { // old_name with compatibility is still parsed, but ignored and results in a warning - XContentParser parser = createParserWithCompatibilityFor( - JsonXContent.jsonXContent, - "{\"old_name\": 1, \"second_field\": \"someString\"}", - RestApiVersion.minimumSupported() - ); + XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, """ + {"old_name": 1, "second_field": "someString"}""", RestApiVersion.minimumSupported()); StructRemovalField parse = StructRemovalField.PARSER.parse(parser, null); assertCriticalWarnings("The field old_name has been removed and is being ignored"); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/DotExpandingXContentParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/DotExpandingXContentParserTests.java index bc346cb2d0fab..a0c4bf5b4a6bb 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/DotExpandingXContentParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/DotExpandingXContentParserTests.java @@ -26,34 +26,36 @@ private void assertXContentMatches(String expected, String actual) throws IOExce public void testEmbeddedObject() throws IOException { - assertXContentMatches( - "{\"test\":{\"with\":{\"dots\":{\"field\":\"value\"}}},\"nodots\":\"value2\"}", - "{\"test.with.dots\":{\"field\":\"value\"},\"nodots\":\"value2\"}" - ); + assertXContentMatches(""" + {"test":{"with":{"dots":{"field":"value"}}},"nodots":"value2"}\ + """, """ + {"test.with.dots":{"field":"value"},"nodots":"value2"}\ + """); } public void testEmbeddedArray() throws IOException { - assertXContentMatches( - "{\"test\":{\"with\":{\"dots\":[\"field\",\"value\"]}},\"nodots\":\"value2\"}", - "{\"test.with.dots\":[\"field\",\"value\"],\"nodots\":\"value2\"}" - ); + assertXContentMatches(""" + {"test":{"with":{"dots":["field","value"]}},"nodots":"value2"}\ + """, """ + {"test.with.dots":["field","value"],"nodots":"value2"}\ + """); } public void testEmbeddedValue() throws IOException { - assertXContentMatches( - "{\"test\":{\"with\":{\"dots\":\"value\"}},\"nodots\":\"value2\"}", - "{\"test.with.dots\":\"value\",\"nodots\":\"value2\"}" - ); + assertXContentMatches(""" + {"test":{"with":{"dots":"value"}},"nodots":"value2"}\ + """, """ + {"test.with.dots":"value","nodots":"value2"}\ + """); } public void testSkipChildren() throws IOException { - XContentParser parser = DotExpandingXContentParser.expandDots( - createParser(JsonXContent.jsonXContent, "{ \"test.with.dots\" : \"value\", \"nodots\" : \"value2\" }") - ); + XContentParser parser = DotExpandingXContentParser.expandDots(createParser(JsonXContent.jsonXContent, """ + { "test.with.dots" : "value", "nodots" : "value2" }""")); parser.nextToken(); // start object assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); @@ -76,9 +78,10 @@ public void testSkipChildren() throws IOException { } public void testNestedExpansions() throws IOException { - assertXContentMatches( - "{\"first\":{\"dot\":{\"second\":{\"dot\":\"value\"},\"third\":\"value\"}},\"nodots\":\"value\"}", - "{\"first.dot\":{\"second.dot\":\"value\",\"third\":\"value\"},\"nodots\":\"value\"}" - ); + assertXContentMatches(""" + {"first":{"dot":{"second":{"dot":"value"},"third":"value"}},"nodots":"value"}\ + """, """ + {"first.dot":{"second.dot":"value","third":"value"},"nodots":"value"}\ + """); } } diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java index 8661a417491f3..874cc28028000 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/InstantiatingObjectParserTests.java @@ -201,7 +201,9 @@ public void testAnnotation() throws IOException { builder.declareString(constructorArg(), new ParseField("b")); builder.declareString(constructorArg(), new ParseField("c")); InstantiatingObjectParser parser = builder.build(); - try (XContentParser contentParser = createParser(JsonXContent.jsonXContent, "{\"a\": 5, \"b\":\"6\", \"c\": \"7\"}")) { + try (XContentParser contentParser = createParser(JsonXContent.jsonXContent, """ + {"a": 5, "b":"6", "c": "7"} + """)) { assertThat(parser.parse(contentParser, null), equalTo(new Annotations(5, "6", 7))); } } @@ -296,7 +298,9 @@ public void testContextAsArgument() throws IOException { builder.declareString(constructorArg(), new ParseField("b")); builder.declareString(constructorArg(), new ParseField("c")); InstantiatingObjectParser parser = builder.build(); - try (XContentParser contentParser = createParser(JsonXContent.jsonXContent, "{\"a\": 5, \"b\":\"6\", \"c\": \"7\"}")) { + try (XContentParser contentParser = createParser(JsonXContent.jsonXContent, """ + {"a": 5, "b":"6", "c": "7"} + """)) { assertThat(parser.parse(contentParser, "context"), equalTo(new ContextArgument("context", 5, "6", 7))); } } diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java index 3bfb29bc2a269..14d330af0400d 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/ObjectParserTests.java @@ -188,10 +188,8 @@ public URI parseURI(XContentParser xContentParser) { } } } - 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")); @@ -533,7 +531,15 @@ public void setString_or_null(String string_or_null) { } public void testParseNamedObject() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": { \"a\": {\"foo\" : 11} }, \"bar\": \"baz\"}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, """ + { + "named": { + "a": { + "foo": 11 + } + }, + "bar": "baz" + }"""); NamedObjectHolder h = NamedObjectHolder.PARSER.apply(parser, null); assertEquals("a", h.named.name); assertEquals(11, h.named.foo); @@ -541,7 +547,8 @@ public void testParseNamedObject() throws IOException { } public void testParseNamedObjectUnexpectedArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ \"a\": {\"foo\" : 11} }]"); + XContentParser parser = createParser(JsonXContent.jsonXContent, """ + {"named": [ "a": {"foo" : 11} }]"""); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] named doesn't support values of type: START_ARRAY")); } @@ -563,7 +570,8 @@ public void testParseNamedObjectsInOrder() throws IOException { } public void testParseNamedObjectsTwoFieldsInArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}, \"b\": {}}]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, """ + {"named": [ {"a": {}, "b": {}}]}"""); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectsHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_objects_holder] failed to parse field [named]")); assertThat( @@ -755,13 +763,14 @@ public void testNoopDeclareObject() throws IOException { assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get()); Exception garbageException = expectThrows( IllegalStateException.class, - () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": {\"garbage\": \"shouldn't\"}}"), null) + () -> parser.parse(createParser(JsonXContent.jsonXContent, """ + {"noop": {"garbage": "shouldn't"}} + """), null) ); assertEquals("parser for [noop] did not end on END_OBJECT", garbageException.getMessage()); - Exception sneakyException = expectThrows( - IllegalStateException.class, - () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": {\"body\": \"shouldn't\"}}"), null) - ); + Exception sneakyException = expectThrows(IllegalStateException.class, () -> parser.parse(createParser(JsonXContent.jsonXContent, """ + {"noop": {"body": "shouldn't"}} + """), null)); assertEquals("parser for [noop] did not end on END_OBJECT", sneakyException.getMessage()); } @@ -785,12 +794,16 @@ public void testNoopDeclareObjectArray() { XContentParseException garbageError = expectThrows( XContentParseException.class, - () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"garbage\": \"shouldn't\"}}]"), null) + () -> parser.parse(createParser(JsonXContent.jsonXContent, """ + {"noop": [{"garbage": "shouldn't"}}] + """), null) ); assertEquals("expected value but got [FIELD_NAME]", garbageError.getCause().getMessage()); XContentParseException sneakyError = expectThrows( XContentParseException.class, - () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"body\": \"shouldn't\"}}]"), null) + () -> parser.parse(createParser(JsonXContent.jsonXContent, """ + {"noop": [{"body": "shouldn't"}}] + """), null) ); assertEquals("expected value but got [FIELD_NAME]", sneakyError.getCause().getMessage()); } @@ -879,18 +892,16 @@ void setName(String name) { } public void testConsumeUnknownFields() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\n" - + " \"test\" : \"foo\",\n" - + " \"test_number\" : 2,\n" - + " \"name\" : \"geoff\",\n" - + " \"test_boolean\" : true,\n" - + " \"test_null\" : null,\n" - + " \"test_array\": [1,2,3,4],\n" - + " \"test_nested\": { \"field\" : \"value\", \"field2\" : [ \"list1\", \"list2\" ] }\n" - + "}" - ); + XContentParser parser = createParser(JsonXContent.jsonXContent, """ + { + "test" : "foo", + "test_number" : 2, + "name" : "geoff", + "test_boolean" : true, + "test_null" : null, + "test_array": [1,2,3,4], + "test_nested": { "field" : "value", "field2" : [ "list1", "list2" ] } + }"""); ObjectParser op = new ObjectParser<>( "unknown", ObjectWithArbitraryFields::setField, @@ -943,7 +954,8 @@ private void setB(long value) { assertThat(obj.a, nullValue()); assertThat(obj.b, equalTo(123L)); - parser = createParser(JsonXContent.jsonXContent, "{\"a\": \"123\", \"b\": \"456\"}"); + parser = createParser(JsonXContent.jsonXContent, """ + {"a": "123", "b": "456"}"""); objectParser = new ObjectParser<>("foo", true, TestStruct::new); objectParser.declareLong(TestStruct::setA, new ParseField("a")); objectParser.declareLong(TestStruct::setB, new ParseField("b")); diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java index 5a7ba68914086..7b063f404a068 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/XContentParserTests.java @@ -130,10 +130,12 @@ private void assertReadListThrowsException(String source) { } public void testReadMapStrings() throws IOException { - Map map = readMapStrings("{\"foo\": {\"kbar\":\"vbar\"}}"); + Map map = readMapStrings(""" + {"foo": {"kbar":"vbar"}}"""); assertThat(map.get("kbar"), equalTo("vbar")); assertThat(map.size(), equalTo(1)); - map = readMapStrings("{\"foo\": {\"kbar\":\"vbar\", \"kbaz\":\"vbaz\"}}"); + map = readMapStrings(""" + {"foo": {"kbar":"vbar", "kbaz":"vbaz"}}"""); assertThat(map.get("kbar"), equalTo("vbar")); assertThat(map.get("kbaz"), equalTo("vbaz")); assertThat(map.size(), equalTo(2)); @@ -142,8 +144,26 @@ public void testReadMapStrings() throws IOException { } public void testMap() throws IOException { - String source = "{\"i\": {\"_doc\": {\"f1\": {\"type\": \"text\", \"analyzer\": \"english\"}, " - + "\"f2\": {\"type\": \"object\", \"properties\": {\"sub1\": {\"type\": \"keyword\", \"foo\": 17}}}}}}"; + String source = """ + { + "i": { + "_doc": { + "f1": { + "type": "text", + "analyzer": "english" + }, + "f2": { + "type": "object", + "properties": { + "sub1": { + "type": "keyword", + "foo": 17 + } + } + } + } + } + }"""; Map f1 = new HashMap<>(); f1.put("type", "text"); f1.put("analyzer", "english"); @@ -339,11 +359,24 @@ public void testNestedMapInList() throws IOException { } public void testGenericMap() throws IOException { - String content = "{" - + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " - + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " - + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" - + "}"; + String content = """ + { + "c": { + "i": 3, + "d": 0.3, + "s": "ccc" + }, + "a": { + "i": 1, + "d": 0.1, + "s": "aaa" + }, + "b": { + "i": 2, + "d": 0.2, + "s": "bbb" + } + }"""; SimpleStruct structA = new SimpleStruct(1, 0.1, "aaa"); SimpleStruct structB = new SimpleStruct(2, 0.2, "bbb"); SimpleStruct structC = new SimpleStruct(3, 0.3, "ccc"); @@ -357,11 +390,24 @@ public void testGenericMap() throws IOException { } public void testGenericMapOrdered() throws IOException { - String content = "{" - + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " - + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " - + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" - + "}"; + String content = """ + { + "c": { + "i": 3, + "d": 0.3, + "s": "ccc" + }, + "a": { + "i": 1, + "d": 0.1, + "s": "aaa" + }, + "b": { + "i": 2, + "d": 0.2, + "s": "bbb" + } + }"""; SimpleStruct structA = new SimpleStruct(1, 0.1, "aaa"); SimpleStruct structB = new SimpleStruct(2, 0.2, "bbb"); SimpleStruct structC = new SimpleStruct(3, 0.3, "ccc"); @@ -376,11 +422,24 @@ public void testGenericMapOrdered() throws IOException { } public void testGenericMap_Failure_MapContainingUnparsableValue() throws IOException { - String content = "{" - + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " - + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": 666 }, " - + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }" - + "}"; + String content = """ + { + "a": { + "i": 1, + "d": 0.1, + "s": "aaa" + }, + "b": { + "i": 2, + "d": 0.2, + "s": 666 + }, + "c": { + "i": 3, + "d": 0.3, + "s": "ccc" + } + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, content)) { XContentParseException exception = expectThrows( XContentParseException.class, diff --git a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java index d7c6eba6d0c97..3612f32978f2a 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java @@ -25,271 +25,727 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { private final JsonFactory JSON_FACTORY = new JsonFactory(); public void testInclusiveFilters() throws Exception { - final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; - - assertResult(SAMPLE, "a", true, "{'a':0}"); - assertResult(SAMPLE, "b", true, "{'b':true}"); - assertResult(SAMPLE, "c", true, "{'c':'c_value'}"); - assertResult(SAMPLE, "d", true, "{'d':[0,1,2]}"); - assertResult(SAMPLE, "e", true, "{'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + final String SAMPLE = """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""; + + assertResult(SAMPLE, "a", true, """ + { + "a": 0 + }"""); + assertResult(SAMPLE, "b", true, """ + { + "b": true + }"""); + assertResult(SAMPLE, "c", true, """ + { + "c": "c_value" + }"""); + assertResult(SAMPLE, "d", true, """ + { + "d": [ 0, 1, 2 ] + }"""); + assertResult(SAMPLE, "e", true, """ + { + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); assertResult(SAMPLE, "z", true, ""); - - assertResult(SAMPLE, "e.f1", true, "{'e':[{'f1':'f1_value'}]}"); - assertResult(SAMPLE, "e.f2", true, "{'e':[{'f2':'f2_value'}]}"); - assertResult(SAMPLE, "e.f*", true, "{'e':[{'f1':'f1_value','f2':'f2_value'}]}"); - assertResult(SAMPLE, "e.*2", true, "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); - - assertResult(SAMPLE, "h.i", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "h.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "*.i", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "*.i.j", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*.j", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "*.i.j.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*.j.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.*.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "*.i.j.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*.j.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.*.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.*.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.k.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "h.*.j.*.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "**.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - - assertResult(SAMPLE, "**.*2", true, "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); - - assertResult(SAMPLE, "h.i.j.k.l,h.i.j.k.l.m", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "e.f1", true, """ + { + "e": [ { "f1": "f1_value" } ] + }"""); + assertResult(SAMPLE, "e.f2", true, """ + { + "e": [ { "f2": "f2_value" } ] + }"""); + assertResult(SAMPLE, "e.f*", true, """ + { + "e": [ { "f1": "f1_value", "f2": "f2_value" } ] + }"""); + assertResult(SAMPLE, "e.*2", true, """ + { + "e": [ { "f2": "f2_value" }, { "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "h.i", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.j", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.j.k", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.j.k.l", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "h.*", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "*.i", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "*.i.j", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.*.j", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.*", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "*.i.j.k", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.*.j.k", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.*.k", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.j.*", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "*.i.j.k.l", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.*.j.k.l", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.*.k.l", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.j.*.l", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h.i.j.k.*", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "h.*.j.*.l", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "**.l", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "**.*2", true, """ + { + "e": [ { "f2": "f2_value" }, { "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "h.i.j.k.l,h.i.j.k.l.m", true, """ + { + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); assertResult(SAMPLE, "a,b,c,d,e.f1,e.f2,e.g1,e.g2,h.i.j.k.l", true, SAMPLE); assertResult(SAMPLE, "", true, ""); assertResult(SAMPLE, "h.", true, ""); } public void testExclusiveFilters() throws Exception { - final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; - - assertResult( - SAMPLE, - "a", - false, - "{'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - assertResult( - SAMPLE, - "b", - false, - "{'a':0,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - assertResult( - SAMPLE, - "c", - false, - "{'a':0,'b':true,'d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - assertResult( - SAMPLE, - "d", - false, - "{'a':0,'b':true,'c':'c_value','e':[{'f1':'f1_value','f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - assertResult(SAMPLE, "e", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult( - SAMPLE, - "h", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "z", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - - assertResult( - SAMPLE, - "e.f1", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f2':'f2_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - assertResult( - SAMPLE, - "e.f2", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'}," - + "{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - assertResult( - SAMPLE, - "e.f*", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'g1':'g1_value','g2':'g2_value'}]," + "'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - assertResult( - SAMPLE, - "e.*2", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}]," - + "'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - - assertResult( - SAMPLE, - "h.i", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.j", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.j.k", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.j.k.l", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - - assertResult( - SAMPLE, - "h.*", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "*.i", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - - assertResult( - SAMPLE, - "*.i.j", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.*.j", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.*", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - - assertResult( - SAMPLE, - "*.i.j.k", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.*.j.k", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.*.k", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.j.*", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - - assertResult( - SAMPLE, - "*.i.j.k.l", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.*.j.k.l", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.*.k.l", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.j.*.l", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "h.i.j.k.*", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - - assertResult( - SAMPLE, - "h.*.j.*.l", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - assertResult( - SAMPLE, - "**.l", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); - - assertResult( - SAMPLE, - "**.*2", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'}," - + "{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}" - ); - - assertResult( - SAMPLE, - "h.i.j.k.l,h.i.j.k.l.m", - false, - "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'}," + "{'g1':'g1_value','g2':'g2_value'}]}" - ); + final String SAMPLE = """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""; + + assertResult(SAMPLE, "a", false, """ + { + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "b", false, """ + { + "a": 0, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "c", false, """ + { + "a": 0, + "b": true, + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "d", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "e", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "h", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "z", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "e.f1", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "e.f2", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value" }, { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "e.f*", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "g1": "g1_value", "g2": "g2_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + assertResult(SAMPLE, "e.*2", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value" }, { "g1": "g1_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "h.i", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.j", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.j.k", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.j.k.l", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "h.*", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "*.i", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "*.i.j", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.*.j", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.*", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "*.i.j.k", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.*.j.k", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.*.k", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.j.*", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "*.i.j.k.l", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.*.j.k.l", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.*.k.l", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.j.*.l", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "h.i.j.k.*", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "h.*.j.*.l", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + assertResult(SAMPLE, "**.l", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); + + assertResult(SAMPLE, "**.*2", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value" }, { "g1": "g1_value" } ], + "h": { + "i": { + "j": { + "k": { + "l": "l_value" + } + } + } + } + }"""); + + assertResult(SAMPLE, "h.i.j.k.l,h.i.j.k.l.m", false, """ + { + "a": 0, + "b": true, + "c": "c_value", + "d": [ 0, 1, 2 ], + "e": [ { "f1": "f1_value", "f2": "f2_value" }, { "g1": "g1_value", "g2": "g2_value" } ] + }"""); assertResult(SAMPLE, "a,b,c,d,e.f1,e.f2,e.g1,e.g2,h.i.j.k.l", false, ""); assertResult(SAMPLE, "", false, SAMPLE); @@ -297,13 +753,59 @@ public void testExclusiveFilters() throws Exception { } public void testInclusiveFiltersWithDots() throws Exception { - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b.c", true, "{'b':{'c':'c_value'}}"); - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b\\.c", true, "{'b.c':'value'}"); + assertResult(""" + { + "a": 0, + "b.c": "value", + "b": { + "c": "c_value" + } + }""", "b.c", true, """ + { + "b": { + "c": "c_value" + } + }"""); + assertResult(""" + { + "a": 0, + "b.c": "value", + "b": { + "c": "c_value" + } + }""", "b\\.c", true, """ + { + "b.c": "value" + }"""); } public void testExclusiveFiltersWithDots() throws Exception { - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b.c", false, "{'a':0,'b.c':'value'}"); - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b\\.c", false, "{'a':0,'b':{'c':'c_value'}}"); + assertResult(""" + { + "a": 0, + "b.c": "value", + "b": { + "c": "c_value" + } + }""", "b.c", false, """ + { + "a": 0, + "b.c": "value" + }"""); + assertResult(""" + { + "a": 0, + "b.c": "value", + "b": { + "c": "c_value" + } + }""", "b\\.c", false, """ + { + "a": 0, + "b": { + "c": "c_value" + } + }"""); } private void assertResult(String input, String filter, boolean inclusive, String expected) throws Exception { @@ -316,17 +818,13 @@ private void assertResult(String input, String filter, boolean inclusive, String true ) ) { - try (JsonParser parser = JSON_FACTORY.createParser(replaceQuotes(input))) { + try (JsonParser parser = JSON_FACTORY.createParser(input)) { while (parser.nextToken() != null) { generator.copyCurrentStructure(parser); } } } - assertThat(os.bytes().utf8ToString(), equalTo(replaceQuotes(expected))); + assertThat(os.bytes().utf8ToString().replaceAll("\\s", ""), equalTo(expected.replaceAll("\\s", ""))); } } - - private String replaceQuotes(String s) { - return s.replace('\'', '"'); - } } diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java index 99712519da3ab..5b3fb137b9be6 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESSolrSynonymParserTests.java @@ -28,7 +28,10 @@ public class ESSolrSynonymParserTests extends ESTokenStreamTestCase { public void testLenientParser() throws IOException, ParseException { ESSolrSynonymParser parser = new ESSolrSynonymParser(true, false, true, new StandardAnalyzer()); - String rules = "&,and\n" + "come,advance,approach\n"; + String rules = """ + &,and + come,advance,approach + """; StringReader rulesReader = new StringReader(rules); parser.parse(rulesReader); SynonymMap synonymMap = parser.build(); @@ -54,7 +57,10 @@ public void testLenientParserWithSomeIncorrectLines() throws IOException, ParseE public void testNonLenientParser() { ESSolrSynonymParser parser = new ESSolrSynonymParser(true, false, false, new StandardAnalyzer()); - String rules = "&,and=>and\n" + "come,advance,approach\n"; + String rules = """ + &,and=>and + come,advance,approach + """; StringReader rulesReader = new StringReader(rules); ParseException ex = expectThrows(ParseException.class, () -> parser.parse(rulesReader)); assertThat(ex.getMessage(), containsString("Invalid synonym rule at line 1")); diff --git a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java index 2bf4c6c830e37..c30d0c85703a2 100644 --- a/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java +++ b/modules/analysis-common/src/test/java/org/elasticsearch/analysis/common/ESWordnetSynonymParserTests.java @@ -28,11 +28,12 @@ public class ESWordnetSynonymParserTests extends ESTokenStreamTestCase { public void testLenientParser() throws IOException, ParseException { ESWordnetSynonymParser parser = new ESWordnetSynonymParser(true, false, true, new StandardAnalyzer()); - String rules = "s(100000001,1,'&',a,1,0).\n" - + "s(100000001,2,'and',a,1,0).\n" - + "s(100000002,1,'come',v,1,0).\n" - + "s(100000002,2,'advance',v,1,0).\n" - + "s(100000002,3,'approach',v,1,0)."; + String rules = """ + s(100000001,1,'&',a,1,0). + s(100000001,2,'and',a,1,0). + s(100000002,1,'come',v,1,0). + s(100000002,2,'advance',v,1,0). + s(100000002,3,'approach',v,1,0)."""; StringReader rulesReader = new StringReader(rules); parser.parse(rulesReader); SynonymMap synonymMap = parser.build(); @@ -46,7 +47,10 @@ public void testLenientParserWithSomeIncorrectLines() throws IOException, ParseE CharArraySet stopSet = new CharArraySet(1, true); stopSet.add("bar"); ESWordnetSynonymParser parser = new ESWordnetSynonymParser(true, false, true, new StandardAnalyzer(stopSet)); - String rules = "s(100000001,1,'foo',v,1,0).\n" + "s(100000001,2,'bar',v,1,0).\n" + "s(100000001,3,'baz',v,1,0)."; + String rules = """ + s(100000001,1,'foo',v,1,0). + s(100000001,2,'bar',v,1,0). + s(100000001,3,'baz',v,1,0)."""; StringReader rulesReader = new StringReader(rules); parser.parse(rulesReader); SynonymMap synonymMap = parser.build(); @@ -58,11 +62,12 @@ public void testLenientParserWithSomeIncorrectLines() throws IOException, ParseE public void testNonLenientParser() { ESWordnetSynonymParser parser = new ESWordnetSynonymParser(true, false, false, new StandardAnalyzer()); - String rules = "s(100000001,1,'&',a,1,0).\n" - + "s(100000001,2,'and',a,1,0).\n" - + "s(100000002,1,'come',v,1,0).\n" - + "s(100000002,2,'advance',v,1,0).\n" - + "s(100000002,3,'approach',v,1,0)."; + String rules = """ + s(100000001,1,'&',a,1,0). + s(100000001,2,'and',a,1,0). + s(100000002,1,'come',v,1,0). + s(100000002,2,'advance',v,1,0). + s(100000002,3,'approach',v,1,0)."""; StringReader rulesReader = new StringReader(rules); ParseException ex = expectThrows(ParseException.class, () -> parser.parse(rulesReader)); assertThat(ex.getMessage(), containsString("Invalid synonym rule at line 1")); diff --git a/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java b/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java index 9df7c1988ccb7..c10e71d0a98db 100644 --- a/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java +++ b/modules/ingest-common/src/internalClusterTest/java/org/elasticsearch/ingest/common/IngestRestartIT.java @@ -61,29 +61,27 @@ public void testFailureInConditionalProcessor() { internalCluster().ensureAtLeastNumDataNodes(1); internalCluster().startMasterOnlyNode(); final String pipelineId = "foo"; - client().admin() - .cluster() - .preparePutPipeline( - pipelineId, - new BytesArray( - "{\n" - + " \"processors\" : [\n" - + " {\"set\" : {\"field\": \"any_field\", \"value\": \"any_value\"}},\n" - + " {\"set\" : {" - + "" - + " \"if\" : " - + "{\"lang\": \"" - + MockScriptEngine.NAME - + "\", \"source\": \"throwing_script\"}," - + " \"field\": \"any_field2\"," - + " \"value\": \"any_value2\"}" - + " }\n" - + " ]\n" - + "}" - ), - XContentType.JSON - ) - .get(); + client().admin().cluster().preparePutPipeline(pipelineId, new BytesArray(""" + { + "processors": [ + { + "set": { + "field": "any_field", + "value": "any_value" + } + }, + { + "set": { + "if": { + "lang": "%s", + "source": "throwing_script" + }, + "field": "any_field2", + "value": "any_value2" + } + } + ] + }""".formatted(MockScriptEngine.NAME)), XContentType.JSON).get(); Exception e = expectThrows( Exception.class, @@ -111,18 +109,14 @@ public void testScriptDisabled() throws Exception { String pipelineIdWithScript = pipelineIdWithoutScript + "_script"; internalCluster().startNode(); - BytesReference pipelineWithScript = new BytesArray( - "{\n" - + " \"processors\" : [\n" - + " {\"script\" : {\"lang\": \"" - + MockScriptEngine.NAME - + "\", \"source\": \"my_script\"}}\n" - + " ]\n" - + "}" - ); - BytesReference pipelineWithoutScript = new BytesArray( - "{\n" + " \"processors\" : [\n" + " {\"set\" : {\"field\": \"y\", \"value\": 0}}\n" + " ]\n" + "}" - ); + BytesReference pipelineWithScript = new BytesArray(""" + { + "processors": [ { "script": { "lang": "%s", "source": "my_script" } } ] + }""".formatted(MockScriptEngine.NAME)); + BytesReference pipelineWithoutScript = new BytesArray(""" + { + "processors": [ { "set": { "field": "y", "value": 0 } } ] + }"""); Consumer checkPipelineExists = (id) -> assertThat( client().admin().cluster().prepareGetPipeline(id).get().pipelines().get(0).getId(), @@ -185,23 +179,16 @@ public Settings onNodeStopped(String nodeName) { public void testPipelineWithScriptProcessorThatHasStoredScript() throws Exception { internalCluster().startNode(); - client().admin() - .cluster() - .preparePutStoredScript() - .setId("1") - .setContent( - new BytesArray("{\"script\": {\"lang\": \"" + MockScriptEngine.NAME + "\", \"source\": \"my_script\"} }"), - XContentType.JSON - ) - .get(); - BytesReference pipeline = new BytesArray( - "{\n" - + " \"processors\" : [\n" - + " {\"set\" : {\"field\": \"y\", \"value\": 0}},\n" - + " {\"script\" : {\"id\": \"1\"}}\n" - + " ]\n" - + "}" - ); + client().admin().cluster().preparePutStoredScript().setId("1").setContent(new BytesArray(""" + {"script": {"lang": "%s", "source": "my_script"} } + """.formatted(MockScriptEngine.NAME)), XContentType.JSON).get(); + BytesReference pipeline = new BytesArray(""" + { + "processors" : [ + {"set" : {"field": "y", "value": 0}}, + {"script" : {"id": "1"}} + ] + }"""); client().admin().cluster().preparePutPipeline("_id", pipeline, XContentType.JSON).get(); client().prepareIndex("index") @@ -240,9 +227,12 @@ public void testWithDedicatedIngestNode() throws Exception { String node = internalCluster().startNode(); String ingestNode = internalCluster().startNode(onlyRole(DiscoveryNodeRole.INGEST_ROLE)); - BytesReference pipeline = new BytesArray( - "{\n" + " \"processors\" : [\n" + " {\"set\" : {\"field\": \"y\", \"value\": 0}}\n" + " ]\n" + "}" - ); + BytesReference pipeline = new BytesArray(""" + { + "processors" : [ + {"set" : {"field": "y", "value": 0}} + ] + }"""); client().admin().cluster().preparePutPipeline("_id", pipeline, XContentType.JSON).get(); client().prepareIndex("index") diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java index d21b06cb74356..a9596fb0083ab 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/JsonProcessorTests.java @@ -192,7 +192,8 @@ public void testAddToRootRecursiveMerge() throws Exception { JsonProcessor jsonProcessor = new JsonProcessor(processorTag, null, "json", null, true, MERGE, false); Map document = new HashMap<>(); - String json = "{\"foo\": {\"bar\": \"baz\"}}"; + String json = """ + {"foo": {"bar": "baz"}}"""; document.put("json", json); Map inner = new HashMap<>(); inner.put("bar", "override_me"); @@ -211,7 +212,8 @@ public void testAddToRootNonRecursiveMerge() throws Exception { JsonProcessor jsonProcessor = new JsonProcessor(processorTag, null, "json", null, true, REPLACE, false); Map document = new HashMap<>(); - String json = "{\"foo\": {\"bar\": \"baz\"}}"; + String json = """ + {"foo": {"bar": "baz"}}"""; document.put("json", json); Map inner = new HashMap<>(); inner.put("bar", "override_me"); diff --git a/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java b/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java index f79b9e01aa854..99f9e7af104c0 100644 --- a/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java +++ b/modules/ingest-geoip/qa/file-based-update/src/test/java/org/elasticsearch/ingest/geoip/UpdateDatabasesIT.java @@ -30,8 +30,13 @@ public class UpdateDatabasesIT extends ESRestTestCase { public void test() throws Exception { - String body = "{\"pipeline\":{\"processors\":[{\"geoip\":{\"field\":\"ip\"}}]}," - + "\"docs\":[{\"_index\":\"index\",\"_id\":\"id\",\"_source\":{\"ip\":\"89.160.20.128\"}}]}"; + String body = """ + { + "pipeline": { + "processors": [ { "geoip": { "field": "ip" } } ] + }, + "docs": [ { "_index": "index", "_id": "id", "_source": { "ip": "89.160.20.128" } } ] + }"""; Request simulatePipelineRequest = new Request("POST", "/_ingest/pipeline/_simulate"); simulatePipelineRequest.setJsonEntity(body); { diff --git a/modules/kibana/src/javaRestTest/java/org/elasticsearch/kibana/KibanaSystemIndexIT.java b/modules/kibana/src/javaRestTest/java/org/elasticsearch/kibana/KibanaSystemIndexIT.java index 50b72b905fd6d..a368515dfd8da 100644 --- a/modules/kibana/src/javaRestTest/java/org/elasticsearch/kibana/KibanaSystemIndexIT.java +++ b/modules/kibana/src/javaRestTest/java/org/elasticsearch/kibana/KibanaSystemIndexIT.java @@ -69,14 +69,20 @@ public void testAliases() throws IOException { public void testBulkToKibanaIndex() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity("{ \"index\" : { \"_index\" : \"" + indexName + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n"); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + """.formatted(indexName)); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(200)); } public void testRefresh() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity("{ \"index\" : { \"_index\" : \"" + indexName + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n"); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + """.formatted(indexName)); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(200)); @@ -94,7 +100,10 @@ public void testRefresh() throws IOException { public void testGetFromKibanaIndex() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity("{ \"index\" : { \"_index\" : \"" + indexName + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n"); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + """.formatted(indexName)); request.addParameter("refresh", "true"); Response response = client().performRequest(request); @@ -110,28 +119,31 @@ public void testGetFromKibanaIndex() throws IOException { public void testMultiGetFromKibanaIndex() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity( - "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n" - + "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"2\" } }\n{ \"baz\" : \"tag\" }\n" - ); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + { "index" : { "_index" : "%s", "_id" : "2" } } + { "baz" : "tag" } + """.formatted(indexName, indexName)); request.addParameter("refresh", "true"); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(200)); Request getRequest = request("GET", "/_mget"); - getRequest.setJsonEntity( - "{ \"docs\" : [ { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"1\" }, " - + "{ \"_index\" : \"" - + indexName - + "\", \"_id\" : \"2\" } ] }\n" - ); + getRequest.setJsonEntity(""" + { + "docs": [ + { + "_index": "%s", + "_id": "1" + }, + { + "_index": "%s", + "_id": "2" + } + ] + }""".formatted(indexName, indexName)); Response getResponse = client().performRequest(getRequest); assertThat(getResponse.getStatusLine().getStatusCode(), is(200)); String responseBody = EntityUtils.toString(getResponse.getEntity()); @@ -143,21 +155,21 @@ public void testMultiGetFromKibanaIndex() throws IOException { public void testSearchFromKibanaIndex() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity( - "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n" - + "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"2\" } }\n{ \"baz\" : \"tag\" }\n" - ); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + { "index" : { "_index" : "%s", "_id" : "2" } } + { "baz" : "tag" } + """.formatted(indexName, indexName)); request.addParameter("refresh", "true"); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(200)); Request searchRequest = request("GET", "/" + indexName + "/_search"); - searchRequest.setJsonEntity("{ \"query\" : { \"match_all\" : {} } }\n"); + searchRequest.setJsonEntity(""" + { "query" : { "match_all" : {} } } + """); Response getResponse = client().performRequest(searchRequest); assertThat(getResponse.getStatusLine().getStatusCode(), is(200)); String responseBody = EntityUtils.toString(getResponse.getEntity()); @@ -169,14 +181,12 @@ public void testSearchFromKibanaIndex() throws IOException { public void testDeleteFromKibanaIndex() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity( - "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n" - + "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"2\" } }\n{ \"baz\" : \"tag\" }\n" - ); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + { "index" : { "_index" : "%s", "_id" : "2" } } + { "baz" : "tag" } + """.formatted(indexName, indexName)); request.addParameter("refresh", "true"); Response response = client().performRequest(request); @@ -189,21 +199,21 @@ public void testDeleteFromKibanaIndex() throws IOException { public void testDeleteByQueryFromKibanaIndex() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity( - "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n" - + "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"2\" } }\n{ \"baz\" : \"tag\" }\n" - ); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + { "index" : { "_index" : "%s", "_id" : "2" } } + { "baz" : "tag" } + """.formatted(indexName, indexName)); request.addParameter("refresh", "true"); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(200)); Request dbqRequest = request("POST", "/" + indexName + "/_delete_by_query"); - dbqRequest.setJsonEntity("{ \"query\" : { \"match_all\" : {} } }\n"); + dbqRequest.setJsonEntity(""" + { "query" : { "match_all" : {} } } + """); Response dbqResponse = client().performRequest(dbqRequest); assertThat(dbqResponse.getStatusLine().getStatusCode(), is(200)); } @@ -279,23 +289,22 @@ public void testIndexingAndUpdatingDocs() throws IOException { public void testScrollingDocs() throws IOException { Request request = request("POST", "/_bulk"); - request.setJsonEntity( - "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"1\" } }\n{ \"foo\" : \"bar\" }\n" - + "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"2\" } }\n{ \"baz\" : \"tag\" }\n" - + "{ \"index\" : { \"_index\" : \"" - + indexName - + "\", \"_id\" : \"3\" } }\n{ \"baz\" : \"tag\" }\n" - ); + request.setJsonEntity(""" + { "index" : { "_index" : "%s", "_id" : "1" } } + { "foo" : "bar" } + { "index" : { "_index" : "%s", "_id" : "2" } } + { "baz" : "tag" } + { "index" : { "_index" : "%s", "_id" : "3" } } + { "baz" : "tag" } + """.formatted(indexName, indexName, indexName)); request.addParameter("refresh", "true"); Response response = client().performRequest(request); assertThat(response.getStatusLine().getStatusCode(), is(200)); Request searchRequest = request("GET", "/" + indexName + "/_search"); - searchRequest.setJsonEntity("{ \"size\" : 1,\n\"query\" : { \"match_all\" : {} } }\n"); + searchRequest.setJsonEntity(""" + { "size" : 1, "query" : { "match_all" : {} } } + """); searchRequest.addParameter("scroll", "1m"); response = client().performRequest(searchRequest); assertThat(response.getStatusLine().getStatusCode(), is(200)); diff --git a/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java b/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java index 53b93c2d98590..1041bd8e14af1 100644 --- a/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java +++ b/modules/lang-expression/src/internalClusterTest/java/org/elasticsearch/script/expression/StoredExpressionIT.java @@ -39,12 +39,8 @@ protected Collection> nodePlugins() { } public void testAllOpsDisabledIndexedScripts() throws IOException { - client().admin() - .cluster() - .preparePutStoredScript() - .setId("script1") - .setContent(new BytesArray("{\"script\": {\"lang\": \"expression\", \"source\": \"2\"} }"), XContentType.JSON) - .get(); + client().admin().cluster().preparePutStoredScript().setId("script1").setContent(new BytesArray(""" + {"script": {"lang": "expression", "source": "2"} }"""), XContentType.JSON).get(); client().prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON).get(); try { client().prepareUpdate("test", "1").setScript(new Script(ScriptType.STORED, null, "script1", Collections.emptyMap())).get(); diff --git a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java index 112d9d33cbb69..35177c743f400 100644 --- a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java +++ b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java @@ -135,23 +135,23 @@ public void testBasic() throws Exception { SearchTemplateResponse searchTemplateResponse1 = response1.getResponse(); assertThat(searchTemplateResponse1.hasResponse(), is(true)); assertHitCount(searchTemplateResponse1.getResponse(), (numDocs / 2) + (numDocs % 2)); - assertThat(searchTemplateResponse1.getSource().utf8ToString(), equalTo("{\"query\":{\"match\":{\"odd\":\"true\"}}}")); + assertThat(searchTemplateResponse1.getSource().utf8ToString(), equalTo(""" + {"query":{"match":{"odd":"true"}}}""")); MultiSearchTemplateResponse.Item response2 = response.getResponses()[1]; assertThat(response2.isFailure(), is(false)); SearchTemplateResponse searchTemplateResponse2 = response2.getResponse(); assertThat(searchTemplateResponse2.hasResponse(), is(false)); - assertThat( - searchTemplateResponse2.getSource().utf8ToString(), - equalTo("{\"query\":{\"match_phrase_prefix\":{\"message\":\"quick brown f\"}}}") - ); + assertThat(searchTemplateResponse2.getSource().utf8ToString(), equalTo(""" + {"query":{"match_phrase_prefix":{"message":"quick brown f"}}}""")); MultiSearchTemplateResponse.Item response3 = response.getResponses()[2]; assertThat(response3.isFailure(), is(false)); SearchTemplateResponse searchTemplateResponse3 = response3.getResponse(); assertThat(searchTemplateResponse3.hasResponse(), is(true)); assertHitCount(searchTemplateResponse3.getResponse(), (numDocs / 2)); - assertThat(searchTemplateResponse3.getSource().utf8ToString(), equalTo("{\"query\":{\"term\":{\"odd\":\"false\"}}}")); + assertThat(searchTemplateResponse3.getSource().utf8ToString(), equalTo(""" + {"query":{"term":{"odd":"false"}}}""")); MultiSearchTemplateResponse.Item response4 = response.getResponses()[3]; assertThat(response4.isFailure(), is(true)); diff --git a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java index 3163408b21181..751f933f5d775 100644 --- a/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java +++ b/modules/lang-mustache/src/internalClusterTest/java/org/elasticsearch/script/mustache/SearchTemplateIT.java @@ -51,7 +51,8 @@ public void setup() throws IOException { // Relates to #6318 public void testSearchRequestFail() throws Exception { - String query = "{ \"query\": {\"match_all\": {}}, \"size\" : \"{{my_size}}\" }"; + String query = """ + { "query": {"match_all": {}}, "size" : "{{my_size}}" }"""; SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); @@ -80,12 +81,13 @@ public void testSearchRequestFail() throws Exception { public void testTemplateQueryAsEscapedString() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); - String query = "{" - + " \"source\" : \"{ \\\"size\\\": \\\"{{size}}\\\", \\\"query\\\":{\\\"match_all\\\":{}}}\"," - + " \"params\":{" - + " \"size\": 1" - + " }" - + "}"; + String query = """ + { + "source": "{ \\"size\\": \\"{{size}}\\", \\"query\\":{\\"match_all\\":{}}}", + "params": { + "size": 1 + } + }"""; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(createParser(JsonXContent.jsonXContent, query)); request.setRequest(searchRequest); SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get(); @@ -99,13 +101,14 @@ public void testTemplateQueryAsEscapedString() throws Exception { public void testTemplateQueryAsEscapedStringStartingWithConditionalClause() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); - String templateString = "{" - + " \"source\" : \"{ {{#use_size}} \\\"size\\\": \\\"{{size}}\\\", {{/use_size}} \\\"query\\\":{\\\"match_all\\\":{}}}\"," - + " \"params\":{" - + " \"size\": 1," - + " \"use_size\": true" - + " }" - + "}"; + String templateString = """ + { + "source": "{ {{#use_size}} \\"size\\": \\"{{size}}\\", {{/use_size}} \\"query\\":{\\"match_all\\":{}}}", + "params": { + "size": 1, + "use_size": true + } + }"""; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(createParser(JsonXContent.jsonXContent, templateString)); request.setRequest(searchRequest); SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get(); @@ -119,13 +122,14 @@ public void testTemplateQueryAsEscapedStringStartingWithConditionalClause() thro public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws Exception { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("_all"); - String templateString = "{" - + " \"source\" : \"{ \\\"query\\\":{\\\"match_all\\\":{}} {{#use_size}}, \\\"size\\\": \\\"{{size}}\\\" {{/use_size}} }\"," - + " \"params\":{" - + " \"size\": 1," - + " \"use_size\": true" - + " }" - + "}"; + String templateString = """ + { + "source": "{ \\"query\\":{\\"match_all\\":{}} {{#use_size}}, \\"size\\": \\"{{size}}\\" {{/use_size}} }", + "params": { + "size": 1, + "use_size": true + } + }"""; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(createParser(JsonXContent.jsonXContent, templateString)); request.setRequest(searchRequest); SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get(); @@ -133,29 +137,19 @@ public void testTemplateQueryAsEscapedStringWithConditionalClauseAtEnd() throws } public void testIndexedTemplateClient() throws Exception { - assertAcked( - client().admin() - .cluster() - .preparePutStoredScript() - .setId("testTemplate") - .setContent( - new BytesArray( - "{" - + " \"script\": {" - + " \"lang\": \"mustache\"," - + " \"source\": {" - + " \"query\": {" - + " \"match\": {" - + " \"theField\": \"{{fieldParam}}\"" - + " }" - + " }" - + " }" - + " }" - + "}" - ), - XContentType.JSON - ) - ); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("testTemplate").setContent(new BytesArray(""" + { + "script": { + "lang": "mustache", + "source": { + "query": { + "match": { + "theField": "{{fieldParam}}" + } + } + } + } + }"""), XContentType.JSON)); GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript("testTemplate").get(); assertNotNull(getResponse.getSource()); @@ -187,18 +181,20 @@ public void testIndexedTemplateClient() throws Exception { public void testIndexedTemplate() throws Exception { - String script = "{" - + " \"script\": {" - + " \"lang\": \"mustache\"," - + " \"source\": {" - + " \"query\": {" - + " \"match\": {" - + " \"theField\": \"{{fieldParam}}\"" - + " }" - + " }" - + " }" - + " }" - + "}"; + String script = """ + { + "script": { + "lang": "mustache", + "source": { + "query": { + "match": { + "theField": "{{fieldParam}}" + } + } + } + } + } + """; assertAcked(client().admin().cluster().preparePutStoredScript().setId("1a").setContent(new BytesArray(script), XContentType.JSON)); assertAcked(client().admin().cluster().preparePutStoredScript().setId("2").setContent(new BytesArray(script), XContentType.JSON)); @@ -250,21 +246,22 @@ public void testIndexedTemplateOverwrite() throws Exception { client().admin().indices().prepareRefresh().get(); int iterations = randomIntBetween(2, 11); - String query = "{" - + " \"script\": {" - + " \"lang\": \"mustache\"," - + " \"source\": {" - + " \"query\": {" - + " \"match_phrase_prefix\": {" - + " \"searchtext\": {" - + " \"query\": \"{{P_Keyword1}}\"," - + " \"slop\": {{slop}}" - + " }" - + " }" - + " }" - + " }" - + " }" - + "}"; + String query = """ + { + "script": { + "lang": "mustache", + "source": { + "query": { + "match_phrase_prefix": { + "searchtext": { + "query": "{{P_Keyword1}}", + "slop": "{{slop}}" + } + } + } + } + } + }"""; for (int i = 1; i < iterations; i++) { assertAcked( client().admin() @@ -308,22 +305,23 @@ public void testIndexedTemplateOverwrite() throws Exception { } public void testIndexedTemplateWithArray() throws Exception { - String multiQuery = "{\n" - + " \"script\": {\n" - + " \"lang\": \"mustache\",\n" - + " \"source\": {\n" - + " \"query\": {\n" - + " \"terms\": {\n" - + " \"theField\": [\n" - + " \"{{#fieldParam}}\",\n" - + " \"{{.}}\",\n" - + " \"{{/fieldParam}}\"\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String multiQuery = """ + { + "script": { + "lang": "mustache", + "source": { + "query": { + "terms": { + "theField": [ + "{{#fieldParam}}", + "{{.}}", + "{{/fieldParam}}" + ] + } + } + } + } + }"""; assertAcked( client().admin().cluster().preparePutStoredScript().setId("4").setContent(new BytesArray(multiQuery), XContentType.JSON) ); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java index f6fcfbb69de3d..caac9d54cfb7c 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateRequestTests.java @@ -66,8 +66,10 @@ public void testParseRequest() throws Exception { } public void testParseWithCarriageReturn() throws Exception { - final String content = "{\"index\":[\"test0\", \"test1\"], \"request_cache\": true}\r\n" - + "{\"source\": {\"query\" : {\"match_{{template}}\" :{}}}, \"params\": {\"template\": \"all\" } }\r\n"; + final String content = """ + {"index":["test0", "test1"], "request_cache": true} + {"source": {"query" : {"match_{{template}}" :{}}}, "params": {"template": "all" } } + """; RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(content), XContentType.JSON) .build(); @@ -104,7 +106,8 @@ public void testMultiSearchTemplateToJson() throws Exception { searchRequest.setBatchedReduceSize(SearchRequest.DEFAULT_BATCHED_REDUCE_SIZE); SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest(searchRequest); - searchTemplateRequest.setScript("{\"query\": { \"match\" : { \"{{field}}\" : \"{{value}}\" }}}"); + searchTemplateRequest.setScript(""" + {"query": { "match" : { "{{field}}" : "{{value}}" }}}"""); searchTemplateRequest.setScriptType(ScriptType.INLINE); searchTemplateRequest.setProfile(randomBoolean()); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java index f96ff25e283b4..2420fed444518 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java @@ -40,40 +40,105 @@ public void setup() { public void testSimpleParameterReplace() { Map compileParams = Collections.singletonMap("content_type", "application/json"); { - String template = "GET _search {\"query\": " - + "{\"boosting\": {" - + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" - + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = """ + GET _search + { + "query": { + "boosting": { + "positive": { + "match": { + "body": "gift" + } + }, + "negative": { + "term": { + "body": { + "value": "solr" + } + } + }, + "negative_boost": {{boost_val}} + } + } + }"""; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); String o = qe.compile(null, template, TemplateScript.CONTEXT, compileParams).newInstance(vars).execute(); - assertEquals( - "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}", - o - ); + assertEquals(""" + GET _search + { + "query": { + "boosting": { + "positive": { + "match": { + "body": "gift" + } + }, + "negative": { + "term": { + "body": { + "value": "solr" + } + } + }, + "negative_boost": 0.3 + } + } + }""", o); } { - String template = "GET _search {\"query\": " - + "{\"boosting\": {" - + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"{{body_val}}\"}" - + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = """ + GET _search + { + "query": { + "boosting": { + "positive": { + "match": { + "body": "gift" + } + }, + "negative": { + "term": { + "body": { + "value": "{{body_val}}" + } + } + }, + "negative_boost": {{boost_val}} + } + } + }"""; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); vars.put("body_val", "\"quick brown\""); String o = qe.compile(null, template, TemplateScript.CONTEXT, compileParams).newInstance(vars).execute(); - assertEquals( - "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}", - o - ); + assertEquals(""" + GET _search + { + "query": { + "boosting": { + "positive": { + "match": { + "body": "gift" + } + }, + "negative": { + "term": { + "body": { + "value": "\\"quick brown\\"" + } + } + }, + "negative_boost": 0.3 + } + } + }""", o); } } public void testSimple() throws IOException { - String templateString = "{" + "\"source\":{\"match_{{template}}\": {}}," + "\"params\":{\"template\":\"all\"}" + "}"; + String templateString = """ + {"source":{"match_{{template}}": {}},"params":{"template":"all"}}"""; XContentParser parser = createParser(JsonXContent.jsonXContent, templateString); Script script = Script.parse(parser); TemplateScript.Factory compiled = qe.compile(null, script.getIdOrCode(), TemplateScript.CONTEXT, Collections.emptyMap()); @@ -82,13 +147,14 @@ public void testSimple() throws IOException { } public void testParseTemplateAsSingleStringWithConditionalClause() throws IOException { - String templateString = "{" - + " \"source\" : \"{ \\\"match_{{#use_it}}{{template}}{{/use_it}}\\\":{} }\"," - + " \"params\":{" - + " \"template\":\"all\"," - + " \"use_it\": true" - + " }" - + "}"; + String templateString = """ + { + "source": "{ \\"match_{{#use_it}}{{template}}{{/use_it}}\\":{} }", + "params": { + "template": "all", + "use_it": true + } + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, templateString); Script script = Script.parse(parser); TemplateScript.Factory compiled = qe.compile(null, script.getIdOrCode(), TemplateScript.CONTEXT, Collections.emptyMap()); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java index 15fb1d1d98173..2c49fe38c56fa 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java @@ -41,21 +41,52 @@ public class MustacheTests extends ESTestCase { private ScriptEngine engine = new MustacheScriptEngine(); public void testBasics() { - String template = "GET _search {\"query\": " - + "{\"boosting\": {" - + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" - + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = """ + GET _search + { + "query": { + "boosting": { + "positive": { + "match": { + "body": "gift" + } + }, + "negative": { + "term": { + "body": { + "value": "solr" + } + } + }, + "negative_boost": {{boost_val}} + } + } + }"""; Map params = singletonMap("boost_val", "0.2"); TemplateScript.Factory factory = engine.compile(null, template, TemplateScript.CONTEXT, Collections.emptyMap()); TemplateScript result = factory.newInstance(params); - assertEquals( - "Mustache templating broken", - "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.2 } }}", - result.execute() - ); + assertEquals("Mustache templating broken", """ + GET _search + { + "query": { + "boosting": { + "positive": { + "match": { + "body": "gift" + } + }, + "negative": { + "term": { + "body": { + "value": "solr" + } + } + }, + "negative_boost": 0.2 + } + } + }""", result.execute()); } public void testArrayAccess() throws Exception { @@ -151,8 +182,20 @@ public void testSimpleMapToJSON() throws Exception { Map ctx = singletonMap("ctx", human0); - assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo("{\"ctx\":{\"name\":\"John Smith\",\"age\":42,\"height\":1.84}}")); - assertScript("{{#toJson}}ctx{{/toJson}}", ctx, equalTo("{\"name\":\"John Smith\",\"age\":42,\"height\":1.84}")); + assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo(XContentHelper.stripWhitespace(""" + { + "ctx": { + "name": "John Smith", + "age": 42, + "height": 1.84 + } + }"""))); + assertScript("{{#toJson}}ctx{{/toJson}}", ctx, equalTo(XContentHelper.stripWhitespace(""" + { + "name": "John Smith", + "age": 42, + "height": 1.84 + }"""))); assertScript("{{#toJson}}ctx.name{{/toJson}}", ctx, equalTo("John Smith")); } @@ -173,34 +216,49 @@ public void testMultipleMapsToJSON() throws Exception { Map ctx = singletonMap("ctx", humans); - assertScript( - "{{#toJson}}.{{/toJson}}", - ctx, - equalTo( - "{\"ctx\":{\"first\":{\"name\":\"John Smith\",\"age\":42,\"height\":1.84},\"second\":" - + "{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}}}" - ) - ); - - assertScript( - "{{#toJson}}ctx{{/toJson}}", - ctx, - equalTo( - "{\"first\":{\"name\":\"John Smith\",\"age\":42,\"height\":1.84},\"second\":" - + "{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}}" - ) - ); - - assertScript("{{#toJson}}ctx.first{{/toJson}}", ctx, equalTo("{\"name\":\"John Smith\",\"age\":42,\"height\":1.84}")); - - assertScript("{{#toJson}}ctx.second{{/toJson}}", ctx, equalTo("{\"name\":\"Dave Smith\",\"age\":27,\"height\":1.71}")); + assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo(XContentHelper.stripWhitespace(""" + { + "ctx": { + "first": { + "name": "John Smith", + "age": 42, + "height": 1.84 + }, + "second": { + "name": "Dave Smith", + "age": 27, + "height": 1.71 + } + } + }"""))); + + assertScript("{{#toJson}}ctx{{/toJson}}", ctx, equalTo(XContentHelper.stripWhitespace(""" + { + "first": { + "name": "John Smith", + "age": 42, + "height": 1.84 + }, + "second": { + "name": "Dave Smith", + "age": 27, + "height": 1.71 + } + }"""))); + + assertScript("{{#toJson}}ctx.first{{/toJson}}", ctx, equalTo(""" + {"name":"John Smith","age":42,"height":1.84}""")); + + assertScript("{{#toJson}}ctx.second{{/toJson}}", ctx, equalTo(""" + {"name":"Dave Smith","age":27,"height":1.71}""")); } public void testSimpleArrayToJSON() throws Exception { String[] array = new String[] { "one", "two", "three" }; Map ctx = singletonMap("array", array); - assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo("{\"array\":[\"one\",\"two\",\"three\"]}")); + assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo(""" + {"array":["one","two","three"]}""")); assertScript("{{#toJson}}array{{/toJson}}", ctx, equalTo("[\"one\",\"two\",\"three\"]")); assertScript("{{#toJson}}array.0{{/toJson}}", ctx, equalTo("one")); assertScript("{{#toJson}}array.1{{/toJson}}", ctx, equalTo("two")); @@ -212,7 +270,8 @@ public void testSimpleListToJSON() throws Exception { List list = Arrays.asList("one", "two", "three"); Map ctx = singletonMap("ctx", list); - assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo("{\"ctx\":[\"one\",\"two\",\"three\"]}")); + assertScript("{{#toJson}}.{{/toJson}}", ctx, equalTo(""" + {"ctx":["one","two","three"]}""")); assertScript("{{#toJson}}ctx{{/toJson}}", ctx, equalTo("[\"one\",\"two\",\"three\"]")); assertScript("{{#toJson}}ctx.0{{/toJson}}", ctx, equalTo("one")); assertScript("{{#toJson}}ctx.1{{/toJson}}", ctx, equalTo("two")); @@ -255,11 +314,9 @@ public void testEmbeddedToJSON() throws Exception { XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2() ); - assertScript( - "{{#ctx.bulks}}{{#toJson}}.{{/toJson}}{{/ctx.bulks}}", - ctx, - equalTo("{\"index\":\"index-1\",\"id\":1,\"type\":\"type-1\"}{\"index\":\"index-2\",\"id\":2,\"type\":\"type-2\"}") - ); + assertScript("{{#ctx.bulks}}{{#toJson}}.{{/toJson}}{{/ctx.bulks}}", ctx, equalTo(""" + {"index":"index-1","id":1,"type":"type-1"}\ + {"index":"index-2","id":2,"type":"type-2"}""")); assertScript("{{#ctx.bulks}}<{{#toJson}}id{{/toJson}}>{{/ctx.bulks}}", ctx, equalTo("<1><2>")); } diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java index 24bf464dde3f8..f4bbda5fbfc96 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/RestMultiSearchTemplateActionTests.java @@ -35,7 +35,10 @@ public void setUpAction() { } public void testTypeInPath() { - String content = "{ \"index\": \"some_index\" } \n" + "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; + String content = """ + { "index": "some_index" } + {"source": {"query" : {"match_all" :{}}}} + """; BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( @@ -47,7 +50,10 @@ public void testTypeInPath() { } public void testTypeInBody() { - String content = "{ \"index\": \"some_index\", \"type\": \"some_type\" } \n" + "{\"source\": {\"query\" : {\"match_all\" :{}}}} \n"; + String content = """ + { "index": "some_index", "type": "some_type" }\s + {"source": {"query" : {"match_all" :{}}}}\s + """; BytesArray bytesContent = new BytesArray(content.getBytes(StandardCharsets.UTF_8)); RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withHeaders( diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java index 57425f0b89269..b3adef392ffc6 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateRequestXContentTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.script.mustache; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.AbstractXContentTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -68,7 +69,8 @@ public void testToXContentWithInlineTemplate() throws IOException { SearchTemplateRequest request = new SearchTemplateRequest(); request.setScriptType(ScriptType.INLINE); - request.setScript("{\"query\": { \"match\" : { \"{{my_field}}\" : \"{{my_value}}\" } } }"); + request.setScript(""" + {"query": { "match" : { "{{my_field}}" : "{{my_value}}" } } }"""); request.setProfile(true); Map scriptParams = new HashMap<>(); @@ -79,7 +81,8 @@ public void testToXContentWithInlineTemplate() throws IOException { XContentType contentType = randomFrom(XContentType.values()); XContentBuilder expectedRequest = XContentFactory.contentBuilder(contentType) .startObject() - .field("source", "{\"query\": { \"match\" : { \"{{my_field}}\" : \"{{my_value}}\" } } }") + .field("source", """ + {"query": { "match" : { "{{my_field}}" : "{{my_value}}" } } }""") .startObject("params") .field("my_field", "foo") .field("my_value", "bar") @@ -125,41 +128,51 @@ public void testToXContentWithStoredTemplate() throws IOException { } public void testFromXContentWithEmbeddedTemplate() throws Exception { - String source = "{" - + " 'source' : {\n" - + " 'query': {\n" - + " 'terms': {\n" - + " 'status': [\n" - + " '{{#status}}',\n" - + " '{{.}}',\n" - + " '{{/status}}'\n" - + " ]\n" - + " }\n" - + " }\n" - + " }" - + "}"; + String source = """ + { + 'source' : { + 'query': { + 'terms': { + 'status': [ + '{{#status}}', + '{{.}}', + '{{/status}}' + ] + } + } + }}"""; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(newParser(source)); - assertThat(request.getScript(), equalTo("{\"query\":{\"terms\":{\"status\":[\"{{#status}}\",\"{{.}}\",\"{{/status}}\"]}}}")); + assertThat(request.getScript(), equalTo(""" + {"query":{"terms":{"status":["{{#status}}","{{.}}","{{/status}}"]}}}""")); assertThat(request.getScriptType(), equalTo(ScriptType.INLINE)); assertThat(request.getScriptParams(), nullValue()); } public void testFromXContentWithEmbeddedTemplateAndParams() throws Exception { - String source = "{" - + " 'source' : {" - + " 'query': { 'match' : { '{{my_field}}' : '{{my_value}}' } }," - + " 'size' : '{{my_size}}'" - + " }," - + " 'params' : {" - + " 'my_field' : 'foo'," - + " 'my_value' : 'bar'," - + " 'my_size' : 5" - + " }" - + "}"; + String source = """ + { + 'source' : { + 'query': { 'match' : { '{{my_field}}' : '{{my_value}}' } }, + 'size' : '{{my_size}}' + }, + 'params' : { + 'my_field' : 'foo', + 'my_value' : 'bar', + 'my_size' : 5 + } + }"""; SearchTemplateRequest request = SearchTemplateRequest.fromXContent(newParser(source)); - assertThat(request.getScript(), equalTo("{\"query\":{\"match\":{\"{{my_field}}\":\"{{my_value}}\"}},\"size\":\"{{my_size}}\"}")); + assertThat(request.getScript(), equalTo(XContentHelper.stripWhitespace(""" + { + "query": { + "match": { + "{{my_field}}": "{{my_value}}" + } + }, + "size": "{{my_size}}" + }"""))); assertThat(request.getScriptType(), equalTo(ScriptType.INLINE)); assertThat(request.getScriptParams().size(), equalTo(3)); assertThat(request.getScriptParams(), hasEntry("my_field", "foo")); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java index d72b8c8538a69..04f3f346e86d0 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicExpressionTests.java @@ -97,7 +97,12 @@ public void testCast() { assertEquals(1, exec("return (int)1.0;")); assertEquals((byte) 100, exec("double x = 100; return (byte)x;")); - assertEquals(3, exec("Map x = new HashMap();\n" + "Object y = x;\n" + "((Map)y).put(2, 3);\n" + "return x.get(2);\n")); + assertEquals(3, exec(""" + Map x = new HashMap(); + Object y = x; + ((Map)y).put(2, 3); + return x.get(2); + """)); } public void testIllegalDefCast() { @@ -112,10 +117,11 @@ public void testCat() { assertEquals("aaabbb", exec("return \"aaa\" + \"bbb\";")); assertEquals("aaabbb", exec("String aaa = \"aaa\", bbb = \"bbb\"; return aaa + bbb;")); - assertEquals( - "aaabbbbbbbbb", - exec("String aaa = \"aaa\", bbb = \"bbb\"; int x;\n" + "for (; x < 3; ++x) \n" + " aaa += bbb;\n" + "return aaa;") - ); + assertEquals("aaabbbbbbbbb", exec(""" + String aaa = "aaa", bbb = "bbb"; int x; + for (; x < 3; ++x) + aaa += bbb; + return aaa;""")); } public void testComp() { @@ -228,11 +234,15 @@ public void testNullSafeDeref() { assertEquals(0, exec("def a = ['other': ['cat': params.t]]; return a.other?.cat?.x", singletonMap("t", t), true)); // Assignments - assertNull(exec("def a = [:];\n" + "a.missing_length = a.missing?.length();\n" + "return a.missing_length", true)); - assertEquals( - 3, - exec("def a = [:];\n" + "a.missing = 'foo';\n" + "a.missing_length = a.missing?.length();\n" + "return a.missing_length", true) - ); + assertNull(exec(""" + def a = [:]; + a.missing_length = a.missing?.length(); + return a.missing_length""", true)); + assertEquals(3, exec(""" + def a = [:]; + a.missing = 'foo'; + a.missing_length = a.missing?.length(); + return a.missing_length""", true)); // Writes, all unsupported at this point // assertEquals(null, exec("org.elasticsearch.painless.FeatureTestObject a = null; return a?.x")); // Read field diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java index cf78091903eda..2b71bf7030ab0 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BasicStatementTests.java @@ -34,45 +34,42 @@ public void testIfStatement() { assertEquals(2, exec("int x = 4; if (x == 5) return 1; else if (x == 4) return 2; else return 0;")); assertEquals(1, exec("int x = 4; if (x == 5) return 1; else if (x == 4) return 1; else return 0;")); - assertEquals( - 3, - exec( - "int x = 5;\n" - + "if (x == 5) {\n" - + " int y = 2;\n" - + " \n" - + " if (y == 2) {\n" - + " x = 3;\n" - + " }\n" - + " \n" - + "}\n" - + "\n" - + "return x;\n" - ) - ); + assertEquals(3, exec(""" + int x = 5; + if (x == 5) { + int y = 2; + + if (y == 2) { + x = 3; + } + + } + + return x; + """)); } public void testWhileStatement() { assertEquals("aaaaaa", exec("String c = \"a\"; int x; while (x < 5) { c += \"a\"; ++x; } return c;")); - Object value = exec( - " byte[][] b = new byte[5][5]; \n" - + " byte x = 0, y; \n" - + " \n" - + " while (x < 5) { \n" - + " y = 0; \n" - + " \n" - + " while (y < 5) { \n" - + " b[x][y] = (byte)(x*y); \n" - + " ++y; \n" - + " } \n" - + " \n" - + " ++x; \n" - + " } \n" - + " \n" - + " return b; \n" - ); + Object value = exec(""" + byte[][] b = new byte[5][5]; + byte x = 0, y; + + while (x < 5) { + y = 0; + + while (y < 5) { + b[x][y] = (byte)(x*y); + ++y; + } + + ++x; + } + + return b; + """); byte[][] b = (byte[][]) value; @@ -86,23 +83,23 @@ public void testWhileStatement() { public void testDoWhileStatement() { assertEquals("aaaaaa", exec("String c = \"a\"; int x; do { c += \"a\"; ++x; } while (x < 5); return c;")); - Object value = exec( - " int[][] b = new int[5][5]; \n" - + " int x = 0, y; \n" - + " \n" - + " do { \n" - + " y = 0; \n" - + " \n" - + " do { \n" - + " b[x][y] = x*y; \n" - + " ++y; \n" - + " } while (y < 5); \n" - + " \n" - + " ++x; \n" - + " } while (x < 5); \n" - + " \n" - + " return b; \n" - ); + Object value = exec(""" + int[][] b = new int[5][5]; + int x = 0, y; + + do { + y = 0; + + do { + b[x][y] = x*y; + ++y; + } while (y < 5); + + ++x; + } while (x < 5); + + return b; + """); int[][] b = (int[][]) value; @@ -119,16 +116,16 @@ public void testForStatement() { assertEquals(6, exec("double test() { return 0.0; }" + "int x, y; for (test(); x < 4; test()) {y += x; ++x;} return y;")); - Object value = exec( - " int[][] b = new int[5][5]; \n" - + " for (int x = 0; x < 5; ++x) { \n" - + " for (int y = 0; y < 5; ++y) { \n" - + " b[x][y] = x*y; \n" - + " } \n" - + " } \n" - + " \n" - + " return b; \n" - ); + Object value = exec(""" + int[][] b = new int[5][5]; + for (int x = 0; x < 5; ++x) { + for (int y = 0; y < 5; ++y) { + b[x][y] = x*y; + } + } + + return b; + """); int[][] b = (int[][]) value; diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java index fdb00e114c95a..77199d30eb4be 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DateTimeTests.java @@ -14,126 +14,85 @@ public class DateTimeTests extends ScriptTestCase { public void testLongToZonedDateTime() { - assertEquals( - ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), - exec( - "long milliSinceEpoch = 434931330000L;" - + "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" - + "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));" - ) - ); + assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec(""" + long milliSinceEpoch = 434931330000L; + Instant instant = Instant.ofEpochMilli(milliSinceEpoch); + return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));""")); } public void testStringToZonedDateTime() { - assertEquals( - ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), - exec( - "String milliSinceEpochString = '434931330000';" - + "long milliSinceEpoch = Long.parseLong(milliSinceEpochString);" - + "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" - + "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));" - ) - ); - - assertEquals( - ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), - exec("String datetime = '1983-10-13T22:15:30Z';" + "return ZonedDateTime.parse(datetime);") - ); - - assertEquals( - ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), - exec( - "String datetime = 'Thu, 13 Oct 1983 22:15:30 GMT';" - + "return ZonedDateTime.parse(datetime, DateTimeFormatter.RFC_1123_DATE_TIME);" - ) - ); - - assertEquals( - ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), - exec( - "String datetime = 'custom y 1983 m 10 d 13 22:15:30 Z';" - + "DateTimeFormatter dtf = DateTimeFormatter.ofPattern(" - + "\"'custom' 'y' yyyy 'm' MM 'd' dd HH:mm:ss VV\");" - + "return ZonedDateTime.parse(datetime, dtf);" - ) - ); + assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec(""" + String milliSinceEpochString = '434931330000'; + long milliSinceEpoch = Long.parseLong(milliSinceEpochString); + Instant instant = Instant.ofEpochMilli(milliSinceEpoch); + return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'));""")); + + assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec(""" + String datetime = '1983-10-13T22:15:30Z'; + return ZonedDateTime.parse(datetime);""")); + + assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec(""" + String datetime = 'Thu, 13 Oct 1983 22:15:30 GMT'; + return ZonedDateTime.parse(datetime, DateTimeFormatter.RFC_1123_DATE_TIME);""")); + + assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec(""" + String datetime = 'custom y 1983 m 10 d 13 22:15:30 Z'; + DateTimeFormatter dtf = DateTimeFormatter.ofPattern( + "'custom' 'y' yyyy 'm' MM 'd' dd HH:mm:ss VV"); + return ZonedDateTime.parse(datetime, dtf);""")); } public void testPiecesToZonedDateTime() { - assertEquals( - ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), - exec( - "int year = 1983;" - + "int month = 10;" - + "int day = 13;" - + "int hour = 22;" - + "int minutes = 15;" - + "int seconds = 30;" - + "int nanos = 0;" - + "String tz = 'Z';" - + "return ZonedDateTime.of(year, month, day, hour, minutes, seconds, nanos, ZoneId.of(tz));" - ) - ); + assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of("Z")), exec(""" + int year = 1983; + int month = 10; + int day = 13; + int hour = 22; + int minutes = 15; + int seconds = 30; + int nanos = 0; + String tz = 'Z'; + return ZonedDateTime.of(year, month, day, hour, minutes, seconds, nanos, ZoneId.of(tz));""")); } public void testZonedDatetimeToLong() { - assertEquals( - 434931330000L, - exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" - + "return zdt.toInstant().toEpochMilli();" - ) - ); + assertEquals(434931330000L, exec(""" + ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z')); + return zdt.toInstant().toEpochMilli();""")); } public void testZonedDateTimeToString() { - assertEquals( - "1983-10-13T22:15:30Z", - exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" - + "return zdt.format(DateTimeFormatter.ISO_INSTANT);" - ) - ); - - assertEquals( - "date: 1983/10/13 time: 22:15:30", - exec( - "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" - + "DateTimeFormatter dtf = DateTimeFormatter.ofPattern(" - + "\"'date:' yyyy/MM/dd 'time:' HH:mm:ss\");" - + "return zdt.format(dtf);" - ) - ); + assertEquals("1983-10-13T22:15:30Z", exec(""" + ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z')); + return zdt.format(DateTimeFormatter.ISO_INSTANT);""")); + + assertEquals("date: 1983/10/13 time: 22:15:30", exec(""" + ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z')); + DateTimeFormatter dtf = DateTimeFormatter.ofPattern( + "'date:' yyyy/MM/dd 'time:' HH:mm:ss"); + return zdt.format(dtf);""")); } public void testZonedDateTimeToPieces() { - assertArrayEquals( - new int[] { 1983, 10, 13, 22, 15, 30, 100 }, - (int[]) exec( - "int[] pieces = new int[7];" - + "ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 100, ZoneId.of('Z'));" - + "pieces[0] = zdt.year;" - + "pieces[1] = zdt.monthValue;" - + "pieces[2] = zdt.dayOfMonth;" - + "pieces[3] = zdt.hour;" - + "pieces[4] = zdt.minute;" - + "pieces[5] = zdt.second;" - + "pieces[6] = zdt.nano;" - + "return pieces;" - ) - ); + assertArrayEquals(new int[] { 1983, 10, 13, 22, 15, 30, 100 }, (int[]) exec(""" + int[] pieces = new int[7]; + ZonedDateTime zdt = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 100, ZoneId.of('Z')); + pieces[0] = zdt.year; + pieces[1] = zdt.monthValue; + pieces[2] = zdt.dayOfMonth; + pieces[3] = zdt.hour; + pieces[4] = zdt.minute; + pieces[5] = zdt.second; + pieces[6] = zdt.nano; + return pieces;""")); } public void testLongManipulation() { - assertEquals( - ZonedDateTime.of(1983, 10, 13, 22, 15, 27, 0, ZoneId.of("Z")), - exec( - "long milliSinceEpoch = 434931330000L;" - + "milliSinceEpoch = milliSinceEpoch - 1000L*3L;" - + "Instant instant = Instant.ofEpochMilli(milliSinceEpoch);" - + "return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'))" - ) - ); + assertEquals(ZonedDateTime.of(1983, 10, 13, 22, 15, 27, 0, ZoneId.of("Z")), exec(""" + long milliSinceEpoch = 434931330000L; + milliSinceEpoch = milliSinceEpoch - 1000L*3L; + Instant instant = Instant.ofEpochMilli(milliSinceEpoch); + return ZonedDateTime.ofInstant(instant, ZoneId.of('Z'))""")); } public void testZonedDateTimeManipulation() { @@ -161,23 +120,15 @@ public void testLongTimeDifference() { } public void testZonedDateTimeDifference() { - assertEquals( - 4989L, - exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));" - + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 13, 22, 15, 35, 0, ZoneId.of('Z'));" - + "return ChronoUnit.MILLIS.between(zdt1, zdt2);" - ) - ); - - assertEquals( - 4L, - exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z'));" - + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" - + "return ChronoUnit.DAYS.between(zdt1, zdt2);" - ) - ); + assertEquals(4989L, exec(""" + ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z')); + ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 13, 22, 15, 35, 0, ZoneId.of('Z')); + return ChronoUnit.MILLIS.between(zdt1, zdt2);""")); + + assertEquals(4L, exec(""" + ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 11000000, ZoneId.of('Z')); + ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z')); + return ChronoUnit.DAYS.between(zdt1, zdt2);""")); } public void compareLongs() { @@ -185,44 +136,28 @@ public void compareLongs() { } public void compareZonedDateTimes() { - assertEquals( - true, - exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" - + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" - + "return zdt1.isBefore(zdt2);" - ) - ); - - assertEquals( - false, - exec( - "ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" - + "ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z'));" - + "return zdt1.isAfter(zdt2);" - ) - ); + assertEquals(true, exec(""" + ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z')); + ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z')); + return zdt1.isBefore(zdt2);""")); + + assertEquals(false, exec(""" + ZonedDateTime zdt1 = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z')); + ZonedDateTime zdt2 = ZonedDateTime.of(1983, 10, 17, 22, 15, 35, 0, ZoneId.of('Z')); + return zdt1.isAfter(zdt2);""")); } public void testTimeZone() { - assertEquals( - ZonedDateTime.of(1983, 10, 13, 15, 15, 30, 0, ZoneId.of("America/Los_Angeles")), - exec( - "ZonedDateTime utc = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z'));" - + "return utc.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));" - ) - ); - - assertEquals( - "Thu, 13 Oct 1983 15:15:30 -0700", - exec( - "String gmtString = 'Thu, 13 Oct 1983 22:15:30 GMT';" - + "ZonedDateTime gmtZdt = ZonedDateTime.parse(gmtString," - + "DateTimeFormatter.RFC_1123_DATE_TIME);" - + "ZonedDateTime pstZdt =" - + "gmtZdt.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));" - + "return pstZdt.format(DateTimeFormatter.RFC_1123_DATE_TIME);" - ) - ); + assertEquals(ZonedDateTime.of(1983, 10, 13, 15, 15, 30, 0, ZoneId.of("America/Los_Angeles")), exec(""" + ZonedDateTime utc = ZonedDateTime.of(1983, 10, 13, 22, 15, 30, 0, ZoneId.of('Z')); + return utc.withZoneSameInstant(ZoneId.of('America/Los_Angeles'));""")); + + assertEquals("Thu, 13 Oct 1983 15:15:30 -0700", exec(""" + String gmtString = 'Thu, 13 Oct 1983 22:15:30 GMT'; + ZonedDateTime gmtZdt = ZonedDateTime.parse(gmtString, + DateTimeFormatter.RFC_1123_DATE_TIME); + ZonedDateTime pstZdt = + gmtZdt.withZoneSameInstant(ZoneId.of('America/Los_Angeles')); + return pstZdt.format(DateTimeFormatter.RFC_1123_DATE_TIME);""")); } } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java index c1516e9323e7d..87b199cd1b43f 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DebugTests.java @@ -47,14 +47,12 @@ public void testExplain() { assertThat(e.getHeaders(painlessLookup), not(hasKey("es.painless_class"))); // You can't catch the explain exception - e = expectScriptThrows( - PainlessExplainError.class, - () -> exec( - "try {\n" + " Debug.explain(params.a)\n" + "} catch (Exception e) {\n" + " return 1\n" + "}", - singletonMap("a", dummy), - true - ) - ); + e = expectScriptThrows(PainlessExplainError.class, () -> exec(""" + try { + Debug.explain(params.a) + } catch (Exception e) { + return 1 + }""", singletonMap("a", dummy), true)); assertSame(dummy, e.getObjectToExplain()); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/JsonTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/JsonTests.java index 8728b87239af9..abdc867cb8eed 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/JsonTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/JsonTests.java @@ -23,7 +23,10 @@ public void testDump() { // pretty print output = exec("Json.dump(params.data, true)", singletonMap("data", singletonMap("hello", "world")), true); - assertEquals("{\n \"hello\" : \"world\"\n}", output); + assertEquals(""" + { + "hello" : "world" + }""", output); } public void testLoad() { diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java index 3ba379ab92b61..0bd074eb698ac 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/UserFunctionTests.java @@ -18,15 +18,16 @@ public void testZeroArgumentUserFunction() { } public void testUserFunctionDefCallRef() { - String source = "String getSource() { 'source'; }\n" - + "int myCompare(int a, int b) { getMulti() * Integer.compare(a, b) }\n" - + "int getMulti() { return -1 }\n" - + "def l = [1, 100, -100];\n" - + "if (myCompare(10, 50) > 0) { l.add(50 + getMulti()) }\n" - + "l.sort(this::myCompare);\n" - + "if (l[0] == 100) { l.remove(l.size() - 1) ; l.sort((a, b) -> -1 * myCompare(a, b)) } \n" - + "if (getSource().startsWith('sour')) { l.add(255); }\n" - + "return l;"; + String source = """ + String getSource() { 'source'; } + int myCompare(int a, int b) { getMulti() * Integer.compare(a, b) } + int getMulti() { return -1 } + def l = [1, 100, -100]; + if (myCompare(10, 50) > 0) { l.add(50 + getMulti()) } + l.sort(this::myCompare); + if (l[0] == 100) { l.remove(l.size() - 1) ; l.sort((a, b) -> -1 * myCompare(a, b)) }\s + if (getSource().startsWith('sour')) { l.add(255); } + return l;"""; assertEquals(List.of(1, 49, 100, 255), exec(source)); assertBytecodeExists(source, "public &getSource()Ljava/lang/String"); assertBytecodeExists(source, "public &getMulti()I"); @@ -36,62 +37,71 @@ public void testUserFunctionDefCallRef() { } public void testChainedUserMethods() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" - + "int getMulti() { -1 }\n" - + "List l = [1, 100, -100];\n" - + "l.sort(this::myCompare);\n" - + "l;\n"; + String source = """ + int myCompare(int a, int b) { getMulti() * (a - b) } + int getMulti() { -1 } + List l = [1, 100, -100]; + l.sort(this::myCompare); + l; + """; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testChainedUserMethodsLambda() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" - + "int getMulti() { -1 }\n" - + "List l = [1, 100, -100];\n" - + "l.sort((a, b) -> myCompare(a, b));\n" - + "l;\n"; + String source = """ + int myCompare(int a, int b) { getMulti() * (a - b) } + int getMulti() { -1 } + List l = [1, 100, -100]; + l.sort((a, b) -> myCompare(a, b)); + l; + """; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testChainedUserMethodsDef() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" - + "int getMulti() { -1 }\n" - + "def l = [1, 100, -100];\n" - + "l.sort(this::myCompare);\n" - + "l;\n"; + String source = """ + int myCompare(int a, int b) { getMulti() * (a - b) } + int getMulti() { -1 } + def l = [1, 100, -100]; + l.sort(this::myCompare); + l; + """; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testChainedUserMethodsLambdaDef() { - String source = "int myCompare(int a, int b) { getMulti() * (a - b) }\n" - + "int getMulti() { -1 }\n" - + "def l = [1, 100, -100];\n" - + "l.sort((a, b) -> myCompare(a, b));\n" - + "l;\n"; + String source = """ + int myCompare(int a, int b) { getMulti() * (a - b) } + int getMulti() { -1 } + def l = [1, 100, -100]; + l.sort((a, b) -> myCompare(a, b)); + l; + """; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testChainedUserMethodsLambdaCaptureDef() { - String source = "int myCompare(int a, int b, int x, int m) { getMulti(m) * (a - b + x) }\n" - + "int getMulti(int m) { -1 * m }\n" - + "def l = [1, 100, -100];\n" - + "int cx = 100;\n" - + "int cm = 1;\n" - + "l.sort((a, b) -> myCompare(a, b, cx, cm));\n" - + "l;\n"; + String source = """ + int myCompare(int a, int b, int x, int m) { getMulti(m) * (a - b + x) } + int getMulti(int m) { -1 * m } + def l = [1, 100, -100]; + int cx = 100; + int cm = 1; + l.sort((a, b) -> myCompare(a, b, cx, cm)); + l; + """; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); } public void testMethodReferenceInUserFunction() { - String source = "int myCompare(int a, int b, String s) { " - + " Map m = ['f': 5];" - + " a - b + m.computeIfAbsent(s, this::getLength) " - + "}\n" - + "int getLength(String s) { s.length() }\n" - + "def l = [1, 0, -2];\n" - + "String s = 'g';\n" - + "l.sort((a, b) -> myCompare(a, b, s));\n" - + "l;\n"; + String source = """ + int myCompare(int a, int b, String s) { Map m = ['f': 5]; a - b + m.computeIfAbsent(s, this::getLength) } + int getLength(String s) { s.length() } + def l = [1, 0, -2]; + String s = 'g'; + l.sort((a, b) -> myCompare(a, b, s)); + l; + """; assertEquals(List.of(-2, 1, 0), exec(source, Map.of("a", 1), false)); } @@ -102,44 +112,54 @@ public void testUserFunctionVirtual() { } public void testUserFunctionRef() { - String source = "int myCompare(int x, int y) { return -1 * x - y }\n" - + "List l = [1, 100, -100];\n" - + "l.sort(this::myCompare);\n" - + "return l;"; + String source = """ + int myCompare(int x, int y) { return -1 * x - y } + List l = [1, 100, -100]; + l.sort(this::myCompare); + return l;"""; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(II)I"); } public void testUserFunctionRefEmpty() { - String source = "int myCompare(int x, int y) { return -1 * x - y }\n" + "[].sort((a, b) -> myCompare(a, b));\n"; + String source = """ + int myCompare(int x, int y) { return -1 * x - y } + [].sort((a, b) -> myCompare(a, b)); + """; assertNull(exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(II)I"); assertBytecodeExists(source, "INVOKEVIRTUAL org/elasticsearch/painless/PainlessScript$Script.&myCompare (II)I"); } public void testUserFunctionCallInLambda() { - String source = "int myCompare(int x, int y) { -1 * ( x - y ) }\n" - + "List l = [1, 100, -100];\n" - + "l.sort((a, b) -> myCompare(a, b));\n" - + "return l;"; + String source = """ + int myCompare(int x, int y) { -1 * ( x - y ) } + List l = [1, 100, -100]; + l.sort((a, b) -> myCompare(a, b)); + return l;"""; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(II)I"); assertBytecodeExists(source, "INVOKEVIRTUAL org/elasticsearch/painless/PainlessScript$Script.&myCompare (II)I"); } public void testUserFunctionLambdaCapture() { - String source = "int myCompare(Object o, int x, int y) { return o != null ? -1 * ( x - y ) : ( x - y ) }\n" - + "List l = [1, 100, -100];\n" - + "Object q = '';\n" - + "l.sort((a, b) -> myCompare(q, a, b));\n" - + "return l;"; + String source = """ + int myCompare(Object o, int x, int y) { return o != null ? -1 * ( x - y ) : ( x - y ) } + List l = [1, 100, -100]; + Object q = ''; + l.sort((a, b) -> myCompare(q, a, b)); + return l;"""; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public &myCompare(Ljava/lang/Object;II)I"); assertBytecodeExists(source, "INVOKEVIRTUAL org/elasticsearch/painless/PainlessScript$Script.&myCompare (Ljava/lang/Object;II)I"); } public void testLambdaCapture() { - String source = "List l = [1, 100, -100];\n" + "int q = -1;\n" + "l.sort((a, b) -> q * ( a - b ));\n" + "return l;"; + String source = """ + List l = [1, 100, -100]; + int q = -1; + l.sort((a, b) -> q * ( a - b )); + return l;"""; assertEquals(List.of(100, 1, -100), exec(source, Map.of("a", 1), false)); assertBytecodeExists(source, "public static synthetic lambda$synthetic$0(ILjava/lang/Object;Ljava/lang/Object;)I"); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java index 011d495d08cbe..f0cd8fbf60b44 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/action/PainlessExecuteApiTests.java @@ -102,11 +102,8 @@ public void testScoreExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=text"); - Request.ContextSetup contextSetup = new Request.ContextSetup( - "index", - new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), - new MatchQueryBuilder("text", "fox") - ); + Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray(""" + {"rank": 4.0, "text": "quick brown fox"}"""), new MatchQueryBuilder("text", "fox")); contextSetup.setXContentType(XContentType.JSON); Request request = new Request( new Script( @@ -126,11 +123,8 @@ public void testBooleanFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=text"); - Request.ContextSetup contextSetup = new Request.ContextSetup( - "index", - new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), - new MatchQueryBuilder("text", "fox") - ); + Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray(""" + {"rank": 4.0, "text": "quick brown fox"}"""), new MatchQueryBuilder("text", "fox")); contextSetup.setXContentType(XContentType.JSON); Request request = new Request( new Script(ScriptType.INLINE, "painless", "emit(doc['rank'].value < params.max_rank)", singletonMap("max_rank", 5.0)), @@ -171,18 +165,10 @@ public void testDateFieldExecutionContext() throws IOException { contextSetup = new Request.ContextSetup("index", new BytesArray("{}"), new MatchAllQueryBuilder()); contextSetup.setXContentType(XContentType.JSON); - request = new Request( - new Script( - ScriptType.INLINE, - "painless", - "emit(ZonedDateTime.parse(\"2021-01-01T00:00:00Z\").toInstant().toEpochMilli());\n" - + "emit(ZonedDateTime.parse(\"1942-05-31T15:16:17Z\").toInstant().toEpochMilli());\n" - + "emit(ZonedDateTime.parse(\"2035-10-13T10:54:19Z\").toInstant().toEpochMilli());", - emptyMap() - ), - "date_field", - contextSetup - ); + request = new Request(new Script(ScriptType.INLINE, "painless", """ + emit(ZonedDateTime.parse("2021-01-01T00:00:00Z").toInstant().toEpochMilli()); + emit(ZonedDateTime.parse("1942-05-31T15:16:17Z").toInstant().toEpochMilli()); + emit(ZonedDateTime.parse("2035-10-13T10:54:19Z").toInstant().toEpochMilli());""", emptyMap()), "date_field", contextSetup); response = innerShardOperation(request, scriptService, indexService); assertEquals( Arrays.asList("2021-01-01T00:00:00.000Z", "1942-05-31T15:16:17.000Z", "2035-10-13T10:54:19.000Z"), @@ -348,11 +334,8 @@ public void testKeywordFieldExecutionContext() throws IOException { ScriptService scriptService = getInstanceFromNode(ScriptService.class); IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "rank", "type=long", "text", "type=keyword"); - Request.ContextSetup contextSetup = new Request.ContextSetup( - "index", - new BytesArray("{\"rank\": 4.0, \"text\": \"quick brown fox\"}"), - new MatchQueryBuilder("text", "fox") - ); + Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray(""" + {"rank": 4.0, "text": "quick brown fox"}"""), new MatchQueryBuilder("text", "fox")); contextSetup.setXContentType(XContentType.JSON); contextSetup.setXContentType(XContentType.JSON); Request request = new Request( diff --git a/modules/legacy-geo/src/test/java/org/elasticsearch/legacygeo/ShapeBuilderTests.java b/modules/legacy-geo/src/test/java/org/elasticsearch/legacygeo/ShapeBuilderTests.java index b7897894c562a..d70dc7981296b 100644 --- a/modules/legacy-geo/src/test/java/org/elasticsearch/legacygeo/ShapeBuilderTests.java +++ b/modules/legacy-geo/src/test/java/org/elasticsearch/legacygeo/ShapeBuilderTests.java @@ -738,39 +738,40 @@ public void testShapeWithCoplanarVerticalPoints() throws Exception { } public void testPolygon3D() { - String expected = "{\n" - + " \"type\" : \"polygon\",\n" - + " \"orientation\" : \"right\",\n" - + " \"coordinates\" : [\n" - + " [\n" - + " [\n" - + " -45.0,\n" - + " 30.0,\n" - + " 100.0\n" - + " ],\n" - + " [\n" - + " 45.0,\n" - + " 30.0,\n" - + " 75.0\n" - + " ],\n" - + " [\n" - + " 45.0,\n" - + " -30.0,\n" - + " 77.0\n" - + " ],\n" - + " [\n" - + " -45.0,\n" - + " -30.0,\n" - + " 101.0\n" - + " ],\n" - + " [\n" - + " -45.0,\n" - + " 30.0,\n" - + " 110.0\n" - + " ]\n" - + " ]\n" - + " ]\n" - + "}"; + String expected = """ + { + "type" : "polygon", + "orientation" : "right", + "coordinates" : [ + [ + [ + -45.0, + 30.0, + 100.0 + ], + [ + 45.0, + 30.0, + 75.0 + ], + [ + 45.0, + -30.0, + 77.0 + ], + [ + -45.0, + -30.0, + 101.0 + ], + [ + -45.0, + 30.0, + 110.0 + ] + ] + ] + }"""; PolygonBuilder pb = new PolygonBuilder( new CoordinatesBuilder().coordinate(new Coordinate(-45, 30, 100)) diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java index 3c65e0038c250..c4e7e35c646a1 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/RankFeatureQueryBuilderTests.java @@ -101,13 +101,23 @@ protected void doAssertLuceneQuery(RankFeatureQueryBuilder queryBuilder, Query q } public void testDefaultScoreFunction() throws IOException { - String query = "{\n" + " \"rank_feature\" : {\n" + " \"field\": \"my_feature_field\"\n" + " }\n" + "}"; + String query = """ + { + "rank_feature" : { + "field": "my_feature_field" + } + }"""; Query parsedQuery = parseQuery(query).toQuery(createSearchExecutionContext()); assertEquals(FeatureField.newSaturationQuery("_feature", "my_feature_field"), parsedQuery); } public void testIllegalField() { - String query = "{\n" + " \"rank_feature\" : {\n" + " \"field\": \"" + TEXT_FIELD_NAME + "\"\n" + " }\n" + "}"; + String query = """ + { + "rank_feature" : { + "field": "%s" + } + }""".formatted(TEXT_FIELD_NAME); IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> parseQuery(query).toQuery(createSearchExecutionContext()) @@ -119,14 +129,15 @@ public void testIllegalField() { } public void testIllegalCombination() { - String query = "{\n" - + " \"rank_feature\" : {\n" - + " \"field\": \"my_negative_feature_field\",\n" - + " \"log\" : {\n" - + " \"scaling_factor\": 4.5\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "rank_feature" : { + "field": "my_negative_feature_field", + "log" : { + "scaling_factor": 4.5 + } + } + }"""; IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> parseQuery(query).toQuery(createSearchExecutionContext()) diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java index 1a898306090e4..349a3ce5ebba4 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java @@ -208,41 +208,42 @@ public void testIllegalValues() { } public void testFromJson() throws IOException { - String query = "{\n" - + " \"has_child\" : {\n" - + " \"query\" : {\n" - + " \"range\" : {\n" - + " \"mapped_string\" : {\n" - + " \"gte\" : \"agJhRET\",\n" - + " \"lte\" : \"zvqIq\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"type\" : \"child\",\n" - + " \"score_mode\" : \"avg\",\n" - + " \"min_children\" : 883170873,\n" - + " \"max_children\" : 1217235442,\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 2.0,\n" - + " \"_name\" : \"WNzYMJKRwePuRBh\",\n" - + " \"inner_hits\" : {\n" - + " \"name\" : \"inner_hits_name\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"from\" : 0,\n" - + " \"size\" : 100,\n" - + " \"version\" : false,\n" - + " \"seq_no_primary_term\" : false,\n" - + " \"explain\" : false,\n" - + " \"track_scores\" : false,\n" - + " \"sort\" : [ {\n" - + " \"mapped_string\" : {\n" - + " \"order\" : \"asc\"\n" - + " }\n" - + " } ]\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "has_child" : { + "query" : { + "range" : { + "mapped_string" : { + "gte" : "agJhRET", + "lte" : "zvqIq", + "boost" : 1.0 + } + } + }, + "type" : "child", + "score_mode" : "avg", + "min_children" : 883170873, + "max_children" : 1217235442, + "ignore_unmapped" : false, + "boost" : 2.0, + "_name" : "WNzYMJKRwePuRBh", + "inner_hits" : { + "name" : "inner_hits_name", + "ignore_unmapped" : false, + "from" : 0, + "size" : 100, + "version" : false, + "seq_no_primary_term" : false, + "explain" : false, + "track_scores" : false, + "sort" : [ { + "mapped_string" : { + "order" : "asc" + } + } ] + } + } + }"""; HasChildQueryBuilder queryBuilder = (HasChildQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); assertEquals(query, queryBuilder.maxChildren(), 1217235442); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java index 057e45a8a3947..8f51277d9ddba 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java @@ -187,22 +187,23 @@ public void testMustRewrite() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"has_parent\" : {\n" - + " \"query\" : {\n" - + " \"term\" : {\n" - + " \"tag\" : {\n" - + " \"value\" : \"something\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"parent_type\" : \"blog\",\n" - + " \"score\" : true,\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "has_parent" : { + "query" : { + "term" : { + "tag" : { + "value" : "something", + "boost" : 1.0 + } + } + }, + "parent_type" : "blog", + "score" : true, + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; HasParentQueryBuilder parsed = (HasParentQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, "blog", parsed.type()); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java index eb3245a816391..b9b78cb804c30 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java @@ -108,15 +108,15 @@ protected void doAssertLuceneQuery(ParentIdQueryBuilder queryBuilder, Query quer } public void testFromJson() throws IOException { - String query = "{\n" - + " \"parent_id\" : {\n" - + " \"type\" : \"child\",\n" - + " \"id\" : \"123\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 3.0,\n" - + " \"_name\" : \"name\"" - + " }\n" - + "}"; + String query = """ + { + "parent_id" : { + "type" : "child", + "id" : "123", + "ignore_unmapped" : false, + "boost" : 3.0, + "_name" : "name" } + }"""; ParentIdQueryBuilder queryBuilder = (ParentIdQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); assertThat(queryBuilder.getType(), Matchers.equalTo("child")); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java index 1038192ce5aab..9003e749f0d9c 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java @@ -253,7 +253,9 @@ public void testRequiredParameters() { public void testFromJsonNoDocumentType() throws IOException { SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - QueryBuilder queryBuilder = parseQuery("{\"percolate\" : { \"document\": {}, \"field\":\"" + queryField + "\"}}"); + QueryBuilder queryBuilder = parseQuery(""" + {"percolate" : { "document": {}, "field":"%s"}} + """.formatted(queryField)); queryBuilder.toQuery(searchExecutionContext); } @@ -264,23 +266,16 @@ public void testFromJsonNoType() throws IOException { documentSource = Collections.singletonList(randomSource(new HashSet<>())); SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - QueryBuilder queryBuilder = parseQuery( - "{\"percolate\" : { \"index\": \"" - + indexedDocumentIndex - + "\", \"id\": \"" - + indexedDocumentId - + "\", \"field\":\"" - + queryField - + "\"}}" - ); + QueryBuilder queryBuilder = parseQuery(""" + {"percolate" : { "index": "%s", "id": "%s", "field":"%s"}} + """.formatted(indexedDocumentIndex, indexedDocumentId, queryField)); rewriteAndFetch(queryBuilder, searchExecutionContext).toQuery(searchExecutionContext); } public void testBothDocumentAndDocumentsSpecified() { - IllegalArgumentException e = expectThrows( - IllegalArgumentException.class, - () -> parseQuery("{\"percolate\" : { \"document\": {}, \"documents\": [{}, {}], \"field\":\"" + queryField + "\"}}") - ); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parseQuery(""" + {"percolate" : { "document": {}, "documents": [{}, {}], "field":"%s"}} + """.formatted(queryField))); assertThat(e.getMessage(), containsString("The following fields are not allowed together: [document, documents]")); } @@ -388,11 +383,9 @@ public void testDisallowExpensiveQueries() { public void testFromJsonWithDocumentType() throws IOException { SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - String queryAsString = "{\"percolate\" : { \"document\": {}, \"document_type\":\"" - + docType - + "\", \"field\":\"" - + queryField - + "\"}}"; + String queryAsString = """ + {"percolate" : { "document": {}, "document_type":"%s", "field":"%s"}} + """.formatted(docType, queryField); XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7); QueryBuilder queryBuilder = parseQuery(parser); queryBuilder.toQuery(searchExecutionContext); @@ -406,13 +399,9 @@ public void testFromJsonWithType() throws IOException { documentSource = Collections.singletonList(randomSource(new HashSet<>())); SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); - String queryAsString = "{\"percolate\" : { \"index\": \"" - + indexedDocumentIndex - + "\", \"type\": \"_doc\", \"id\": \"" - + indexedDocumentId - + "\", \"field\":\"" - + queryField - + "\"}}"; + String queryAsString = """ + {"percolate" : { "index": "%s", "type": "_doc", "id": "%s", "field":"%s"}} + """.formatted(indexedDocumentIndex, indexedDocumentId, queryField); XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, queryAsString, RestApiVersion.V_7); QueryBuilder queryBuilder = parseQuery(parser); rewriteAndFetch(queryBuilder, searchExecutionContext).toQuery(searchExecutionContext); diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java index 9273b8b913094..d415946530e7c 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/DiscountedCumulativeGainTests.java @@ -288,20 +288,13 @@ public void testMetricDetails() { assertEquals(expectedNdcg, detail.getNDCG(), 0.0); assertEquals(unratedDocs, detail.getUnratedDocs()); if (idcg != 0) { - assertEquals( - "{\"dcg\":{\"dcg\":" - + dcg - + ",\"ideal_dcg\":" - + idcg - + ",\"normalized_dcg\":" - + expectedNdcg - + ",\"unrated_docs\":" - + unratedDocs - + "}}", - Strings.toString(detail) - ); + assertEquals(""" + {"dcg":{"dcg":%s,"ideal_dcg":%s,"normalized_dcg":%s,"unrated_docs":%s}}\ + """.formatted(dcg, idcg, expectedNdcg, unratedDocs), Strings.toString(detail)); } else { - assertEquals("{\"dcg\":{\"dcg\":" + dcg + ",\"unrated_docs\":" + unratedDocs + "}}", Strings.toString(detail)); + assertEquals(""" + {"dcg":{"dcg":%s,"unrated_docs":%s}}\ + """.formatted(dcg, unratedDocs), Strings.toString(detail)); } } diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java index 474903dea5deb..4dfb1643d8f3d 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RankEvalResponseTests.java @@ -19,6 +19,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchParseException; @@ -154,32 +155,45 @@ public void testToXContent() throws IOException { ); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); String xContent = BytesReference.bytes(response.toXContent(builder, ToXContent.EMPTY_PARAMS)).utf8ToString(); - assertEquals( - ("{" - + " \"metric_score\": 0.123," - + " \"details\": {" - + " \"coffee_query\": {" - + " \"metric_score\": 0.1," - + " \"unrated_docs\": [{\"_index\":\"index\",\"_id\":\"456\"}]," - + " \"hits\":[{\"hit\":{\"_index\":\"index\",\"_id\":\"123\",\"_score\":1.0}," - + " \"rating\":5}," - + " {\"hit\":{\"_index\":\"index\",\"_id\":\"456\",\"_score\":1.0}," - + " \"rating\":null}" - + " ]" - + " }" - + " }," - + " \"failures\": {" - + " \"beer_query\": {" - + " \"error\" : {\"root_cause\": [{\"type\":\"parsing_exception\", \"reason\":\"someMsg\",\"line\":0,\"col\":0}]," - + " \"type\":\"parsing_exception\"," - + " \"reason\":\"someMsg\"," - + " \"line\":0,\"col\":0" - + " }" - + " }" - + " }" - + "}").replaceAll("\\s+", ""), - xContent - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "metric_score": 0.123, + "details": { + "coffee_query": { + "metric_score": 0.1, + "unrated_docs": [ { "_index": "index", "_id": "456" } ], + "hits": [ + { + "hit": { + "_index": "index", + "_id": "123", + "_score": 1.0 + }, + "rating": 5 + }, + { + "hit": { + "_index": "index", + "_id": "456", + "_score": 1.0 + }, + "rating": null + } + ] + } + }, + "failures": { + "beer_query": { + "error": { + "root_cause": [ { "type": "parsing_exception", "reason": "someMsg", "line": 0, "col": 0 } ], + "type": "parsing_exception", + "reason": "someMsg", + "line": 0, + "col": 0 + } + } + } + }"""), xContent); } private static RatedSearchHit searchHit(String index, int docId, Integer rating) { diff --git a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java index 63408f9503a5c..c5a09d67d94d0 100644 --- a/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java +++ b/modules/rank-eval/src/test/java/org/elasticsearch/index/rankeval/RatedRequestsTests.java @@ -212,17 +212,13 @@ public void testDuplicateRatedDocThrowsException() { IllegalArgumentException.class, () -> new RatedRequest("test_query", ratedDocs, new SearchSourceBuilder()) ); - assertEquals( - "Found duplicate rated document key [{\"_index\":\"index1\",\"_id\":\"id1\"}] in evaluation request [test_query]", - ex.getMessage() - ); + assertEquals(""" + Found duplicate rated document key [{"_index":"index1","_id":"id1"}] in evaluation request [test_query]""", ex.getMessage()); Map params = new HashMap<>(); params.put("key", "value"); ex = expectThrows(IllegalArgumentException.class, () -> new RatedRequest("test_query", ratedDocs, params, "templateId")); - assertEquals( - "Found duplicate rated document key [{\"_index\":\"index1\",\"_id\":\"id1\"}] in evaluation request [test_query]", - ex.getMessage() - ); + assertEquals(""" + Found duplicate rated document key [{"_index":"index1","_id":"id1"}] in evaluation request [test_query]""", ex.getMessage()); } public void testNullSummaryFieldsTreatment() { @@ -303,27 +299,65 @@ public void testProfileNotAllowed() { * matter for parsing xContent */ public void testParseFromXContent() throws IOException { - String querySpecString = " {\n" - + " \"id\": \"my_qa_query\",\n" - + " \"request\": {\n" - + " \"query\": {\n" - + " \"bool\": {\n" - + " \"must\": [\n" - + " {\"match\": {\"beverage\": \"coffee\"}},\n" - + " {\"term\": {\"browser\": {\"value\": \"safari\"}}},\n" - + " {\"term\": {\"time_of_day\": " - + " {\"value\": \"morning\",\"boost\": 2}}},\n" - + " {\"term\": {\"ip_location\": " - + " {\"value\": \"ams\",\"boost\": 10}}}]}\n" - + " },\n" - + " \"size\": 10\n" - + " },\n" - + " \"summary_fields\" : [\"title\"],\n" - + " \"ratings\": [\n" - + " {\"_index\": \"test\" , \"_id\": \"1\", \"rating\" : 1 },\n" - + " {\"_index\": \"test\", \"rating\" : 0, \"_id\": \"2\"},\n" - + " {\"_id\": \"3\", \"_index\": \"test\", \"rating\" : 1} ]" - + "}\n"; + String querySpecString = """ + { + "id": "my_qa_query", + "request": { + "query": { + "bool": { + "must": [ + { + "match": { + "beverage": "coffee" + } + }, + { + "term": { + "browser": { + "value": "safari" + } + } + }, + { + "term": { + "time_of_day": { + "value": "morning", + "boost": 2 + } + } + }, + { + "term": { + "ip_location": { + "value": "ams", + "boost": 10 + } + } + } + ] + } + }, + "size": 10 + }, + "summary_fields": [ "title" ], + "ratings": [ + { + "_index": "test", + "_id": "1", + "rating": 1 + }, + { + "_index": "test", + "rating": 0, + "_id": "2" + }, + { + "_id": "3", + "_index": "test", + "rating": 1 + } + ] + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, querySpecString)) { RatedRequest specification = RatedRequest.fromXContent(parser); assertEquals("my_qa_query", specification.getId()); diff --git a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java index f22b9236bce57..d5efa4272690b 100644 --- a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java +++ b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java @@ -41,16 +41,15 @@ public void setupTestIndex() throws IOException { public void testReindex() throws IOException { Request request = new Request("POST", "/_reindex"); - request.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\":\"test\"\n" - + " },\n" - + " \"dest\":{\n" - + " \"index\":\"des\"\n" - + " }\n" - + "}" - ); + request.setJsonEntity(""" + { + "source":{ + "index":"test" + }, + "dest":{ + "index":"des" + } + }"""); Map response = entityAsMap(client().performRequest(request)); assertThat(response, hasEntry("total", count)); assertThat(response, hasEntry("created", count)); @@ -64,39 +63,33 @@ public void testReindexFromRemote() throws IOException { String remote = "http://" + http.get("publish_address"); Request request = new Request("POST", "/_reindex"); if (randomBoolean()) { - request.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\":\"test\",\n" - + " \"remote\":{\n" - + " \"host\":\"" - + remote - + "\"\n" - + " }\n" - + " }\n," - + " \"dest\":{\n" - + " \"index\":\"des\"\n" - + " }\n" - + "}" - ); + request.setJsonEntity(""" + { + "source": { + "index": "test", + "remote": { + "host": "%s" + } + }, + "dest": { + "index": "des" + } + }""".formatted(remote)); } else { // Test with external version_type - request.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\":\"test\",\n" - + " \"remote\":{\n" - + " \"host\":\"" - + remote - + "\"\n" - + " }\n" - + " }\n," - + " \"dest\":{\n" - + " \"index\":\"des\",\n" - + " \"version_type\": \"external\"\n" - + " }\n" - + "}" - ); + request.setJsonEntity(""" + { + "source": { + "index": "test", + "remote": { + "host": "%s" + } + }, + "dest": { + "index": "des", + "version_type": "external" + } + }""".formatted(remote)); } Map response = entityAsMap(client().performRequest(request)); assertThat(response, hasEntry("total", count)); @@ -111,7 +104,12 @@ public void testUpdateByQuery() throws IOException { public void testDeleteByQuery() throws IOException { Request request = new Request("POST", "/test/_delete_by_query"); - request.setJsonEntity("{\n" + " \"query\":{\n" + " \"match_all\": {}\n" + " }\n" + "}"); + request.setJsonEntity(""" + { + "query":{ + "match_all": {} + } + }"""); Map response = entityAsMap(client().performRequest(request)); assertThat(response, hasEntry("total", count)); assertThat(response, hasEntry("deleted", count)); diff --git a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java index 209ed34afcc4e..869f5c440f0a0 100644 --- a/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java +++ b/modules/reindex/src/javaRestTest/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java @@ -48,41 +48,35 @@ private void oldEsTestCase(String portPropertyName, String requestsPerSecond) th Request reindex = new Request("POST", "/_reindex"); if (randomBoolean()) { // Reindex using the external version_type - reindex.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\": \"test\",\n" - + " \"size\": 1,\n" - + " \"remote\": {\n" - + " \"host\": \"http://127.0.0.1:" - + oldEsPort - + "\"\n" - + " }\n" - + " },\n" - + " \"dest\": {\n" - + " \"index\": \"test\",\n" - + " \"version_type\": \"external\"\n" - + " }\n" - + "}" - ); + reindex.setJsonEntity(""" + { + "source":{ + "index": "test", + "size": 1, + "remote": { + "host": "http://127.0.0.1:%s" + } + }, + "dest": { + "index": "test", + "version_type": "external" + } + }""".formatted(oldEsPort)); } else { // Reindex using the default internal version_type - reindex.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\": \"test\",\n" - + " \"size\": 1,\n" - + " \"remote\": {\n" - + " \"host\": \"http://127.0.0.1:" - + oldEsPort - + "\"\n" - + " }\n" - + " },\n" - + " \"dest\": {\n" - + " \"index\": \"test\"\n" - + " }\n" - + "}" - ); + reindex.setJsonEntity(""" + { + "source":{ + "index": "test", + "size": 1, + "remote": { + "host": "http://127.0.0.1:%s" + } + }, + "dest": { + "index": "test" + } + }""".formatted(oldEsPort)); } reindex.addParameter("refresh", "true"); reindex.addParameter("pretty", "true"); diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java index 75a72ec34571a..c32a061d3e20d 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/CancelTests.java @@ -238,14 +238,13 @@ public void testReindexCancel() throws Exception { } public void testUpdateByQueryCancel() throws Exception { - BytesReference pipeline = new BytesArray( - "{\n" - + " \"description\" : \"sets processed to true\",\n" - + " \"processors\" : [ {\n" - + " \"test\" : {}\n" - + " } ]\n" - + "}" - ); + BytesReference pipeline = new BytesArray(""" + { + "description" : "sets processed to true", + "processors" : [ { + "test" : {} + } ] + }"""); assertAcked(client().admin().cluster().preparePutPipeline("set-processed", pipeline, XContentType.JSON).get()); testCancel(UpdateByQueryAction.NAME, updateByQuery().setPipeline("set-processed").source(INDEX), (response, total, modified) -> { @@ -282,14 +281,13 @@ public void testReindexCancelWithWorkers() throws Exception { } public void testUpdateByQueryCancelWithWorkers() throws Exception { - BytesReference pipeline = new BytesArray( - "{\n" - + " \"description\" : \"sets processed to true\",\n" - + " \"processors\" : [ {\n" - + " \"test\" : {}\n" - + " } ]\n" - + "}" - ); + BytesReference pipeline = new BytesArray(""" + { + "description" : "sets processed to true", + "processors" : [ { + "test" : {} + } ] + }"""); assertAcked(client().admin().cluster().preparePutPipeline("set-processed", pipeline, XContentType.JSON).get()); testCancel( diff --git a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java index 0077e0555ff88..48570aa4e2b7a 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/reindex/remote/RemoteRequestBuildersTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.ESTestCase; @@ -227,10 +228,14 @@ public void testInitialSearchEntity() throws IOException { searchRequest.source().fetchSource(new String[] { "in1", "in2" }, new String[] { "out" }); entity = initialSearch(searchRequest, new BytesArray(query), remoteVersion).getEntity(); assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue()); - assertEquals( - "{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}", - Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "query": %s, + "_source": { + "includes": [ "in1", "in2" ], + "excludes": [ "out" ] + } + }""".formatted(query)), Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8))); // Invalid XContent fails RuntimeException e = expectThrows( diff --git a/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java b/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java index e99cd86691f2f..57d29d2bf8af5 100644 --- a/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java +++ b/plugins/analysis-icu/src/internalClusterTest/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperIT.java @@ -347,11 +347,9 @@ public void testNumerics() throws Exception { assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom( - true, - client().prepareIndex(index).setId("1").setSource("{\"collate\":\"foobar-10\"}", XContentType.JSON), - client().prepareIndex(index).setId("2").setSource("{\"collate\":\"foobar-9\"}", XContentType.JSON) - ); + indexRandom(true, client().prepareIndex(index).setId("1").setSource(""" + {"collate":"foobar-10"}""", XContentType.JSON), client().prepareIndex(index).setId("2").setSource(""" + {"collate":"foobar-9"}""", XContentType.JSON)); SearchRequest request = new SearchRequest().indices(index) .source(new SearchSourceBuilder().fetchSource(false).sort("collate", SortOrder.ASC)); @@ -386,13 +384,11 @@ public void testIgnoreAccentsButNotCase() throws Exception { assertAcked(client().admin().indices().prepareCreate(index).setMapping(builder)); - indexRandom( - true, - client().prepareIndex(index).setId("1").setSource("{\"id\":\"1\",\"collate\":\"résumé\"}", XContentType.JSON), - client().prepareIndex(index).setId("2").setSource("{\"id\":\"2\",\"collate\":\"Resume\"}", XContentType.JSON), - client().prepareIndex(index).setId("3").setSource("{\"id\":\"3\",\"collate\":\"resume\"}", XContentType.JSON), - client().prepareIndex(index).setId("4").setSource("{\"id\":\"4\",\"collate\":\"Résumé\"}", XContentType.JSON) - ); + indexRandom(true, client().prepareIndex(index).setId("1").setSource(""" + {"id":"1","collate":"résumé"}""", XContentType.JSON), client().prepareIndex(index).setId("2").setSource(""" + {"id":"2","collate":"Resume"}""", XContentType.JSON), client().prepareIndex(index).setId("3").setSource(""" + {"id":"3","collate":"resume"}""", XContentType.JSON), client().prepareIndex(index).setId("4").setSource(""" + {"id":"4","collate":"Résumé"}""", XContentType.JSON)); SearchRequest request = new SearchRequest().indices(index) .source(new SearchSourceBuilder().fetchSource(false).sort("collate", SortOrder.ASC).sort("id", SortOrder.DESC)); diff --git a/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java b/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java index bbe35747c12eb..b691a778153f3 100644 --- a/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java +++ b/plugins/discovery-ec2/qa/amazon-ec2/src/yamlRestTest/java/org/elasticsearch/discovery/ec2/AmazonEC2Fixture.java @@ -110,23 +110,14 @@ protected Response handle(final Request request) throws IOException { || ("/latest/meta-data/iam/security-credentials/my_iam_profile".equals(request.getPath()) && HttpGet.METHOD_NAME.equals(request.getMethod()))) { final Date expiration = new Date(new Date().getTime() + TimeUnit.DAYS.toMillis(1)); - final String response = "{" - + "\"AccessKeyId\": \"" - + "ec2_integration_test_access_key" - + "\"," - + "\"Expiration\": \"" - + DateUtils.formatISO8601Date(expiration) - + "\"," - + "\"RoleArn\": \"" - + "test" - + "\"," - + "\"SecretAccessKey\": \"" - + "ec2_integration_test_secret_key" - + "\"," - + "\"Token\": \"" - + "test" - + "\"" - + "}"; + final String response = """ + { + "AccessKeyId": "ec2_integration_test_access_key", + "Expiration": "%s", + "RoleArn": "test", + "SecretAccessKey": "ec2_integration_test_secret_key", + "Token": "test" + }""".formatted(DateUtils.formatISO8601Date(expiration)); final Map headers = new HashMap<>(contentType("application/json")); return new Response(RestStatus.OK.getStatus(), headers, response.getBytes(UTF_8)); diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java index cda32b2165ded..4b5d4c708dbbe 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java @@ -203,7 +203,9 @@ public void testWriteBlobWithRetries() throws Exception { assertThat(content.isPresent(), is(true)); assertThat(content.get().v1(), equalTo(blobContainer.path().buildAsString() + "write_blob_max_retries")); if (Objects.deepEquals(bytes, BytesReference.toBytes(content.get().v2()))) { - byte[] response = ("{\"bucket\":\"bucket\",\"name\":\"" + content.get().v1() + "\"}").getBytes(UTF_8); + byte[] response = """ + {"bucket":"bucket","name":"%s"} + """.formatted(content.get().v1()).getBytes(UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/json"); exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length); exchange.getResponseBody().write(response); diff --git a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java index ffb142a85c59d..e761e77ec7224 100644 --- a/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java +++ b/plugins/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageClientSettingsTests.java @@ -207,23 +207,15 @@ private static Tuple randomCredential(final S credentialBuilder.setPrivateKeyId("private_key_id_" + clientName); credentialBuilder.setScopes(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)); final String encodedPrivateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded()); - final String serviceAccount = "{\"type\":\"service_account\"," - + "\"project_id\":\"project_id_" - + clientName - + "\"," - + "\"private_key_id\":\"private_key_id_" - + clientName - + "\"," - + "\"private_key\":\"-----BEGIN PRIVATE KEY-----\\n" - + encodedPrivateKey - + "\\n-----END PRIVATE KEY-----\\n\"," - + "\"client_email\":\"" - + clientName - + "\"," - + "\"client_id\":\"id_" - + clientName - + "\"" - + "}"; + final String serviceAccount = """ + { + "type": "service_account", + "project_id": "project_id_%s", + "private_key_id": "private_key_id_%s", + "private_key": "-----BEGIN PRIVATE KEY-----\\n%s\\n-----END PRIVATE KEY-----\\n", + "client_email": "%s", + "client_id": "id_%s" + }""".formatted(clientName, clientName, encodedPrivateKey, clientName, clientName); return Tuple.tuple(credentialBuilder.build(), serviceAccount.getBytes(StandardCharsets.UTF_8)); } diff --git a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java index 5b0beaf0c992a..06f4c75e9c64c 100644 --- a/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java +++ b/plugins/repository-hdfs/src/test/java/org/elasticsearch/repositories/hdfs/HaHdfsFailoverTestSuiteIT.java @@ -100,27 +100,22 @@ public void testHAFailoverWithRepository() throws Exception { // Create repository { Request request = new Request("PUT", "/_snapshot/hdfs_ha_repo_read"); - request.setJsonEntity( - "{" - + "\"type\":\"hdfs\"," - + "\"settings\":{" - + "\"uri\": \"hdfs://ha-hdfs/\",\n" - + "\"path\": \"/user/elasticsearch/existing/readonly-repository\"," - + "\"readonly\": \"true\"," - + securityCredentials(securityEnabled, esKerberosPrincipal) - + "\"conf.dfs.nameservices\": \"ha-hdfs\"," - + "\"conf.dfs.ha.namenodes.ha-hdfs\": \"nn1,nn2\"," - + "\"conf.dfs.namenode.rpc-address.ha-hdfs.nn1\": \"localhost:" - + nn1Port - + "\"," - + "\"conf.dfs.namenode.rpc-address.ha-hdfs.nn2\": \"localhost:" - + nn2Port - + "\"," - + "\"conf.dfs.client.failover.proxy.provider.ha-hdfs\": " - + "\"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider\"" - + "}" - + "}" - ); + request.setJsonEntity(""" + { + "type": "hdfs", + "settings": { + "uri": "hdfs://ha-hdfs/", + "path": "/user/elasticsearch/existing/readonly-repository", + "readonly": "true", + %s + "conf.dfs.nameservices": "ha-hdfs", + "conf.dfs.ha.namenodes.ha-hdfs": "nn1,nn2", + "conf.dfs.namenode.rpc-address.ha-hdfs.nn1": "localhost:%s", + "conf.dfs.namenode.rpc-address.ha-hdfs.nn2": "localhost:%s", + "conf.dfs.client.failover.proxy.provider.ha-hdfs": \ + "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider" + } + }""".formatted(securityCredentials(securityEnabled, esKerberosPrincipal), nn1Port, nn2Port)); Response response = client.performRequest(request); Assert.assertEquals(200, response.getStatusLine().getStatusCode()); @@ -144,7 +139,8 @@ public void testHAFailoverWithRepository() throws Exception { private String securityCredentials(boolean securityEnabled, String kerberosPrincipal) { if (securityEnabled) { - return "\"security.principal\": \"" + kerberosPrincipal + "\"," + "\"conf.dfs.data.transfer.protection\": \"authentication\","; + return """ + "security.principal": "%s","conf.dfs.data.transfer.protection": "authentication",""".formatted(kerberosPrincipal); } else { return ""; } diff --git a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java index 78aede78bc87a..4781bea7679c7 100644 --- a/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java +++ b/plugins/repository-s3/src/test/java/org/elasticsearch/repositories/s3/S3BlobContainerRetriesTests.java @@ -257,12 +257,13 @@ public void testWriteLargeBlob() throws Exception { if ("POST".equals(exchange.getRequestMethod()) && exchange.getRequestURI().getQuery().equals("uploads")) { // initiate multipart upload request if (countDownInitiate.countDown()) { - byte[] response = ("\n" - + "\n" - + " bucket\n" - + " write_large_blob\n" - + " TEST\n" - + "").getBytes(StandardCharsets.UTF_8); + byte[] response = (""" + + + bucket + write_large_blob + TEST + """).getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/xml"); exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); exchange.getResponseBody().write(response); @@ -289,11 +290,12 @@ public void testWriteLargeBlob() throws Exception { // complete multipart upload request if (countDownComplete.countDown()) { Streams.readFully(exchange.getRequestBody()); - byte[] response = ("\n" - + "\n" - + " bucket\n" - + " write_large_blob\n" - + "").getBytes(StandardCharsets.UTF_8); + byte[] response = (""" + + + bucket + write_large_blob + """).getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/xml"); exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); exchange.getResponseBody().write(response); @@ -351,12 +353,13 @@ public void testWriteLargeBlobStreaming() throws Exception { if ("POST".equals(exchange.getRequestMethod()) && exchange.getRequestURI().getQuery().equals("uploads")) { // initiate multipart upload request if (countDownInitiate.countDown()) { - byte[] response = ("\n" - + "\n" - + " bucket\n" - + " write_large_blob_streaming\n" - + " TEST\n" - + "").getBytes(StandardCharsets.UTF_8); + byte[] response = (""" + + + bucket + write_large_blob_streaming + TEST + """).getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/xml"); exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); exchange.getResponseBody().write(response); @@ -382,11 +385,12 @@ public void testWriteLargeBlobStreaming() throws Exception { // complete multipart upload request if (countDownComplete.countDown()) { Streams.readFully(exchange.getRequestBody()); - byte[] response = ("\n" - + "\n" - + " bucket\n" - + " write_large_blob_streaming\n" - + "").getBytes(StandardCharsets.UTF_8); + byte[] response = (""" + + + bucket + write_large_blob_streaming + """).getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/xml"); exchange.sendResponseHeaders(HttpStatus.SC_OK, response.length); exchange.getResponseBody().write(response); diff --git a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java index 1db93493d31a4..174506388a6fb 100644 --- a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java +++ b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java @@ -569,26 +569,31 @@ public void testShrinkAfterUpgrade() throws IOException { public void testRollover() throws IOException { if (isRunningAgainstOldCluster()) { Request createIndex = new Request("PUT", "/" + index + "-000001"); - createIndex.setJsonEntity("{" + " \"aliases\": {" + " \"" + index + "_write\": {}" + " }" + "}"); + createIndex.setJsonEntity(""" + { + "aliases": { + "%s_write": {} + } + }""".formatted(index)); client().performRequest(createIndex); } int bulkCount = 10; - StringBuilder bulk = new StringBuilder(); - for (int i = 0; i < bulkCount; i++) { - bulk.append("{\"index\":{}}\n"); - bulk.append("{\"test\":\"test\"}\n"); - } + String bulk = """ + {"index":{}} + {"test":"test"} + """.repeat(bulkCount); Request bulkRequest = new Request("POST", "/" + index + "_write/_bulk"); - bulkRequest.setJsonEntity(bulk.toString()); + bulkRequest.setJsonEntity(bulk); bulkRequest.addParameter("refresh", ""); assertThat(EntityUtils.toString(client().performRequest(bulkRequest).getEntity()), containsString("\"errors\":false")); if (isRunningAgainstOldCluster()) { Request rolloverRequest = new Request("POST", "/" + index + "_write/_rollover"); - rolloverRequest.setJsonEntity("{" + " \"conditions\": {" + " \"max_docs\": 5" + " }" + "}"); + rolloverRequest.setJsonEntity(""" + { "conditions": { "max_docs": 5 }}"""); client().performRequest(rolloverRequest); assertThat( @@ -623,7 +628,8 @@ void assertBasicSearchWorks(int count) throws IOException { logger.info("--> testing basic search with sort"); { Request searchRequest = new Request("GET", "/" + index + "/_search"); - searchRequest.setJsonEntity("{ \"sort\": [{ \"int\" : \"asc\" }]}"); + searchRequest.setJsonEntity(""" + { "sort": [{ "int" : "asc" }]}"""); Map response = entityAsMap(client().performRequest(searchRequest)); assertNoFailures(response); assertTotalHits(count, response); @@ -632,7 +638,8 @@ void assertBasicSearchWorks(int count) throws IOException { logger.info("--> testing exists filter"); { Request searchRequest = new Request("GET", "/" + index + "/_search"); - searchRequest.setJsonEntity("{ \"query\": { \"exists\" : {\"field\": \"string\"} }}"); + searchRequest.setJsonEntity(""" + { "query": { "exists" : {"field": "string"} }}"""); Map response = entityAsMap(client().performRequest(searchRequest)); assertNoFailures(response); assertTotalHits(count, response); @@ -641,7 +648,8 @@ void assertBasicSearchWorks(int count) throws IOException { logger.info("--> testing field with dots in the name"); { Request searchRequest = new Request("GET", "/" + index + "/_search"); - searchRequest.setJsonEntity("{ \"query\": { \"exists\" : {\"field\": \"field.with.dots\"} }}"); + searchRequest.setJsonEntity(""" + { "query": { "exists" : {"field": "field.with.dots"} }}"""); Map response = entityAsMap(client().performRequest(searchRequest)); assertNoFailures(response); assertTotalHits(count, response); @@ -678,7 +686,17 @@ void assertAllSearchWorks(int count) throws IOException { void assertBasicAggregationWorks() throws IOException { // histogram on a long Request longHistogramRequest = new Request("GET", "/" + index + "/_search"); - longHistogramRequest.setJsonEntity("{ \"aggs\": { \"histo\" : {\"histogram\" : {\"field\": \"int\", \"interval\": 10}} }}"); + longHistogramRequest.setJsonEntity(""" + { + "aggs": { + "histo": { + "histogram": { + "field": "int", + "interval": 10 + } + } + } + }"""); Map longHistogram = entityAsMap(client().performRequest(longHistogramRequest)); assertNoFailures(longHistogram); List histoBuckets = (List) XContentMapValues.extractValue("aggregations.histo.buckets", longHistogram); @@ -691,7 +709,16 @@ void assertBasicAggregationWorks() throws IOException { // terms on a boolean Request boolTermsRequest = new Request("GET", "/" + index + "/_search"); - boolTermsRequest.setJsonEntity("{ \"aggs\": { \"bool_terms\" : {\"terms\" : {\"field\": \"bool\"}} }}"); + boolTermsRequest.setJsonEntity(""" + { + "aggs": { + "bool_terms": { + "terms": { + "field": "bool" + } + } + } + }"""); Map boolTerms = entityAsMap(client().performRequest(boolTermsRequest)); List termsBuckets = (List) XContentMapValues.extractValue("aggregations.bool_terms.buckets", boolTerms); int termsCount = 0; @@ -704,17 +731,20 @@ void assertBasicAggregationWorks() throws IOException { void assertRealtimeGetWorks() throws IOException { Request disableAutoRefresh = new Request("PUT", "/" + index + "/_settings"); - disableAutoRefresh.setJsonEntity("{ \"index\": { \"refresh_interval\" : -1 }}"); + disableAutoRefresh.setJsonEntity(""" + { "index": { "refresh_interval" : -1 }}"""); client().performRequest(disableAutoRefresh); Request searchRequest = new Request("GET", "/" + index + "/_search"); - searchRequest.setJsonEntity("{ \"query\": { \"match_all\" : {} }}"); + searchRequest.setJsonEntity(""" + { "query": { "match_all" : {} }}"""); Map searchResponse = entityAsMap(client().performRequest(searchRequest)); Map hit = (Map) ((List) (XContentMapValues.extractValue("hits.hits", searchResponse))).get(0); String docId = (String) hit.get("_id"); Request updateRequest = new Request("POST", "/" + index + "/_update/" + docId); - updateRequest.setJsonEntity("{ \"doc\" : { \"foo\": \"bar\"}}"); + updateRequest.setJsonEntity(""" + { "doc" : { "foo": "bar"}}"""); client().performRequest(updateRequest); Request getRequest = new Request("GET", "/" + index + "/_doc/" + docId); @@ -724,13 +754,21 @@ void assertRealtimeGetWorks() throws IOException { assertTrue("doc does not contain 'foo' key: " + source, source.containsKey("foo")); Request enableAutoRefresh = new Request("PUT", "/" + index + "/_settings"); - enableAutoRefresh.setJsonEntity("{ \"index\": { \"refresh_interval\" : \"1s\" }}"); + enableAutoRefresh.setJsonEntity(""" + { "index": { "refresh_interval" : "1s" }}"""); client().performRequest(enableAutoRefresh); } void assertStoredBinaryFields(int count) throws Exception { Request request = new Request("GET", "/" + index + "/_search"); - request.setJsonEntity("{ \"query\": { \"match_all\" : {} }, \"size\": 100, \"stored_fields\": \"binary\"}"); + request.setJsonEntity(""" + { + "query": { + "match_all": {} + }, + "size": 100, + "stored_fields": "binary" + }"""); Map rsp = entityAsMap(client().performRequest(request)); assertTotalHits(count, rsp); @@ -952,9 +990,9 @@ public void testSnapshotRestore() throws IOException { // Stick a routing attribute into to cluster settings so we can see it after the restore Request addRoutingSettings = new Request("PUT", "/_cluster/settings"); - addRoutingSettings.setJsonEntity( - "{\"persistent\": {\"cluster.routing.allocation.exclude.test_attr\": \"" + getOldClusterVersion() + "\"}}" - ); + addRoutingSettings.setJsonEntity(""" + {"persistent": {"cluster.routing.allocation.exclude.test_attr": "%s"}} + """.formatted(getOldClusterVersion())); client().performRequest(addRoutingSettings); // Stick a template into the cluster so we can see it after the restore @@ -1226,7 +1264,8 @@ private void checkSnapshot(final String snapshotName, final int count, final Ver // Remove the routing setting and template so we can test restoring them. Request clearRoutingFromSettings = new Request("PUT", "/_cluster/settings"); - clearRoutingFromSettings.setJsonEntity("{\"persistent\":{\"cluster.routing.allocation.exclude.test_attr\": null}}"); + clearRoutingFromSettings.setJsonEntity(""" + {"persistent":{"cluster.routing.allocation.exclude.test_attr": null}}"""); client().performRequest(clearRoutingFromSettings); client().performRequest(new Request("DELETE", "/_template/test_template")); @@ -1252,8 +1291,10 @@ private void checkSnapshot(final String snapshotName, final int count, final Ver int extras = between(1, 100); StringBuilder bulk = new StringBuilder(); for (int i = 0; i < extras; i++) { - bulk.append("{\"index\":{\"_id\":\"").append(count + i).append("\"}}\n"); - bulk.append("{\"test\":\"test\"}\n"); + bulk.append(""" + {"index":{"_id":"%s"}} + {"test":"test"} + """.formatted(count + i)); } Request writeToRestoredRequest = new Request("POST", "/restored_" + index + "/_bulk"); @@ -1557,21 +1598,23 @@ public void testSystemIndexMetadataIsUpgraded() throws Exception { Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); - bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\"}}\n" + "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); + bulk.setJsonEntity(""" + {"index": {"_index": "test_index_old"}} + {"f1": "v1", "f2": "v2"} + """); client().performRequest(bulk); // start a async reindex job Request reindex = new Request("POST", "/_reindex"); - reindex.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\":\"test_index_old\"\n" - + " },\n" - + " \"dest\":{\n" - + " \"index\":\"test_index_reindex\"\n" - + " }\n" - + "}" - ); + reindex.setJsonEntity(""" + { + "source":{ + "index":"test_index_old" + }, + "dest":{ + "index":"test_index_reindex" + } + }"""); reindex.addParameter("wait_for_completion", "false"); Map response = entityAsMap(client().performRequest(reindex)); String taskId = (String) response.get("task"); @@ -1606,14 +1649,13 @@ public void testSystemIndexMetadataIsUpgraded() throws Exception { if (minimumNodeVersion().before(SYSTEM_INDEX_ENFORCEMENT_VERSION)) { // Create an alias to make sure it gets upgraded properly Request putAliasRequest = new Request("POST", "/_aliases"); - putAliasRequest.setJsonEntity( - "{\n" - + " \"actions\": [\n" - + " {\"add\": {\"index\": \".tasks\", \"alias\": \"test-system-alias\"}},\n" - + " {\"add\": {\"index\": \"test_index_reindex\", \"alias\": \"test-system-alias\"}}\n" - + " ]\n" - + "}" - ); + putAliasRequest.setJsonEntity(""" + { + "actions": [ + {"add": {"index": ".tasks", "alias": "test-system-alias"}}, + {"add": {"index": "test_index_reindex", "alias": "test-system-alias"}} + ] + }"""); putAliasRequest.setOptions(expectVersionSpecificWarnings(v -> { v.current(systemIndexWarning); v.compatible(systemIndexWarning); diff --git a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java index 2235adeffdf63..6d5b44adc6441 100644 --- a/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java +++ b/qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/QueryBuilderBWCIT.java @@ -62,51 +62,57 @@ public class QueryBuilderBWCIT extends AbstractFullClusterRestartTestCase { private static final List CANDIDATES = new ArrayList<>(); static { - addCandidate("\"match\": { \"keyword_field\": \"value\"}", new MatchQueryBuilder("keyword_field", "value")); + addCandidate(""" + "match": { "keyword_field": "value"} + """, new MatchQueryBuilder("keyword_field", "value")); + addCandidate(""" + "match": { "keyword_field": {"query": "value", "operator": "and"} } + """, new MatchQueryBuilder("keyword_field", "value").operator(Operator.AND)); + addCandidate(""" + "match": { "keyword_field": {"query": "value", "analyzer": "english"} } + """, new MatchQueryBuilder("keyword_field", "value").analyzer("english")); + addCandidate(""" + "match": { "keyword_field": {"query": "value", "minimum_should_match": 3} } + """, new MatchQueryBuilder("keyword_field", "value").minimumShouldMatch("3")); + addCandidate(""" + "match": { "keyword_field": {"query": "value", "fuzziness": "auto"} } + """, new MatchQueryBuilder("keyword_field", "value").fuzziness(Fuzziness.AUTO)); + addCandidate(""" + "match_phrase": { "keyword_field": "value"} + """, new MatchPhraseQueryBuilder("keyword_field", "value")); + addCandidate(""" + "match_phrase": { "keyword_field": {"query": "value", "slop": 3}} + """, new MatchPhraseQueryBuilder("keyword_field", "value").slop(3)); + addCandidate(""" + "range": { "long_field": {"gte": 1, "lte": 9}} + """, new RangeQueryBuilder("long_field").from(1).to(9)); addCandidate( - "\"match\": { \"keyword_field\": {\"query\": \"value\", \"operator\": \"and\"} }", - new MatchQueryBuilder("keyword_field", "value").operator(Operator.AND) - ); - addCandidate( - "\"match\": { \"keyword_field\": {\"query\": \"value\", \"analyzer\": \"english\"} }", - new MatchQueryBuilder("keyword_field", "value").analyzer("english") - ); - addCandidate( - "\"match\": { \"keyword_field\": {\"query\": \"value\", \"minimum_should_match\": 3} }", - new MatchQueryBuilder("keyword_field", "value").minimumShouldMatch("3") - ); - addCandidate( - "\"match\": { \"keyword_field\": {\"query\": \"value\", \"fuzziness\": \"auto\"} }", - new MatchQueryBuilder("keyword_field", "value").fuzziness(Fuzziness.AUTO) - ); - addCandidate("\"match_phrase\": { \"keyword_field\": \"value\"}", new MatchPhraseQueryBuilder("keyword_field", "value")); - addCandidate( - "\"match_phrase\": { \"keyword_field\": {\"query\": \"value\", \"slop\": 3}}", - new MatchPhraseQueryBuilder("keyword_field", "value").slop(3) - ); - addCandidate("\"range\": { \"long_field\": {\"gte\": 1, \"lte\": 9}}", new RangeQueryBuilder("long_field").from(1).to(9)); - addCandidate( - "\"bool\": { \"must_not\": [{\"match_all\": {}}], \"must\": [{\"match_all\": {}}], " - + "\"filter\": [{\"match_all\": {}}], \"should\": [{\"match_all\": {}}]}", + """ + "bool": { "must_not": [{"match_all": {}}], "must": [{"match_all": {}}], "filter": [{"match_all": {}}], \ + "should": [{"match_all": {}}]} + """, new BoolQueryBuilder().mustNot(new MatchAllQueryBuilder()) .must(new MatchAllQueryBuilder()) .filter(new MatchAllQueryBuilder()) .should(new MatchAllQueryBuilder()) ); addCandidate( - "\"dis_max\": {\"queries\": [{\"match_all\": {}},{\"match_all\": {}},{\"match_all\": {}}], \"tie_breaker\": 0.01}", + """ + "dis_max": {"queries": [{"match_all": {}},{"match_all": {}},{"match_all": {}}], "tie_breaker": 0.01} + """, new DisMaxQueryBuilder().add(new MatchAllQueryBuilder()) .add(new MatchAllQueryBuilder()) .add(new MatchAllQueryBuilder()) .tieBreaker(0.01f) ); + addCandidate(""" + "constant_score": {"filter": {"match_all": {}}, "boost": 0.1} + """, new ConstantScoreQueryBuilder(new MatchAllQueryBuilder()).boost(0.1f)); addCandidate( - "\"constant_score\": {\"filter\": {\"match_all\": {}}, \"boost\": 0.1}", - new ConstantScoreQueryBuilder(new MatchAllQueryBuilder()).boost(0.1f) - ); - addCandidate( - "\"function_score\": {\"query\": {\"match_all\": {}}," - + "\"functions\": [{\"random_score\": {}, \"filter\": {\"match_all\": {}}, \"weight\": 0.2}]}", + """ + "function_score": {"query": {"match_all": {}},"functions": [{"random_score": {}, "filter": {"match_all": {}}, \ + "weight": 0.2}]} + """, new FunctionScoreQueryBuilder( new MatchAllQueryBuilder(), new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { @@ -117,22 +123,28 @@ public class QueryBuilderBWCIT extends AbstractFullClusterRestartTestCase { ) ); addCandidate( - "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " - + "{ \"span_term\": { \"keyword_field\": \"value2\" }}]}", + """ + "span_near": {"clauses": [{ "span_term": { "keyword_field": "value1" }}, \ + { "span_term": { "keyword_field": "value2" }}]} + """, new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 0).addClause( new SpanTermQueryBuilder("keyword_field", "value2") ) ); addCandidate( - "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " - + "{ \"span_term\": { \"keyword_field\": \"value2\" }}], \"slop\": 2}", + """ + "span_near": {"clauses": [{ "span_term": { "keyword_field": "value1" }}, \ + { "span_term": { "keyword_field": "value2" }}], "slop": 2} + """, new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 2).addClause( new SpanTermQueryBuilder("keyword_field", "value2") ) ); addCandidate( - "\"span_near\": {\"clauses\": [{ \"span_term\": { \"keyword_field\": \"value1\" }}, " - + "{ \"span_term\": { \"keyword_field\": \"value2\" }}], \"slop\": 2, \"in_order\": false}", + """ + "span_near": {"clauses": [{ "span_term": { "keyword_field": "value1" }}, \ + { "span_term": { "keyword_field": "value2" }}], "slop": 2, "in_order": false} + """, new SpanNearQueryBuilder(new SpanTermQueryBuilder("keyword_field", "value1"), 2).addClause( new SpanTermQueryBuilder("keyword_field", "value2") ).inOrder(false) @@ -195,12 +207,9 @@ public void testQueryBuilderBWC() throws Exception { for (int i = 0; i < CANDIDATES.size(); i++) { QueryBuilder expectedQueryBuilder = (QueryBuilder) CANDIDATES.get(i)[1]; Request request = new Request("GET", "/" + index + "/_search"); - request.setJsonEntity( - "{\"query\": {\"ids\": {\"values\": [\"" - + Integer.toString(i) - + "\"]}}, " - + "\"docvalue_fields\": [{\"field\":\"query.query_builder_field\"}]}" - ); + request.setJsonEntity(""" + {"query": {"ids": {"values": ["%s"]}}, "docvalue_fields": [{"field":"query.query_builder_field"}]} + """.formatted(i)); Response rsp = client().performRequest(request); assertEquals(200, rsp.getStatusLine().getStatusCode()); Map hitRsp = (Map) ((List) ((Map) toMap(rsp).get("hits")).get("hits")).get(0); diff --git a/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java b/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java index e664de150b449..d7af475e15dc7 100644 --- a/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java +++ b/qa/logging-config/src/test/java/org/elasticsearch/common/logging/ESJsonLayoutTests.java @@ -11,6 +11,8 @@ import org.hamcrest.Matchers; import org.junit.BeforeClass; +import java.util.Locale; + public class ESJsonLayoutTests extends ESTestCase { @BeforeClass public static void initNodeName() { @@ -25,23 +27,17 @@ public void testLayout() { ESJsonLayout server = ESJsonLayout.newBuilder().setType("server").build(); String conversionPattern = server.getPatternLayout().getConversionPattern(); - assertThat( - conversionPattern, - Matchers.equalTo( - "{" - + "\"type\": \"server\", " - + "\"timestamp\": \"%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}\", " - + "\"level\": \"%p\", " - + "\"component\": \"%c{1.}\", " - + "\"cluster.name\": \"${sys:es.logs.cluster_name}\", " - + "\"node.name\": \"%node_name\", " - + "\"message\": \"%notEmpty{%enc{%marker}{JSON} }%enc{%.-10000m}{JSON}\"" - + "%notEmpty{, %node_and_cluster_id }" - + "%notEmpty{, %CustomMapFields }" - + "%exceptionAsJson }" - + System.lineSeparator() - ) - ); + assertThat(conversionPattern, Matchers.equalTo(String.format(Locale.ROOT, """ + {\ + "type": "server", \ + "timestamp": "%%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}", \ + "level": "%%p", \ + "component": "%%c{1.}", \ + "cluster.name": "${sys:es.logs.cluster_name}", \ + "node.name": "%%node_name", \ + "message": "%%notEmpty{%%enc{%%marker}{JSON} }%%enc{%%.-10000m}{JSON}"%%notEmpty{, \ + %%node_and_cluster_id }%%notEmpty{, %%CustomMapFields }%%exceptionAsJson \ + }%n"""))); } public void testLayoutWithAdditionalFieldOverride() { @@ -49,21 +45,14 @@ public void testLayoutWithAdditionalFieldOverride() { String conversionPattern = server.getPatternLayout().getConversionPattern(); // message field is removed as is expected to be provided by a field from a message - assertThat( - conversionPattern, - Matchers.equalTo( - "{" - + "\"type\": \"server\", " - + "\"timestamp\": \"%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}\", " - + "\"level\": \"%p\", " - + "\"component\": \"%c{1.}\", " - + "\"cluster.name\": \"${sys:es.logs.cluster_name}\", " - + "\"node.name\": \"%node_name\"" - + "%notEmpty{, %node_and_cluster_id }" - + "%notEmpty{, %CustomMapFields }" - + "%exceptionAsJson }" - + System.lineSeparator() - ) - ); + assertThat(conversionPattern, Matchers.equalTo(String.format(Locale.ROOT, """ + {\ + "type": "server", \ + "timestamp": "%%d{yyyy-MM-dd'T'HH:mm:ss,SSSZZ}", \ + "level": "%%p", \ + "component": "%%c{1.}", \ + "cluster.name": "${sys:es.logs.cluster_name}", \ + "node.name": "%%node_name"%%notEmpty{, %%node_and_cluster_id }%%notEmpty{, %%CustomMapFields }%%exceptionAsJson \ + }%n"""))); } } diff --git a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java index 86e85b98dbe37..758f3557b991a 100644 --- a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java +++ b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/RareTermsIT.java @@ -30,8 +30,10 @@ private int indexDocs(int numDocs, int id) throws Exception { final Request request = new Request("POST", "/_bulk"); final StringBuilder builder = new StringBuilder(); for (int i = 0; i < numDocs; ++i) { - builder.append("{ \"index\" : { \"_index\" : \"" + index + "\", \"_id\": \"" + id++ + "\" } }\n"); - builder.append("{\"str_value\" : \"s" + i + "\"}\n"); + builder.append(""" + { "index" : { "_index" : "%s", "_id": "%s" } } + {"str_value" : "s%s"} + """.formatted(index, id++, i)); } request.setJsonEntity(builder.toString()); assertOK(client().performRequest(request)); @@ -60,9 +62,17 @@ public void testSingleValuedString() throws Exception { private void assertNumRareTerms(int maxDocs, int rareTerms) throws IOException { final Request request = new Request("POST", index + "/_search"); - request.setJsonEntity( - "{\"aggs\" : {\"rareTerms\" : {\"rare_terms\" : {\"field\" : \"str_value.keyword\", \"max_doc_count\" : " + maxDocs + "}}}}" - ); + request.setJsonEntity(""" + { + "aggs": { + "rareTerms": { + "rare_terms": { + "field": "str_value.keyword", + "max_doc_count": %s + } + } + } + }""".formatted(maxDocs)); final Response response = client().performRequest(request); assertOK(response); final Object o = XContentMapValues.extractValue("aggregations.rareTerms.buckets", responseAsMap(response)); diff --git a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java index 1908a2a473e96..10e991c37febb 100644 --- a/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java +++ b/qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/SearchWithMinCompatibleSearchNodeIT.java @@ -84,19 +84,11 @@ public void testMinVersionAsNewVersion() throws Exception { responseException.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.INTERNAL_SERVER_ERROR.getStatus()) ); - assertThat( - responseException.getMessage(), - containsString("{\"error\":{\"root_cause\":[],\"type\":\"search_phase_execution_exception\"") - ); - assertThat( - responseException.getMessage(), - containsString( - "caused_by\":{\"type\":\"version_mismatch_exception\"," - + "\"reason\":\"One of the shards is incompatible with the required minimum version [" - + newVersion - + "]\"" - ) - ); + assertThat(responseException.getMessage(), containsString(""" + {"error":{"root_cause":[],"type":"search_phase_execution_exception\"""")); + assertThat(responseException.getMessage(), containsString(""" + caused_by":{"type":"version_mismatch_exception",\ + "reason":"One of the shards is incompatible with the required minimum version [%s]\"""".formatted(newVersion))); }); } } @@ -109,7 +101,8 @@ public void testMinVersionAsOldVersion() throws Exception { "POST", index + "/_search?min_compatible_shard_node=" + bwcVersion + "&ccs_minimize_roundtrips=false" ); - oldVersionRequest.setJsonEntity("{\"query\":{\"match_all\":{}},\"_source\":false}"); + oldVersionRequest.setJsonEntity(""" + {"query":{"match_all":{}},"_source":false}"""); assertBusy(() -> { assertWithBwcVersionCheck(() -> { Response response = client.performRequest(oldVersionRequest); @@ -143,10 +136,9 @@ public void testCcsMinimizeRoundtripsIsFalse() throws Exception { responseException.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()) ); - assertThat( - responseException.getMessage(), - containsString("{\"error\":{\"root_cause\":[{\"type\":\"action_request_validation_exception\"") - ); + assertThat(responseException.getMessage(), containsString(""" + {"error":{"root_cause":[{"type":"action_request_validation_exception"\ + """)); assertThat( responseException.getMessage(), containsString( diff --git a/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java b/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java index 9f2fb4d514da8..6f1911ec78d4c 100644 --- a/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java +++ b/qa/multi-cluster-search/src/test/java/org/elasticsearch/search/CCSDuelIT.java @@ -178,13 +178,23 @@ private static void indexDocuments(String idPrefix) throws IOException, Interrup int numShards = randomIntBetween(1, 5); CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX_NAME); createIndexRequest.settings(Settings.builder().put("index.number_of_shards", numShards).put("index.number_of_replicas", 0)); - createIndexRequest.mapping( - "{\"properties\":{" - + "\"id\":{\"type\":\"keyword\"}," - + "\"suggest\":{\"type\":\"completion\"}," - + "\"join\":{\"type\":\"join\", \"relations\": {\"question\":\"answer\"}}}}", - XContentType.JSON - ); + createIndexRequest.mapping(""" + { + "properties": { + "id": { + "type": "keyword" + }, + "suggest": { + "type": "completion" + }, + "join": { + "type": "join", + "relations": { + "question": "answer" + } + } + } + }""", XContentType.JSON); CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); diff --git a/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java b/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java index 9879604cbf24c..d7b6773e461ec 100644 --- a/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java +++ b/qa/no-bootstrap-tests/src/test/java/org/elasticsearch/bootstrap/SpawnerNoBootstrapTests.java @@ -48,7 +48,13 @@ */ public class SpawnerNoBootstrapTests extends LuceneTestCase { - private static final String CONTROLLER_SOURCE = "#!/bin/bash\n" + "\n" + "echo I am alive\n" + "\n" + "read SOMETHING\n"; + private static final String CONTROLLER_SOURCE = """ + #!/bin/bash + + echo I am alive + + read SOMETHING + """; /** * Simplest case: a module with no controller daemon. diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/test/PackagesSecurityAutoConfigurationTests.java b/qa/os/src/test/java/org/elasticsearch/packaging/test/PackagesSecurityAutoConfigurationTests.java index baafe77a8d483..a9d3a02754d64 100644 --- a/qa/os/src/test/java/org/elasticsearch/packaging/test/PackagesSecurityAutoConfigurationTests.java +++ b/qa/os/src/test/java/org/elasticsearch/packaging/test/PackagesSecurityAutoConfigurationTests.java @@ -154,10 +154,7 @@ public void test71ReconfigureFailsWhenKeyStorePasswordWrong() throws Exception { verifyPackageInstallation(installation, distribution(), sh); verifySecurityAutoConfigured(installation); assertNotNull(installation.getElasticPassword()); - Shell.Result changePassword = installation.executables().keystoreTool.run( - "passwd", - "some-password" + "\n" + "some-password" + "\n" - ); + Shell.Result changePassword = installation.executables().keystoreTool.run("passwd", "some-password\nsome-password\n"); assertThat(changePassword.exitCode, equalTo(0)); Shell.Result result = installation.executables().nodeReconfigureTool.run( "--enrollment-token a-token", diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/util/Shell.java b/qa/os/src/test/java/org/elasticsearch/packaging/util/Shell.java index 6802ac54fdacb..86cf22c1df1d4 100644 --- a/qa/os/src/test/java/org/elasticsearch/packaging/util/Shell.java +++ b/qa/os/src/test/java/org/elasticsearch/packaging/util/Shell.java @@ -87,28 +87,19 @@ public void chown(Path path) throws Exception { public void chown(Path path, String newOwner) throws Exception { logger.info("Chowning " + path + " to " + newOwner); Platforms.onLinux(() -> run("chown -R elasticsearch:elasticsearch " + path)); - Platforms.onWindows( - () -> run( - String.format( - Locale.ROOT, - "$account = New-Object System.Security.Principal.NTAccount '%s'; " - + "$pathInfo = Get-Item '%s'; " - + "$toChown = @(); " - + "if ($pathInfo.PSIsContainer) { " - + " $toChown += Get-ChildItem '%s' -Recurse; " - + "}" - + "$toChown += $pathInfo; " - + "$toChown | ForEach-Object { " - + " $acl = Get-Acl $_.FullName; " - + " $acl.SetOwner($account); " - + " Set-Acl $_.FullName $acl " - + "}", - newOwner, - path, - path - ) - ) - ); + Platforms.onWindows(() -> run(String.format(Locale.ROOT, """ + $account = New-Object System.Security.Principal.NTAccount '%s'; + $pathInfo = Get-Item '%s'; + $toChown = @(); + if ($pathInfo.PSIsContainer) { + $toChown += Get-ChildItem '%s' -Recurse; + } + $toChown += $pathInfo; + $toChown | ForEach-Object { + $acl = Get-Acl $_.FullName; + $acl.SetOwner($account); + Set-Acl $_.FullName $acl + }""", newOwner, path, path))); } public void extractZip(Path zipPath, Path destinationDir) throws Exception { diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/util/docker/Docker.java b/qa/os/src/test/java/org/elasticsearch/packaging/util/docker/Docker.java index 7308ca8245df1..286abe9676935 100644 --- a/qa/os/src/test/java/org/elasticsearch/packaging/util/docker/Docker.java +++ b/qa/os/src/test/java/org/elasticsearch/packaging/util/docker/Docker.java @@ -191,14 +191,18 @@ public static void waitForElasticsearchToStart() { if (isElasticsearchRunning == false) { final Shell.Result dockerLogs = getContainerLogs(); - fail( - "Elasticsearch container did not start successfully.\n\nps output:\n" - + psOutput - + "\n\nStdout:\n" - + dockerLogs.stdout - + "\n\nStderr:\n" - + dockerLogs.stderr - ); + fail(""" + Elasticsearch container did not start successfully. + + ps output: + %s + + Stdout: + %s + + Stderr: + %s\ + """.formatted(psOutput, dockerLogs.stdout, dockerLogs.stderr)); } } @@ -225,7 +229,15 @@ private static void waitForElasticsearchToExit() { if (isElasticsearchRunning) { final Shell.Result dockerLogs = getContainerLogs(); - fail("Elasticsearch container didn't exit.\n\nStdout:\n" + dockerLogs.stdout + "\n\nStderr:\n" + dockerLogs.stderr); + fail(""" + Elasticsearch container didn't exit. + + Stdout: + %s + + Stderr: + %s\ + """.formatted(dockerLogs.stdout, dockerLogs.stderr)); } } diff --git a/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java b/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java index 1445feaeb2bbd..9c436cf9c5608 100644 --- a/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java +++ b/qa/repository-multi-version/src/test/java/org/elasticsearch/upgrades/MultiVersionRepositoryAccessIT.java @@ -324,18 +324,15 @@ private static void createSnapshot(RestHighLevelClient client, String repoName, private void createIndex(RestHighLevelClient client, String name, int shards) throws IOException { final Request putIndexRequest = new Request("PUT", "/" + name); - putIndexRequest.setJsonEntity( - "{\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"number_of_shards\" : " - + shards - + ", \n" - + " \"number_of_replicas\" : 0 \n" - + " }\n" - + " }\n" - + "}" - ); + putIndexRequest.setJsonEntity(""" + { + "settings" : { + "index" : { + "number_of_shards" : %s, + "number_of_replicas" : 0 + } + } + }""".formatted(shards)); final Response response = client.getLowLevelClient().performRequest(putIndexRequest); assertThat(response.getStatusLine().getStatusCode(), is(HttpURLConnection.HTTP_OK)); } diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java index 8d46490608652..4f7b1da59b812 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/FeatureUpgradeIT.java @@ -36,21 +36,23 @@ public void testGetFeatureUpgradeStatus() throws Exception { Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); - bulk.setJsonEntity("{\"index\": {\"_index\": \"feature_test_index_old\"}}\n" + "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); + bulk.setJsonEntity(""" + {"index": {"_index": "feature_test_index_old"}} + {"f1": "v1", "f2": "v2"} + """); client().performRequest(bulk); // start a async reindex job Request reindex = new Request("POST", "/_reindex"); - reindex.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\":\"feature_test_index_old\"\n" - + " },\n" - + " \"dest\":{\n" - + " \"index\":\"feature_test_index_reindex\"\n" - + " }\n" - + "}" - ); + reindex.setJsonEntity(""" + { + "source":{ + "index":"feature_test_index_old" + }, + "dest":{ + "index":"feature_test_index_reindex" + } + }"""); reindex.addParameter("wait_for_completion", "false"); Map response = entityAsMap(client().performRequest(reindex)); String taskId = (String) response.get("task"); diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java index ddf44ba7bd89b..239320d584099 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java @@ -135,12 +135,13 @@ public void testIndexing() throws IOException { public void testAutoIdWithOpTypeCreate() throws IOException { final String indexName = "auto_id_and_op_type_create_index"; - StringBuilder b = new StringBuilder(); - b.append("{\"create\": {\"_index\": \"").append(indexName).append("\"}}\n"); - b.append("{\"f1\": \"v\"}\n"); + String b = """ + {"create": {"_index": "%s"}} + {"f1": "v"} + """.formatted(indexName); Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); - bulk.setJsonEntity(b.toString()); + bulk.setJsonEntity(b); switch (CLUSTER_TYPE) { case OLD: @@ -333,10 +334,10 @@ private void tsdbBulk(StringBuilder bulk, String dim, long timeStart, long timeE long delta = TimeUnit.SECONDS.toMillis(20); double value = (timeStart - TSDB_TIMES[0]) / TimeUnit.SECONDS.toMillis(20) * rate; for (long t = timeStart; t < timeEnd; t += delta) { - bulk.append("{\"index\": {\"_index\": \"tsdb\"}}\n"); - bulk.append("{\"@timestamp\": ").append(t); - bulk.append(", \"dim\": \"").append(dim).append("\""); - bulk.append(", \"value\": ").append(value).append("}\n"); + bulk.append(""" + {"index": {"_index": "tsdb"}} + {"@timestamp": %s, "dim": "%s", "value": %s} + """.formatted(t, dim, value)); value += rate; } } diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java index f814f899ae3c5..b53766f70acb3 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SystemIndicesUpgradeIT.java @@ -34,21 +34,23 @@ public void testSystemIndicesUpgrades() throws Exception { Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); - bulk.setJsonEntity("{\"index\": {\"_index\": \"test_index_old\"}}\n" + "{\"f1\": \"v1\", \"f2\": \"v2\"}\n"); + bulk.setJsonEntity(""" + {"index": {"_index": "test_index_old"}} + {"f1": "v1", "f2": "v2"} + """); client().performRequest(bulk); // start a async reindex job Request reindex = new Request("POST", "/_reindex"); - reindex.setJsonEntity( - "{\n" - + " \"source\":{\n" - + " \"index\":\"test_index_old\"\n" - + " },\n" - + " \"dest\":{\n" - + " \"index\":\"test_index_reindex\"\n" - + " }\n" - + "}" - ); + reindex.setJsonEntity(""" + { + "source":{ + "index":"test_index_old" + }, + "dest":{ + "index":"test_index_reindex" + } + }"""); reindex.addParameter("wait_for_completion", "false"); Map response = entityAsMap(client().performRequest(reindex)); String taskId = (String) response.get("task"); @@ -83,14 +85,13 @@ public void testSystemIndicesUpgrades() throws Exception { if (minimumNodeVersion().before(SYSTEM_INDEX_ENFORCEMENT_VERSION)) { // Create an alias to make sure it gets upgraded properly Request putAliasRequest = new Request("POST", "/_aliases"); - putAliasRequest.setJsonEntity( - "{\n" - + " \"actions\": [\n" - + " {\"add\": {\"index\": \".tasks\", \"alias\": \"test-system-alias\"}},\n" - + " {\"add\": {\"index\": \"test_index_reindex\", \"alias\": \"test-system-alias\"}}\n" - + " ]\n" - + "}" - ); + putAliasRequest.setJsonEntity(""" + { + "actions": [ + {"add": {"index": ".tasks", "alias": "test-system-alias"}}, + {"add": {"index": "test_index_reindex", "alias": "test-system-alias"}} + ] + }"""); putAliasRequest.setOptions(expectVersionSpecificWarnings(v -> { v.current(systemIndexWarning); v.compatible(systemIndexWarning); diff --git a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java index ecab6cfbc9808..40e63b4ae32d7 100644 --- a/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java +++ b/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/XPackIT.java @@ -48,14 +48,20 @@ public void skipIfNotXPack() { */ public void testBasicFeature() throws IOException { Request bulk = new Request("POST", "/sql_test/_bulk"); - bulk.setJsonEntity("{\"index\":{}}\n" + "{\"f\": \"1\"}\n" + "{\"index\":{}}\n" + "{\"f\": \"2\"}\n"); + bulk.setJsonEntity(""" + {"index":{}} + {"f": "1"} + {"index":{}} + {"f": "2"} + """); bulk.addParameter("refresh", "true"); client().performRequest(bulk); Request sql = new Request("POST", "/_sql"); sql.setJsonEntity("{\"query\": \"SELECT * FROM sql_test WHERE f > 1 ORDER BY f ASC\"}"); String response = EntityUtils.toString(client().performRequest(sql).getEntity()); - assertEquals("{\"columns\":[{\"name\":\"f\",\"type\":\"text\"}],\"rows\":[[\"2\"]]}", response); + assertEquals(""" + {"columns":[{"name":"f","type":"text"}],"rows":[["2"]]}""", response); } /** @@ -77,23 +83,23 @@ public void testTrialLicense() throws IOException { assertEquals("{\"count\":0,\"jobs\":[]}", noJobs); Request createJob = new Request("PUT", "/_ml/anomaly_detectors/test_job"); - createJob.setJsonEntity( - "{\n" - + " \"analysis_config\" : {\n" - + " \"bucket_span\": \"10m\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"sum\",\n" - + " \"field_name\": \"total\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\n" - + " \"time_field\": \"timestamp\",\n" - + " \"time_format\": \"epoch_ms\"\n" - + " }\n" - + "}\n" - ); + createJob.setJsonEntity(""" + { + "analysis_config" : { + "bucket_span": "10m", + "detectors": [ + { + "function": "sum", + "field_name": "total" + } + ] + }, + "data_description": { + "time_field": "timestamp", + "time_format": "epoch_ms" + } + } + """); client().performRequest(createJob); } } diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DanglingIndicesRestIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DanglingIndicesRestIT.java index 491007bd5e74a..d4d2147261147 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DanglingIndicesRestIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/DanglingIndicesRestIT.java @@ -218,19 +218,20 @@ private Map createIndices(String... indices) throws IOException assert indices.length > 0; for (String index : indices) { - String indexSettings = "{" - + " \"settings\": {" - + " \"index\": {" - + " \"number_of_shards\": 1," - + " \"number_of_replicas\": 2," - + " \"routing\": {" - + " \"allocation\": {" - + " \"total_shards_per_node\": 1" - + " }" - + " }" - + " }" - + " }" - + "}"; + String indexSettings = """ + { + "settings": { + "index": { + "number_of_shards": 1, + "number_of_replicas": 2, + "routing": { + "allocation": { + "total_shards_per_node": 1 + } + } + } + } + }"""; Request request = new Request("PUT", "/" + index); request.setJsonEntity(indexSettings); assertOK(getRestClient().performRequest(request)); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java index cf434bb180884..2e41e311073a9 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/HttpCompressionIT.java @@ -25,12 +25,13 @@ public class HttpCompressionIT extends ESRestTestCase { private static final String GZIP_ENCODING = "gzip"; - private static final String SAMPLE_DOCUMENT = "{\n" - + " \"name\": {\n" - + " \"first name\": \"Steve\",\n" - + " \"last name\": \"Jobs\"\n" - + " }\n" - + "}"; + private static final String SAMPLE_DOCUMENT = """ + { + "name": { + "first name": "Steve", + "last name": "Jobs" + } + }"""; public void testCompressesResponseIfRequested() throws IOException { Request request = new Request("POST", "/company/_doc/2"); diff --git a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java index de3358967d12a..f7e3e98075726 100644 --- a/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java +++ b/qa/smoke-test-http/src/test/java/org/elasticsearch/http/IndexingPressureRestIT.java @@ -49,9 +49,16 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { @SuppressWarnings("unchecked") public void testIndexingPressureStats() throws IOException { Request createRequest = new Request("PUT", "/index_name"); - createRequest.setJsonEntity( - "{\"settings\": {\"index\": {\"number_of_shards\": 1, \"number_of_replicas\": 1, " + "\"write.wait_for_active_shards\": 2}}}" - ); + createRequest.setJsonEntity(""" + { + "settings": { + "index": { + "number_of_shards": 1, + "number_of_replicas": 1, + "write.wait_for_active_shards": 2 + } + } + }"""); final Response indexCreatedResponse = getRestClient().performRequest(createRequest); assertThat(indexCreatedResponse.getStatusLine().getStatusCode(), equalTo(OK.getStatus())); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java index 8d231de5715d9..ce22a30ac1dc2 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainIT.java @@ -373,13 +373,10 @@ public void testUnassignedReplicaWithPriorCopy() throws Exception { assertThat(d.getExplanation(), startsWith("a copy of this shard is already allocated to this node [")); } else if (d.label().equals("filter") && nodeHoldingPrimary == false) { assertEquals(Decision.Type.NO, d.type()); - assertEquals( - "node does not match index setting [index.routing.allocation.include] " - + "filters [_name:\"" - + primaryNodeName - + "\"]", - d.getExplanation() - ); + assertEquals(""" + node does not match index setting [index.routing.allocation.include] \ + filters [_name:"%s"]\ + """.formatted(primaryNodeName), d.getExplanation()); } else { assertEquals(Decision.Type.YES, d.type()); assertNotNull(d.getExplanation()); @@ -948,10 +945,9 @@ public void testBetterBalanceButCannotAllocate() throws Exception { for (Decision d : result.getCanAllocateDecision().getDecisions()) { if (d.label().equals("filter")) { assertEquals(Decision.Type.NO, d.type()); - assertEquals( - "node does not match index setting [index.routing.allocation.include] filters [_name:\"" + primaryNodeName + "\"]", - d.getExplanation() - ); + assertEquals(""" + node does not match index setting [index.routing.allocation.include] filters [_name:"%s"]\ + """.formatted(primaryNodeName), d.getExplanation()); } else { assertEquals(Decision.Type.YES, d.type()); assertNotNull(d.getExplanation()); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsIT.java index 07201d4286963..c1b91e15cd752 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsIT.java @@ -254,15 +254,26 @@ public void testFieldTypes() { assertThat(response.getStatus(), Matchers.equalTo(ClusterHealthStatus.GREEN)); assertTrue(response.getIndicesStats().getMappings().getFieldTypeStats().isEmpty()); - client().admin().indices().prepareCreate("test1").setMapping("{\"properties\":{\"foo\":{\"type\": \"keyword\"}}}").get(); - client().admin() - .indices() - .prepareCreate("test2") - .setMapping( - "{\"properties\":{\"foo\":{\"type\": \"keyword\"},\"bar\":{\"properties\":{\"baz\":{\"type\":\"keyword\"}," - + "\"eggplant\":{\"type\":\"integer\"}}}}}" - ) - .get(); + client().admin().indices().prepareCreate("test1").setMapping(""" + {"properties":{"foo":{"type": "keyword"}}}""").get(); + client().admin().indices().prepareCreate("test2").setMapping(""" + { + "properties": { + "foo": { + "type": "keyword" + }, + "bar": { + "properties": { + "baz": { + "type": "keyword" + }, + "eggplant": { + "type": "integer" + } + } + } + } + }""").get(); response = client().admin().cluster().prepareClusterStats().get(); assertThat(response.getIndicesStats().getMappings().getFieldTypeStats().size(), equalTo(3)); Set stats = response.getIndicesStats().getMappings().getFieldTypeStats(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/search/PointInTimeIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/search/PointInTimeIT.java index e89d02a2e7893..32c8ee1d6ec8a 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/search/PointInTimeIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/search/PointInTimeIT.java @@ -280,10 +280,8 @@ public void testCanMatch() throws Exception { .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(5, 10)) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(IndexSettings.INDEX_SEARCH_IDLE_AFTER.getKey(), TimeValue.timeValueMillis(randomIntBetween(50, 100))); - assertAcked( - prepareCreate("test").setSettings(settings) - .setMapping("{\"properties\":{\"created_date\":{\"type\": \"date\", \"format\": \"yyyy-MM-dd\"}}}") - ); + assertAcked(prepareCreate("test").setSettings(settings).setMapping(""" + {"properties":{"created_date":{"type": "date", "format": "yyyy-MM-dd"}}}""")); ensureGreen("test"); String pitId = openPointInTime(new String[] { "test*" }, TimeValue.timeValueMinutes(2)); try { diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/search/TransportSearchIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/search/TransportSearchIT.java index c233c7a2b90b8..9bc3dd450af2a 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/search/TransportSearchIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/search/TransportSearchIT.java @@ -420,10 +420,8 @@ public void testSearchIdle() throws Exception { .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 5)) .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplicas) .put(IndexSettings.INDEX_SEARCH_IDLE_AFTER.getKey(), TimeValue.timeValueMillis(randomIntBetween(50, 500))); - assertAcked( - prepareCreate("test").setSettings(settings) - .setMapping("{\"properties\":{\"created_date\":{\"type\": \"date\", \"format\": \"yyyy-MM-dd\"}}}") - ); + assertAcked(prepareCreate("test").setSettings(settings).setMapping(""" + {"properties":{"created_date":{"type": "date", "format": "yyyy-MM-dd"}}}""")); ensureGreen("test"); assertBusy(() -> { for (String node : internalCluster().nodesInclude("test")) { diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/support/WaitActiveShardCountIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/support/WaitActiveShardCountIT.java index ced597dc4ca7a..1b19e8dd83e08 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/support/WaitActiveShardCountIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/support/WaitActiveShardCountIT.java @@ -126,6 +126,8 @@ public void testReplicationWaitsForActiveShardCount() throws Exception { } private String source(String id, String nameValue) { - return "{ \"type1\" : { \"id\" : \"" + id + "\", \"name\" : \"" + nameValue + "\" } }"; + return """ + { "type1" : { "id" : "%s", "name" : "%s" } } + """.formatted(id, nameValue); } } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/aliases/IndexAliasesIT.java b/server/src/internalClusterTest/java/org/elasticsearch/aliases/IndexAliasesIT.java index 95ecf7ab54d47..aaa8444ee7127 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/aliases/IndexAliasesIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/aliases/IndexAliasesIT.java @@ -212,10 +212,8 @@ public void testFilteringAliases() throws Exception { logger.info("--> making sure that filter was stored with alias [alias1] and filter [user:kimchy]"); ClusterState clusterState = admin().cluster().prepareState().get().getState(); IndexMetadata indexMd = clusterState.metadata().index("test"); - assertThat( - indexMd.getAliases().get("alias1").filter().string(), - equalTo("{\"term\":{\"user\":{\"value\":\"kimchy\",\"boost\":1.0}}}") - ); + assertThat(indexMd.getAliases().get("alias1").filter().string(), equalTo(""" + {"term":{"user":{"value":"kimchy","boost":1.0}}}""")); } @@ -800,7 +798,8 @@ public void testSameAlias() { Metadata metadata = internalCluster().clusterService().state().metadata(); IndexAbstraction ia = metadata.getIndicesLookup().get("alias1"); AliasMetadata aliasMetadata = AliasMetadata.getFirstAliasMetadata(metadata, ia); - assertThat(aliasMetadata.getFilter().toString(), equalTo("{\"term\":{\"name\":{\"value\":\"bar\",\"boost\":1.0}}}")); + assertThat(aliasMetadata.getFilter().toString(), equalTo(""" + {"term":{"name":{"value":"bar","boost":1.0}}}""")); logger.info("--> deleting alias1"); stopWatch.start(); @@ -1050,35 +1049,27 @@ public void testCreateIndexWithAliases() { } public void testCreateIndexWithAliasesInSource() throws Exception { - assertAcked( - prepareCreate("test").setSource( - "{\n" - + " \"aliases\" : {\n" - + " \"alias1\" : {},\n" - + " \"alias2\" : {\"filter\" : {\"match_all\": {}}},\n" - + " \"alias3\" : { \"index_routing\" : \"index\", \"search_routing\" : \"search\"},\n" - + " \"alias4\" : {\"is_hidden\": true}\n" - + " }\n" - + "}", - XContentType.JSON - ) - ); + assertAcked(prepareCreate("test").setSource(""" + { + "aliases" : { + "alias1" : {}, + "alias2" : {"filter" : {"match_all": {}}}, + "alias3" : { "index_routing" : "index", "search_routing" : "search"}, + "alias4" : {"is_hidden": true} + } + }""", XContentType.JSON)); checkAliases(); } public void testCreateIndexWithAliasesSource() { - assertAcked( - prepareCreate("test").setMapping("field", "type=text") - .setAliases( - "{\n" - + " \"alias1\" : {},\n" - + " \"alias2\" : {\"filter\" : {\"term\": {\"field\":\"value\"}}},\n" - + " \"alias3\" : { \"index_routing\" : \"index\", \"search_routing\" : \"search\"},\n" - + " \"alias4\" : {\"is_hidden\": true}\n" - + "}" - ) - ); + assertAcked(prepareCreate("test").setMapping("field", "type=text").setAliases(""" + { + "alias1" : {}, + "alias2" : {"filter" : {"term": {"field":"value"}}}, + "alias3" : { "index_routing" : "index", "search_routing" : "search"}, + "alias4" : {"is_hidden": true} + }""")); checkAliases(); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/cluster/SimpleDataNodesIT.java b/server/src/internalClusterTest/java/org/elasticsearch/cluster/SimpleDataNodesIT.java index 92326c9bf9c35..bd1b6143bb316 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/cluster/SimpleDataNodesIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/cluster/SimpleDataNodesIT.java @@ -31,7 +31,8 @@ @ClusterScope(scope = Scope.TEST, numDataNodes = 0) public class SimpleDataNodesIT extends ESIntegTestCase { - private static final String SOURCE = "{\"type1\":{\"id\":\"1\",\"name\":\"test\"}}"; + private static final String SOURCE = """ + {"type1":{"id":"1","name":"test"}}"""; public void testIndexingBeforeAndAfterDataNodesStart() { internalCluster().startNode(nonDataNode()); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java b/server/src/internalClusterTest/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java index ab3190293e596..8859bf4da4cdd 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/cluster/SpecificMasterNodesIT.java @@ -295,12 +295,22 @@ public void testAliasFilterValidation() { logger.info("--> start data node / non master node"); internalCluster().startDataOnlyNode(); - assertAcked( - prepareCreate("test").setMapping( - "{\"properties\" : {\"table_a\" : { \"type\" : \"nested\", " - + "\"properties\" : {\"field_a\" : { \"type\" : \"keyword\" },\"field_b\" :{ \"type\" : \"keyword\" }}}}}" - ) - ); + assertAcked(prepareCreate("test").setMapping(""" + { + "properties": { + "table_a": { + "type": "nested", + "properties": { + "field_a": { + "type": "keyword" + }, + "field_b": { + "type": "keyword" + } + } + } + } + }""")); client().admin() .indices() .prepareAliases() diff --git a/server/src/internalClusterTest/java/org/elasticsearch/discovery/MasterDisruptionIT.java b/server/src/internalClusterTest/java/org/elasticsearch/discovery/MasterDisruptionIT.java index 82b0a9171f479..492a65d6f37b7 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/discovery/MasterDisruptionIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/discovery/MasterDisruptionIT.java @@ -142,19 +142,12 @@ public void testIsolateMasterAndVerifyClusterStateConsensus() throws Exception { assertEquals("different meta data version", state.metadata().version(), nodeState.metadata().version()); assertEquals("different routing", state.routingTable().toString(), nodeState.routingTable().toString()); } catch (AssertionError t) { - fail( - "failed comparing cluster state: " - + t.getMessage() - + "\n" - + "--- cluster state of node [" - + nodes.get(0) - + "]: ---\n" - + state - + "\n--- cluster state [" - + node - + "]: ---\n" - + nodeState - ); + fail(""" + failed comparing cluster state: %s + --- cluster state of node [%s]: --- + %s + --- cluster state [%s]: --- + %s""".formatted(t.getMessage(), nodes.get(0), state, node, nodeState)); } } @@ -209,12 +202,9 @@ public void testVerifyApiBlocksDuringPartition() throws Exception { success = false; } if (success == false) { - fail( - "node [" - + node - + "] has no master or has blocks, despite of being on the right side of the partition. State dump:\n" - + nodeState - ); + fail(""" + node [%s] has no master or has blocks, despite of being on the right side of the partition. State dump: + %s""".formatted(node, nodeState)); } } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/gateway/GatewayIndexStateIT.java b/server/src/internalClusterTest/java/org/elasticsearch/gateway/GatewayIndexStateIT.java index 3e39adbe487ff..955dac12e7514 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/gateway/GatewayIndexStateIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/gateway/GatewayIndexStateIT.java @@ -466,18 +466,15 @@ public void testRecoverMissingAnalyzer() throws Exception { internalCluster().startNode(); prepareCreate("test").setSettings( Settings.builder().put("index.analysis.analyzer.test.tokenizer", "standard").put("index.number_of_shards", "1") - ) - .setMapping( - "{\n" - + " \"properties\": {\n" - + " \"field1\": {\n" - + " \"type\": \"text\",\n" - + " \"analyzer\": \"test\"\n" - + " }\n" - + " }\n" - + " }}" - ) - .get(); + ).setMapping(""" + { + "properties": { + "field1": { + "type": "text", + "analyzer": "test" + } + } + }""").get(); logger.info("--> indexing a simple document"); client().prepareIndex("test").setId("1").setSource("field1", "value one").setRefreshPolicy(IMMEDIATE).get(); logger.info("--> waiting for green status"); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/get/GetActionIT.java b/server/src/internalClusterTest/java/org/elasticsearch/get/GetActionIT.java index b74420e874dc7..3728eb90a7793 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/get/GetActionIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/get/GetActionIT.java @@ -669,31 +669,33 @@ public void testGetFieldsComplexField() throws Exception { } public void testUngeneratedFieldsThatAreNeverStored() throws IOException { - String createIndexSource = "{\n" - + " \"settings\": {\n" - + " \"index.translog.flush_threshold_size\": \"1pb\",\n" - + " \"refresh_interval\": \"-1\"\n" - + " },\n" - + " \"mappings\": {\n" - + " \"_doc\": {\n" - + " \"properties\": {\n" - + " \"suggest\": {\n" - + " \"type\": \"completion\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String createIndexSource = """ + { + "settings": { + "index.translog.flush_threshold_size": "1pb", + "refresh_interval": "-1" + }, + "mappings": { + "_doc": { + "properties": { + "suggest": { + "type": "completion" + } + } + } + } + }"""; assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource, XContentType.JSON)); ensureGreen(); - String doc = "{\n" - + " \"suggest\": {\n" - + " \"input\": [\n" - + " \"Nevermind\",\n" - + " \"Nirvana\"\n" - + " ]\n" - + " }\n" - + "}"; + String doc = """ + { + "suggest": { + "input": [ + "Nevermind", + "Nirvana" + ] + } + }"""; index("test", "1", doc); String[] fieldsList = { "suggest" }; @@ -708,12 +710,13 @@ public void testUngeneratedFieldsThatAreNeverStored() throws IOException { } public void testUngeneratedFieldsThatAreAlwaysStored() throws IOException { - String createIndexSource = "{\n" - + " \"settings\": {\n" - + " \"index.translog.flush_threshold_size\": \"1pb\",\n" - + " \"refresh_interval\": \"-1\"\n" - + " }\n" - + "}"; + String createIndexSource = """ + { + "settings": { + "index.translog.flush_threshold_size": "1pb", + "refresh_interval": "-1" + } + }"""; assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource, XContentType.JSON)); ensureGreen(); @@ -731,16 +734,21 @@ public void testUngeneratedFieldsThatAreAlwaysStored() throws IOException { } public void testUngeneratedFieldsNotPartOfSourceStored() throws IOException { - String createIndexSource = "{\n" - + " \"settings\": {\n" - + " \"index.translog.flush_threshold_size\": \"1pb\",\n" - + " \"refresh_interval\": \"-1\"\n" - + " }\n" - + "}"; + String createIndexSource = """ + { + "settings": { + "index.translog.flush_threshold_size": "1pb", + "refresh_interval": "-1" + } + }"""; assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource, XContentType.JSON)); ensureGreen(); - String doc = "{\n" + " \"text\": \"some text.\"\n" + "}\n"; + String doc = """ + { + "text": "some text." + } + """; client().prepareIndex("test").setId("1").setSource(doc, XContentType.JSON).setRouting("1").get(); String[] fieldsList = { "_routing" }; // before refresh - document is only in translog @@ -781,37 +789,39 @@ public void testGeneratedStringFieldsStored() throws IOException { void indexSingleDocumentWithStringFieldsGeneratedFromText(boolean stored, boolean sourceEnabled) { String storedString = stored ? "true" : "false"; - String createIndexSource = "{\n" - + " \"settings\": {\n" - + " \"index.translog.flush_threshold_size\": \"1pb\",\n" - + " \"refresh_interval\": \"-1\"\n" - + " },\n" - + " \"mappings\": {\n" - + " \"_doc\": {\n" - + " \"_source\" : {\"enabled\" : " - + sourceEnabled - + "}," - + " \"properties\": {\n" - + " \"text1\": {\n" - + " \"type\": \"text\",\n" - + " \"store\": \"" - + storedString - + "\"" - + " },\n" - + " \"text2\": {\n" - + " \"type\": \"text\",\n" - + " \"store\": \"" - + storedString - + "\"" - + " }" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String createIndexSource = """ + { + "settings": { + "index.translog.flush_threshold_size": "1pb", + "refresh_interval": "-1" + }, + "mappings": { + "_doc": { + "_source": { + "enabled": %s + }, + "properties": { + "text1": { + "type": "text", + "store": "%s" + }, + "text2": { + "type": "text", + "store": "%s" + } + } + } + } + }""".formatted(sourceEnabled, storedString, storedString); assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSource(createIndexSource, XContentType.JSON)); ensureGreen(); - String doc = "{\n" + " \"text1\": \"some text.\"\n," + " \"text2\": \"more text.\"\n" + "}\n"; + String doc = """ + { + "text1": "some text." + , "text2": "more text." + } + """; index("test", "1", doc); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/index/FinalPipelineIT.java b/server/src/internalClusterTest/java/org/elasticsearch/index/FinalPipelineIT.java index 1340966e63ada..bf84069fe1de5 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/index/FinalPipelineIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/index/FinalPipelineIT.java @@ -79,7 +79,8 @@ public void testFinalPipelineCantChangeDestination() { final Settings settings = Settings.builder().put(IndexSettings.FINAL_PIPELINE.getKey(), "final_pipeline").build(); createIndex("index", settings); - final BytesReference finalPipelineBody = new BytesArray("{\"processors\": [{\"changing_dest\": {}}]}"); + final BytesReference finalPipelineBody = new BytesArray(""" + {"processors": [{"changing_dest": {}}]}"""); client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet(); final IllegalStateException e = expectThrows( @@ -96,13 +97,15 @@ public void testFinalPipelineOfOldDestinationIsNotInvoked() { .build(); createIndex("index", settings); - BytesReference defaultPipelineBody = new BytesArray("{\"processors\": [{\"changing_dest\": {}}]}"); + BytesReference defaultPipelineBody = new BytesArray(""" + {"processors": [{"changing_dest": {}}]}"""); client().admin() .cluster() .putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)) .actionGet(); - BytesReference finalPipelineBody = new BytesArray("{\"processors\": [{\"final\": {\"exists\":\"no_such_field\"}}]}"); + BytesReference finalPipelineBody = new BytesArray(""" + {"processors": [{"final": {"exists":"no_such_field"}}]}"""); client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet(); IndexResponse indexResponse = client().prepareIndex("index") @@ -123,13 +126,15 @@ public void testFinalPipelineOfNewDestinationIsInvoked() { settings = Settings.builder().put(IndexSettings.FINAL_PIPELINE.getKey(), "final_pipeline").build(); createIndex("target", settings); - BytesReference defaultPipelineBody = new BytesArray("{\"processors\": [{\"changing_dest\": {}}]}"); + BytesReference defaultPipelineBody = new BytesArray(""" + {"processors": [{"changing_dest": {}}]}"""); client().admin() .cluster() .putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)) .actionGet(); - BytesReference finalPipelineBody = new BytesArray("{\"processors\": [{\"final\": {}}]}"); + BytesReference finalPipelineBody = new BytesArray(""" + {"processors": [{"final": {}}]}"""); client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet(); IndexResponse indexResponse = client().prepareIndex("index") @@ -150,13 +155,15 @@ public void testDefaultPipelineOfNewDestinationIsNotInvoked() { settings = Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "target_default_pipeline").build(); createIndex("target", settings); - BytesReference defaultPipelineBody = new BytesArray("{\"processors\": [{\"changing_dest\": {}}]}"); + BytesReference defaultPipelineBody = new BytesArray(""" + {"processors": [{"changing_dest": {}}]}"""); client().admin() .cluster() .putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)) .actionGet(); - BytesReference targetPipeline = new BytesArray("{\"processors\": [{\"final\": {}}]}"); + BytesReference targetPipeline = new BytesArray(""" + {"processors": [{"final": {}}]}"""); client().admin() .cluster() .putPipeline(new PutPipelineRequest("target_default_pipeline", targetPipeline, XContentType.JSON)) @@ -186,12 +193,14 @@ public void testFinalPipeline() { } public void testRequestPipelineAndFinalPipeline() { - final BytesReference requestPipelineBody = new BytesArray("{\"processors\": [{\"request\": {}}]}"); + final BytesReference requestPipelineBody = new BytesArray(""" + {"processors": [{"request": {}}]}"""); client().admin() .cluster() .putPipeline(new PutPipelineRequest("request_pipeline", requestPipelineBody, XContentType.JSON)) .actionGet(); - final BytesReference finalPipelineBody = new BytesArray("{\"processors\": [{\"final\": {\"exists\":\"request\"}}]}"); + final BytesReference finalPipelineBody = new BytesArray(""" + {"processors": [{"final": {"exists":"request"}}]}"""); client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet(); final Settings settings = Settings.builder().put(IndexSettings.FINAL_PIPELINE.getKey(), "final_pipeline").build(); createIndex("index", settings); @@ -212,12 +221,14 @@ public void testRequestPipelineAndFinalPipeline() { } public void testDefaultAndFinalPipeline() { - final BytesReference defaultPipelineBody = new BytesArray("{\"processors\": [{\"default\": {}}]}"); + final BytesReference defaultPipelineBody = new BytesArray(""" + {"processors": [{"default": {}}]}"""); client().admin() .cluster() .putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)) .actionGet(); - final BytesReference finalPipelineBody = new BytesArray("{\"processors\": [{\"final\": {\"exists\":\"default\"}}]}"); + final BytesReference finalPipelineBody = new BytesArray(""" + {"processors": [{"final": {"exists":"default"}}]}"""); client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet(); final Settings settings = Settings.builder() .put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline") @@ -240,12 +251,14 @@ public void testDefaultAndFinalPipeline() { } public void testDefaultAndFinalPipelineFromTemplates() { - final BytesReference defaultPipelineBody = new BytesArray("{\"processors\": [{\"default\": {}}]}"); + final BytesReference defaultPipelineBody = new BytesArray(""" + {"processors": [{"default": {}}]}"""); client().admin() .cluster() .putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)) .actionGet(); - final BytesReference finalPipelineBody = new BytesArray("{\"processors\": [{\"final\": {\"exists\":\"default\"}}]}"); + final BytesReference finalPipelineBody = new BytesArray(""" + {"processors": [{"final": {"exists":"default"}}]}"""); client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet(); final int lowOrder = randomIntBetween(0, Integer.MAX_VALUE - 1); final int highOrder = randomIntBetween(lowOrder + 1, Integer.MAX_VALUE); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/index/mapper/DynamicMappingIT.java b/server/src/internalClusterTest/java/org/elasticsearch/index/mapper/DynamicMappingIT.java index 6bc6accbfa76f..3d4b624017722 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/index/mapper/DynamicMappingIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/index/mapper/DynamicMappingIT.java @@ -348,14 +348,16 @@ public void testBulkRequestWithNotFoundDynamicTemplate() throws Exception { } public void testDynamicRuntimeNoConflicts() { - assertAcked(client().admin().indices().prepareCreate("test").setMapping("{\"_doc\":{\"dynamic\":\"runtime\"}}").get()); + assertAcked(client().admin().indices().prepareCreate("test").setMapping(""" + {"_doc":{"dynamic":"runtime"}}""").get()); List docs = new ArrayList<>(); // the root is mapped dynamic:runtime hence there are no type conflicts docs.add(new IndexRequest("test").source("one.two.three", new int[] { 1, 2, 3 })); docs.add(new IndexRequest("test").source("one.two", 3.5)); docs.add(new IndexRequest("test").source("one", "one")); - docs.add(new IndexRequest("test").source("{\"one\":{\"two\": { \"three\": \"three\"}}}", XContentType.JSON)); + docs.add(new IndexRequest("test").source(""" + {"one":{"two": { "three": "three"}}}""", XContentType.JSON)); Collections.shuffle(docs, random()); BulkRequest bulkRequest = new BulkRequest(); for (IndexRequest doc : docs) { @@ -380,16 +382,21 @@ public void testDynamicRuntimeNoConflicts() { } public void testDynamicRuntimeObjectFields() { - assertAcked( - client().admin() - .indices() - .prepareCreate("test") - .setMapping( - "{\"_doc\":{\"properties\":{" - + "\"obj\":{\"properties\":{\"runtime\":{\"type\":\"object\",\"dynamic\":\"runtime\"}}}}}}" - ) - .get() - ); + assertAcked(client().admin().indices().prepareCreate("test").setMapping(""" + { + "_doc": { + "properties": { + "obj": { + "properties": { + "runtime": { + "type": "object", + "dynamic": "runtime" + } + } + } + } + } + }""").get()); List docs = new ArrayList<>(); docs.add(new IndexRequest("test").source("obj.one", 1)); @@ -397,7 +404,8 @@ public void testDynamicRuntimeObjectFields() { // obj.runtime is mapped dynamic:runtime hence there are no type conflicts docs.add(new IndexRequest("test").source("obj.runtime.one.two", "test")); docs.add(new IndexRequest("test").source("obj.runtime.one", "one")); - docs.add(new IndexRequest("test").source("{\"obj\":{\"runtime\":{\"one\":{\"two\": 1}}}}", XContentType.JSON)); + docs.add(new IndexRequest("test").source(""" + {"obj":{"runtime":{"one":{"two": 1}}}}""", XContentType.JSON)); Collections.shuffle(docs, random()); BulkRequest bulkRequest = new BulkRequest(); for (IndexRequest doc : docs) { @@ -435,16 +443,25 @@ public void testDynamicRuntimeObjectFields() { exception.getMessage() ); - assertAcked( - client().admin() - .indices() - .preparePutMapping("test") - .setSource( - "{\"_doc\":{\"properties\":{\"obj\":{\"properties\":" - + "{\"runtime\":{\"properties\":{\"dynamic\":{\"type\":\"object\", \"dynamic\":true}}}}}}}}", - XContentType.JSON - ) - ); + assertAcked(client().admin().indices().preparePutMapping("test").setSource(""" + { + "_doc": { + "properties": { + "obj": { + "properties": { + "runtime": { + "properties": { + "dynamic": { + "type": "object", + "dynamic": true + } + } + } + } + } + } + } + }""", XContentType.JSON)); // the parent object has been mapped dynamic:true, hence the field gets indexed // we use a fixed doc id here to make sure this document and the one we sent later with a conflicting type diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java index 98503c03d4b04..5266e73c9126e 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/IndicesLifecycleListenerIT.java @@ -244,16 +244,11 @@ private static void assertShardStatesMatch( try { assertBusy(waitPredicate, 1, TimeUnit.MINUTES); } catch (AssertionError ae) { - fail( - "failed to observe expect shard states\n" - + "expected: [" - + numShards - + "] shards with states: " - + Strings.arrayToCommaDelimitedString(shardStates) - + "\n" - + "observed:\n" - + stateChangeListener - ); + fail(""" + failed to observe expect shard states + expected: [%d] shards with states: %s + observed: + %s""".formatted(numShards, Strings.arrayToCommaDelimitedString(shardStates), stateChangeListener)); } stateChangeListener.shardStates.clear(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/ConcurrentDynamicTemplateIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/ConcurrentDynamicTemplateIT.java index e84ba41f80ac2..b272da9e5c813 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/ConcurrentDynamicTemplateIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/ConcurrentDynamicTemplateIT.java @@ -28,16 +28,21 @@ public class ConcurrentDynamicTemplateIT extends ESIntegTestCase { // see #3544 public void testConcurrentDynamicMapping() throws Exception { final String fieldName = "field"; - final String mapping = "{" - + "\"dynamic_templates\": [" - + "{ \"" - + fieldName - + "\": {" - + "\"path_match\": \"*\"," - + "\"mapping\": {" - + "\"type\": \"text\"," - + "\"store\": true," - + "\"analyzer\": \"whitespace\" } } } ] }"; + final String mapping = """ + { + "dynamic_templates": [ + { + "%s": { + "path_match": "*", + "mapping": { + "type": "text", + "store": true, + "analyzer": "whitespace" + } + } + } + ] + }""".formatted(fieldName); // The 'fieldNames' array is used to help with retrieval of index terms // after testing diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java index 276c6790faed2..ec2c9332db4c6 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/MalformedDynamicTemplateIT.java @@ -33,19 +33,21 @@ protected boolean forbidPrivateIndexSettings() { * to be able to index new documents into them. Indexing should issue a deprecation warning though. */ public void testBWCMalformedDynamicTemplate() { - String mapping = "{ \"dynamic_templates\": [\n" - + " {\n" - + " \"my_template\": {\n" - + " \"mapping\": {\n" - + " \"ignore_malformed\": true,\n" // this parameter is not supported by "keyword" field type - + " \"type\": \"keyword\"\n" - + " },\n" - + " \"path_match\": \"*\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}}"; + // this parameter is not supported by "keyword" field type + String mapping = """ + { "dynamic_templates": [ + { + "my_template": { + "mapping": { + "ignore_malformed": true, + "type": "keyword" + }, + "path_match": "*" + } + } + ] + } + }}"""; String indexName = "malformed_dynamic_template"; assertAcked( prepareCreate(indexName).setSettings( diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java index e4a11db656400..8b9a44041efab 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/UpdateMappingIntegrationIT.java @@ -120,25 +120,22 @@ public void testUpdateMappingWithoutType() { .indices() .prepareCreate("test") .setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)) - .setMapping("{\"properties\":{\"body\":{\"type\":\"text\"}}}") + .setMapping(""" + {"properties":{"body":{"type":"text"}}} + """) .execute() .actionGet(); client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); - AcknowledgedResponse putMappingResponse = client().admin() - .indices() - .preparePutMapping("test") - .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON) - .execute() - .actionGet(); + AcknowledgedResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setSource(""" + {"properties":{"date":{"type":"integer"}}} + """, XContentType.JSON).execute().actionGet(); assertThat(putMappingResponse.isAcknowledged(), equalTo(true)); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet(); - assertThat( - getMappingsResponse.mappings().get("test").source().toString(), - equalTo("{\"_doc\":{\"properties\":{\"body\":{\"type\":\"text\"},\"date\":{\"type\":\"integer\"}}}}") - ); + assertThat(getMappingsResponse.mappings().get("test").source().toString(), equalTo(""" + {"_doc":{"properties":{"body":{"type":"text"},"date":{"type":"integer"}}}}""")); } public void testUpdateMappingWithoutTypeMultiObjects() { @@ -150,20 +147,14 @@ public void testUpdateMappingWithoutTypeMultiObjects() { .actionGet(); client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); - AcknowledgedResponse putMappingResponse = client().admin() - .indices() - .preparePutMapping("test") - .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON) - .execute() - .actionGet(); + AcknowledgedResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setSource(""" + {"properties":{"date":{"type":"integer"}}}""", XContentType.JSON).execute().actionGet(); assertThat(putMappingResponse.isAcknowledged(), equalTo(true)); GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet(); - assertThat( - getMappingsResponse.mappings().get("test").source().toString(), - equalTo("{\"_doc\":{\"properties\":{\"date\":{\"type\":\"integer\"}}}}") - ); + assertThat(getMappingsResponse.mappings().get("test").source().toString(), equalTo(""" + {"_doc":{"properties":{"date":{"type":"integer"}}}}""")); } public void testUpdateMappingWithConflicts() { @@ -171,18 +162,17 @@ public void testUpdateMappingWithConflicts() { .indices() .prepareCreate("test") .setSettings(Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0)) - .setMapping("{\"properties\":{\"body\":{\"type\":\"text\"}}}") + .setMapping(""" + {"properties":{"body":{"type":"text"}}} + """) .execute() .actionGet(); client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); try { - client().admin() - .indices() - .preparePutMapping("test") - .setSource("{\"_doc\":{\"properties\":{\"body\":{\"type\":\"integer\"}}}}", XContentType.JSON) - .execute() - .actionGet(); + client().admin().indices().preparePutMapping("test").setSource(""" + {"_doc":{"properties":{"body":{"type":"integer"}}}} + """, XContentType.JSON).execute().actionGet(); fail("Expected MergeMappingException"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("mapper [body] cannot be changed from type [text] to [integer]")); @@ -190,19 +180,13 @@ public void testUpdateMappingWithConflicts() { } public void testUpdateMappingWithNormsConflicts() { - client().admin() - .indices() - .prepareCreate("test") - .setMapping("{\"properties\":{\"body\":{\"type\":\"text\", \"norms\": false }}}") - .execute() - .actionGet(); + client().admin().indices().prepareCreate("test").setMapping(""" + {"properties":{"body":{"type":"text", "norms": false }}} + """).execute().actionGet(); try { - client().admin() - .indices() - .preparePutMapping("test") - .setSource("{\"_doc\":{\"properties\":{\"body\":{\"type\":\"text\", \"norms\": true }}}}", XContentType.JSON) - .execute() - .actionGet(); + client().admin().indices().preparePutMapping("test").setSource(""" + {"_doc":{"properties":{"body":{"type":"text", "norms": true }}}} + """, XContentType.JSON).execute().actionGet(); fail("Expected MergeMappingException"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), containsString("Cannot update parameter [norms] from [false] to [true]")); @@ -217,17 +201,15 @@ public void testUpdateMappingNoChanges() { .indices() .prepareCreate("test") .setSettings(Settings.builder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0)) - .setMapping("{\"properties\":{\"body\":{\"type\":\"text\"}}}") + .setMapping(""" + {"properties":{"body":{"type":"text"}}}""") .execute() .actionGet(); client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); - AcknowledgedResponse putMappingResponse = client().admin() - .indices() - .preparePutMapping("test") - .setSource("{\"_doc\":{\"properties\":{\"body\":{\"type\":\"text\"}}}}", XContentType.JSON) - .execute() - .actionGet(); + AcknowledgedResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setSource(""" + {"_doc":{"properties":{"body":{"type":"text"}}}} + """, XContentType.JSON).execute().actionGet(); // no changes, we return assertThat(putMappingResponse.isAcknowledged(), equalTo(true)); @@ -311,12 +293,9 @@ public void testPutMappingsWithBlocks() { for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE)) { try { enableIndexBlock("test", block); - assertAcked( - client().admin() - .indices() - .preparePutMapping("test") - .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON) - ); + assertAcked(client().admin().indices().preparePutMapping("test").setSource(""" + {"properties":{"date":{"type":"integer"}}} + """, XContentType.JSON)); } finally { disableIndexBlock("test", block); } @@ -325,12 +304,9 @@ public void testPutMappingsWithBlocks() { for (String block : Arrays.asList(SETTING_READ_ONLY, SETTING_BLOCKS_METADATA)) { try { enableIndexBlock("test", block); - assertBlocked( - client().admin() - .indices() - .preparePutMapping("test") - .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON) - ); + assertBlocked(client().admin().indices().preparePutMapping("test").setSource(""" + {"properties":{"date":{"type":"integer"}}} + """, XContentType.JSON)); } finally { disableIndexBlock("test", block); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/memory/breaker/CircuitBreakerServiceIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/memory/breaker/CircuitBreakerServiceIT.java index 936196f3ef411..1b927cd3fd2ab 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/memory/breaker/CircuitBreakerServiceIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/memory/breaker/CircuitBreakerServiceIT.java @@ -166,13 +166,22 @@ public void testRamAccountingTermsEnum() throws Exception { final Client client = client(); // Create an index where the mappings have a field data filter - assertAcked( - prepareCreate("ramtest").setSource( - "{\"mappings\": {\"type\": {\"properties\": {\"test\": " - + "{\"type\": \"text\",\"fielddata\": true,\"fielddata_frequency_filter\": {\"max\": 10000}}}}}}", - XContentType.JSON - ) - ); + assertAcked(prepareCreate("ramtest").setSource(""" + { + "mappings": { + "type": { + "properties": { + "test": { + "type": "text", + "fielddata": true, + "fielddata_frequency_filter": { + "max": 10000 + } + } + } + } + } + }""", XContentType.JSON)); ensureGreen("ramtest"); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/stats/IndexStatsIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/stats/IndexStatsIT.java index 514dcd11a0b25..d753527f2aec6 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/stats/IndexStatsIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/stats/IndexStatsIT.java @@ -1001,15 +1001,31 @@ public void testMultiIndex() throws Exception { } public void testCompletionFieldsParam() throws Exception { - assertAcked( - prepareCreate("test1").setMapping( - "{ \"properties\": { \"bar\": { \"type\": \"text\", \"fields\": { \"completion\": { \"type\": \"completion\" }}}" - + ",\"baz\": { \"type\": \"text\", \"fields\": { \"completion\": { \"type\": \"completion\" }}}}}" - ) - ); + assertAcked(prepareCreate("test1").setMapping(""" + { + "properties": { + "bar": { + "type": "text", + "fields": { + "completion": { + "type": "completion" + } + } + }, + "baz": { + "type": "text", + "fields": { + "completion": { + "type": "completion" + } + } + } + } + }""")); ensureGreen(); - client().prepareIndex("test1").setId(Integer.toString(1)).setSource("{\"bar\":\"bar\",\"baz\":\"baz\"}", XContentType.JSON).get(); + client().prepareIndex("test1").setId(Integer.toString(1)).setSource(""" + {"bar":"bar","baz":"baz"}""", XContentType.JSON).get(); refresh(); IndicesStatsRequestBuilder builder = client().admin().indices().prepareStats(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/template/ComposableTemplateIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/template/ComposableTemplateIT.java index 066e4dad481d2..29c38c07fcbd7 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/template/ComposableTemplateIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/template/ComposableTemplateIT.java @@ -22,42 +22,28 @@ public class ComposableTemplateIT extends ESIntegTestCase { // See: https://github.com/elastic/elasticsearch/issues/58643 public void testComponentTemplatesCanBeUpdatedAfterRestart() throws Exception { - ComponentTemplate ct = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"dynamic\": false,\n" - + " \"properties\": {\n" - + " \"foo\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - 3L, - Collections.singletonMap("eggplant", "potato") - ); + ComponentTemplate ct = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "dynamic": false, + "properties": { + "foo": { + "type": "text" + } + } + }"""), null), 3L, Collections.singletonMap("eggplant", "potato")); client().execute(PutComponentTemplateAction.INSTANCE, new PutComponentTemplateAction.Request("my-ct").componentTemplate(ct)).get(); ComposableIndexTemplate cit = new ComposableIndexTemplate( Collections.singletonList("coleslaw"), - new Template( - null, - new CompressedXContent( - "{\n" - + " \"dynamic\": false,\n" - + " \"properties\": {\n" - + " \"foo\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), + new Template(null, new CompressedXContent(""" + { + "dynamic": false, + "properties": { + "foo": { + "type": "keyword" + } + } + }"""), null), Collections.singletonList("my-ct"), 4L, 5L, @@ -71,42 +57,28 @@ public void testComponentTemplatesCanBeUpdatedAfterRestart() throws Exception { internalCluster().fullRestart(); ensureGreen(); - ComponentTemplate ct2 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"dynamic\": true,\n" - + " \"properties\": {\n" - + " \"foo\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - 3L, - Collections.singletonMap("eggplant", "potato") - ); + ComponentTemplate ct2 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "dynamic": true, + "properties": { + "foo": { + "type": "keyword" + } + } + }"""), null), 3L, Collections.singletonMap("eggplant", "potato")); client().execute(PutComponentTemplateAction.INSTANCE, new PutComponentTemplateAction.Request("my-ct").componentTemplate(ct2)).get(); ComposableIndexTemplate cit2 = new ComposableIndexTemplate( Collections.singletonList("coleslaw"), - new Template( - null, - new CompressedXContent( - "{\n" - + " \"dynamic\": true,\n" - + " \"properties\": {\n" - + " \"foo\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), + new Template(null, new CompressedXContent(""" + { + "dynamic": true, + "properties": { + "foo": { + "type": "integer" + } + } + }"""), null), Collections.singletonList("my-ct"), 4L, 5L, diff --git a/server/src/internalClusterTest/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java b/server/src/internalClusterTest/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java index d20113c7cf414..85e188b30c1a5 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java @@ -565,27 +565,19 @@ public void testIndexTemplateWithAliases() throws Exception { } public void testIndexTemplateWithAliasesInSource() { - client().admin() - .indices() - .preparePutTemplate("template_1") - .setSource( - new BytesArray( - "{\n" - + " \"index_patterns\" : \"*\",\n" - + " \"aliases\" : {\n" - + " \"my_alias\" : {\n" - + " \"filter\" : {\n" - + " \"term\" : {\n" - + " \"field\" : \"value2\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ), - XContentType.JSON - ) - .get(); + client().admin().indices().preparePutTemplate("template_1").setSource(new BytesArray(""" + { + "index_patterns": "*", + "aliases": { + "my_alias": { + "filter": { + "term": { + "field": "value2" + } + } + } + } + }"""), XContentType.JSON).get(); assertAcked(prepareCreate("test_index")); ensureGreen(); @@ -607,24 +599,21 @@ public void testIndexTemplateWithAliasesInSource() { } public void testIndexTemplateWithAliasesSource() { - client().admin() - .indices() - .preparePutTemplate("template_1") - .setPatterns(Collections.singletonList("te*")) - .setAliases( - " {\n" - + " \"alias1\" : {},\n" - + " \"alias2\" : {\n" - + " \"filter\" : {\n" - + " \"term\" : {\n" - + " \"field\" : \"value2\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"alias3\" : { \"routing\" : \"1\" }" - + " }\n" - ) - .get(); + client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).setAliases(""" + { + "alias1": {}, + "alias2": { + "filter": { + "term": { + "field": "value2" + } + } + }, + "alias3": { + "routing": "1" + } + } + """).get(); assertAcked(prepareCreate("test_index")); ensureGreen(); @@ -830,25 +819,19 @@ public void testCombineTemplates() throws Exception { // Now, a complete mapping with two separated templates is error // base template - client().admin() - .indices() - .preparePutTemplate("template_1") - .setPatterns(Collections.singletonList("*")) - .setSettings( - " {\n" - + " \"index\" : {\n" - + " \"analysis\" : {\n" - + " \"analyzer\" : {\n" - + " \"custom_1\" : {\n" - + " \"tokenizer\" : \"standard\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n", - XContentType.JSON - ) - .get(); + client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("*")).setSettings(""" + { + "index": { + "analysis": { + "analyzer": { + "custom_1": { + "tokenizer": "standard" + } + } + } + } + } + """, XContentType.JSON).get(); // put template using custom_1 analyzer MapperParsingException e = expectThrows( diff --git a/server/src/internalClusterTest/java/org/elasticsearch/script/StoredScriptsIT.java b/server/src/internalClusterTest/java/org/elasticsearch/script/StoredScriptsIT.java index 5e956d499c391..9f51754d6bd1f 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/script/StoredScriptsIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/script/StoredScriptsIT.java @@ -40,13 +40,9 @@ protected Collection> nodePlugins() { } public void testBasics() { - assertAcked( - client().admin() - .cluster() - .preparePutStoredScript() - .setId("foobar") - .setContent(new BytesArray("{\"script\": {\"lang\": \"" + LANG + "\", \"source\": \"1\"} }"), XContentType.JSON) - ); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("foobar").setContent(new BytesArray(""" + {"script": {"lang": "%s", "source": "1"} } + """.formatted(LANG)), XContentType.JSON)); String script = client().admin().cluster().prepareGetStoredScript("foobar").get().getSource().getSource(); assertNotNull(script); assertEquals("1", script); @@ -57,12 +53,9 @@ public void testBasics() { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> client().admin() - .cluster() - .preparePutStoredScript() - .setId("id#") - .setContent(new BytesArray("{\"script\": {\"lang\": \"" + LANG + "\", \"source\": \"1\"} }"), XContentType.JSON) - .get() + () -> client().admin().cluster().preparePutStoredScript().setId("id#").setContent(new BytesArray(""" + {"script": {"lang": "%s", "source": "1"} } + """.formatted(LANG)), XContentType.JSON).get() ); assertEquals("Validation Failed: 1: id cannot contain '#' for stored script;", e.getMessage()); } @@ -70,15 +63,9 @@ public void testBasics() { public void testMaxScriptSize() { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> client().admin() - .cluster() - .preparePutStoredScript() - .setId("foobar") - .setContent( - new BytesArray("{\"script\": { \"lang\": \"" + LANG + "\"," + " \"source\":\"0123456789abcdef\"} }"), - XContentType.JSON - ) - .get() + () -> client().admin().cluster().preparePutStoredScript().setId("foobar").setContent(new BytesArray(""" + {"script": { "lang": "%s", "source":"0123456789abcdef"} }\ + """.formatted(LANG)), XContentType.JSON).get() ); assertEquals("exceeded max allowed stored script size in bytes [64] with size [65] for script [foobar]", e.getMessage()); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/NestedIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/NestedIT.java index 7441378c2aa4a..7632bac9739e3 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/NestedIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/NestedIT.java @@ -432,26 +432,50 @@ public void testParentFilterResolvedCorrectly() throws Exception { ensureGreen("idx2"); List indexRequests = new ArrayList<>(2); - indexRequests.add( - client().prepareIndex("idx2") - .setId("1") - .setSource( - "{\"dates\": {\"month\": {\"label\": \"2014-11\", \"end\": \"2014-11-30\", \"start\": \"2014-11-01\"}, " - + "\"day\": \"2014-11-30\"}, \"comments\": [{\"cid\": 3,\"identifier\": \"29111\"}, {\"cid\": 4,\"tags\": [" - + "{\"tid\" :44,\"name\": \"Roles\"}], \"identifier\": \"29101\"}]}", - XContentType.JSON - ) - ); - indexRequests.add( - client().prepareIndex("idx2") - .setId("2") - .setSource( - "{\"dates\": {\"month\": {\"label\": \"2014-12\", \"end\": \"2014-12-31\", \"start\": \"2014-12-01\"}, " - + "\"day\": \"2014-12-03\"}, \"comments\": [{\"cid\": 1, \"identifier\": \"29111\"}, {\"cid\": 2,\"tags\": [" - + "{\"tid\" : 22, \"name\": \"DataChannels\"}], \"identifier\": \"29101\"}]}", - XContentType.JSON - ) - ); + indexRequests.add(client().prepareIndex("idx2").setId("1").setSource(""" + { + "dates": { + "month": { + "label": "2014-11", + "end": "2014-11-30", + "start": "2014-11-01" + }, + "day": "2014-11-30" + }, + "comments": [ + { + "cid": 3, + "identifier": "29111" + }, + { + "cid": 4, + "tags": [ { "tid": 44, "name": "Roles" } ], + "identifier": "29101" + } + ] + }""", XContentType.JSON)); + indexRequests.add(client().prepareIndex("idx2").setId("2").setSource(""" + { + "dates": { + "month": { + "label": "2014-12", + "end": "2014-12-31", + "start": "2014-12-01" + }, + "day": "2014-12-03" + }, + "comments": [ + { + "cid": 1, + "identifier": "29111" + }, + { + "cid": 2, + "tags": [ { "tid": 22, "name": "DataChannels" } ], + "identifier": "29101" + } + ] + }""", XContentType.JSON)); indexRandom(true, indexRequests); SearchResponse response = client().prepareSearch("idx2") diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java index adc5651cc728a..ce85fc300716e 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/bucket/SignificantTermsSignificanceScoreIT.java @@ -12,6 +12,7 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.plugins.Plugin; @@ -149,40 +150,49 @@ public void testXContentResponse() throws Exception { classes.toXContent(responseBuilder, ToXContent.EMPTY_PARAMS); responseBuilder.endObject(); - String result = "{\"class\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0," - + "\"buckets\":[" - + "{" - + "\"key\":\"0\"," - + "\"doc_count\":4," - + "\"sig_terms\":{" - + "\"doc_count\":4," - + "\"bg_count\":7," - + "\"buckets\":[" - + "{" - + "\"key\":" - + (type.equals("long") ? "0," : "\"0\",") - + "\"doc_count\":4," - + "\"score\":0.39999999999999997," - + "\"bg_count\":5" - + "}" - + "]" - + "}" - + "}," - + "{" - + "\"key\":\"1\"," - + "\"doc_count\":3," - + "\"sig_terms\":{" - + "\"doc_count\":3," - + "\"bg_count\":7," - + "\"buckets\":[" - + "{" - + "\"key\":" - + (type.equals("long") ? "1," : "\"1\",") - + "\"doc_count\":3," - + "\"score\":0.75," - + "\"bg_count\":4" - + "}]}}]}}"; - assertThat(Strings.toString(responseBuilder), equalTo(result)); + String result = """ + { + "class": { + "doc_count_error_upper_bound": 0, + "sum_other_doc_count": 0, + "buckets": [ + { + "key": "0", + "doc_count": 4, + "sig_terms": { + "doc_count": 4, + "bg_count": 7, + "buckets": [ + { + "key": %s, + "doc_count": 4, + "score": 0.39999999999999997, + "bg_count": 5 + } + ] + } + }, + { + "key": "1", + "doc_count": 3, + "sig_terms": { + "doc_count": 3, + "bg_count": 7, + "buckets": [ + { + "key":%s, + "doc_count": 3, + "score": 0.75, + "bg_count": 4 + } + ] + } + } + ] + } + } + """.formatted(type.equals("long") ? "0" : "\"0\"", type.equals("long") ? "1" : "\"1\""); + assertThat(Strings.toString(responseBuilder), equalTo(XContentHelper.stripWhitespace(result))); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricIT.java index 032aaf3ba37eb..7201164ff0a24 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricIT.java @@ -303,63 +303,21 @@ public void setupSuiteScopeCluster() throws Exception { // When using the MockScriptPlugin we can map Stored scripts to inline scripts: // the id of the stored script is used in test method while the source of the stored script // must match a predefined script from CustomScriptPlugin.pluginScripts() method - assertAcked( - client().admin() - .cluster() - .preparePutStoredScript() - .setId("initScript_stored") - .setContent( - new BytesArray( - "{\"script\": {\"lang\": \"" + MockScriptPlugin.NAME + "\"," + " \"source\": \"vars.multiplier = 3\"} }" - ), - XContentType.JSON - ) - ); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("initScript_stored").setContent(new BytesArray(""" + {"script": {"lang": "%s", "source": "vars.multiplier = 3"} } + """.formatted(MockScriptPlugin.NAME)), XContentType.JSON)); - assertAcked( - client().admin() - .cluster() - .preparePutStoredScript() - .setId("mapScript_stored") - .setContent( - new BytesArray( - "{\"script\": {\"lang\": \"" + MockScriptPlugin.NAME + "\"," + " \"source\": \"state.list.add(vars.multiplier)\"} }" - ), - XContentType.JSON - ) - ); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("mapScript_stored").setContent(new BytesArray(""" + {"script": {"lang": "%s", "source": "state.list.add(vars.multiplier)"} } + """.formatted(MockScriptPlugin.NAME)), XContentType.JSON)); - assertAcked( - client().admin() - .cluster() - .preparePutStoredScript() - .setId("combineScript_stored") - .setContent( - new BytesArray( - "{\"script\": {\"lang\": \"" - + MockScriptPlugin.NAME - + "\"," - + " \"source\": \"sum state values as a new aggregation\"} }" - ), - XContentType.JSON - ) - ); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("combineScript_stored").setContent(new BytesArray(""" + {"script": {"lang": "%s", "source": "sum state values as a new aggregation"} } + """.formatted(MockScriptPlugin.NAME)), XContentType.JSON)); - assertAcked( - client().admin() - .cluster() - .preparePutStoredScript() - .setId("reduceScript_stored") - .setContent( - new BytesArray( - "{\"script\": {\"lang\": \"" - + MockScriptPlugin.NAME - + "\"," - + " \"source\": \"sum all states (lists) values as a new aggregation\"} }" - ), - XContentType.JSON - ) - ); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("reduceScript_stored").setContent(new BytesArray(""" + {"script": {"lang": "%s", "source": "sum all states (lists) values as a new aggregation"} } + """.formatted(MockScriptPlugin.NAME)), XContentType.JSON)); indexRandom(true, builders); ensureSearchable(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/pipeline/BucketSelectorIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/pipeline/BucketSelectorIT.java index b0f55882d8e3a..ba777be5ff736 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/pipeline/BucketSelectorIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/aggregations/pipeline/BucketSelectorIT.java @@ -457,15 +457,13 @@ public void testStoredScript() { .preparePutStoredScript() .setId("my_script") // Source is not interpreted but my_script is defined in CustomScriptPlugin - .setContent( - new BytesArray( - "{ \"script\": { \"lang\": \"" - + CustomScriptPlugin.NAME - + "\", " - + "\"source\": \"Double.isNaN(_value0) ? false : (_value0 + _value1 > 100)\" } }" - ), - XContentType.JSON - ) + .setContent(new BytesArray(""" + { + "script": { + "lang": "%s", + "source": "Double.isNaN(_value0) ? false : (_value0 + _value1 > 100)" + } + }""".formatted(CustomScriptPlugin.NAME)), XContentType.JSON) ); Script script = new Script(ScriptType.STORED, null, "my_script", Collections.emptyMap()); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java index 0f4643c305fac..1bc02fa0b18b3 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java @@ -3074,16 +3074,16 @@ public void testFastVectorHighlighterPhraseBoost() throws Exception { */ private void phraseBoostTestCase(String highlighterType) { ensureGreen(); - StringBuilder text = new StringBuilder(); - text.append("words words junk junk junk junk junk junk junk junk highlight junk junk junk junk together junk\n"); - for (int i = 0; i < 10; i++) { - text.append("junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk\n"); - } - text.append("highlight words together\n"); - for (int i = 0; i < 10; i++) { - text.append("junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk\n"); - } - indexDoc("test", "1", "field1", text.toString()); + String text = """ + words words junk junk junk junk junk junk junk junk highlight junk junk junk junk together junk + """ + """ + junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk + """.repeat(10) + """ + highlight words together + """ + """ + junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk + """.repeat(10); + indexDoc("test", "1", "field1", text); refresh(); // Match queries diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/nested/SimpleNestedIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/nested/SimpleNestedIT.java index 6c7e1fa430d78..b5c91b7f19b52 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/nested/SimpleNestedIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/nested/SimpleNestedIT.java @@ -688,141 +688,128 @@ public void testSimpleNestedSortingWithNestedFilterMissing() throws Exception { } public void testNestedSortWithMultiLevelFiltering() throws Exception { - assertAcked( - prepareCreate("test").setMapping( - "{\n" - + " \"properties\": {\n" - + " \"acl\": {\n" - + " \"type\": \"nested\",\n" - + " \"properties\": {\n" - + " \"access_id\": {\"type\": \"keyword\"},\n" - + " \"operation\": {\n" - + " \"type\": \"nested\",\n" - + " \"properties\": {\n" - + " \"name\": {\"type\": \"keyword\"},\n" - + " \"user\": {\n" - + " \"type\": \"nested\",\n" - + " \"properties\": {\n" - + " \"username\": {\"type\": \"keyword\"},\n" - + " \"id\": {\"type\": \"integer\"}\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ) - ); + assertAcked(prepareCreate("test").setMapping(""" + { + "properties": { + "acl": { + "type": "nested", + "properties": { + "access_id": {"type": "keyword"}, + "operation": { + "type": "nested", + "properties": { + "name": {"type": "keyword"}, + "user": { + "type": "nested", + "properties": { + "username": {"type": "keyword"}, + "id": {"type": "integer"} + } + } + } + } + } + } + } + }""")); ensureGreen(); - client().prepareIndex("test") - .setId("1") - .setSource( - "{\n" - + " \"acl\": [\n" - + " {\n" - + " \"access_id\": 1,\n" - + " \"operation\": [\n" - + " {\n" - + " \"name\": \"read\",\n" - + " \"user\": [\n" - + " {\"username\": \"matt\", \"id\": 1},\n" - + " {\"username\": \"shay\", \"id\": 2},\n" - + " {\"username\": \"adrien\", \"id\": 3}\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"name\": \"write\",\n" - + " \"user\": [\n" - + " {\"username\": \"shay\", \"id\": 2},\n" - + " {\"username\": \"adrien\", \"id\": 3}\n" - + " ]\n" - + " }\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"access_id\": 2,\n" - + " \"operation\": [\n" - + " {\n" - + " \"name\": \"read\",\n" - + " \"user\": [\n" - + " {\"username\": \"jim\", \"id\": 4},\n" - + " {\"username\": \"shay\", \"id\": 2}\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"name\": \"write\",\n" - + " \"user\": [\n" - + " {\"username\": \"shay\", \"id\": 2}\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"name\": \"execute\",\n" - + " \"user\": [\n" - + " {\"username\": \"shay\", \"id\": 2}\n" - + " ]\n" - + " }\n" - + " ]\n" - + " }\n" - + " ]\n" - + "}", - XContentType.JSON - ) - .get(); - - client().prepareIndex("test") - .setId("2") - .setSource( - "{\n" - + " \"acl\": [\n" - + " {\n" - + " \"access_id\": 1,\n" - + " \"operation\": [\n" - + " {\n" - + " \"name\": \"read\",\n" - + " \"user\": [\n" - + " {\"username\": \"matt\", \"id\": 1},\n" - + " {\"username\": \"luca\", \"id\": 5}\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"name\": \"execute\",\n" - + " \"user\": [\n" - + " {\"username\": \"luca\", \"id\": 5}\n" - + " ]\n" - + " }\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"access_id\": 3,\n" - + " \"operation\": [\n" - + " {\n" - + " \"name\": \"read\",\n" - + " \"user\": [\n" - + " {\"username\": \"matt\", \"id\": 1}\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"name\": \"write\",\n" - + " \"user\": [\n" - + " {\"username\": \"matt\", \"id\": 1}\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"name\": \"execute\",\n" - + " \"user\": [\n" - + " {\"username\": \"matt\", \"id\": 1}\n" - + " ]\n" - + " }\n" - + " ]\n" - + " }\n" - + " ]\n" - + "}", - XContentType.JSON - ) - .get(); + client().prepareIndex("test").setId("1").setSource(""" + { + "acl": [ + { + "access_id": 1, + "operation": [ + { + "name": "read", + "user": [ + {"username": "matt", "id": 1}, + {"username": "shay", "id": 2}, + {"username": "adrien", "id": 3} + ] + }, + { + "name": "write", + "user": [ + {"username": "shay", "id": 2}, + {"username": "adrien", "id": 3} + ] + } + ] + }, + { + "access_id": 2, + "operation": [ + { + "name": "read", + "user": [ + {"username": "jim", "id": 4}, + {"username": "shay", "id": 2} + ] + }, + { + "name": "write", + "user": [ + {"username": "shay", "id": 2} + ] + }, + { + "name": "execute", + "user": [ + {"username": "shay", "id": 2} + ] + } + ] + } + ] + }""", XContentType.JSON).get(); + + client().prepareIndex("test").setId("2").setSource(""" + { + "acl": [ + { + "access_id": 1, + "operation": [ + { + "name": "read", + "user": [ + {"username": "matt", "id": 1}, + {"username": "luca", "id": 5} + ] + }, + { + "name": "execute", + "user": [ + {"username": "luca", "id": 5} + ] + } + ] + }, + { + "access_id": 3, + "operation": [ + { + "name": "read", + "user": [ + {"username": "matt", "id": 1} + ] + }, + { + "name": "write", + "user": [ + {"username": "matt", "id": 1} + ] + }, + { + "name": "execute", + "user": [ + {"username": "matt", "id": 1} + ] + } + ] + } + ] + }""", XContentType.JSON).get(); refresh(); // access id = 1, read, max value, asc, should use matt and shay @@ -929,71 +916,60 @@ public void testNestedSortWithMultiLevelFiltering() throws Exception { // https://github.com/elastic/elasticsearch/issues/31554 public void testLeakingSortValues() throws Exception { - assertAcked( - prepareCreate("test").setSettings(Settings.builder().put("number_of_shards", 1)) - .setMapping( - "{\"_doc\":{\n" - + " \"dynamic\": \"strict\",\n" - + " \"properties\": {\n" - + " \"nested1\": {\n" - + " \"type\": \"nested\",\n" - + " \"properties\": {\n" - + " \"nested2\": {\n" - + " \"type\": \"nested\",\n" - + " \"properties\": {\n" - + " \"nested2_keyword\": {\n" - + " \"type\": \"keyword\"\n" - + " },\n" - + " \"sortVal\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }}\n" - ) - ); + assertAcked(prepareCreate("test").setSettings(Settings.builder().put("number_of_shards", 1)).setMapping(""" + { + "_doc": { + "dynamic": "strict", + "properties": { + "nested1": { + "type": "nested", + "properties": { + "nested2": { + "type": "nested", + "properties": { + "nested2_keyword": { + "type": "keyword" + }, + "sortVal": { + "type": "integer" + } + } + } + } + } + } + } + } + """)); ensureGreen(); - client().prepareIndex("test") - .setId("1") - .setSource( - "{\n" - + " \"nested1\": [\n" - + " {\n" - + " \"nested2\": [\n" - + " {\n" - + " \"nested2_keyword\": \"nested2_bar\",\n" - + " \"sortVal\": 1\n" - + " }\n" - + " ]\n" - + " }\n" - + " ]\n" - + "}", - XContentType.JSON - ) - .get(); - - client().prepareIndex("test") - .setId("2") - .setSource( - "{\n" - + " \"nested1\": [\n" - + " {\n" - + " \"nested2\": [\n" - + " {\n" - + " \"nested2_keyword\": \"nested2_bar\",\n" - + " \"sortVal\": 2\n" - + " }\n" - + " ]\n" - + " } \n" - + " ]\n" - + "}", - XContentType.JSON - ) - .get(); + client().prepareIndex("test").setId("1").setSource(""" + { + "nested1": [ + { + "nested2": [ + { + "nested2_keyword": "nested2_bar", + "sortVal": 1 + } + ] + } + ] + }""", XContentType.JSON).get(); + + client().prepareIndex("test").setId("2").setSource(""" + { + "nested1": [ + { + "nested2": [ + { + "nested2_keyword": "nested2_bar", + "sortVal": 2 + } + ] + } + ] + }""", XContentType.JSON).get(); refresh(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/query/SearchQueryIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/query/SearchQueryIT.java index 45819c407b74b..e2dba7ad1487c 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/query/SearchQueryIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/query/SearchQueryIT.java @@ -1366,17 +1366,29 @@ public void testIntervals() throws InterruptedException { client().prepareIndex("test").setId("1").setSource("description", "it's cold outside, there's no kind of atmosphere") ); - String json = "{ \"intervals\" : " - + "{ \"description\": { " - + " \"all_of\" : {" - + " \"ordered\" : \"true\"," - + " \"intervals\" : [" - + " { \"any_of\" : {" - + " \"intervals\" : [" - + " { \"match\" : { \"query\" : \"cold\" } }," - + " { \"match\" : { \"query\" : \"outside\" } } ] } }," - + " { \"match\" : { \"query\" : \"atmosphere\" } } ]," - + " \"max_gaps\" : 30 } } } }"; + String json = """ + { + "intervals": { + "description": { + "all_of": { + "ordered": "true", + "intervals": [ + { + "any_of": { + "intervals": [ { "match": { "query": "cold" } }, { "match": { "query": "outside" } } ] + } + }, + { + "match": { + "query": "atmosphere" + } + } + ], + "max_gaps": 30 + } + } + } + }"""; SearchResponse response = client().prepareSearch("test").setQuery(wrapperQuery(json)).get(); assertHitCount(response, 1L); } diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/scroll/SearchScrollIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/scroll/SearchScrollIT.java index 450d97710611a..9f0fb7c01ca6e 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/scroll/SearchScrollIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/scroll/SearchScrollIT.java @@ -688,7 +688,9 @@ public void testScrollRewrittenToMatchNoDocs() { .indices() .prepareCreate("test") .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numShards)) - .setMapping("{\"properties\":{\"created_date\":{\"type\": \"date\", \"format\": \"yyyy-MM-dd\"}}}") + .setMapping(""" + {"properties":{"created_date":{"type": "date", "format": "yyyy-MM-dd"}}} + """) ); client().prepareIndex("test").setId("1").setSource("created_date", "2020-01-01").get(); client().prepareIndex("test").setId("2").setSource("created_date", "2020-01-02").get(); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/search/searchafter/SearchAfterIT.java b/server/src/internalClusterTest/java/org/elasticsearch/search/searchafter/SearchAfterIT.java index 76dc3a961bfb4..b1e450ef3e45d 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/search/searchafter/SearchAfterIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/search/searchafter/SearchAfterIT.java @@ -446,13 +446,8 @@ public void testScrollAndSearchAfterWithBigIndex() { if (randomBoolean()) { indexSettings.put("sort.field", "timestamp").put("sort.order", randomFrom("desc", "asc")); } - assertAcked( - client().admin() - .indices() - .prepareCreate("test") - .setSettings(indexSettings) - .setMapping("{\"properties\":{\"timestamp\":{\"type\": \"date\", \"format\": \"epoch_millis\"}}}") - ); + assertAcked(client().admin().indices().prepareCreate("test").setSettings(indexSettings).setMapping(""" + {"properties":{"timestamp":{"type": "date", "format": "epoch_millis"}}}""")); Randomness.shuffle(timestamps); final BulkRequestBuilder bulk = client().prepareBulk(); bulk.setRefreshPolicy(IMMEDIATE); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java b/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java index 3a5ddf5ae87bc..136a39db3c7b2 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/validate/SimpleValidateQueryIT.java @@ -368,17 +368,9 @@ public void testIrrelevantPropertiesBeforeQuery() { ensureGreen(); refresh(); - assertThat( - client().admin() - .indices() - .prepareValidateQuery("test") - .setQuery( - QueryBuilders.wrapperQuery(new BytesArray("{\"foo\": \"bar\", \"query\": {\"term\" : { \"user\" : \"kimchy\" }}}")) - ) - .get() - .isValid(), - equalTo(false) - ); + assertThat(client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.wrapperQuery(new BytesArray(""" + {"foo": "bar", "query": {"term" : { "user" : "kimchy" }}} + """))).get().isValid(), equalTo(false)); } public void testIrrelevantPropertiesAfterQuery() { @@ -386,17 +378,9 @@ public void testIrrelevantPropertiesAfterQuery() { ensureGreen(); refresh(); - assertThat( - client().admin() - .indices() - .prepareValidateQuery("test") - .setQuery( - QueryBuilders.wrapperQuery(new BytesArray("{\"query\": {\"term\" : { \"user\" : \"kimchy\" }}, \"foo\": \"bar\"}")) - ) - .get() - .isValid(), - equalTo(false) - ); + assertThat(client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.wrapperQuery(new BytesArray(""" + {"query": {"term" : { "user" : "kimchy" }}, "foo": "bar"} + """))).get().isValid(), equalTo(false)); } private static void assertExplanation(QueryBuilder queryBuilder, Matcher matcher, boolean withRewrite) { diff --git a/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java b/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java index e81a46193c6ee..0d6d7af6828e3 100644 --- a/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java +++ b/server/src/main/java/org/elasticsearch/bootstrap/JNANatives.java @@ -92,15 +92,11 @@ static void tryMlockall() { if (Constants.LINUX) { // give specific instructions for the linux case to make it easy String user = System.getProperty("user.name"); - logger.warn( - "These can be adjusted by modifying /etc/security/limits.conf, for example: \n" - + "\t# allow user '{}' mlockall\n" - + "\t{} soft memlock unlimited\n" - + "\t{} hard memlock unlimited", - user, - user, - user - ); + logger.warn(""" + These can be adjusted by modifying /etc/security/limits.conf, for example: + \t# allow user '{}' mlockall + \t{} soft memlock unlimited + \t{} hard memlock unlimited""", user, user, user); logger.warn("If you are logged in interactively, you will have to re-login for the new limits to take effect."); } } else { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java index 1dcc8fdf6f291..176e01f931fe6 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java @@ -636,25 +636,17 @@ private static String normalizeValue(NamedAnalyzer normalizer, String field, Str final CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class); ts.reset(); if (ts.incrementToken() == false) { - throw new IllegalStateException( - "The normalization token stream is " - + "expected to produce exactly 1 token, but got 0 for analyzer " - + normalizer - + " and input \"" - + value - + "\"" - ); + throw new IllegalStateException(""" + The normalization token stream is expected to produce exactly 1 token, \ + but got 0 for analyzer %s and input "%s" + """.formatted(normalizer, value)); } final String newValue = termAtt.toString(); if (ts.incrementToken()) { - throw new IllegalStateException( - "The normalization token stream is " - + "expected to produce exactly 1 token, but got 2+ for analyzer " - + normalizer - + " and input \"" - + value - + "\"" - ); + throw new IllegalStateException(""" + The normalization token stream is expected to produce exactly 1 token, \ + but got 2+ for analyzer %s and input "%s" + """.formatted(normalizer, value)); } ts.end(); return newValue; diff --git a/server/src/main/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommand.java b/server/src/main/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommand.java index 8b049550e59ee..619144ca5d1ec 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommand.java +++ b/server/src/main/java/org/elasticsearch/index/shard/RemoveCorruptedShardDataCommand.java @@ -197,13 +197,12 @@ public static boolean isCorruptMarkerFileIsPresent(final Directory directory) th protected void dropCorruptMarkerFiles(Terminal terminal, Path path, Directory directory, boolean clean) throws IOException { if (clean) { - confirm( - "This shard has been marked as corrupted but no corruption can now be detected.\n" - + "This may indicate an intermittent hardware problem. The corruption marker can be \n" - + "removed, but there is a risk that data has been undetectably lost.\n\n" - + "Are you taking a risk of losing documents and proceed with removing a corrupted marker ?", - terminal - ); + confirm(""" + This shard has been marked as corrupted but no corruption can now be detected. + This may indicate an intermittent hardware problem. The corruption marker can be + removed, but there is a risk that data has been undetectably lost. + + Are you taking a risk of losing documents and proceed with removing a corrupted marker ?""", terminal); } String[] files = directory.listAll(); for (String file : files) { diff --git a/server/src/main/java/org/elasticsearch/indices/SystemIndices.java b/server/src/main/java/org/elasticsearch/indices/SystemIndices.java index eafe27ad446e6..fe520258b63fb 100644 --- a/server/src/main/java/org/elasticsearch/indices/SystemIndices.java +++ b/server/src/main/java/org/elasticsearch/indices/SystemIndices.java @@ -644,11 +644,10 @@ Collection getSystemIndexDescriptors() { public static void validateFeatureName(String name, String plugin) { if (SnapshotsService.NO_FEATURE_STATES_VALUE.equalsIgnoreCase(name)) { throw new IllegalArgumentException( - "feature name cannot be reserved name [\"" - + SnapshotsService.NO_FEATURE_STATES_VALUE - + "\"], but was for plugin [" - + plugin - + "]" + "feature name cannot be reserved name [\"%s\"], but was for plugin [%s]".formatted( + SnapshotsService.NO_FEATURE_STATES_VALUE, + plugin + ) ); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketsAggregator.java index 449c46078d37b..3b310d78483a6 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/BucketsAggregator.java @@ -406,14 +406,9 @@ public BucketComparator bucketComparator(String key, SortOrder order) { if (key == null || "doc_count".equals(key)) { return (lhs, rhs) -> order.reverseMul() * Long.compare(bucketDocCount(lhs), bucketDocCount(rhs)); } - throw new IllegalArgumentException( - "Ordering on a single-bucket aggregation can only be done on its doc_count. " - + "Either drop the key (a la \"" - + name() - + "\") or change it to \"doc_count\" (a la \"" - + name() - + ".doc_count\") or \"key\"." - ); + throw new IllegalArgumentException(""" + Ordering on a single-bucket aggregation can only be done on its doc_count. \ + Either drop the key (a la "%s") or change it to "doc_count" (a la "%s.doc_count") or "key".""".formatted(name(), name())); } public static boolean descendsFromGlobalAggregator(Aggregator parent) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/NXYSignificanceHeuristic.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/NXYSignificanceHeuristic.java index b00123d80708c..31918cfb7042a 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/NXYSignificanceHeuristic.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/heuristic/NXYSignificanceHeuristic.java @@ -26,10 +26,9 @@ public abstract class NXYSignificanceHeuristic extends SignificanceHeuristic { protected static final ParseField INCLUDE_NEGATIVES_FIELD = new ParseField("include_negatives"); - protected static final String SCORE_ERROR_MESSAGE = ", does your background filter not include all documents in the bucket? " - + "If so and it is intentional, set \"" - + BACKGROUND_IS_SUPERSET.getPreferredName() - + "\": false"; + protected static final String SCORE_ERROR_MESSAGE = """ + , does your background filter not include all documents in the bucket? If so and it is intentional, set "%s": false + """.formatted(BACKGROUND_IS_SUPERSET.getPreferredName()); protected final boolean backgroundIsSuperset; diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/NumericMetricsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/NumericMetricsAggregator.java index 081a82690a5f8..c35e521b3feea 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/NumericMetricsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/NumericMetricsAggregator.java @@ -33,14 +33,9 @@ protected SingleValue(String name, AggregationContext context, Aggregator parent @Override public BucketComparator bucketComparator(String key, SortOrder order) { if (key != null && false == "value".equals(key)) { - throw new IllegalArgumentException( - "Ordering on a single-value metrics aggregation can only be done on its value. " - + "Either drop the key (a la \"" - + name() - + "\") or change it to \"value\" (a la \"" - + name() - + ".value\")" - ); + throw new IllegalArgumentException(""" + Ordering on a single-value metrics aggregation can only be done on its value. \ + Either drop the key (a la "%s") or change it to "value" (a la "%s.value")""".formatted(name(), name())); } return (lhs, rhs) -> Comparators.compareDiscardNaN(metric(lhs), metric(rhs), order == SortOrder.ASC); } diff --git a/server/src/main/java/org/elasticsearch/transport/RemoteConnectionStrategy.java b/server/src/main/java/org/elasticsearch/transport/RemoteConnectionStrategy.java index 67358986907a7..99c3ac679eb48 100644 --- a/server/src/main/java/org/elasticsearch/transport/RemoteConnectionStrategy.java +++ b/server/src/main/java/org/elasticsearch/transport/RemoteConnectionStrategy.java @@ -431,15 +431,12 @@ public void validate(T value, Map, Object> settings, boolean isPresen ConnectionStrategy modeType = (ConnectionStrategy) settings.get(concrete); if (isPresent && modeType.equals(expectedStrategy) == false) { throw new IllegalArgumentException( - "Setting \"" - + key - + "\" cannot be used with the configured \"" - + concrete.getKey() - + "\" [required=" - + expectedStrategy.name() - + ", configured=" - + modeType.name() - + "]" + "Setting \"%s\" cannot be used with the configured \"%s\" [required=%s, configured=%s]".formatted( + key, + concrete.getKey(), + expectedStrategy.name(), + modeType.name() + ) ); } } diff --git a/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java b/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java index 2aed3896208c0..506a21da65675 100644 --- a/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java +++ b/server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java @@ -222,10 +222,27 @@ public void testDeduplicate() throws IOException { builder.startObject(); ex.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - String expected = "{\"type\":\"search_phase_execution_exception\",\"reason\":\"all shards failed\",\"phase\":\"search\"," - + "\"grouped\":true,\"failed_shards\":[{\"shard\":1,\"index\":\"foo\",\"node\":\"node_1\",\"reason\":" - + "{\"type\":\"parsing_exception\",\"reason\":\"foobar\",\"line\":1,\"col\":2}}]}"; - assertEquals(expected, Strings.toString(builder)); + String expected = """ + { + "type": "search_phase_execution_exception", + "reason": "all shards failed", + "phase": "search", + "grouped": true, + "failed_shards": [ + { + "shard": 1, + "index": "foo", + "node": "node_1", + "reason": { + "type": "parsing_exception", + "reason": "foobar", + "line": 1, + "col": 2 + } + } + ] + }"""; + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); } { ShardSearchFailure failure = new ShardSearchFailure( @@ -249,12 +266,39 @@ public void testDeduplicate() throws IOException { builder.startObject(); ex.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - String expected = "{\"type\":\"search_phase_execution_exception\",\"reason\":\"all shards failed\"," - + "\"phase\":\"search\",\"grouped\":true,\"failed_shards\":[{\"shard\":1,\"index\":\"foo\",\"node\":\"node_1\"," - + "\"reason\":{\"type\":\"parsing_exception\",\"reason\":\"foobar\",\"line\":1,\"col\":2}},{\"shard\":1," - + "\"index\":\"foo1\",\"node\":\"node_1\",\"reason\":{\"type\":\"query_shard_exception\",\"reason\":\"foobar\"," - + "\"index_uuid\":\"_na_\",\"index\":\"foo1\"}}]}"; - assertEquals(expected, Strings.toString(builder)); + + String expected = """ + { + "type": "search_phase_execution_exception", + "reason": "all shards failed", + "phase": "search", + "grouped": true, + "failed_shards": [ + { + "shard": 1, + "index": "foo", + "node": "node_1", + "reason": { + "type": "parsing_exception", + "reason": "foobar", + "line": 1, + "col": 2 + } + }, + { + "shard": 1, + "index": "foo1", + "node": "node_1", + "reason": { + "type": "query_shard_exception", + "reason": "foobar", + "index_uuid": "_na_", + "index": "foo1" + } + } + ] + }"""; + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); } { ShardSearchFailure failure = new ShardSearchFailure( @@ -277,11 +321,32 @@ public void testDeduplicate() throws IOException { builder.startObject(); ex.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - String expected = "{\"type\":\"search_phase_execution_exception\",\"reason\":\"all shards failed\"," - + "\"phase\":\"search\",\"grouped\":true,\"failed_shards\":[{\"shard\":1,\"index\":\"foo\",\"node\":\"node_1\"," - + "\"reason\":{\"type\":\"parsing_exception\",\"reason\":\"foobar\",\"line\":1,\"col\":2}}]," - + "\"caused_by\":{\"type\":\"null_pointer_exception\",\"reason\":null}}"; - assertEquals(expected, Strings.toString(builder)); + + String expected = """ + { + "type": "search_phase_execution_exception", + "reason": "all shards failed", + "phase": "search", + "grouped": true, + "failed_shards": [ + { + "shard": 1, + "index": "foo", + "node": "node_1", + "reason": { + "type": "parsing_exception", + "reason": "foobar", + "line": 1, + "col": 2 + } + } + ], + "caused_by": { + "type": "null_pointer_exception", + "reason": null + } + }"""; + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); } } @@ -341,16 +406,19 @@ public void testGetDetailedMessage() { public void testToXContent() throws IOException { { ElasticsearchException e = new ElasticsearchException("test"); - assertExceptionAsJson(e, "{\"type\":\"exception\",\"reason\":\"test\"}"); + assertExceptionAsJson(e, """ + {"type":"exception","reason":"test"}"""); } { ElasticsearchException e = new IndexShardRecoveringException(new ShardId("_test", "_0", 5)); - assertExceptionAsJson( - e, - "{\"type\":\"index_shard_recovering_exception\"," - + "\"reason\":\"CurrentState[RECOVERING] Already recovering\",\"index_uuid\":\"_0\"," - + "\"shard\":\"5\",\"index\":\"_test\"}" - ); + assertExceptionAsJson(e, """ + { + "type": "index_shard_recovering_exception", + "reason": "CurrentState[RECOVERING] Already recovering", + "index_uuid": "_0", + "shard": "5", + "index": "_test" + }"""); } { ElasticsearchException e = new BroadcastShardOperationFailedException( @@ -358,38 +426,65 @@ public void testToXContent() throws IOException { "foo", new IllegalStateException("bar") ); - assertExceptionAsJson(e, "{\"type\":\"illegal_state_exception\",\"reason\":\"bar\"}"); + assertExceptionAsJson(e, """ + { + "type": "illegal_state_exception", + "reason": "bar" + }"""); } { ElasticsearchException e = new ElasticsearchException(new IllegalArgumentException("foo")); - assertExceptionAsJson( - e, - "{\"type\":\"exception\",\"reason\":\"java.lang.IllegalArgumentException: foo\"," - + "\"caused_by\":{\"type\":\"illegal_argument_exception\",\"reason\":\"foo\"}}" - ); + assertExceptionAsJson(e, """ + { + "type": "exception", + "reason": "java.lang.IllegalArgumentException: foo", + "caused_by": { + "type": "illegal_argument_exception", + "reason": "foo" + } + }"""); } { ElasticsearchException e = new SearchParseException(SHARD_TARGET, "foo", new XContentLocation(1, 0)); - assertExceptionAsJson(e, "{\"type\":\"search_parse_exception\",\"reason\":\"foo\",\"line\":1,\"col\":0}"); + + assertExceptionAsJson(e, """ + {"type":"search_parse_exception","reason":"foo","line":1,"col":0}"""); } { ElasticsearchException ex = new ElasticsearchException( "foo", new ElasticsearchException("bar", new IllegalArgumentException("index is closed", new RuntimeException("foobar"))) ); - assertExceptionAsJson( - ex, - "{\"type\":\"exception\",\"reason\":\"foo\",\"caused_by\":{\"type\":\"exception\"," - + "\"reason\":\"bar\",\"caused_by\":{\"type\":\"illegal_argument_exception\",\"reason\":\"index is closed\"," - + "\"caused_by\":{\"type\":\"runtime_exception\",\"reason\":\"foobar\"}}}}" - ); + assertExceptionAsJson(ex, """ + { + "type": "exception", + "reason": "foo", + "caused_by": { + "type": "exception", + "reason": "bar", + "caused_by": { + "type": "illegal_argument_exception", + "reason": "index is closed", + "caused_by": { + "type": "runtime_exception", + "reason": "foobar" + } + } + } + }"""); } { ElasticsearchException e = new ElasticsearchException("foo", new IllegalStateException("bar")); - assertExceptionAsJson( - e, - "{\"type\":\"exception\",\"reason\":\"foo\"," + "\"caused_by\":{\"type\":\"illegal_state_exception\",\"reason\":\"bar\"}}" - ); + + assertExceptionAsJson(e, """ + { + "type": "exception", + "reason": "foo", + "caused_by": { + "type": "illegal_state_exception", + "reason": "bar" + } + }"""); // Test the same exception but with the "rest.exception.stacktrace.skip" parameter disabled: the stack_trace must be present // in the JSON. Since the stack can be large, it only checks the beginning of the JSON. @@ -406,11 +501,11 @@ public void testToXContent() throws IOException { assertThat( actual, startsWith( - "{\"type\":\"exception\",\"reason\":\"foo\"," - + "\"caused_by\":{\"type\":\"illegal_state_exception\",\"reason\":\"bar\"," - + "\"stack_trace\":\"java.lang.IllegalStateException: bar" - + (Constants.WINDOWS ? "\\r\\n" : "\\n") - + "\\tat org.elasticsearch." + """ + {"type":"exception","reason":"foo","caused_by":{"type":"illegal_state_exception","reason":"bar",\ + "stack_trace":"java.lang.IllegalStateException: bar%s\\tat org.elasticsearch.""".formatted( + Constants.WINDOWS ? "\\r\\n" : "\\n" + ) ) ); } @@ -425,11 +520,13 @@ public void testGenerateThrowableToXContent() throws IOException { } else { ex = new FileNotFoundException("foo not found"); } - assertExceptionAsJson(ex, "{\"type\":\"file_not_found_exception\",\"reason\":\"foo not found\"}"); + assertExceptionAsJson(ex, """ + {"type":"file_not_found_exception","reason":"foo not found"}"""); } { ParsingException ex = new ParsingException(1, 2, "foobar", null); - assertExceptionAsJson(ex, "{\"type\":\"parsing_exception\",\"reason\":\"foobar\",\"line\":1,\"col\":2}"); + assertExceptionAsJson(ex, """ + {"type":"parsing_exception","reason":"foobar","line":1,"col":2}"""); } { // test equivalence @@ -441,7 +538,8 @@ public void testGenerateThrowableToXContent() throws IOException { }); assertEquals(throwableString, toXContentString); - assertEquals("{\"type\":\"file_not_found_exception\",\"reason\":\"foo not found\"}", toXContentString); + assertEquals(""" + {"type":"file_not_found_exception","reason":"foo not found"}""", toXContentString); } { // render header and metadata @@ -450,10 +548,23 @@ public void testGenerateThrowableToXContent() throws IOException { ex.addMetadata("es.test2", "value2"); ex.addHeader("test", "some value"); ex.addHeader("test_multi", "some value", "another value"); - String expected = "{\"type\":\"parsing_exception\",\"reason\":\"foobar\",\"line\":1,\"col\":2," - + "\"test1\":\"value1\",\"test2\":\"value2\"," - + "\"header\":{\"test_multi\":" - + "[\"some value\",\"another value\"],\"test\":\"some value\"}}"; + + String expected = """ + { + "type": "parsing_exception", + "reason": "foobar", + "line": 1, + "col": 2, + "test1": "value1", + "test2": "value2", + "header": { + "test_multi": [ + "some value", + "another value" + ], + "test": "some value" + } + }"""; assertExceptionAsJson(ex, expected); } } @@ -471,28 +582,29 @@ public void testToXContentWithHeadersAndMetadata() throws IOException { e.addMetadata("es.metadata_foo_0", "foo_0"); e.addMetadata("es.metadata_foo_1", "foo_1"); - final String expectedJson = "{" - + "\"type\":\"exception\"," - + "\"reason\":\"foo\"," - + "\"metadata_foo_0\":\"foo_0\"," - + "\"metadata_foo_1\":\"foo_1\"," - + "\"caused_by\":{" - + "\"type\":\"exception\"," - + "\"reason\":\"bar\"," - + "\"caused_by\":{" - + "\"type\":\"exception\"," - + "\"reason\":\"baz\"," - + "\"caused_by\":{" - + "\"type\":\"cluster_block_exception\"," - + "\"reason\":\"blocked by: [SERVICE_UNAVAILABLE/2/no master];\"" - + "}" - + "}" - + "}," - + "\"header\":{" - + "\"foo_0\":\"0\"," - + "\"foo_1\":\"1\"" - + "}" - + "}"; + final String expectedJson = """ + { + "type": "exception", + "reason": "foo", + "metadata_foo_0": "foo_0", + "metadata_foo_1": "foo_1", + "caused_by": { + "type": "exception", + "reason": "bar", + "caused_by": { + "type": "exception", + "reason": "baz", + "caused_by": { + "type": "cluster_block_exception", + "reason": "blocked by: [SERVICE_UNAVAILABLE/2/no master];" + } + } + }, + "header": { + "foo_0": "0", + "foo_1": "1" + } + }"""; assertExceptionAsJson(e, expectedJson); diff --git a/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java b/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java index be59a20167e13..5d787e66ad5c6 100644 --- a/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java +++ b/server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java @@ -488,9 +488,11 @@ public void testClusterBlockException() throws IOException { public void testNotSerializableExceptionWrapper() throws IOException { NotSerializableExceptionWrapper ex = serialize(new NotSerializableExceptionWrapper(new NullPointerException())); - assertEquals("{\"type\":\"null_pointer_exception\",\"reason\":\"null_pointer_exception: null\"}", Strings.toString(ex)); + assertEquals(""" + {"type":"null_pointer_exception","reason":"null_pointer_exception: null"}""", Strings.toString(ex)); ex = serialize(new NotSerializableExceptionWrapper(new IllegalArgumentException("nono!"))); - assertEquals("{\"type\":\"illegal_argument_exception\",\"reason\":\"illegal_argument_exception: nono!\"}", Strings.toString(ex)); + assertEquals(""" + {"type":"illegal_argument_exception","reason":"illegal_argument_exception: nono!"}""", Strings.toString(ex)); class UnknownException extends Exception { UnknownException(final String message) { diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java index 76ceeb5cd8601..13ce8462f1fb5 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplainActionTests.java @@ -24,6 +24,7 @@ import org.elasticsearch.cluster.routing.allocation.allocator.ShardsAllocator; import org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.gateway.TestGatewayAllocator; import org.elasticsearch.xcontent.ToXContent; @@ -94,33 +95,39 @@ public ShardAllocationDecision decideShardAllocation(ShardRouting shard, Routing explanation = "the shard is in the process of initializing on node [], " + "wait until initialization has completed"; } assertEquals( - "{\"index\":\"idx\",\"shard\":0,\"primary\":true,\"current_state\":\"" - + shardRoutingState.toString().toLowerCase(Locale.ROOT) - + "\"" - + (shard.unassignedInfo() != null - ? ",\"unassigned_info\":{" - + "\"reason\":\"" - + shard.unassignedInfo().getReason() - + "\"," - + "\"at\":\"" - + UnassignedInfo.DATE_TIME_FORMATTER.format( - Instant.ofEpochMilli(shard.unassignedInfo().getUnassignedTimeInMillis()) + XContentHelper.stripWhitespace( + """ + { + "index": "idx", + "shard": 0, + "primary": true, + "current_state": "%s" + %s, + "current_node": { + "id": "%s", + "name": "%s", + "transport_address": "%s" + }, + "explanation": "%s" + }""".formatted( + shardRoutingState.toString().toLowerCase(Locale.ROOT), + shard.unassignedInfo() != null + ? """ + ,"unassigned_info": {"reason": "%s", "at": "%s", "last_allocation_status": "%s"} + """.formatted( + shard.unassignedInfo().getReason(), + UnassignedInfo.DATE_TIME_FORMATTER.format( + Instant.ofEpochMilli(shard.unassignedInfo().getUnassignedTimeInMillis()) + ), + AllocationDecision.fromAllocationStatus(shard.unassignedInfo().getLastAllocationStatus()) ) - + "\"," - + "\"last_allocation_status\":\"" - + AllocationDecision.fromAllocationStatus(shard.unassignedInfo().getLastAllocationStatus()) - + "\"}" - : "") - + ",\"current_node\":" - + "{\"id\":\"" - + cae.getCurrentNode().getId() - + "\",\"name\":\"" - + cae.getCurrentNode().getName() - + "\",\"transport_address\":\"" - + cae.getCurrentNode().getAddress() - + "\"},\"explanation\":\"" - + explanation - + "\"}", + : "", + cae.getCurrentNode().getId(), + cae.getCurrentNode().getName(), + cae.getCurrentNode().getAddress(), + explanation + ) + ), Strings.toString(builder) ); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanationTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanationTests.java index 0e68f3cb217fd..51a7ee8a04e2e 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanationTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanationTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.ESTestCase; @@ -82,15 +83,24 @@ public void testExplanationToXContent() throws Exception { ClusterAllocationExplanation cae = randomClusterAllocationExplanation(true, true); XContentBuilder builder = XContentFactory.jsonBuilder(); cae.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\"index\":\"idx\",\"shard\":0,\"primary\":true,\"current_state\":\"started\",\"current_node\":" - + "{\"id\":\"node-0\",\"name\":\"\",\"transport_address\":\"" - + cae.getCurrentNode().getAddress() - + "\",\"weight_ranking\":3},\"can_remain_on_current_node\":\"yes\",\"can_rebalance_cluster\":\"yes\"," - + "\"can_rebalance_to_other_node\":\"no\",\"rebalance_explanation\":\"cannot rebalance as no target node exists " - + "that can both allocate this shard and improve the cluster balance\"}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "index": "idx", + "shard": 0, + "primary": true, + "current_state": "started", + "current_node": { + "id": "node-0", + "name": "", + "transport_address": "%s", + "weight_ranking": 3 + }, + "can_remain_on_current_node": "yes", + "can_rebalance_cluster": "yes", + "can_rebalance_to_other_node": "no", + "rebalance_explanation": "cannot rebalance as no target node exists that can both allocate this shard \ + and improve the cluster balance" + }""".formatted(cae.getCurrentNode().getAddress())), Strings.toString(builder)); } public void testRandomShardExplanationToXContent() throws Exception { @@ -98,24 +108,34 @@ public void testRandomShardExplanationToXContent() throws Exception { XContentBuilder builder = XContentFactory.jsonBuilder(); cae.toXContent(builder, ToXContent.EMPTY_PARAMS); final String actual = Strings.toString(builder); + assertThat(actual, equalTo(XContentHelper.stripWhitespace(""" + { + "note": "%s", + "index": "idx", + "shard": 0, + "primary": true, + "current_state": "started", + "current_node": { + "id": "node-0", + "name": "", + "transport_address": "%s", + "weight_ranking": 3 + }, + "can_remain_on_current_node": "yes", + "can_rebalance_cluster": "yes", + "can_rebalance_to_other_node": "no", + "rebalance_explanation": "cannot rebalance as no target node exists that can both allocate this shard \ + and improve the cluster balance" + }""".formatted(ClusterAllocationExplanation.NO_SHARD_SPECIFIED_MESSAGE, cae.getCurrentNode().getAddress())))); assertThat( actual, allOf( - equalTo( - "{\"note\":\"" - + ClusterAllocationExplanation.NO_SHARD_SPECIFIED_MESSAGE - + "\",\"index\":\"idx\",\"shard\":0,\"primary\":true,\"current_state\":\"started\",\"current_node\":" - + "{\"id\":\"node-0\",\"name\":\"\",\"transport_address\":\"" - + cae.getCurrentNode().getAddress() - + "\",\"weight_ranking\":3},\"can_remain_on_current_node\":\"yes\",\"can_rebalance_cluster\":\"yes\"," - + "\"can_rebalance_to_other_node\":\"no\",\"rebalance_explanation\":\"cannot rebalance as no target node exists " - + "that can both allocate this shard and improve the cluster balance\"}" - ), // no point in asserting the precise wording of the message into this test, but we care that the note contains these bits: containsString("No shard was specified in the explain API request"), containsString("specify the target shard in the request") ) ); + } private static ClusterAllocationExplanation randomClusterAllocationExplanation(boolean assignedShard, boolean specificShard) { diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java index 9b9149249e31b..057ac1cf4524f 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java @@ -23,6 +23,7 @@ import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -61,106 +62,100 @@ public void testToXContent() throws IOException { XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); clusterRerouteResponse.toXContent(builder, ToXContent.EMPTY_PARAMS); assertEquals( - "{\n" - + " \"acknowledged\" : true,\n" - + " \"state\" : {\n" - + " \"cluster_uuid\" : \"_na_\",\n" - + " \"version\" : 0,\n" - + " \"state_uuid\" : \"" - + clusterState.stateUUID() - + "\",\n" - + " \"master_node\" : \"node0\",\n" - + " \"blocks\" : { },\n" - + " \"nodes\" : {\n" - + " \"node0\" : {\n" - + " \"name\" : \"\",\n" - + " \"ephemeral_id\" : \"" - + node0.getEphemeralId() - + "\",\n" - + " \"transport_address\" : \"0.0.0.0:9000\",\n" - + " \"attributes\" : { },\n" - + " \"roles\" : [\n" - + " \"data\",\n" - + " \"data_cold\",\n" - + " \"data_content\",\n" - + " \"data_frozen\",\n" - + " \"data_hot\",\n" - + " \"data_warm\",\n" - + " \"ingest\",\n" - + " \"master\",\n" - + " \"ml\",\n" - + " \"remote_cluster_client\",\n" - + " \"transform\",\n" - + " \"voting_only\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"_na_\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 0,\n" - + " \"last_committed_config\" : [ ],\n" - + " \"last_accepted_config\" : [ ],\n" - + " \"voting_config_exclusions\" : [ ]\n" - + " },\n" - + " \"templates\" : { },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 1,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"shard\" : {\n" - + " \"check_on_startup\" : \"true\"\n" - + " },\n" - + " \"number_of_shards\" : \"1\",\n" - + " \"number_of_replicas\" : \"0\",\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"max_script_fields\" : \"10\"\n" - + " }\n" - + " },\n" - + " \"mappings\" : { },\n" - + " \"aliases\" : [ ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 0\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [ ]\n" - + " },\n" - + " \"rollover_info\" : { },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " },\n" - + " \"routing_table\" : {\n" - + " \"indices\" : { }\n" - + " },\n" - + " \"routing_nodes\" : {\n" - + " \"unassigned\" : [ ],\n" - + " \"nodes\" : {\n" - + " \"node0\" : [ ]\n" - + " }\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) + XContentHelper.stripWhitespace(""" + { + "acknowledged": true, + "state": { + "cluster_uuid": "_na_", + "version": 0, + "state_uuid": "%s", + "master_node": "node0", + "blocks": {}, + "nodes": { + "node0": { + "name": "", + "ephemeral_id": "%s", + "transport_address": "0.0.0.0:9000", + "attributes": {}, + "roles": [ + "data", + "data_cold", + "data_content", + "data_frozen", + "data_hot", + "data_warm", + "ingest", + "master", + "ml", + "remote_cluster_client", + "transform", + "voting_only" + ] + } + }, + "metadata": { + "cluster_uuid": "_na_", + "cluster_uuid_committed": false, + "cluster_coordination": { + "term": 0, + "last_committed_config": [], + "last_accepted_config": [], + "voting_config_exclusions": [] + }, + "templates": {}, + "indices": { + "index": { + "version": 1, + "mapping_version": 1, + "settings_version": 1, + "aliases_version": 1, + "routing_num_shards": 1, + "state": "open", + "settings": { + "index": { + "shard": { + "check_on_startup": "true" + }, + "number_of_shards": "1", + "number_of_replicas": "0", + "version": { + "created": "%s" + }, + "max_script_fields": "10" + } + }, + "mappings": {}, + "aliases": [], + "primary_terms": { + "0": 0 + }, + "in_sync_allocations": { + "0": [] + }, + "rollover_info": {}, + "system": false, + "timestamp_range": { + "shards": [] + } + } + }, + "index-graveyard": { + "tombstones": [] + } + }, + "routing_table": { + "indices": {} + }, + "routing_nodes": { + "unassigned": [], + "nodes": { + "node0": [] + } + } + } + }""".formatted(clusterState.stateUUID(), node0.getEphemeralId(), Version.CURRENT.id)), + XContentHelper.stripWhitespace(Strings.toString(builder)) ); - } { XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); @@ -168,37 +163,33 @@ public void testToXContent() throws IOException { params.put("explain", "true"); params.put("metric", "version,master_node"); clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params)); - assertEquals( - "{\n" - + " \"acknowledged\" : true,\n" - + " \"state\" : {\n" - + " \"cluster_uuid\" : \"_na_\",\n" - + " \"version\" : 0,\n" - + " \"state_uuid\" : \"" - + clusterState.stateUUID() - + "\",\n" - + " \"master_node\" : \"node0\"\n" - + " },\n" - + " \"explanations\" : [\n" - + " {\n" - + " \"command\" : \"allocate_replica\",\n" - + " \"parameters\" : {\n" - + " \"index\" : \"index\",\n" - + " \"shard\" : 0,\n" - + " \"node\" : \"node0\"\n" - + " },\n" - + " \"decisions\" : [\n" - + " {\n" - + " \"decider\" : null,\n" - + " \"decision\" : \"YES\",\n" - + " \"explanation\" : \"none\"\n" - + " }\n" - + " ]\n" - + " }\n" - + " ]\n" - + "}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "acknowledged": true, + "state": { + "cluster_uuid": "_na_", + "version": 0, + "state_uuid": "%s", + "master_node": "node0" + }, + "explanations": [ + { + "command": "allocate_replica", + "parameters": { + "index": "index", + "shard": 0, + "node": "node0" + }, + "decisions": [ + { + "decider": null, + "decision": "YES", + "explanation": "none" + } + ] + } + ] + }""".formatted(clusterState.stateUUID())), XContentHelper.stripWhitespace(Strings.toString(builder))); } { XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); @@ -206,60 +197,58 @@ public void testToXContent() throws IOException { params.put("metric", "metadata"); params.put("settings_filter", "index.number*,index.version.created"); clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params)); - assertEquals( - "{\n" - + " \"acknowledged\" : true,\n" - + " \"state\" : {\n" - + " \"cluster_uuid\" : \"_na_\",\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"_na_\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 0,\n" - + " \"last_committed_config\" : [ ],\n" - + " \"last_accepted_config\" : [ ],\n" - + " \"voting_config_exclusions\" : [ ]\n" - + " },\n" - + " \"templates\" : { },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 1,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"max_script_fields\" : \"10\",\n" - + " \"shard\" : {\n" - + " \"check_on_startup\" : \"true\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : { },\n" - + " \"aliases\" : [ ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 0\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [ ]\n" - + " },\n" - + " \"rollover_info\" : { },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "acknowledged" : true, + "state" : { + "cluster_uuid" : "_na_", + "metadata" : { + "cluster_uuid" : "_na_", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 0, + "last_committed_config" : [ ], + "last_accepted_config" : [ ], + "voting_config_exclusions" : [ ] + }, + "templates" : { }, + "indices" : { + "index" : { + "version" : 1, + "mapping_version" : 1, + "settings_version" : 1, + "aliases_version" : 1, + "routing_num_shards" : 1, + "state" : "open", + "settings" : { + "index" : { + "max_script_fields" : "10", + "shard" : { + "check_on_startup" : "true" + } + } + }, + "mappings" : { }, + "aliases" : [ ], + "primary_terms" : { + "0" : 0 + }, + "in_sync_allocations" : { + "0" : [ ] + }, + "rollover_info" : { }, + "system" : false, + "timestamp_range" : { + "shards" : [ ] + } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + } + } + }"""), XContentHelper.stripWhitespace(Strings.toString(builder))); } } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatusTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatusTests.java index 70a6abcea4c95..c5042c6cccb21 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatusTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatusTests.java @@ -66,110 +66,94 @@ public void testToString() throws Exception { break; } - String expected = "{\n" - + " \"snapshot\" : \"test-snap\",\n" - + " \"repository\" : \"test-repo\",\n" - + " \"uuid\" : \"" - + uuid - + "\",\n" - + " \"state\" : \"" - + state.toString() - + "\",\n" - + " \"include_global_state\" : " - + includeGlobalState - + ",\n" - + " \"shards_stats\" : {\n" - + " \"initializing\" : " - + initializingShards - + ",\n" - + " \"started\" : " - + startedShards - + ",\n" - + " \"finalizing\" : " - + finalizingShards - + ",\n" - + " \"done\" : " - + doneShards - + ",\n" - + " \"failed\" : " - + failedShards - + ",\n" - + " \"total\" : " - + totalShards - + "\n" - + " },\n" - + " \"stats\" : {\n" - + " \"incremental\" : {\n" - + " \"file_count\" : 0,\n" - + " \"size_in_bytes\" : 0\n" - + " },\n" - + " \"total\" : {\n" - + " \"file_count\" : 0,\n" - + " \"size_in_bytes\" : 0\n" - + " },\n" - + " \"start_time_in_millis\" : 0,\n" - + " \"time_in_millis\" : 0\n" - + " },\n" - + " \"indices\" : {\n" - + " \"" - + indexName - + "\" : {\n" - + " \"shards_stats\" : {\n" - + " \"initializing\" : " - + initializingShards - + ",\n" - + " \"started\" : " - + startedShards - + ",\n" - + " \"finalizing\" : " - + finalizingShards - + ",\n" - + " \"done\" : " - + doneShards - + ",\n" - + " \"failed\" : " - + failedShards - + ",\n" - + " \"total\" : " - + totalShards - + "\n" - + " },\n" - + " \"stats\" : {\n" - + " \"incremental\" : {\n" - + " \"file_count\" : 0,\n" - + " \"size_in_bytes\" : 0\n" - + " },\n" - + " \"total\" : {\n" - + " \"file_count\" : 0,\n" - + " \"size_in_bytes\" : 0\n" - + " },\n" - + " \"start_time_in_millis\" : 0,\n" - + " \"time_in_millis\" : 0\n" - + " },\n" - + " \"shards\" : {\n" - + " \"" - + shardId - + "\" : {\n" - + " \"stage\" : \"" - + shardStage.toString() - + "\",\n" - + " \"stats\" : {\n" - + " \"incremental\" : {\n" - + " \"file_count\" : 0,\n" - + " \"size_in_bytes\" : 0\n" - + " },\n" - + " \"total\" : {\n" - + " \"file_count\" : 0,\n" - + " \"size_in_bytes\" : 0\n" - + " },\n" - + " \"start_time_in_millis\" : 0,\n" - + " \"time_in_millis\" : 0\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String expected = """ + { + "snapshot" : "test-snap", + "repository" : "test-repo", + "uuid" : "%s", + "state" : "%s", + "include_global_state" : %s, + "shards_stats" : { + "initializing" : %s, + "started" : %s, + "finalizing" : %s, + "done" : %s, + "failed" : %s, + "total" : %s + }, + "stats" : { + "incremental" : { + "file_count" : 0, + "size_in_bytes" : 0 + }, + "total" : { + "file_count" : 0, + "size_in_bytes" : 0 + }, + "start_time_in_millis" : 0, + "time_in_millis" : 0 + }, + "indices" : { + "%s" : { + "shards_stats" : { + "initializing" : %s, + "started" : %s, + "finalizing" : %s, + "done" : %s, + "failed" : %s, + "total" : %s + }, + "stats" : { + "incremental" : { + "file_count" : 0, + "size_in_bytes" : 0 + }, + "total" : { + "file_count" : 0, + "size_in_bytes" : 0 + }, + "start_time_in_millis" : 0, + "time_in_millis" : 0 + }, + "shards" : { + "%s" : { + "stage" : "%s", + "stats" : { + "incremental" : { + "file_count" : 0, + "size_in_bytes" : 0 + }, + "total" : { + "file_count" : 0, + "size_in_bytes" : 0 + }, + "start_time_in_millis" : 0, + "time_in_millis" : 0 + } + } + } + } + } + }""".formatted( + uuid, + state.toString(), + includeGlobalState, + initializingShards, + startedShards, + finalizingShards, + doneShards, + failedShards, + totalShards, + indexName, + initializingShards, + startedShards, + finalizingShards, + doneShards, + failedShards, + totalShards, + shardId, + shardStage.toString() + ); assertEquals(expected, status.toString()); } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/AnalysisStatsTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/AnalysisStatsTests.java index d2927063eab20..f5fa66d1965c6 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/AnalysisStatsTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/AnalysisStatsTests.java @@ -218,7 +218,8 @@ protected AnalysisStats mutateInstance(AnalysisStats instance) throws IOExceptio } public void testAccountsRegularIndices() { - String mapping = "{\"properties\":{\"bar\":{\"type\":\"text\",\"analyzer\":\"german\"}}}"; + String mapping = """ + {"properties":{"bar":{"type":"text","analyzer":"german"}}}"""; Settings settings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) @@ -234,7 +235,8 @@ public void testAccountsRegularIndices() { } public void testIgnoreSystemIndices() { - String mapping = "{\"properties\":{\"bar\":{\"type\":\"text\",\"analyzer\":\"german\"}}}"; + String mapping = """ + {"properties":{"bar":{"type":"text","analyzer":"german"}}}"""; Settings settings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsNodesTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsNodesTests.java index cadc9966f0099..daf2717f1664d 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsNodesTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsNodesTests.java @@ -52,7 +52,8 @@ public void testNetworkTypesToXContent() throws Exception { ); stats = new ClusterStatsNodes.NetworkTypes(nodeInfos); assertEquals( - "{" + "\"transport_types\":{\"custom\":1}," + "\"http_types\":{\"custom\":2}" + "}", + """ + {"transport_types":{"custom":1},"http_types":{"custom":2}}""", toXContent(stats, XContentType.JSON, randomBoolean()).utf8ToString() ); } @@ -82,7 +83,7 @@ public void testIngestStats() throws Exception { ClusterStatsNodes.IngestStats stats = new ClusterStatsNodes.IngestStats(Collections.singletonList(nodeStats)); assertThat(stats.pipelineCount, equalTo(nodeStats.getIngestStats().getProcessorStats().size())); - String processorStatsString = "{"; + StringBuilder processorStatsString = new StringBuilder("{"); Iterator> iter = processorStats.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = iter.next(); @@ -91,34 +92,17 @@ public void testIngestStats() throws Exception { long failedCount = statValues[1]; long current = statValues[2]; long timeInMillis = statValues[3]; - processorStatsString += "\"" - + entry.getKey() - + "\":{\"count\":" - + count - + ",\"failed\":" - + failedCount - + ",\"current\":" - + current - + ",\"time_in_millis\":" - + timeInMillis - + "}"; + processorStatsString.append(""" + "%s":{"count":%s,"failed":%s,"current":%s,"time_in_millis":%s}\ + """.formatted(entry.getKey(), count, failedCount, current, timeInMillis)); if (iter.hasNext()) { - processorStatsString += ","; + processorStatsString.append(","); } } - processorStatsString += "}"; - assertThat( - toXContent(stats, XContentType.JSON, false).utf8ToString(), - equalTo( - "{\"ingest\":{" - + "\"number_of_pipelines\":" - + stats.pipelineCount - + "," - + "\"processor_stats\":" - + processorStatsString - + "}}" - ) - ); + processorStatsString.append("}"); + assertThat(toXContent(stats, XContentType.JSON, false).utf8ToString(), equalTo(""" + {"ingest":{"number_of_pipelines":%s,"processor_stats":%s}}\ + """.formatted(stats.pipelineCount, processorStatsString))); } public void testIndexPressureStats() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/IndexFeatureStatsTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/IndexFeatureStatsTests.java index 2f97e786b1ffe..a000d6d9bc35e 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/IndexFeatureStatsTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/IndexFeatureStatsTests.java @@ -28,9 +28,8 @@ protected IndexFeatureStats createTestInstance() { public void testToXContent() { IndexFeatureStats testInstance = createTestInstance(); - assertEquals( - "{\"name\":\"" + testInstance.name + "\",\"count\":" + testInstance.count + ",\"index_count\":" + testInstance.indexCount + "}", - Strings.toString(testInstance) - ); + assertEquals(""" + {"name":"%s","count":%s,"index_count":%s}\ + """.formatted(testInstance.name, testInstance.count, testInstance.indexCount), Strings.toString(testInstance)); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/MappingStatsTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/MappingStatsTests.java index f65e8ccc74f53..a639d628536bf 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/MappingStatsTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/stats/MappingStatsTests.java @@ -39,149 +39,149 @@ public void testToXContent() { Script script2 = new Script("doc['field']"); Script script3 = new Script("params._source.field + params._source.field \n + params._source.field"); Script script4 = new Script("params._source.field"); - String mapping = "{" - + " \"runtime\" : {" - + " \"keyword1\": {" - + " \"type\": \"keyword\"," - + " \"script\": " - + Strings.toString(script1) - + " }," - + " \"keyword2\": {" - + " \"type\": \"keyword\"" - + " }," - + " \"object.keyword3\": {" - + " \"type\": \"keyword\"," - + " \"script\": " - + Strings.toString(script2) - + " }," - + " \"long\": {" - + " \"type\": \"long\"," - + " \"script\": " - + Strings.toString(script3) - + " }," - + " \"long2\": {" - + " \"type\": \"long\"," - + " \"script\": " - + Strings.toString(script4) - + " }" - + " }," - + " \"properties\":{" - + " \"object\":{" - + " \"type\":\"object\"," - + " \"properties\":{" - + " \"keyword3\":{" - + " \"type\": \"keyword\"" - + " }" - + " }" - + " }," - + " \"long3\": {" - + " \"type\":\"long\"," - + " \"script\": " - + Strings.toString(script3) - + " }," - + " \"long4\": {" - + " \"type\":\"long\"," - + " \"script\": " - + Strings.toString(script4) - + " }," - + " \"keyword3\": {" - + " \"type\": \"keyword\"," - + " \"script\": " - + Strings.toString(script1) - + " }" - + " }" - + "}"; + String mapping = """ + { + "runtime": { + "keyword1": { + "type": "keyword", + "script": %s + }, + "keyword2": { + "type": "keyword" + }, + "object.keyword3": { + "type": "keyword", + "script": %s + }, + "long": { + "type": "long", + "script": %s + }, + "long2": { + "type": "long", + "script": %s + } + }, + "properties": { + "object": { + "type": "object", + "properties": { + "keyword3": { + "type": "keyword" + } + } + }, + "long3": { + "type": "long", + "script": %s + }, + "long4": { + "type": "long", + "script": %s + }, + "keyword3": { + "type": "keyword", + "script": %s + } + } + }""".formatted( + Strings.toString(script1), + Strings.toString(script2), + Strings.toString(script3), + Strings.toString(script4), + Strings.toString(script3), + Strings.toString(script4), + Strings.toString(script1) + ); IndexMetadata meta = IndexMetadata.builder("index").settings(settings).putMapping(mapping).build(); IndexMetadata meta2 = IndexMetadata.builder("index2").settings(settings).putMapping(mapping).build(); Metadata metadata = Metadata.builder().put(meta, false).put(meta2, false).build(); MappingStats mappingStats = MappingStats.of(metadata, () -> {}); - assertEquals( - "{\n" - + " \"mappings\" : {\n" - + " \"field_types\" : [\n" - + " {\n" - + " \"name\" : \"keyword\",\n" - + " \"count\" : 4,\n" - + " \"index_count\" : 2,\n" - + " \"script_count\" : 2,\n" - + " \"lang\" : [\n" - + " \"painless\"\n" - + " ],\n" - + " \"lines_max\" : 1,\n" - + " \"lines_total\" : 2,\n" - + " \"chars_max\" : 47,\n" - + " \"chars_total\" : 94,\n" - + " \"source_max\" : 1,\n" - + " \"source_total\" : 2,\n" - + " \"doc_max\" : 2,\n" - + " \"doc_total\" : 4\n" - + " },\n" - + " {\n" - + " \"name\" : \"long\",\n" - + " \"count\" : 4,\n" - + " \"index_count\" : 2,\n" - + " \"script_count\" : 4,\n" - + " \"lang\" : [\n" - + " \"painless\"\n" - + " ],\n" - + " \"lines_max\" : 2,\n" - + " \"lines_total\" : 6,\n" - + " \"chars_max\" : 68,\n" - + " \"chars_total\" : 176,\n" - + " \"source_max\" : 3,\n" - + " \"source_total\" : 8,\n" - + " \"doc_max\" : 0,\n" - + " \"doc_total\" : 0\n" - + " },\n" - + " {\n" - + " \"name\" : \"object\",\n" - + " \"count\" : 2,\n" - + " \"index_count\" : 2,\n" - + " \"script_count\" : 0\n" - + " }\n" - + " ],\n" - + " \"runtime_field_types\" : [\n" - + " {\n" - + " \"name\" : \"keyword\",\n" - + " \"count\" : 6,\n" - + " \"index_count\" : 2,\n" - + " \"scriptless_count\" : 2,\n" - + " \"shadowed_count\" : 2,\n" - + " \"lang\" : [\n" - + " \"painless\"\n" - + " ],\n" - + " \"lines_max\" : 1,\n" - + " \"lines_total\" : 4,\n" - + " \"chars_max\" : 47,\n" - + " \"chars_total\" : 118,\n" - + " \"source_max\" : 1,\n" - + " \"source_total\" : 2,\n" - + " \"doc_max\" : 2,\n" - + " \"doc_total\" : 6\n" - + " },\n" - + " {\n" - + " \"name\" : \"long\",\n" - + " \"count\" : 4,\n" - + " \"index_count\" : 2,\n" - + " \"scriptless_count\" : 0,\n" - + " \"shadowed_count\" : 0,\n" - + " \"lang\" : [\n" - + " \"painless\"\n" - + " ],\n" - + " \"lines_max\" : 2,\n" - + " \"lines_total\" : 6,\n" - + " \"chars_max\" : 68,\n" - + " \"chars_total\" : 176,\n" - + " \"source_max\" : 3,\n" - + " \"source_total\" : 8,\n" - + " \"doc_max\" : 0,\n" - + " \"doc_total\" : 0\n" - + " }\n" - + " ]\n" - + " }\n" - + "}", - Strings.toString(mappingStats, true, true) - ); + assertEquals(""" + { + "mappings" : { + "field_types" : [ + { + "name" : "keyword", + "count" : 4, + "index_count" : 2, + "script_count" : 2, + "lang" : [ + "painless" + ], + "lines_max" : 1, + "lines_total" : 2, + "chars_max" : 47, + "chars_total" : 94, + "source_max" : 1, + "source_total" : 2, + "doc_max" : 2, + "doc_total" : 4 + }, + { + "name" : "long", + "count" : 4, + "index_count" : 2, + "script_count" : 4, + "lang" : [ + "painless" + ], + "lines_max" : 2, + "lines_total" : 6, + "chars_max" : 68, + "chars_total" : 176, + "source_max" : 3, + "source_total" : 8, + "doc_max" : 0, + "doc_total" : 0 + }, + { + "name" : "object", + "count" : 2, + "index_count" : 2, + "script_count" : 0 + } + ], + "runtime_field_types" : [ + { + "name" : "keyword", + "count" : 6, + "index_count" : 2, + "scriptless_count" : 2, + "shadowed_count" : 2, + "lang" : [ + "painless" + ], + "lines_max" : 1, + "lines_total" : 4, + "chars_max" : 47, + "chars_total" : 118, + "source_max" : 1, + "source_total" : 2, + "doc_max" : 2, + "doc_total" : 6 + }, + { + "name" : "long", + "count" : 4, + "index_count" : 2, + "scriptless_count" : 0, + "shadowed_count" : 0, + "lang" : [ + "painless" + ], + "lines_max" : 2, + "lines_total" : 6, + "chars_max" : 68, + "chars_total" : 176, + "source_max" : 3, + "source_total" : 8, + "doc_max" : 0, + "doc_total" : 0 + } + ] + } + }""", Strings.toString(mappingStats, true, true)); } @Override @@ -272,7 +272,8 @@ protected MappingStats mutateInstance(MappingStats instance) throws IOException } public void testAccountsRegularIndices() { - String mapping = "{\"properties\":{\"bar\":{\"type\":\"long\"}}}"; + String mapping = """ + {"properties":{"bar":{"type":"long"}}}"""; Settings settings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) @@ -288,7 +289,8 @@ public void testAccountsRegularIndices() { } public void testIgnoreSystemIndices() { - String mapping = "{\"properties\":{\"bar\":{\"type\":\"long\"}}}"; + String mapping = """ + {"properties":{"bar":{"type":"long"}}}"""; Settings settings = Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 4) diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java index 655c7b212a364..4f8ec1ecb39ad 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponseTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.action.admin.indices.close.CloseIndexResponse.IndexResult; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.index.shard.ShardId; @@ -139,10 +140,8 @@ public void testToXContent() throws IOException { Index index = new Index("test", "uuid"); IndexResult indexResult = new CloseIndexResponse.IndexResult(index); CloseIndexResponse closeIndexResponse = new CloseIndexResponse(true, true, Collections.singletonList(indexResult)); - assertEquals( - "{\"acknowledged\":true,\"shards_acknowledged\":true,\"indices\":{\"test\":{\"closed\":true}}}", - Strings.toString(closeIndexResponse) - ); + assertEquals(""" + {"acknowledged":true,"shards_acknowledged":true,"indices":{"test":{"closed":true}}}""", Strings.toString(closeIndexResponse)); CloseIndexResponse.ShardResult[] shards = new CloseIndexResponse.ShardResult[1]; shards[0] = new CloseIndexResponse.ShardResult( @@ -152,14 +151,32 @@ public void testToXContent() throws IOException { ); indexResult = new CloseIndexResponse.IndexResult(index, shards); closeIndexResponse = new CloseIndexResponse(true, true, Collections.singletonList(indexResult)); - assertEquals( - "{\"acknowledged\":true,\"shards_acknowledged\":true," - + "\"indices\":{\"test\":{\"closed\":false,\"failedShards\":{\"0\":{" - + "\"failures\":[{\"node\":\"nodeId\",\"shard\":0,\"index\":\"test\",\"status\":\"INTERNAL_SERVER_ERROR\"," - + "\"reason\":{\"type\":\"action_not_found_transport_exception\"," - + "\"reason\":\"No handler for action [test]\"}}]}}}}}", - Strings.toString(closeIndexResponse) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "acknowledged": true, + "shards_acknowledged": true, + "indices": { + "test": { + "closed": false, + "failedShards": { + "0": { + "failures": [ + { + "node": "nodeId", + "shard": 0, + "index": "test", + "status": "INTERNAL_SERVER_ERROR", + "reason": { + "type": "action_not_found_transport_exception", + "reason": "No handler for action [test]" + } + } + ] + } + } + } + } + }"""), Strings.toString(closeIndexResponse)); } private CloseIndexResponse randomResponse() { diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilderTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilderTests.java index e7922f22e19bd..addc9ba56e67a 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestBuilderTests.java @@ -52,11 +52,11 @@ public void testSetSource() throws IOException { ElasticsearchParseException e = expectThrows( ElasticsearchParseException.class, - () -> { builder.setSource("{\"" + KEY + "\" : \"" + VALUE + "\"}", XContentType.JSON); } + () -> builder.setSource("{ \"%s\": \"%s\" }".formatted(KEY, VALUE), XContentType.JSON) ); assertEquals(String.format(Locale.ROOT, "unknown key [%s] for create index", KEY), e.getMessage()); - builder.setSource("{\"settings\" : {\"" + KEY + "\" : \"" + VALUE + "\"}}", XContentType.JSON); + builder.setSource("{ \"settings\": { \"%s\": \"%s\" }}".formatted(KEY, VALUE), XContentType.JSON); assertEquals(VALUE, builder.request().settings().get(KEY)); XContentBuilder xContent = XContentFactory.jsonBuilder() diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java index 8ac1816398d77..4997d4edbea58 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java @@ -50,20 +50,21 @@ public void testSimpleSerialization() throws IOException { } public void testTopLevelKeys() { - String createIndex = "{\n" - + " \"FOO_SHOULD_BE_ILLEGAL_HERE\": {\n" - + " \"BAR_IS_THE_SAME\": 42\n" - + " },\n" - + " \"mappings\": {\n" - + " \"test\": {\n" - + " \"properties\": {\n" - + " \"field1\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String createIndex = """ + { + "FOO_SHOULD_BE_ILLEGAL_HERE": { + "BAR_IS_THE_SAME": 42 + }, + "mappings": { + "test": { + "properties": { + "field1": { + "type": "text" + } + } + } + } + }"""; CreateIndexRequest request = new CreateIndexRequest(); ElasticsearchParseException e = expectThrows( diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java index 3a2b681806cd1..5b568fe5ddb31 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponseTests.java @@ -60,13 +60,15 @@ protected CreateIndexResponse doParseInstance(XContentParser parser) { public void testToXContent() { CreateIndexResponse response = new CreateIndexResponse(true, false, "index_name"); String output = Strings.toString(response); - assertEquals("{\"acknowledged\":true,\"shards_acknowledged\":false,\"index\":\"index_name\"}", output); + assertEquals(""" + {"acknowledged":true,"shards_acknowledged":false,"index":"index_name"}""", output); } public void testToAndFromXContentIndexNull() throws IOException { CreateIndexResponse response = new CreateIndexResponse(true, false, null); String output = Strings.toString(response); - assertEquals("{\"acknowledged\":true,\"shards_acknowledged\":false,\"index\":null}", output); + assertEquals(""" + {"acknowledged":true,"shards_acknowledged":false,"index":null}""", output); try (XContentParser parser = createParser(JsonXContent.jsonXContent, output)) { CreateIndexResponse parsedResponse = CreateIndexResponse.fromXContent(parser); assertNull(parsedResponse.index()); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestTests.java index 4b5d40a780f6c..44ef109df9d44 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverRequestTests.java @@ -227,10 +227,8 @@ public void testParsingWithType() throws Exception { request.fromXContent(true, parser); Map> conditions = request.getConditions(); assertThat(conditions.size(), equalTo(2)); - assertThat( - request.getCreateIndexRequest().mappings(), - equalTo("{\"_doc\":{\"properties\":{\"field1\":{\"index\":\"not_analyzed\",\"type\":\"string\"}}}}") - ); + assertThat(request.getCreateIndexRequest().mappings(), equalTo(""" + {"_doc":{"properties":{"field1":{"index":"not_analyzed","type":"string"}}}}""")); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java index 5f509e583ed8c..f8a1a8161edb7 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeRequestTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.RandomCreateIndexGenerator; import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; @@ -67,14 +68,16 @@ public void testToXContent() throws IOException { ResizeRequest request = new ResizeRequest("target", "source"); request.setMaxPrimaryShardSize(new ByteSizeValue(100, ByteSizeUnit.MB)); String actualRequestBody = Strings.toString(request); - assertEquals("{\"settings\":{},\"aliases\":{},\"max_primary_shard_size\":\"100mb\"}", actualRequestBody); + assertEquals(""" + {"settings":{},"aliases":{},"max_primary_shard_size":"100mb"}""", actualRequestBody); } { ResizeRequest request = new ResizeRequest(); CreateIndexRequest target = new CreateIndexRequest("target"); Alias alias = new Alias("test_alias"); alias.routing("1"); - alias.filter("{\"term\":{\"year\":2016}}"); + alias.filter(""" + {"term":{"year":2016}}"""); alias.writeIndex(true); target.alias(alias); Settings.Builder settings = Settings.builder(); @@ -82,9 +85,26 @@ public void testToXContent() throws IOException { target.settings(settings); request.setTargetIndex(target); String actualRequestBody = Strings.toString(request); - String expectedRequestBody = "{\"settings\":{\"index\":{\"number_of_shards\":\"10\"}}," - + "\"aliases\":{\"test_alias\":{\"filter\":{\"term\":{\"year\":2016}},\"routing\":\"1\",\"is_write_index\":true}}}"; - assertEquals(expectedRequestBody, actualRequestBody); + String expectedRequestBody = """ + { + "settings": { + "index": { + "number_of_shards": "10" + } + }, + "aliases": { + "test_alias": { + "filter": { + "term": { + "year": 2016 + } + }, + "routing": "1", + "is_write_index": true + } + } + }"""; + assertEquals(XContentHelper.stripWhitespace(expectedRequestBody), actualRequestBody); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java index ee2d3fc66848b..3a65e4d8665fd 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/shrink/ResizeResponseTests.java @@ -18,7 +18,8 @@ public class ResizeResponseTests extends AbstractSerializingTestCase()); String output = Strings.toString(response); - assertEquals("{\"_shards\":{\"total\":10,\"successful\":10,\"failed\":0},\"valid\":true}", output); + assertEquals(""" + {"_shards":{"total":10,"successful":10,"failed":0},"valid":true}""", output); } } diff --git a/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java b/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java index 3506d677fffce..6aa0a832d83f2 100644 --- a/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java +++ b/server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorTests.java @@ -234,7 +234,10 @@ public void testConcurrentExecutions() throws Exception { CountDownLatch startGate = new CountDownLatch(1 + concurrentClients); IndexRequest indexRequest = new IndexRequest(); - String bulkRequest = "{ \"index\" : { \"_index\" : \"test\", \"_id\" : \"1\" } }\n" + "{ \"field1\" : \"value1\" }\n"; + String bulkRequest = """ + { "index" : { "_index" : "test", "_id" : "1" } } + { "field1" : "value1" } + """; BytesReference bytesReference = BytesReference.fromByteBuffers( new ByteBuffer[] { ByteBuffer.wrap(bulkRequest.getBytes(StandardCharsets.UTF_8)) } ); @@ -274,31 +277,28 @@ public void testConcurrentExecutions() throws Exception { if (exceptionRef.get() != null) { logger.error("exception(s) caught during test", exceptionRef.get()); } + String message = """ + + Expected Bulks: %s + Requested Bulks: %s + Successful Bulks: %s + Failed Bulks: %ds + Max Documents: %s + Max Batch Size: %s + Concurrent Clients: %s + Concurrent Bulk Requests: %s + """; fail( - "\nExpected Bulks: " - + expectedExecutions - + "\n" - + "Requested Bulks: " - + requestCount.get() - + "\n" - + "Successful Bulks: " - + successCount.get() - + "\n" - + "Failed Bulks: " - + failureCount.get() - + "\n" - + "Max Documents: " - + maxDocuments - + "\n" - + "Max Batch Size: " - + maxBatchSize - + "\n" - + "Concurrent Clients: " - + concurrentClients - + "\n" - + "Concurrent Bulk Requests: " - + concurrentBulkRequests - + "\n" + message.formatted( + expectedExecutions, + requestCount.get(), + successCount.get(), + failureCount.get(), + maxDocuments, + maxBatchSize, + concurrentClients, + concurrentBulkRequests + ) ); } } @@ -362,7 +362,10 @@ public void testConcurrentExecutionsWithFlush() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(concurrentClients); IndexRequest indexRequest = new IndexRequest(); - String bulkRequest = "{ \"index\" : { \"_index\" : \"test\", \"_id\" : \"1\" } }\n" + "{ \"field1\" : \"value1\" }\n"; + String bulkRequest = """ + { "index" : { "_index" : "test", "_id" : "1" } } + { "field1" : "value1" } + """; BytesReference bytesReference = BytesReference.fromByteBuffers( new ByteBuffer[] { ByteBuffer.wrap(bulkRequest.getBytes(StandardCharsets.UTF_8)) } ); @@ -404,31 +407,28 @@ public void testConcurrentExecutionsWithFlush() throws Exception { if (exceptionRef.get() != null) { logger.error("exception(s) caught during test", exceptionRef.get()); } + String message = """ + + Requested Bulks: %d + Successful Bulks: %d + Failed Bulks: %d + Total Documents: %d + Max Documents: %d + Max Batch Size: %d + Concurrent Clients: %d + Concurrent Bulk Requests: %d + """; fail( - "\nRequested Bulks: " - + requestCount.get() - + "\n" - + "Successful Bulks: " - + successCount.get() - + "\n" - + "Failed Bulks: " - + failureCount.get() - + "\n" - + "Total Documents: " - + docCount.get() - + "\n" - + "Max Documents: " - + maxDocuments - + "\n" - + "Max Batch Size: " - + maxBatchSize - + "\n" - + "Concurrent Clients: " - + concurrentClients - + "\n" - + "Concurrent Bulk Requests: " - + concurrentBulkRequests - + "\n" + message.formatted( + requestCount.get(), + successCount.get(), + failureCount.get(), + docCount.get(), + maxDocuments, + maxBatchSize, + concurrentClients, + concurrentBulkRequests + ) ); } } diff --git a/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestParserTests.java b/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestParserTests.java index be1e5b6971da0..11d831363a32d 100644 --- a/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestParserTests.java +++ b/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestParserTests.java @@ -23,7 +23,10 @@ public class BulkRequestParserTests extends ESTestCase { public void testIndexRequest() throws IOException { - BytesArray request = new BytesArray("{ \"index\":{ \"_id\": \"bar\" } }\n{}\n"); + BytesArray request = new BytesArray(""" + { "index":{ "_id": "bar" } } + {} + """); BulkRequestParser parser = new BulkRequestParser(randomBoolean(), RestApiVersion.current()); final AtomicBoolean parsed = new AtomicBoolean(); parser.parse(request, "foo", null, null, null, null, false, XContentType.JSON, (indexRequest, type) -> { @@ -49,7 +52,10 @@ public void testIndexRequest() throws IOException { req -> fail() ); - request = new BytesArray("{ \"index\":{ \"_id\": \"bar\", \"require_alias\": true } }\n{}\n"); + request = new BytesArray(""" + { "index":{ "_id": "bar", "require_alias": true } } + {} + """); parser.parse( request, "foo", @@ -64,7 +70,10 @@ public void testIndexRequest() throws IOException { req -> fail() ); - request = new BytesArray("{ \"index\":{ \"_id\": \"bar\", \"require_alias\": false } }\n{}\n"); + request = new BytesArray(""" + { "index":{ "_id": "bar", "require_alias": false } } + {} + """); parser.parse( request, "foo", @@ -81,7 +90,9 @@ public void testIndexRequest() throws IOException { } public void testDeleteRequest() throws IOException { - BytesArray request = new BytesArray("{ \"delete\":{ \"_id\": \"bar\" } }\n"); + BytesArray request = new BytesArray(""" + { "delete":{ "_id": "bar" } } + """); BulkRequestParser parser = new BulkRequestParser(randomBoolean(), RestApiVersion.current()); final AtomicBoolean parsed = new AtomicBoolean(); parser.parse( @@ -106,7 +117,10 @@ public void testDeleteRequest() throws IOException { } public void testUpdateRequest() throws IOException { - BytesArray request = new BytesArray("{ \"update\":{ \"_id\": \"bar\" } }\n{}\n"); + BytesArray request = new BytesArray(""" + { "update":{ "_id": "bar" } } + {} + """); BulkRequestParser parser = new BulkRequestParser(randomBoolean(), RestApiVersion.current()); final AtomicBoolean parsed = new AtomicBoolean(); parser.parse(request, "foo", null, null, null, null, false, XContentType.JSON, (req, type) -> fail(), updateRequest -> { @@ -132,7 +146,10 @@ public void testUpdateRequest() throws IOException { req -> fail() ); - request = new BytesArray("{ \"update\":{ \"_id\": \"bar\", \"require_alias\": true } }\n{}\n"); + request = new BytesArray(""" + { "update":{ "_id": "bar", "require_alias": true } } + {} + """); parser.parse( request, "foo", @@ -147,7 +164,10 @@ public void testUpdateRequest() throws IOException { req -> fail() ); - request = new BytesArray("{ \"update\":{ \"_id\": \"bar\", \"require_alias\": false } }\n{}\n"); + request = new BytesArray(""" + { "update":{ "_id": "bar", "require_alias": false } } + {} + """); parser.parse( request, "foo", @@ -164,7 +184,9 @@ public void testUpdateRequest() throws IOException { } public void testBarfOnLackOfTrailingNewline() { - BytesArray request = new BytesArray("{ \"index\":{ \"_id\": \"bar\" } }\n{}"); + BytesArray request = new BytesArray(""" + { "index":{ "_id": "bar" } } + {}"""); BulkRequestParser parser = new BulkRequestParser(randomBoolean(), RestApiVersion.current()); IllegalArgumentException e = expectThrows( IllegalArgumentException.class, @@ -186,7 +208,10 @@ public void testBarfOnLackOfTrailingNewline() { } public void testFailOnExplicitIndex() { - BytesArray request = new BytesArray("{ \"index\":{ \"_index\": \"foo\", \"_id\": \"bar\" } }\n{}\n"); + BytesArray request = new BytesArray(""" + { "index":{ "_index": "foo", "_id": "bar" } } + {} + """); BulkRequestParser parser = new BulkRequestParser(randomBoolean(), RestApiVersion.current()); IllegalArgumentException ex = expectThrows( @@ -209,7 +234,10 @@ public void testFailOnExplicitIndex() { } public void testTypesStillParsedForBulkMonitoring() throws IOException { - BytesArray request = new BytesArray("{ \"index\":{ \"_type\": \"quux\", \"_id\": \"bar\" } }\n{}\n"); + BytesArray request = new BytesArray(""" + { "index":{ "_type": "quux", "_id": "bar" } } + {} + """); BulkRequestParser parser = new BulkRequestParser(false, RestApiVersion.current()); final AtomicBoolean parsed = new AtomicBoolean(); parser.parse(request, "foo", null, null, null, null, false, XContentType.JSON, (indexRequest, type) -> { @@ -223,10 +251,12 @@ public void testTypesStillParsedForBulkMonitoring() throws IOException { } public void testParseDeduplicatesParameterStrings() throws IOException { - BytesArray request = new BytesArray( - "{ \"index\":{ \"_index\": \"bar\", \"pipeline\": \"foo\", \"routing\": \"blub\"} }\n{}\n" - + "{ \"index\":{ \"_index\": \"bar\", \"pipeline\": \"foo\", \"routing\": \"blub\" } }\n{}\n" - ); + BytesArray request = new BytesArray(""" + { "index":{ "_index": "bar", "pipeline": "foo", "routing": "blub"} } + {} + { "index":{ "_index": "bar", "pipeline": "foo", "routing": "blub" } } + {} + """); BulkRequestParser parser = new BulkRequestParser(randomBoolean(), RestApiVersion.current()); final List indexRequests = new ArrayList<>(); parser.parse( diff --git a/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java b/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java index 48082caee71a8..5158366c78f40 100644 --- a/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/bulk/BulkRequestTests.java @@ -55,7 +55,10 @@ public void testSimpleBulk1() throws Exception { } public void testSimpleBulkWithCarriageReturn() throws Exception { - String bulkAction = "{ \"index\":{\"_index\":\"test\",\"_id\":\"1\"} }\r\n{ \"field1\" : \"value1\" }\r\n"; + String bulkAction = """ + { "index":{"_index":"test","_id":"1"} } + { "field1" : "value1" } + """; BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, XContentType.JSON); assertThat(bulkRequest.numberOfActions(), equalTo(1)); @@ -182,7 +185,10 @@ public void testSimpleBulk10() throws Exception { } public void testBulkActionShouldNotContainArray() throws Exception { - String bulkAction = "{ \"index\":{\"_index\":[\"index1\", \"index2\"],\"_id\":\"1\"} }\r\n" + "{ \"field1\" : \"value1\" }\r\n"; + String bulkAction = """ + { "index":{"_index":["index1", "index2"],"_id":"1"} }\r + { "field1" : "value1" }\r + """; BulkRequest bulkRequest = new BulkRequest(); IllegalArgumentException exc = expectThrows( IllegalArgumentException.class, @@ -195,9 +201,15 @@ public void testBulkActionShouldNotContainArray() throws Exception { } public void testBulkEmptyObject() throws Exception { - String bulkIndexAction = "{ \"index\":{\"_index\":\"test\",\"_id\":\"1\"} }\r\n"; - String bulkIndexSource = "{ \"field1\" : \"value1\" }\r\n"; - String emptyObject = "{}\r\n"; + String bulkIndexAction = """ + { "index":{"_index":"test","_id":"1"} } + """; + String bulkIndexSource = """ + { "field1" : "value1" } + """; + String emptyObject = """ + {} + """; StringBuilder bulk = new StringBuilder(); int emptyLine; if (randomBoolean()) { @@ -354,25 +366,17 @@ public void testBulkTerminatedByNewline() throws Exception { } public void testDynamicTemplates() throws Exception { - BytesArray data = new BytesArray( - "{ \"index\":{\"_index\":\"test\",\"dynamic_templates\":{\"baz\":\"t1\", \"foo.bar\":\"t2\"}}}\n" - + "{ \"field1\" : \"value1\" }\n" - + - - "{ \"delete\" : { \"_index\" : \"test\", \"_id\" : \"2\" } }\n" - + - - "{ \"create\" : {\"_index\":\"test\",\"dynamic_templates\":{\"bar\":\"t1\"}}}\n" - + "{ \"field1\" : \"value3\" }\n" - + - - "{ \"create\" : {\"dynamic_templates\":{\"foo.bar\":\"xyz\"}}}\n" - + "{ \"field1\" : \"value3\" }\n" - + - - "{ \"index\" : {\"dynamic_templates\":{}}}\n" - + "{ \"field1\" : \"value3\" }\n" - ); + BytesArray data = new BytesArray(""" + { "index":{"_index":"test","dynamic_templates":{"baz":"t1", "foo.bar":"t2"}}} + { "field1" : "value1" } + { "delete" : { "_index" : "test", "_id" : "2" } } + { "create" : {"_index":"test","dynamic_templates":{"bar":"t1"}}} + { "field1" : "value3" } + { "create" : {"dynamic_templates":{"foo.bar":"xyz"}}} + { "field1" : "value3" } + { "index" : {"dynamic_templates":{}}} + { "field1" : "value3" } + """); BulkRequest bulkRequest = new BulkRequest().add(data, null, XContentType.JSON); assertThat(bulkRequest.requests, hasSize(5)); assertThat(((IndexRequest) bulkRequest.requests.get(0)).getDynamicTemplates(), equalTo(Map.of("baz", "t1", "foo.bar", "t2"))); @@ -382,27 +386,29 @@ public void testDynamicTemplates() throws Exception { } public void testInvalidDynamicTemplates() { - BytesArray deleteWithDynamicTemplates = new BytesArray( - "{ \"delete\" : { \"_index\" : \"test\", \"_id\" : \"2\", \"dynamic_templates\":{\"baz\":\"t1\"}} }\n" - ); + BytesArray deleteWithDynamicTemplates = new BytesArray(""" + {"delete" : { "_index" : "test", "_id" : "2", "dynamic_templates":{"baz":"t1"}} } + """); IllegalArgumentException error = expectThrows( IllegalArgumentException.class, () -> new BulkRequest().add(deleteWithDynamicTemplates, null, XContentType.JSON) ); assertThat(error.getMessage(), equalTo("Delete request in line [1] does not accept dynamic_templates")); - BytesArray updateWithDynamicTemplates = new BytesArray( - "{ \"update\" : {\"dynamic_templates\":{\"foo.bar\":\"xyz\"}}}\n" + "{ \"field1\" : \"value3\" }\n" - ); + BytesArray updateWithDynamicTemplates = new BytesArray(""" + { "update" : {"dynamic_templates":{"foo.bar":"xyz"}}} + { "field1" : "value3" } + """); error = expectThrows( IllegalArgumentException.class, () -> new BulkRequest().add(updateWithDynamicTemplates, null, XContentType.JSON) ); assertThat(error.getMessage(), equalTo("Update request in line [2] does not accept dynamic_templates")); - BytesArray invalidDynamicTemplates = new BytesArray( - "{ \"index\":{\"_index\":\"test\",\"dynamic_templates\":[]}\n" + "{ \"field1\" : \"value1\" }\n" - ); + BytesArray invalidDynamicTemplates = new BytesArray(""" + { "index":{"_index":"test","dynamic_templates":[]} + { "field1" : "value1" } + """); error = expectThrows(IllegalArgumentException.class, () -> new BulkRequest().add(invalidDynamicTemplates, null, XContentType.JSON)); assertThat( error.getMessage(), @@ -413,7 +419,10 @@ public void testInvalidDynamicTemplates() { } public void testBulkActionWithoutCurlyBrace() throws Exception { - String bulkAction = "{ \"index\":{\"_index\":\"test\",\"_id\":\"1\"} \n" + "{ \"field1\" : \"value1\" }\n"; + String bulkAction = """ + { "index":{"_index":"test","_id":"1"}\s + { "field1" : "value1" } + """; BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, XContentType.JSON); @@ -424,7 +433,10 @@ public void testBulkActionWithoutCurlyBrace() throws Exception { } public void testBulkActionWithAdditionalKeys() throws Exception { - String bulkAction = "{ \"index\":{\"_index\":\"test\",\"_id\":\"1\"}, \"a\":\"b\"} \n" + "{ \"field1\" : \"value1\" }\n"; + String bulkAction = """ + { "index":{"_index":"test","_id":"1"}, "a":"b"}\s + { "field1" : "value1" } + """; BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, XContentType.JSON); @@ -435,7 +447,10 @@ public void testBulkActionWithAdditionalKeys() throws Exception { } public void testBulkActionWithTrailingData() throws Exception { - String bulkAction = "{ \"index\":{\"_index\":\"test\",\"_id\":\"1\"} } {\"a\":\"b\"} \n" + "{ \"field1\" : \"value1\" }\n"; + String bulkAction = """ + { "index":{"_index":"test","_id":"1"} } {"a":"b"}\s + { "field1" : "value1" } + """; BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, XContentType.JSON); @@ -446,7 +461,9 @@ public void testBulkActionWithTrailingData() throws Exception { } public void testUnsupportedAction() throws Exception { - String bulkAction = "{ \"get\":{\"_index\":\"test\",\"_id\":\"1\"} }\n"; + String bulkAction = """ + { "get":{"_index":"test","_id":"1"} } + """; BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, XContentType.JSON); diff --git a/server/src/test/java/org/elasticsearch/action/bulk/TransportShardBulkActionTests.java b/server/src/test/java/org/elasticsearch/action/bulk/TransportShardBulkActionTests.java index d36d8fe12be07..84c808667b243 100644 --- a/server/src/test/java/org/elasticsearch/action/bulk/TransportShardBulkActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/bulk/TransportShardBulkActionTests.java @@ -86,13 +86,20 @@ public class TransportShardBulkActionTests extends IndexShardTestCase { .build(); private IndexMetadata indexMetadata() throws IOException { - return IndexMetadata.builder("index") - .putMapping( - "{\"properties\":{\"foo\":{\"type\":\"text\",\"fields\":" + "{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}" - ) - .settings(idxSettings) - .primaryTerm(0, 1) - .build(); + return IndexMetadata.builder("index").putMapping(""" + { + "properties": { + "foo": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + }""").settings(idxSettings).primaryTerm(0, 1).build(); } public void testExecuteBulkIndexRequest() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java b/server/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java index 67db1ba07b87c..d26b1c764ddb8 100644 --- a/server/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java @@ -11,6 +11,7 @@ import org.elasticsearch.action.support.replication.ReplicationResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.index.shard.ShardId; @@ -29,26 +30,40 @@ public class DeleteResponseTests extends ESTestCase { - public void testToXContent() { + public void testToXContent() throws IOException { { DeleteResponse response = new DeleteResponse(new ShardId("index", "index_uuid", 0), "id", 3, 17, 5, true); String output = Strings.toString(response); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":5,\"result\":\"deleted\"," - + "\"_shards\":null,\"_seq_no\":3,\"_primary_term\":17}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 5, + "result": "deleted", + "_shards": null, + "_seq_no": 3, + "_primary_term": 17 + }"""), output); } { DeleteResponse response = new DeleteResponse(new ShardId("index", "index_uuid", 0), "id", -1, 0, 7, true); response.setForcedRefresh(true); response.setShardInfo(new ReplicationResponse.ShardInfo(10, 5)); String output = Strings.toString(response); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":7,\"result\":\"deleted\"," - + "\"forced_refresh\":true,\"_shards\":{\"total\":10,\"successful\":5,\"failed\":0}}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 7, + "result": "deleted", + "forced_refresh": true, + "_shards": { + "total": 10, + "successful": 5, + "failed": 0 + } + } + """), output); } } diff --git a/server/src/test/java/org/elasticsearch/action/explain/ExplainResponseTests.java b/server/src/test/java/org/elasticsearch/action/explain/ExplainResponseTests.java index 5820db6551b4c..f77e39f46ed2a 100644 --- a/server/src/test/java/org/elasticsearch/action/explain/ExplainResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/explain/ExplainResponseTests.java @@ -83,7 +83,8 @@ public void testToXContent() throws IOException { 1, -1, true, - new BytesArray("{ \"field1\" : " + "\"value1\", \"field2\":\"value2\"}"), + new BytesArray(""" + { "field1" : "value1", "field2":"value2"}"""), singletonMap("field1", new DocumentField("field1", singletonList("value1"))), null ); @@ -94,30 +95,32 @@ public void testToXContent() throws IOException { String generatedResponse = BytesReference.bytes(builder).utf8ToString().replaceAll("\\s+", ""); - String expectedResponse = ("{\n" - + " \"_index\":\"index\",\n" - + " \"_id\":\"1\",\n" - + " \"matched\":true,\n" - + " \"explanation\":{\n" - + " \"value\":1.0,\n" - + " \"description\":\"description\",\n" - + " \"details\":[]\n" - + " },\n" - + " \"get\":{\n" - + " \"_seq_no\":0," - + " \"_primary_term\":1," - + " \"found\":true,\n" - + " \"_source\":{\n" - + " \"field1\":\"value1\",\n" - + " \"field2\":\"value2\"\n" - + " },\n" - + " \"fields\":{\n" - + " \"field1\":[\n" - + " \"value1\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}").replaceAll("\\s+", ""); + String expectedResponse = (""" + { + "_index": "index", + "_id": "1", + "matched": true, + "explanation": { + "value": 1.0, + "description": "description", + "details": [] + }, + "get": { + "_seq_no": 0, + "_primary_term": 1, + "found": true, + "_source": { + "field1": "value1", + "field2": "value2" + }, + "fields": { + "field1": [ + "value1" + ] + } + } + } + """).replaceAll("\\s+", ""); assertThat(expectedResponse, equalTo(generatedResponse)); } diff --git a/server/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequestTests.java b/server/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequestTests.java index e4afc4084d2a1..566fa43a8da62 100644 --- a/server/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesRequestTests.java @@ -121,24 +121,21 @@ public void testToXContent() throws IOException { request.runtimeFields(singletonMap("day_of_week", singletonMap("type", "keyword"))); XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); String xContent = BytesReference.bytes(request.toXContent(builder, ToXContent.EMPTY_PARAMS)).utf8ToString(); - assertEquals( - ("{" - + " \"index_filter\": {\n" - + " \"term\": {\n" - + " \"field\": {\n" - + " \"value\": \"value\",\n" - + " \"boost\": 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"runtime_mappings\": {\n" - + " \"day_of_week\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + "}").replaceAll("\\s+", ""), - xContent - ); + assertEquals((""" + { "index_filter": { + "term": { + "field": { + "value": "value", + "boost": 1.0 + } + } + }, + "runtime_mappings": { + "day_of_week": { + "type": "keyword" + } + } + }""").replaceAll("\\s+", ""), xContent); } public void testValidation() { diff --git a/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java b/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java index 7a1248dd0262b..0e44a1721650a 100644 --- a/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java @@ -108,49 +108,54 @@ public void testToXContent() throws IOException { response.toXContent(builder, ToXContent.EMPTY_PARAMS); String generatedResponse = BytesReference.bytes(builder).utf8ToString(); - assertEquals( - ("{" - + " \"indices\": [\"index1\",\"index2\",\"index3\",\"index4\"]," - + " \"fields\": {" - + " \"rating\": { " - + " \"keyword\": {" - + " \"type\": \"keyword\"," - + " \"metadata_field\": false," - + " \"searchable\": false," - + " \"aggregatable\": true," - + " \"time_series_dimension\": true," - + " \"indices\": [\"index3\", \"index4\"]," - + " \"non_searchable_indices\": [\"index4\"] " - + " }," - + " \"long\": {" - + " \"type\": \"long\"," - + " \"metadata_field\": false," - + " \"searchable\": true," - + " \"aggregatable\": false," - + " \"time_series_metric\": \"counter\"," - + " \"indices\": [\"index1\", \"index2\"]," - + " \"non_aggregatable_indices\": [\"index1\"]," - + " \"non_dimension_indices\":[\"index4\"] " - + " }" - + " }," - + " \"title\": { " - + " \"text\": {" - + " \"type\": \"text\"," - + " \"metadata_field\": false," - + " \"searchable\": true," - + " \"aggregatable\": false" - + " }" - + " }" - + " }," - + " \"failed_indices\":2," - + " \"failures\":[" - + " { \"indices\": [\"errorindex\", \"errorindex2\"]," - + " \"failure\" : {\"error\":{\"root_cause\":[{\"type\":\"illegal_argument_exception\"," - + " \"reason\":\"test\"}],\"type\":\"illegal_argument_exception\",\"reason\":\"test\"}}}" - + " ]" - + "}").replaceAll("\\s+", ""), - generatedResponse - ); + assertEquals(""" + { + "indices": [ "index1", "index2", "index3", "index4" ], + "fields": { + "rating": { + "keyword": { + "type": "keyword", + "metadata_field": false, + "searchable": false, + "aggregatable": true, + "time_series_dimension": true, + "indices": [ "index3", "index4" ], + "non_searchable_indices": [ "index4" ] + }, + "long": { + "type": "long", + "metadata_field": false, + "searchable": true, + "aggregatable": false, + "time_series_metric": "counter", + "indices": [ "index1", "index2" ], + "non_aggregatable_indices": [ "index1" ], + "non_dimension_indices": [ "index4" ] + } + }, + "title": { + "text": { + "type": "text", + "metadata_field": false, + "searchable": true, + "aggregatable": false + } + } + }, + "failed_indices": 2, + "failures": [ + { + "indices": [ "errorindex", "errorindex2" ], + "failure": { + "error": { + "root_cause": [ { "type": "illegal_argument_exception", "reason": "test" } ], + "type": "illegal_argument_exception", + "reason": "test" + } + } + } + ] + }""".replaceAll("\\s+", ""), generatedResponse); } private static FieldCapabilitiesResponse createSimpleResponse() { diff --git a/server/src/test/java/org/elasticsearch/action/get/GetResponseTests.java b/server/src/test/java/org/elasticsearch/action/get/GetResponseTests.java index b3bb6c7c764cc..ab72bf17beca9 100644 --- a/server/src/test/java/org/elasticsearch/action/get/GetResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/get/GetResponseTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.document.DocumentField; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.test.ESTestCase; @@ -79,7 +80,7 @@ private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws assertEquals(expectedGetResponse.getSourceAsString(), parsedGetResponse.getSourceAsString()); } - public void testToXContent() { + public void testToXContent() throws IOException { { GetResponse getResponse = new GetResponse( new GetResult( @@ -89,26 +90,39 @@ public void testToXContent() { 1, 1, true, - new BytesArray("{ \"field1\" : " + "\"value1\", \"field2\":\"value2\"}"), + new BytesArray(""" + { "field1" : "value1", "field2":"value2"}"""), Collections.singletonMap("field1", new DocumentField("field1", Collections.singletonList("value1"))), null ) ); String output = Strings.toString(getResponse); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":1,\"_seq_no\":0,\"_primary_term\":1," - + "\"found\":true,\"_source\":{ \"field1\" : \"value1\", \"field2\":\"value2\"},\"fields\":{\"field1\":[\"value1\"]}}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 1, + "_seq_no": 0, + "_primary_term": 1, + "found": true, + "_source": { + "field1": "value1", + "field2": "value2" + }, + "fields": { + "field1": [ "value1" ] + } + }"""), XContentHelper.stripWhitespace(output)); } { GetResponse getResponse = new GetResponse(new GetResult("index", "id", UNASSIGNED_SEQ_NO, 0, 1, false, null, null, null)); String output = Strings.toString(getResponse); - assertEquals("{\"_index\":\"index\",\"_id\":\"id\",\"found\":false}", output); + assertEquals(""" + {"_index":"index","_id":"id","found":false}""", output); } } - public void testToString() { + public void testToString() throws IOException { GetResponse getResponse = new GetResponse( new GetResult( "index", @@ -117,16 +131,28 @@ public void testToString() { 1, 1, true, - new BytesArray("{ \"field1\" : " + "\"value1\", \"field2\":\"value2\"}"), + new BytesArray(""" + { "field1" : "value1", "field2":"value2"}"""), Collections.singletonMap("field1", new DocumentField("field1", Collections.singletonList("value1"))), null ) ); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":1,\"_seq_no\":0,\"_primary_term\":1," - + "\"found\":true,\"_source\":{ \"field1\" : \"value1\", \"field2\":\"value2\"},\"fields\":{\"field1\":[\"value1\"]}}", - getResponse.toString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 1, + "_seq_no": 0, + "_primary_term": 1, + "found": true, + "_source": { + "field1": "value1", + "field2": "value2" + }, + "fields": { + "field1": [ "value1" ] + } + }"""), XContentHelper.stripWhitespace(getResponse.toString())); } public void testEqualsAndHashcode() { diff --git a/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java b/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java index 471c81936a771..feceaaf700a69 100644 --- a/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/index/IndexRequestTests.java @@ -139,23 +139,19 @@ public void testIndexResponse() { assertEquals(successful, indexResponse.getShardInfo().getSuccessful()); assertEquals(forcedRefresh, indexResponse.forcedRefresh()); assertEquals( - "IndexResponse[index=" - + shardId.getIndexName() - + ",id=" - + id - + ",version=" - + version - + ",result=" - + (created ? "created" : "updated") - + ",seqNo=" - + SequenceNumbers.UNASSIGNED_SEQ_NO - + ",primaryTerm=" - + 0 - + ",shards={\"total\":" - + total - + ",\"successful\":" - + successful - + ",\"failed\":0}]", + """ + IndexResponse[index=%s,id=%s,version=%s,result=%s,seqNo=%s,primaryTerm=%s,shards=\ + {"total":%s,"successful":%s,"failed":0}]\ + """.formatted( + shardId.getIndexName(), + id, + version, + created ? "created" : "updated", + SequenceNumbers.UNASSIGNED_SEQ_NO, + 0, + total, + successful + ), indexResponse.toString() ); } @@ -253,7 +249,9 @@ public void testToStringSizeLimit() throws UnsupportedEncodingException { request.source(source, XContentType.JSON); assertEquals("index {[index][null], source[" + source + "]}", request.toString()); - source = "{\"name\":\"" + randomUnicodeOfLength(IndexRequest.MAX_SOURCE_LENGTH_IN_TOSTRING) + "\"}"; + source = """ + {"name":"%s"} + """.formatted(randomUnicodeOfLength(IndexRequest.MAX_SOURCE_LENGTH_IN_TOSTRING)); request.source(source, XContentType.JSON); int actualBytes = source.getBytes("UTF-8").length; assertEquals( diff --git a/server/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java b/server/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java index 2ab33d7042903..f251fa55b03de 100644 --- a/server/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/index/IndexResponseTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.action.support.replication.ReplicationResponse; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.seqno.SequenceNumbers; import org.elasticsearch.index.shard.ShardId; @@ -30,26 +31,39 @@ public class IndexResponseTests extends ESTestCase { - public void testToXContent() { + public void testToXContent() throws IOException { { IndexResponse indexResponse = new IndexResponse(new ShardId("index", "index_uuid", 0), "id", 3, 17, 5, true); String output = Strings.toString(indexResponse); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":5,\"result\":\"created\",\"_shards\":null," - + "\"_seq_no\":3,\"_primary_term\":17}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 5, + "result": "created", + "_shards": null, + "_seq_no": 3, + "_primary_term": 17 + }"""), output); } { IndexResponse indexResponse = new IndexResponse(new ShardId("index", "index_uuid", 0), "id", -1, 17, 7, true); indexResponse.setForcedRefresh(true); indexResponse.setShardInfo(new ReplicationResponse.ShardInfo(10, 5)); String output = Strings.toString(indexResponse); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":7,\"result\":\"created\"," - + "\"forced_refresh\":true,\"_shards\":{\"total\":10,\"successful\":5,\"failed\":0}}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 7, + "result": "created", + "forced_refresh": true, + "_shards": { + "total": 10, + "successful": 5, + "failed": 0 + } + }"""), output); } } diff --git a/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java b/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java index bfd44cd77a124..4a50968880804 100644 --- a/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/main/MainResponseTests.java @@ -13,6 +13,7 @@ import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.AbstractSerializingTestCase; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xcontent.ToXContent; @@ -69,42 +70,38 @@ public void testToXContent() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); assertEquals( - "{" - + "\"name\":\"nodeName\"," - + "\"cluster_name\":\"clusterName\"," - + "\"cluster_uuid\":\"" - + clusterUUID - + "\"," - + "\"version\":{" - + "\"number\":\"" - + build.getQualifiedVersion() - + "\"," - + "\"build_flavor\":\"" - + current.flavor().displayName() - + "\"," - + "\"build_type\":\"" - + current.type().displayName() - + "\"," - + "\"build_hash\":\"" - + current.hash() - + "\"," - + "\"build_date\":\"" - + current.date() - + "\"," - + "\"build_snapshot\":" - + current.isSnapshot() - + "," - + "\"lucene_version\":\"" - + version.luceneVersion.toString() - + "\"," - + "\"minimum_wire_compatibility_version\":\"" - + version.minimumCompatibilityVersion().toString() - + "\"," - + "\"minimum_index_compatibility_version\":\"" - + version.minimumIndexCompatibilityVersion().toString() - + "\"}," - + "\"tagline\":\"You Know, for Search\"" - + "}", + XContentHelper.stripWhitespace( + """ + { + "name": "nodeName", + "cluster_name": "clusterName", + "cluster_uuid": "%s", + "version": { + "number": "%s", + "build_flavor": "%s", + "build_type": "%s", + "build_hash": "%s", + "build_date": "%s", + "build_snapshot": %s, + "lucene_version": "%s", + "minimum_wire_compatibility_version": "%s", + "minimum_index_compatibility_version": "%s" + }, + "tagline": "You Know, for Search" + } + """.formatted( + clusterUUID, + build.getQualifiedVersion(), + current.flavor().displayName(), + current.type().displayName(), + current.hash(), + current.date(), + current.isSnapshot(), + version.luceneVersion.toString(), + version.minimumCompatibilityVersion().toString(), + version.minimumIndexCompatibilityVersion().toString() + ) + ), Strings.toString(builder) ); } diff --git a/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java b/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java index b789755fb5c5f..c53cc19cfdbdd 100644 --- a/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/MultiSearchRequestTests.java @@ -84,8 +84,10 @@ public void testSimpleAdd() throws Exception { } public void testFailWithUnknownKey() { - final String requestContent = "{\"index\":\"test\", \"ignore_unavailable\" : true, \"unknown_key\" : \"open,closed\"}}\r\n" - + "{\"query\" : {\"match_all\" :{}}}\r\n"; + final String requestContent = """ + {"index":"test", "ignore_unavailable" : true, "unknown_key" : "open,closed"}} + {"query" : {"match_all" :{}}} + """; FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent( new BytesArray(requestContent), XContentType.JSON @@ -98,8 +100,10 @@ public void testFailWithUnknownKey() { } public void testSimpleAddWithCarriageReturn() throws Exception { - final String requestContent = "{\"index\":\"test\", \"ignore_unavailable\" : true, \"expand_wildcards\" : \"open,closed\"}}\r\n" - + "{\"query\" : {\"match_all\" :{}}}\r\n"; + final String requestContent = """ + {"index":"test", "ignore_unavailable" : true, "expand_wildcards" : "open,closed"}} + {"query" : {"match_all" :{}}} + """; FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent( new BytesArray(requestContent), XContentType.JSON @@ -114,8 +118,10 @@ public void testSimpleAddWithCarriageReturn() throws Exception { } public void testDefaultIndicesOptions() throws IOException { - final String requestContent = "{\"index\":\"test\", \"expand_wildcards\" : \"open,closed\"}}\r\n" - + "{\"query\" : {\"match_all\" :{}}}\r\n"; + final String requestContent = """ + {"index":"test", "expand_wildcards" : "open,closed"}} + {"query" : {"match_all" :{}}} + """; FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent( new BytesArray(requestContent), XContentType.JSON @@ -178,7 +184,7 @@ public void testNoMetadata() throws Exception { } } - public void testResponseErrorToXContent() { + public void testResponseErrorToXContent() throws IOException { long tookInMillis = randomIntBetween(1, 1000); MultiSearchResponse response = new MultiSearchResponse( new MultiSearchResponse.Item[] { @@ -187,21 +193,28 @@ public void testResponseErrorToXContent() { tookInMillis ); - assertEquals( - "{\"took\":" - + tookInMillis - + ",\"responses\":[" - + "{" - + "\"error\":{\"root_cause\":[{\"type\":\"illegal_state_exception\",\"reason\":\"foobar\"}]," - + "\"type\":\"illegal_state_exception\",\"reason\":\"foobar\"},\"status\":500" - + "}," - + "{" - + "\"error\":{\"root_cause\":[{\"type\":\"illegal_state_exception\",\"reason\":\"baaaaaazzzz\"}]," - + "\"type\":\"illegal_state_exception\",\"reason\":\"baaaaaazzzz\"},\"status\":500" - + "}" - + "]}", - Strings.toString(response) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "took": %s, + "responses": [ + { + "error": { + "root_cause": [ { "type": "illegal_state_exception", "reason": "foobar" } ], + "type": "illegal_state_exception", + "reason": "foobar" + }, + "status": 500 + }, + { + "error": { + "root_cause": [ { "type": "illegal_state_exception", "reason": "baaaaaazzzz" } ], + "type": "illegal_state_exception", + "reason": "baaaaaazzzz" + }, + "status": 500 + } + ] + }""".formatted(tookInMillis)), Strings.toString(response)); } public void testMaxConcurrentSearchRequests() { @@ -428,18 +441,17 @@ public void testWritingExpandWildcards() throws IOException { } public void testEmptyFirstLine1() throws Exception { - MultiSearchRequest request = parseMultiSearchRequestFromString( - "\n" - + "\n" - + "{ \"query\": {\"match_all\": {}}}\n" - + "{}\n" - + "{ \"query\": {\"match_all\": {}}}\n" - + "\n" - + "{ \"query\": {\"match_all\": {}}}\n" - + "{}\n" - + "{ \"query\": {\"match_all\": {}}}\n", - RestApiVersion.V_7 - ); + MultiSearchRequest request = parseMultiSearchRequestFromString(""" + + + { "query": {"match_all": {}}} + {} + { "query": {"match_all": {}}} + + { "query": {"match_all": {}}} + {} + { "query": {"match_all": {}}} + """, RestApiVersion.V_7); assertThat(request.requests().size(), equalTo(4)); for (SearchRequest searchRequest : request.requests()) { assertThat(searchRequest.indices().length, equalTo(0)); @@ -452,18 +464,17 @@ public void testEmptyFirstLine1() throws Exception { } public void testEmptyFirstLine2() throws Exception { - MultiSearchRequest request = parseMultiSearchRequestFromString( - "\n" - + "{}\n" - + "{ \"query\": {\"match_all\": {}}}\n" - + "\n" - + "{ \"query\": {\"match_all\": {}}}\n" - + "{}\n" - + "{ \"query\": {\"match_all\": {}}}\n" - + "\n" - + "{ \"query\": {\"match_all\": {}}}\n", - RestApiVersion.V_7 - ); + MultiSearchRequest request = parseMultiSearchRequestFromString(""" + + {} + { "query": {"match_all": {}}} + + { "query": {"match_all": {}}} + {} + { "query": {"match_all": {}}} + + { "query": {"match_all": {}}} + """, RestApiVersion.V_7); assertThat(request.requests().size(), equalTo(4)); for (SearchRequest searchRequest : request.requests()) { assertThat(searchRequest.indices().length, equalTo(0)); 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 2666820406853..0e2365beb0c9c 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java @@ -53,40 +53,39 @@ public void testToXContent() throws IOException { ); // Failures are grouped (by default) - final String expectedJson = XContentHelper.stripWhitespace( - "{" - + " \"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)); + final String expectedJson = XContentHelper.stripWhitespace(""" + { + "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(XContentHelper.stripWhitespace(expectedJson), Strings.toString(exception)); } public void testToAndFromXContent() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchResponseTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchResponseTests.java index 012b383ede640..f04e55c07175c 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchResponseTests.java @@ -215,7 +215,7 @@ public void testFromXContentWithFailures() throws IOException { } } - public void testToXContent() { + public void testToXContent() throws IOException { SearchHit hit = new SearchHit(1, "id1", Collections.emptyMap(), Collections.emptyMap()); hit.score(2.0f); SearchHit[] hits = new SearchHit[] { hit }; @@ -238,27 +238,26 @@ public void testToXContent() { ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY ); - StringBuilder expectedString = new StringBuilder(); - expectedString.append("{"); - { - expectedString.append("\"took\":0,"); - expectedString.append("\"timed_out\":false,"); - expectedString.append("\"_shards\":"); + String expectedString = XContentHelper.stripWhitespace(""" { - expectedString.append("{\"total\":0,"); - expectedString.append("\"successful\":0,"); - expectedString.append("\"skipped\":0,"); - expectedString.append("\"failed\":0},"); - } - expectedString.append("\"hits\":"); - { - expectedString.append("{\"total\":{\"value\":100,\"relation\":\"eq\"},"); - expectedString.append("\"max_score\":1.5,"); - expectedString.append("\"hits\":[{\"_id\":\"id1\",\"_score\":2.0}]}"); - } - } - expectedString.append("}"); - assertEquals(expectedString.toString(), Strings.toString(response)); + "took": 0, + "timed_out": false, + "_shards": { + "total": 0, + "successful": 0, + "skipped": 0, + "failed": 0 + }, + "hits": { + "total": { + "value": 100, + "relation": "eq" + }, + "max_score": 1.5, + "hits": [ { "_id": "id1", "_score": 2.0 } ] + } + }"""); + assertEquals(expectedString, Strings.toString(response)); } { SearchResponse response = new SearchResponse( @@ -279,33 +278,31 @@ public void testToXContent() { ShardSearchFailure.EMPTY_ARRAY, new SearchResponse.Clusters(5, 3, 2) ); - StringBuilder expectedString = new StringBuilder(); - expectedString.append("{"); - { - expectedString.append("\"took\":0,"); - expectedString.append("\"timed_out\":false,"); - expectedString.append("\"_shards\":"); - { - expectedString.append("{\"total\":0,"); - expectedString.append("\"successful\":0,"); - expectedString.append("\"skipped\":0,"); - expectedString.append("\"failed\":0},"); - } - expectedString.append("\"_clusters\":"); + String expectedString = XContentHelper.stripWhitespace(""" { - expectedString.append("{\"total\":5,"); - expectedString.append("\"successful\":3,"); - expectedString.append("\"skipped\":2},"); - } - expectedString.append("\"hits\":"); - { - expectedString.append("{\"total\":{\"value\":100,\"relation\":\"eq\"},"); - expectedString.append("\"max_score\":1.5,"); - expectedString.append("\"hits\":[{\"_id\":\"id1\",\"_score\":2.0}]}"); - } - } - expectedString.append("}"); - assertEquals(expectedString.toString(), Strings.toString(response)); + "took": 0, + "timed_out": false, + "_shards": { + "total": 0, + "successful": 0, + "skipped": 0, + "failed": 0 + }, + "_clusters": { + "total": 5, + "successful": 3, + "skipped": 2 + }, + "hits": { + "total": { + "value": 100, + "relation": "eq" + }, + "max_score": 1.5, + "hits": [ { "_id": "id1", "_score": 2.0 } ] + } + }"""); + assertEquals(expectedString, Strings.toString(response)); } } diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchScrollRequestTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchScrollRequestTests.java index dc759eb6e6fe6..73c8b35706b3e 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchScrollRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchScrollRequestTests.java @@ -96,7 +96,8 @@ public void testToXContent() throws IOException { searchScrollRequest.scroll("1m"); try (XContentBuilder builder = JsonXContent.contentBuilder()) { searchScrollRequest.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"scroll_id\":\"SCROLL_ID\",\"scroll\":\"1m\"}", Strings.toString(builder)); + assertEquals(""" + {"scroll_id":"SCROLL_ID","scroll":"1m"}""", Strings.toString(builder)); } } diff --git a/server/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java b/server/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java index d706b361bb5c7..2fab9b5dae8b6 100644 --- a/server/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/ShardSearchFailureTests.java @@ -11,6 +11,7 @@ import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchShardTarget; @@ -95,19 +96,18 @@ public void testToXContent() throws IOException { new SearchShardTarget("nodeId", new ShardId(new Index("indexName", "indexUuid"), 123), null) ); BytesReference xContent = toXContent(failure, XContentType.JSON, randomBoolean()); - assertEquals( - "{\"shard\":123," - + "\"index\":\"indexName\"," - + "\"node\":\"nodeId\"," - + "\"reason\":{" - + "\"type\":\"parsing_exception\"," - + "\"reason\":\"some message\"," - + "\"line\":0," - + "\"col\":0" - + "}" - + "}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "shard": 123, + "index": "indexName", + "node": "nodeId", + "reason": { + "type": "parsing_exception", + "reason": "some message", + "line": 0, + "col": 0 + } + }"""), xContent.utf8ToString()); } public void testToXContentWithClusterAlias() throws IOException { @@ -116,19 +116,18 @@ public void testToXContentWithClusterAlias() throws IOException { new SearchShardTarget("nodeId", new ShardId(new Index("indexName", "indexUuid"), 123), "cluster1") ); BytesReference xContent = toXContent(failure, XContentType.JSON, randomBoolean()); - assertEquals( - "{\"shard\":123," - + "\"index\":\"cluster1:indexName\"," - + "\"node\":\"nodeId\"," - + "\"reason\":{" - + "\"type\":\"parsing_exception\"," - + "\"reason\":\"some message\"," - + "\"line\":0," - + "\"col\":0" - + "}" - + "}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "shard": 123, + "index": "cluster1:indexName", + "node": "nodeId", + "reason": { + "type": "parsing_exception", + "reason": "some message", + "line": 0, + "col": 0 + } + }"""), xContent.utf8ToString()); } public void testSerialization() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/action/support/DefaultShardOperationFailedExceptionTests.java b/server/src/test/java/org/elasticsearch/action/support/DefaultShardOperationFailedExceptionTests.java index 27d889336f5e8..aa0b6b0de9251 100644 --- a/server/src/test/java/org/elasticsearch/action/support/DefaultShardOperationFailedExceptionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/DefaultShardOperationFailedExceptionTests.java @@ -20,6 +20,7 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.RestStatus; @@ -71,8 +72,8 @@ public void testToXContent() throws IOException { { DefaultShardOperationFailedException exception = new DefaultShardOperationFailedException(new ElasticsearchException("foo")); assertEquals( - "{\"shard\":-1,\"index\":null,\"status\":\"INTERNAL_SERVER_ERROR\"," - + "\"reason\":{\"type\":\"exception\",\"reason\":\"foo\"}}", + """ + {"shard":-1,"index":null,"status":"INTERNAL_SERVER_ERROR","reason":{"type":"exception","reason":"foo"}}""", Strings.toString(exception) ); } @@ -80,21 +81,35 @@ public void testToXContent() throws IOException { DefaultShardOperationFailedException exception = new DefaultShardOperationFailedException( new ElasticsearchException("foo", new IllegalArgumentException("bar")) ); - assertEquals( - "{\"shard\":-1,\"index\":null,\"status\":\"INTERNAL_SERVER_ERROR\",\"reason\":{\"type\":\"exception\"," - + "\"reason\":\"foo\",\"caused_by\":{\"type\":\"illegal_argument_exception\",\"reason\":\"bar\"}}}", - Strings.toString(exception) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "shard": -1, + "index": null, + "status": "INTERNAL_SERVER_ERROR", + "reason": { + "type": "exception", + "reason": "foo", + "caused_by": { + "type": "illegal_argument_exception", + "reason": "bar" + } + } + }"""), Strings.toString(exception)); } { DefaultShardOperationFailedException exception = new DefaultShardOperationFailedException( new BroadcastShardOperationFailedException(new ShardId("test", "_uuid", 2), "foo", new IllegalStateException("bar")) ); - assertEquals( - "{\"shard\":2,\"index\":\"test\",\"status\":\"INTERNAL_SERVER_ERROR\"," - + "\"reason\":{\"type\":\"illegal_state_exception\",\"reason\":\"bar\"}}", - Strings.toString(exception) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "shard": 2, + "index": "test", + "status": "INTERNAL_SERVER_ERROR", + "reason": { + "type": "illegal_state_exception", + "reason": "bar" + } + }"""), Strings.toString(exception)); } { DefaultShardOperationFailedException exception = new DefaultShardOperationFailedException( @@ -102,11 +117,16 @@ public void testToXContent() throws IOException { 1, new IllegalArgumentException("foo") ); - assertEquals( - "{\"shard\":1,\"index\":\"test\",\"status\":\"BAD_REQUEST\"," - + "\"reason\":{\"type\":\"illegal_argument_exception\",\"reason\":\"foo\"}}", - Strings.toString(exception) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "shard": 1, + "index": "test", + "status": "BAD_REQUEST", + "reason": { + "type": "illegal_argument_exception", + "reason": "foo" + } + }"""), Strings.toString(exception)); } } diff --git a/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java b/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java index 507c2fdb2abaf..0babb61fdbe9f 100644 --- a/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/replication/ReplicationResponseTests.java @@ -14,6 +14,7 @@ import org.elasticsearch.common.breaker.CircuitBreaker; import org.elasticsearch.common.breaker.CircuitBreakingException; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.RestStatus; @@ -65,14 +66,39 @@ public void testShardInfoToXContent() throws IOException { ) ); String output = Strings.toString(shardInfo); - assertEquals( - "{\"total\":6,\"successful\":4,\"failed\":2,\"failures\":[{\"_index\":\"index\",\"_shard\":3," - + "\"_node\":\"_node_id\",\"reason\":{\"type\":\"illegal_argument_exception\",\"reason\":\"Wrong\"}," - + "\"status\":\"BAD_REQUEST\",\"primary\":false},{\"_index\":\"index\",\"_shard\":1,\"_node\":\"_node_id\"," - + "\"reason\":{\"type\":\"circuit_breaking_exception\",\"reason\":\"Wrong\",\"bytes_wanted\":12,\"bytes_limit\":21" - + ",\"durability\":\"PERMANENT\"},\"status\":\"NOT_ACCEPTABLE\",\"primary\":true}]}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "total": 6, + "successful": 4, + "failed": 2, + "failures": [ + { + "_index": "index", + "_shard": 3, + "_node": "_node_id", + "reason": { + "type": "illegal_argument_exception", + "reason": "Wrong" + }, + "status": "BAD_REQUEST", + "primary": false + }, + { + "_index": "index", + "_shard": 1, + "_node": "_node_id", + "reason": { + "type": "circuit_breaking_exception", + "reason": "Wrong", + "bytes_wanted": 12, + "bytes_limit": 21, + "durability": "PERMANENT" + }, + "status": "NOT_ACCEPTABLE", + "primary": true + } + ] + }"""), output); } } diff --git a/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationAllPermitsAcquisitionTests.java b/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationAllPermitsAcquisitionTests.java index 2ac73a578d9a5..16692dbd3ca64 100644 --- a/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationAllPermitsAcquisitionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationAllPermitsAcquisitionTests.java @@ -146,7 +146,8 @@ public void setUp() throws Exception { IndexMetadata indexMetadata = IndexMetadata.builder(shardId.getIndexName()) .settings(indexSettings) .primaryTerm(shardId.id(), primary.getOperationPrimaryTerm()) - .putMapping("{ \"properties\": { \"value\": { \"type\": \"short\"}}}") + .putMapping(""" + { "properties": { "value": { "type": "short"}}}""") .build(); state.metadata(Metadata.builder().put(indexMetadata, false).generateClusterUuidIfNeeded()); diff --git a/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsTests.java b/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsTests.java index 63cdbd432575c..75c72b045bc67 100644 --- a/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsTests.java +++ b/server/src/test/java/org/elasticsearch/action/termvectors/GetTermVectorsTests.java @@ -191,12 +191,12 @@ public void testRandomPayloadWithDelimitedPayloadTokenFilter() throws IOExceptio for (int k = 0; k < docsAndPositions.freq(); k++) { docsAndPositions.nextPosition(); if (docsAndPositions.getPayload() != null) { - String infoString = "\nterm: " - + term - + " has payload \n" - + docsAndPositions.getPayload().toString() - + "\n but should have payload \n" - + curPayloads.get(k).toString(); + String infoString = """ + + term: %s has payload\s + %s + but should have payload\s + %s""".formatted(term, docsAndPositions.getPayload().toString(), curPayloads.get(k).toString()); assertThat(infoString, docsAndPositions.getPayload(), equalTo(curPayloads.get(k))); } else { String infoString = "\nterm: " + term + " has no payload but should have payload \n" + curPayloads.get(k).toString(); diff --git a/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java b/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java index e5dd6416868c1..503c2925cf854 100644 --- a/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java +++ b/server/src/test/java/org/elasticsearch/action/termvectors/TermVectorsUnitTests.java @@ -161,9 +161,8 @@ private void checkIfStandardTermVector(TermVectorsResponse inResponse) throws IO } public void testRestRequestParsing() throws Exception { - BytesReference inputBytes = new BytesArray( - " {\"fields\" : [\"a\", \"b\",\"c\"], \"offsets\":false, \"positions\":false, \"payloads\":true}" - ); + BytesReference inputBytes = new BytesArray(""" + {"fields" : ["a", "b","c"], "offsets":false, "positions":false, "payloads":true}"""); TermVectorsRequest tvr = new TermVectorsRequest(null, null); XContentParser parser = createParser(JsonXContent.jsonXContent, inputBytes); @@ -199,9 +198,8 @@ public void testRestRequestParsing() throws Exception { } public void testRequestParsingThrowsException() throws Exception { - BytesReference inputBytes = new BytesArray( - " {\"fields\" : \"a, b,c \", \"offsets\":false, \"positions\":false, \"payloads\":true, \"meaningless_term\":2}" - ); + BytesReference inputBytes = new BytesArray(""" + {"fields" : "a, b,c ", "offsets":false, "positions":false, "payloads":true, "meaningless_term":2}"""); TermVectorsRequest tvr = new TermVectorsRequest(null, null); boolean threwException = false; try { diff --git a/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java b/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java index 4634f892c12dd..95c02ecc25dbc 100644 --- a/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java @@ -621,23 +621,14 @@ public void testUpdateScript() throws Exception { public void testToString() throws IOException { UpdateRequest request = new UpdateRequest("test", "1").script(mockInlineScript("ctx._source.body = \"foo\"")); - assertThat( - request.toString(), - equalTo( - "update {[test][1], doc_as_upsert[false], " - + "script[Script{type=inline, lang='mock', idOrCode='ctx._source.body = \"foo\"', options={}, params={}}], " - + "scripted_upsert[false], detect_noop[true]}" - ) - ); + assertThat(request.toString(), equalTo(""" + update {[test][1], doc_as_upsert[false], script[Script{type=inline, lang='mock', idOrCode='ctx._source.body = "foo"', \ + options={}, params={}}], scripted_upsert[false], detect_noop[true]}""")); request = new UpdateRequest("test", "1").fromXContent( createParser(JsonXContent.jsonXContent, new BytesArray("{\"doc\": {\"body\": \"bar\"}}")) ); - assertThat( - request.toString(), - equalTo( - "update {[test][1], doc_as_upsert[false], " - + "doc[index {[null][null], source[{\"body\":\"bar\"}]}], scripted_upsert[false], detect_noop[true]}" - ) - ); + assertThat(request.toString(), equalTo(""" + update {[test][1], doc_as_upsert[false], doc[index {[null][null], source[{"body":"bar"}]}], \ + scripted_upsert[false], detect_noop[true]}""")); } } diff --git a/server/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java b/server/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java index 7a7fb5f6ddaff..f8875e41e3e3d 100644 --- a/server/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/update/UpdateResponseTests.java @@ -15,6 +15,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.document.DocumentField; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.get.GetResultTests; @@ -46,11 +47,18 @@ public void testToXContent() throws IOException { { UpdateResponse updateResponse = new UpdateResponse(new ShardId("index", "index_uuid", 0), "id", -2, 0, 0, NOT_FOUND); String output = Strings.toString(updateResponse); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":0,\"result\":\"not_found\"," - + "\"_shards\":{\"total\":0,\"successful\":0,\"failed\":0}}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 0, + "result": "not_found", + "_shards": { + "total": 0, + "successful": 0, + "failed": 0 + } + }"""), output); } { UpdateResponse updateResponse = new UpdateResponse( @@ -63,14 +71,24 @@ public void testToXContent() throws IOException { DELETED ); String output = Strings.toString(updateResponse); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":1,\"result\":\"deleted\"," - + "\"_shards\":{\"total\":10,\"successful\":6,\"failed\":0},\"_seq_no\":3,\"_primary_term\":17}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 1, + "result": "deleted", + "_shards": { + "total": 10, + "successful": 6, + "failed": 0 + }, + "_seq_no": 3, + "_primary_term": 17 + }"""), output); } { - BytesReference source = new BytesArray("{\"title\":\"Book title\",\"isbn\":\"ABC-123\"}"); + BytesReference source = new BytesArray(""" + {"title":"Book title","isbn":"ABC-123"}"""); Map fields = new HashMap<>(); fields.put("title", new DocumentField("title", Collections.singletonList("Book title"))); fields.put("isbn", new DocumentField("isbn", Collections.singletonList("ABC-123"))); @@ -87,14 +105,33 @@ public void testToXContent() throws IOException { updateResponse.setGetResult(new GetResult("books", "1", 0, 1, 2, true, source, fields, null)); String output = Strings.toString(updateResponse); - assertEquals( - "{\"_index\":\"books\",\"_id\":\"1\",\"_version\":2,\"result\":\"updated\"," - + "\"_shards\":{\"total\":3,\"successful\":2,\"failed\":0},\"_seq_no\":7,\"_primary_term\":17,\"get\":{" - + "\"_seq_no\":0,\"_primary_term\":1,\"found\":true," - + "\"_source\":{\"title\":\"Book title\",\"isbn\":\"ABC-123\"},\"fields\":{\"isbn\":[\"ABC-123\"],\"title\":[\"Book " - + "title\"]}}}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "books", + "_id": "1", + "_version": 2, + "result": "updated", + "_shards": { + "total": 3, + "successful": 2, + "failed": 0 + }, + "_seq_no": 7, + "_primary_term": 17, + "get": { + "_seq_no": 0, + "_primary_term": 1, + "found": true, + "_source": { + "title": "Book title", + "isbn": "ABC-123" + }, + "fields": { + "isbn": [ "ABC-123" ], + "title": [ "Book title" ] + } + } + }"""), output); } } diff --git a/server/src/test/java/org/elasticsearch/cluster/ClusterStateTests.java b/server/src/test/java/org/elasticsearch/cluster/ClusterStateTests.java index 809ca6dc6360c..07822e893d6d5 100644 --- a/server/src/test/java/org/elasticsearch/cluster/ClusterStateTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/ClusterStateTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.RestStatus; @@ -112,210 +113,198 @@ public void testToXContent() throws IOException { String ephemeralId = clusterState.getNodes().get("nodeId1").getEphemeralId(); String allocationId = index.getShards().get(1).getAllAllocationIds().iterator().next(); - XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); + XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); clusterState.toXContent(builder, new ToXContent.MapParams(singletonMap(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_API))); builder.endObject(); - assertEquals( - "{\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"version\" : 0,\n" - + " \"state_uuid\" : \"stateUUID\",\n" - + " \"master_node\" : \"masterNodeId\",\n" - + " \"blocks\" : {\n" - + " \"global\" : {\n" - + " \"1\" : {\n" - + " \"description\" : \"description\",\n" - + " \"retryable\" : true,\n" - + " \"disable_state_persistence\" : true,\n" - + " \"levels\" : [\n" - + " \"read\",\n" - + " \"write\",\n" - + " \"metadata_read\",\n" - + " \"metadata_write\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"2\" : {\n" - + " \"description\" : \"description2\",\n" - + " \"retryable\" : false,\n" - + " \"levels\" : [\n" - + " \"read\",\n" - + " \"write\",\n" - + " \"metadata_read\",\n" - + " \"metadata_write\"\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"nodes\" : {\n" - + " \"nodeId1\" : {\n" - + " \"name\" : \"\",\n" - + " \"ephemeral_id\" : \"" - + ephemeralId - + "\",\n" - + " \"transport_address\" : \"127.0.0.1:111\",\n" - + " \"attributes\" : { },\n" - + " \"roles\" : [\n" - + " \"data\",\n" - + " \"data_cold\",\n" - + " \"data_content\",\n" - + " \"data_frozen\",\n" - + " \"data_hot\",\n" - + " \"data_warm\",\n" - + " \"ingest\",\n" - + " \"master\",\n" - + " \"ml\",\n" - + " \"remote_cluster_client\",\n" - + " \"transform\",\n" - + " \"voting_only\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 1,\n" - + " \"last_committed_config\" : [\n" - + " \"commitedConfigurationNodeId\"\n" - + " ],\n" - + " \"last_accepted_config\" : [\n" - + " \"acceptedConfigurationNodeId\"\n" - + " ],\n" - + " \"voting_config_exclusions\" : [\n" - + " {\n" - + " \"node_id\" : \"exlucdedNodeId\",\n" - + " \"node_name\" : \"excludedNodeName\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"templates\" : {\n" - + " \"template\" : {\n" - + " \"order\" : 0,\n" - + " \"index_patterns\" : [\n" - + " \"pattern1\",\n" - + " \"pattern2\"\n" - + " ],\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"key1\" : { }\n" - + " },\n" - + " \"aliases\" : { }\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 1,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"number_of_shards\" : \"1\",\n" - + " \"number_of_replicas\" : \"2\",\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"type\" : {\n" - + " \"type1\" : {\n" - + " \"key\" : \"value\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"aliases\" : [\n" - + " \"alias\"\n" - + " ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 1\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [\n" - + " \"allocationId\"\n" - + " ]\n" - + " },\n" - + " \"rollover_info\" : {\n" - + " \"rolloveAlias\" : {\n" - + " \"met_conditions\" : { },\n" - + " \"time\" : 1\n" - + " }\n" - + " },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " },\n" - + " \"routing_table\" : {\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"shards\" : {\n" - + " \"1\" : [\n" - + " {\n" - + " \"state\" : \"STARTED\",\n" - + " \"primary\" : true,\n" - + " \"node\" : \"nodeId2\",\n" - + " \"relocating_node\" : null,\n" - + " \"shard\" : 1,\n" - + " \"index\" : \"index\",\n" - + " \"allocation_id\" : {\n" - + " \"id\" : \"" - + allocationId - + "\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"routing_nodes\" : {\n" - + " \"unassigned\" : [ ],\n" - + " \"nodes\" : {\n" - + " \"nodeId2\" : [\n" - + " {\n" - + " \"state\" : \"STARTED\",\n" - + " \"primary\" : true,\n" - + " \"node\" : \"nodeId2\",\n" - + " \"relocating_node\" : null,\n" - + " \"shard\" : 1,\n" - + " \"index\" : \"index\",\n" - + " \"allocation_id\" : {\n" - + " \"id\" : \"" - + allocationId - + "\"\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"nodeId1\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "cluster_uuid": "clusterUUID", + "version": 0, + "state_uuid": "stateUUID", + "master_node": "masterNodeId", + "blocks": { + "global": { + "1": { + "description": "description", + "retryable": true, + "disable_state_persistence": true, + "levels": [ + "read", + "write", + "metadata_read", + "metadata_write" + ] + } + }, + "indices": { + "index": { + "2": { + "description": "description2", + "retryable": false, + "levels": [ + "read", + "write", + "metadata_read", + "metadata_write" + ] + } + } + } + }, + "nodes": { + "nodeId1": { + "name": "", + "ephemeral_id": "%s", + "transport_address": "127.0.0.1:111", + "attributes": {}, + "roles": [ + "data", + "data_cold", + "data_content", + "data_frozen", + "data_hot", + "data_warm", + "ingest", + "master", + "ml", + "remote_cluster_client", + "transform", + "voting_only" + ] + } + }, + "metadata": { + "cluster_uuid": "clusterUUID", + "cluster_uuid_committed": false, + "cluster_coordination": { + "term": 1, + "last_committed_config": [ + "commitedConfigurationNodeId" + ], + "last_accepted_config": [ + "acceptedConfigurationNodeId" + ], + "voting_config_exclusions": [ + { + "node_id": "exlucdedNodeId", + "node_name": "excludedNodeName" + } + ] + }, + "templates": { + "template": { + "order": 0, + "index_patterns": [ + "pattern1", + "pattern2" + ], + "settings": { + "index": { + "version": { + "created": "%s" + } + } + }, + "mappings": { + "key1": {} + }, + "aliases": {} + } + }, + "indices": { + "index": { + "version": 1, + "mapping_version": 1, + "settings_version": 1, + "aliases_version": 1, + "routing_num_shards": 1, + "state": "open", + "settings": { + "index": { + "number_of_shards": "1", + "number_of_replicas": "2", + "version": { + "created": "%s" + } + } + }, + "mappings": { + "type": { + "type1": { + "key": "value" + } + } + }, + "aliases": [ + "alias" + ], + "primary_terms": { + "0": 1 + }, + "in_sync_allocations": { + "0": [ + "allocationId" + ] + }, + "rollover_info": { + "rolloveAlias": { + "met_conditions": {}, + "time": 1 + } + }, + "system": false, + "timestamp_range": { + "shards": [] + } + } + }, + "index-graveyard": { + "tombstones": [] + } + }, + "routing_table": { + "indices": { + "index": { + "shards": { + "1": [ + { + "state": "STARTED", + "primary": true, + "node": "nodeId2", + "relocating_node": null, + "shard": 1, + "index": "index", + "allocation_id": { + "id": "%s" + } + } + ] + } + } + } + }, + "routing_nodes": { + "unassigned": [], + "nodes": { + "nodeId2": [ + { + "state": "STARTED", + "primary": true, + "node": "nodeId2", + "relocating_node": null, + "shard": 1, + "index": "index", + "allocation_id": { + "id": "%s" + } + } + ], + "nodeId1": [] + } + } + }""".formatted(ephemeralId, Version.CURRENT.id, Version.CURRENT.id, allocationId, allocationId)), Strings.toString(builder)); } @@ -339,197 +328,185 @@ public void testToXContent_FlatSettingTrue_ReduceMappingFalse() throws IOExcepti clusterState.toXContent(builder, new ToXContent.MapParams(mapParams)); builder.endObject(); - assertEquals( - "{\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"version\" : 0,\n" - + " \"state_uuid\" : \"stateUUID\",\n" - + " \"master_node\" : \"masterNodeId\",\n" - + " \"blocks\" : {\n" - + " \"global\" : {\n" - + " \"1\" : {\n" - + " \"description\" : \"description\",\n" - + " \"retryable\" : true,\n" - + " \"disable_state_persistence\" : true,\n" - + " \"levels\" : [\n" - + " \"read\",\n" - + " \"write\",\n" - + " \"metadata_read\",\n" - + " \"metadata_write\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"2\" : {\n" - + " \"description\" : \"description2\",\n" - + " \"retryable\" : false,\n" - + " \"levels\" : [\n" - + " \"read\",\n" - + " \"write\",\n" - + " \"metadata_read\",\n" - + " \"metadata_write\"\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"nodes\" : {\n" - + " \"nodeId1\" : {\n" - + " \"name\" : \"\",\n" - + " \"ephemeral_id\" : \"" - + ephemeralId - + "\",\n" - + " \"transport_address\" : \"127.0.0.1:111\",\n" - + " \"attributes\" : { },\n" - + " \"roles\" : [\n" - + " \"data\",\n" - + " \"data_cold\",\n" - + " \"data_content\",\n" - + " \"data_frozen\",\n" - + " \"data_hot\",\n" - + " \"data_warm\",\n" - + " \"ingest\",\n" - + " \"master\",\n" - + " \"ml\",\n" - + " \"remote_cluster_client\",\n" - + " \"transform\",\n" - + " \"voting_only\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 1,\n" - + " \"last_committed_config\" : [\n" - + " \"commitedConfigurationNodeId\"\n" - + " ],\n" - + " \"last_accepted_config\" : [\n" - + " \"acceptedConfigurationNodeId\"\n" - + " ],\n" - + " \"voting_config_exclusions\" : [\n" - + " {\n" - + " \"node_id\" : \"exlucdedNodeId\",\n" - + " \"node_name\" : \"excludedNodeName\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"templates\" : {\n" - + " \"template\" : {\n" - + " \"order\" : 0,\n" - + " \"index_patterns\" : [\n" - + " \"pattern1\",\n" - + " \"pattern2\"\n" - + " ],\n" - + " \"settings\" : {\n" - + " \"index.version.created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"key1\" : { }\n" - + " },\n" - + " \"aliases\" : { }\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 1,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index.number_of_replicas\" : \"2\",\n" - + " \"index.number_of_shards\" : \"1\",\n" - + " \"index.version.created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"type\" : {\n" - + " \"type1\" : {\n" - + " \"key\" : \"value\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"aliases\" : [\n" - + " \"alias\"\n" - + " ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 1\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [\n" - + " \"allocationId\"\n" - + " ]\n" - + " },\n" - + " \"rollover_info\" : {\n" - + " \"rolloveAlias\" : {\n" - + " \"met_conditions\" : { },\n" - + " \"time\" : 1\n" - + " }\n" - + " },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " },\n" - + " \"routing_table\" : {\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"shards\" : {\n" - + " \"1\" : [\n" - + " {\n" - + " \"state\" : \"STARTED\",\n" - + " \"primary\" : true,\n" - + " \"node\" : \"nodeId2\",\n" - + " \"relocating_node\" : null,\n" - + " \"shard\" : 1,\n" - + " \"index\" : \"index\",\n" - + " \"allocation_id\" : {\n" - + " \"id\" : \"" - + allocationId - + "\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"routing_nodes\" : {\n" - + " \"unassigned\" : [ ],\n" - + " \"nodes\" : {\n" - + " \"nodeId2\" : [\n" - + " {\n" - + " \"state\" : \"STARTED\",\n" - + " \"primary\" : true,\n" - + " \"node\" : \"nodeId2\",\n" - + " \"relocating_node\" : null,\n" - + " \"shard\" : 1,\n" - + " \"index\" : \"index\",\n" - + " \"allocation_id\" : {\n" - + " \"id\" : \"" - + allocationId - + "\"\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"nodeId1\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "cluster_uuid" : "clusterUUID", + "version" : 0, + "state_uuid" : "stateUUID", + "master_node" : "masterNodeId", + "blocks" : { + "global" : { + "1" : { + "description" : "description", + "retryable" : true, + "disable_state_persistence" : true, + "levels" : [ + "read", + "write", + "metadata_read", + "metadata_write" + ] + } + }, + "indices" : { + "index" : { + "2" : { + "description" : "description2", + "retryable" : false, + "levels" : [ + "read", + "write", + "metadata_read", + "metadata_write" + ] + } + } + } + }, + "nodes" : { + "nodeId1" : { + "name" : "", + "ephemeral_id" : "%s", + "transport_address" : "127.0.0.1:111", + "attributes" : { }, + "roles" : [ + "data", + "data_cold", + "data_content", + "data_frozen", + "data_hot", + "data_warm", + "ingest", + "master", + "ml", + "remote_cluster_client", + "transform", + "voting_only" + ] + } + }, + "metadata" : { + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 1, + "last_committed_config" : [ + "commitedConfigurationNodeId" + ], + "last_accepted_config" : [ + "acceptedConfigurationNodeId" + ], + "voting_config_exclusions" : [ + { + "node_id" : "exlucdedNodeId", + "node_name" : "excludedNodeName" + } + ] + }, + "templates" : { + "template" : { + "order" : 0, + "index_patterns" : [ + "pattern1", + "pattern2" + ], + "settings" : { + "index.version.created" : "%s" + }, + "mappings" : { + "key1" : { } + }, + "aliases" : { } + } + }, + "indices" : { + "index" : { + "version" : 1, + "mapping_version" : 1, + "settings_version" : 1, + "aliases_version" : 1, + "routing_num_shards" : 1, + "state" : "open", + "settings" : { + "index.number_of_replicas" : "2", + "index.number_of_shards" : "1", + "index.version.created" : "%s" + }, + "mappings" : { + "type" : { + "type1" : { + "key" : "value" + } + } + }, + "aliases" : [ + "alias" + ], + "primary_terms" : { + "0" : 1 + }, + "in_sync_allocations" : { + "0" : [ + "allocationId" + ] + }, + "rollover_info" : { + "rolloveAlias" : { + "met_conditions" : { }, + "time" : 1 + } + }, + "system" : false, + "timestamp_range" : { + "shards" : [ ] + } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + }, + "routing_table" : { + "indices" : { + "index" : { + "shards" : { + "1" : [ + { + "state" : "STARTED", + "primary" : true, + "node" : "nodeId2", + "relocating_node" : null, + "shard" : 1, + "index" : "index", + "allocation_id" : { + "id" : "%s" + } + } + ] + } + } + } + }, + "routing_nodes" : { + "unassigned" : [ ], + "nodes" : { + "nodeId2" : [ + { + "state" : "STARTED", + "primary" : true, + "node" : "nodeId2", + "relocating_node" : null, + "shard" : 1, + "index" : "index", + "allocation_id" : { + "id" : "%s" + } + } + ], + "nodeId1" : [ ] + } + } + }""".formatted(ephemeralId, Version.CURRENT.id, Version.CURRENT.id, allocationId, allocationId), Strings.toString(builder)); } @@ -554,203 +531,191 @@ public void testToXContent_FlatSettingFalse_ReduceMappingTrue() throws IOExcepti clusterState.toXContent(builder, new ToXContent.MapParams(mapParams)); builder.endObject(); - assertEquals( - "{\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"version\" : 0,\n" - + " \"state_uuid\" : \"stateUUID\",\n" - + " \"master_node\" : \"masterNodeId\",\n" - + " \"blocks\" : {\n" - + " \"global\" : {\n" - + " \"1\" : {\n" - + " \"description\" : \"description\",\n" - + " \"retryable\" : true,\n" - + " \"disable_state_persistence\" : true,\n" - + " \"levels\" : [\n" - + " \"read\",\n" - + " \"write\",\n" - + " \"metadata_read\",\n" - + " \"metadata_write\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"2\" : {\n" - + " \"description\" : \"description2\",\n" - + " \"retryable\" : false,\n" - + " \"levels\" : [\n" - + " \"read\",\n" - + " \"write\",\n" - + " \"metadata_read\",\n" - + " \"metadata_write\"\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"nodes\" : {\n" - + " \"nodeId1\" : {\n" - + " \"name\" : \"\",\n" - + " \"ephemeral_id\" : \"" - + ephemeralId - + "\",\n" - + " \"transport_address\" : \"127.0.0.1:111\",\n" - + " \"attributes\" : { },\n" - + " \"roles\" : [\n" - + " \"data\",\n" - + " \"data_cold\",\n" - + " \"data_content\",\n" - + " \"data_frozen\",\n" - + " \"data_hot\",\n" - + " \"data_warm\",\n" - + " \"ingest\",\n" - + " \"master\",\n" - + " \"ml\",\n" - + " \"remote_cluster_client\",\n" - + " \"transform\",\n" - + " \"voting_only\"\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 1,\n" - + " \"last_committed_config\" : [\n" - + " \"commitedConfigurationNodeId\"\n" - + " ],\n" - + " \"last_accepted_config\" : [\n" - + " \"acceptedConfigurationNodeId\"\n" - + " ],\n" - + " \"voting_config_exclusions\" : [\n" - + " {\n" - + " \"node_id\" : \"exlucdedNodeId\",\n" - + " \"node_name\" : \"excludedNodeName\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"templates\" : {\n" - + " \"template\" : {\n" - + " \"order\" : 0,\n" - + " \"index_patterns\" : [\n" - + " \"pattern1\",\n" - + " \"pattern2\"\n" - + " ],\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : { },\n" - + " \"aliases\" : { }\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 1,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"number_of_shards\" : \"1\",\n" - + " \"number_of_replicas\" : \"2\",\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"type\" : {\n" - + " \"type1\" : {\n" - + " \"key\" : \"value\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"aliases\" : [\n" - + " \"alias\"\n" - + " ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 1\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [\n" - + " \"allocationId\"\n" - + " ]\n" - + " },\n" - + " \"rollover_info\" : {\n" - + " \"rolloveAlias\" : {\n" - + " \"met_conditions\" : { },\n" - + " \"time\" : 1\n" - + " }\n" - + " },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " },\n" - + " \"routing_table\" : {\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"shards\" : {\n" - + " \"1\" : [\n" - + " {\n" - + " \"state\" : \"STARTED\",\n" - + " \"primary\" : true,\n" - + " \"node\" : \"nodeId2\",\n" - + " \"relocating_node\" : null,\n" - + " \"shard\" : 1,\n" - + " \"index\" : \"index\",\n" - + " \"allocation_id\" : {\n" - + " \"id\" : \"" - + allocationId - + "\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"routing_nodes\" : {\n" - + " \"unassigned\" : [ ],\n" - + " \"nodes\" : {\n" - + " \"nodeId2\" : [\n" - + " {\n" - + " \"state\" : \"STARTED\",\n" - + " \"primary\" : true,\n" - + " \"node\" : \"nodeId2\",\n" - + " \"relocating_node\" : null,\n" - + " \"shard\" : 1,\n" - + " \"index\" : \"index\",\n" - + " \"allocation_id\" : {\n" - + " \"id\" : \"" - + allocationId - + "\"\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"nodeId1\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "cluster_uuid" : "clusterUUID", + "version" : 0, + "state_uuid" : "stateUUID", + "master_node" : "masterNodeId", + "blocks" : { + "global" : { + "1" : { + "description" : "description", + "retryable" : true, + "disable_state_persistence" : true, + "levels" : [ + "read", + "write", + "metadata_read", + "metadata_write" + ] + } + }, + "indices" : { + "index" : { + "2" : { + "description" : "description2", + "retryable" : false, + "levels" : [ + "read", + "write", + "metadata_read", + "metadata_write" + ] + } + } + } + }, + "nodes" : { + "nodeId1" : { + "name" : "", + "ephemeral_id" : "%s", + "transport_address" : "127.0.0.1:111", + "attributes" : { }, + "roles" : [ + "data", + "data_cold", + "data_content", + "data_frozen", + "data_hot", + "data_warm", + "ingest", + "master", + "ml", + "remote_cluster_client", + "transform", + "voting_only" + ] + } + }, + "metadata" : { + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 1, + "last_committed_config" : [ + "commitedConfigurationNodeId" + ], + "last_accepted_config" : [ + "acceptedConfigurationNodeId" + ], + "voting_config_exclusions" : [ + { + "node_id" : "exlucdedNodeId", + "node_name" : "excludedNodeName" + } + ] + }, + "templates" : { + "template" : { + "order" : 0, + "index_patterns" : [ + "pattern1", + "pattern2" + ], + "settings" : { + "index" : { + "version" : { + "created" : "%s" + } + } + }, + "mappings" : { }, + "aliases" : { } + } + }, + "indices" : { + "index" : { + "version" : 1, + "mapping_version" : 1, + "settings_version" : 1, + "aliases_version" : 1, + "routing_num_shards" : 1, + "state" : "open", + "settings" : { + "index" : { + "number_of_shards" : "1", + "number_of_replicas" : "2", + "version" : { + "created" : "%s" + } + } + }, + "mappings" : { + "type" : { + "type1" : { + "key" : "value" + } + } + }, + "aliases" : [ + "alias" + ], + "primary_terms" : { + "0" : 1 + }, + "in_sync_allocations" : { + "0" : [ + "allocationId" + ] + }, + "rollover_info" : { + "rolloveAlias" : { + "met_conditions" : { }, + "time" : 1 + } + }, + "system" : false, + "timestamp_range" : { + "shards" : [ ] + } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + }, + "routing_table" : { + "indices" : { + "index" : { + "shards" : { + "1" : [ + { + "state" : "STARTED", + "primary" : true, + "node" : "nodeId2", + "relocating_node" : null, + "shard" : 1, + "index" : "index", + "allocation_id" : { + "id" : "%s" + } + } + ] + } + } + } + }, + "routing_nodes" : { + "unassigned" : [ ], + "nodes" : { + "nodeId2" : [ + { + "state" : "STARTED", + "primary" : true, + "node" : "nodeId2", + "relocating_node" : null, + "shard" : 1, + "index" : "index", + "allocation_id" : { + "id" : "%s" + } + } + ], + "nodeId1" : [ ] + } + } + }""".formatted(ephemeralId, Version.CURRENT.id, Version.CURRENT.id, allocationId, allocationId), Strings.toString(builder)); } @@ -793,76 +758,72 @@ public void testToXContentSameTypeName() throws IOException { clusterState.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals( - "{\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"version\" : 0,\n" - + " \"state_uuid\" : \"stateUUID\",\n" - + " \"master_node\" : null,\n" - + " \"blocks\" : { },\n" - + " \"nodes\" : { },\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 0,\n" - + " \"last_committed_config\" : [ ],\n" - + " \"last_accepted_config\" : [ ],\n" - + " \"voting_config_exclusions\" : [ ]\n" - + " },\n" - + " \"templates\" : { },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 2,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"number_of_shards\" : \"1\",\n" - + " \"number_of_replicas\" : \"2\",\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"type\" : {\n" - + " \"key\" : \"value\"\n" - + " }\n" - + " },\n" - + " \"aliases\" : [ ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 1\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [ ]\n" - + " },\n" - + " \"rollover_info\" : { },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " },\n" - + " \"routing_table\" : {\n" - + " \"indices\" : { }\n" - + " },\n" - + " \"routing_nodes\" : {\n" - + " \"unassigned\" : [ ],\n" - + " \"nodes\" : { }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "cluster_uuid" : "clusterUUID", + "version" : 0, + "state_uuid" : "stateUUID", + "master_node" : null, + "blocks" : { }, + "nodes" : { }, + "metadata" : { + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 0, + "last_committed_config" : [ ], + "last_accepted_config" : [ ], + "voting_config_exclusions" : [ ] + }, + "templates" : { }, + "indices" : { + "index" : { + "version" : 2, + "mapping_version" : 1, + "settings_version" : 1, + "aliases_version" : 1, + "routing_num_shards" : 1, + "state" : "open", + "settings" : { + "index" : { + "number_of_shards" : "1", + "number_of_replicas" : "2", + "version" : { + "created" : "%s" + } + } + }, + "mappings" : { + "type" : { + "key" : "value" + } + }, + "aliases" : [ ], + "primary_terms" : { + "0" : 1 + }, + "in_sync_allocations" : { + "0" : [ ] + }, + "rollover_info" : { }, + "system" : false, + "timestamp_range" : { + "shards" : [ ] + } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + }, + "routing_table" : { + "indices" : { } + }, + "routing_nodes" : { + "unassigned" : [ ], + "nodes" : { } + } + }""".formatted(Version.CURRENT.id), Strings.toString(builder)); } private ClusterState buildClusterState() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/cluster/SnapshotDeletionsInProgressTests.java b/server/src/test/java/org/elasticsearch/cluster/SnapshotDeletionsInProgressTests.java index 7196f11feaabe..b9946a2bd6038 100644 --- a/server/src/test/java/org/elasticsearch/cluster/SnapshotDeletionsInProgressTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/SnapshotDeletionsInProgressTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.cluster; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -40,14 +41,19 @@ public void testXContent() throws IOException { sdip.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); String json = Strings.toString(builder); - assertThat( - json, - equalTo( - "{\"snapshot_deletions\":[{\"repository\":\"repo\",\"snapshots\":[]," - + "\"start_time\":\"1993-05-06T13:17:47.638Z\",\"start_time_millis\":736694267638,\"repository_state_id\":0," - + "\"state\":\"STARTED\"}]}" - ) - ); + assertThat(json, equalTo(XContentHelper.stripWhitespace(""" + { + "snapshot_deletions": [ + { + "repository": "repo", + "snapshots": [], + "start_time": "1993-05-06T13:17:47.638Z", + "start_time_millis": 736694267638, + "repository_state_id": 0, + "state": "STARTED" + } + ] + }"""))); } } } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/ComponentTemplateTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/ComponentTemplateTests.java index a4a3c8c901aca..d13854870fdcd 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/ComponentTemplateTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/ComponentTemplateTests.java @@ -204,29 +204,27 @@ public void testMappingsEquals() throws IOException { { String randomString = randomAlphaOfLength(10); - CompressedXContent m1 = new CompressedXContent("{\"properties\":{\"" + randomString + "\":{\"type\":\"keyword\"}}}"); - CompressedXContent m2 = new CompressedXContent("{\"properties\":{\"" + randomString + "\":{\"type\":\"keyword\"}}}"); + CompressedXContent m1 = new CompressedXContent(""" + {"properties":{"%s":{"type":"keyword"}}} + """.formatted(randomString)); + CompressedXContent m2 = new CompressedXContent(""" + {"properties":{"%s":{"type":"keyword"}}} + """.formatted(randomString)); assertThat(Template.mappingsEquals(m1, m2), equalTo(true)); } { CompressedXContent m1 = randomMappings(); - CompressedXContent m2 = new CompressedXContent("{\"properties\":{\"" + randomAlphaOfLength(10) + "\":{\"type\":\"keyword\"}}}"); + CompressedXContent m2 = new CompressedXContent(""" + {"properties":{"%s":{"type":"keyword"}}} + """.formatted(randomAlphaOfLength(10))); assertThat(Template.mappingsEquals(m1, m2), equalTo(false)); } { - Map map = XContentHelper.convertToMap( - new BytesArray( - "{\"" - + MapperService.SINGLE_MAPPING_NAME - + "\":{\"properties\":{\"" - + randomAlphaOfLength(10) - + "\":{\"type\":\"keyword\"}}}}" - ), - true, - XContentType.JSON - ).v2(); + Map map = XContentHelper.convertToMap(new BytesArray(""" + {"%s":{"properties":{"%s":{"type":"keyword"}}}} + """.formatted(MapperService.SINGLE_MAPPING_NAME, randomAlphaOfLength(10))), true, XContentType.JSON).v2(); Map reduceMap = Template.reduceMapping(map); CompressedXContent m1 = new CompressedXContent(Strings.toString(XContentFactory.jsonBuilder().map(map))); CompressedXContent m2 = new CompressedXContent(Strings.toString(XContentFactory.jsonBuilder().map(reduceMap))); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamAliasTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamAliasTests.java index 4f9460cb8ced1..768fa643c958b 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamAliasTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/DataStreamAliasTests.java @@ -88,7 +88,8 @@ public void testUpdateFilter() { assertThat(result.getDataStreams(), containsInAnyOrder("ds-1", "ds-2")); assertThat(result.getWriteDataStream(), nullValue()); assertThat(result.getFilter(), notNullValue()); - assertThat(result.getFilter().string(), equalTo("{\"term\":{\"field\":\"value\"}}")); + assertThat(result.getFilter().string(), equalTo(""" + {"term":{"field":"value"}}""")); } // noop update to filter: { @@ -114,7 +115,8 @@ public void testUpdateFilter() { assertThat(result.getDataStreams(), containsInAnyOrder("ds-1", "ds-2")); assertThat(result.getWriteDataStream(), nullValue()); assertThat(result.getFilter(), notNullValue()); - assertThat(result.getFilter().string(), equalTo("{\"term\":{\"field\":\"value1\"}}")); + assertThat(result.getFilter().string(), equalTo(""" + {"term":{"field":"value1"}}""")); } // Filter not specified, keep existing filter: { @@ -128,7 +130,8 @@ public void testUpdateFilter() { assertThat(result, sameInstance(alias)); assertThat(result.getDataStreams(), containsInAnyOrder("ds-1", "ds-2")); assertThat(result.getWriteDataStream(), nullValue()); - assertThat(result.getFilter().string(), equalTo("{\"term\":{\"field\":\"value\"}}")); + assertThat(result.getFilter().string(), equalTo(""" + {"term":{"field":"value"}}""")); } } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadataTests.java index c1e4784f98062..ea52685ccf72b 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/IndexTemplateMetadataTests.java @@ -31,15 +31,24 @@ public class IndexTemplateMetadataTests extends ESTestCase { public void testIndexTemplateMetadataXContentRoundTrip() throws Exception { - String template = "{\"index_patterns\" : [ \".test-*\" ],\"order\" : 1000," - + "\"settings\" : {\"number_of_shards\" : 1,\"number_of_replicas\" : 0}," - + "\"mappings\" : {\"doc\" :" - + "{\"properties\":{\"" - + randomAlphaOfLength(10) - + "\":{\"type\":\"text\"},\"" - + randomAlphaOfLength(10) - + "\":{\"type\":\"keyword\"}}" - + "}}}"; + String template = """ + { + "index_patterns": [ ".test-*" ], + "order": 1000, + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0 + }, + "mappings": { + "doc": { + "properties": { + "%s": { + "type": "keyword" + } + } + } + } + }""".formatted(randomAlphaOfLength(10), randomAlphaOfLength(10)); BytesReference templateBytes = new BytesArray(template); final IndexTemplateMetadata indexTemplateMetadata; @@ -103,15 +112,24 @@ public void testValidateInvalidIndexPatterns() throws Exception { }); assertThat(nullPatternError.getMessage(), equalTo("Index patterns must not be null or empty; got null")); - final String templateWithEmptyPattern = "{\"index_patterns\" : [],\"order\" : 1000," - + "\"settings\" : {\"number_of_shards\" : 10,\"number_of_replicas\" : 1}," - + "\"mappings\" : {\"doc\" :" - + "{\"properties\":{\"" - + randomAlphaOfLength(10) - + "\":{\"type\":\"text\"},\"" - + randomAlphaOfLength(10) - + "\":{\"type\":\"keyword\"}}" - + "}}}"; + final String templateWithEmptyPattern = """ + { + "index_patterns": [], + "order": 1000, + "settings": { + "number_of_shards": 10, + "number_of_replicas": 1 + }, + "mappings": { + "doc": { + "properties": { + "%s": { + "type": "keyword" + } + } + } + } + }""".formatted(randomAlphaOfLength(10), randomAlphaOfLength(10)); try ( XContentParser parser = XContentHelper.createParser( NamedXContentRegistry.EMPTY, @@ -127,15 +145,26 @@ public void testValidateInvalidIndexPatterns() throws Exception { assertThat(ex.getMessage(), equalTo("Index patterns must not be null or empty; got []")); } - final String templateWithoutPattern = "{\"order\" : 1000," - + "\"settings\" : {\"number_of_shards\" : 10,\"number_of_replicas\" : 1}," - + "\"mappings\" : {\"doc\" :" - + "{\"properties\":{\"" - + randomAlphaOfLength(10) - + "\":{\"type\":\"text\"},\"" - + randomAlphaOfLength(10) - + "\":{\"type\":\"keyword\"}}" - + "}}}"; + final String templateWithoutPattern = """ + { + "order": 1000, + "settings": { + "number_of_shards": 10, + "number_of_replicas": 1 + }, + "mappings": { + "doc": { + "properties": { + "%s": { + "type": "text" + }, + "%s": { + "type": "keyword" + } + } + } + } + }""".formatted(randomAlphaOfLength(10), randomAlphaOfLength(10)); try ( XContentParser parser = XContentHelper.createParser( NamedXContentRegistry.EMPTY, @@ -153,7 +182,8 @@ public void testValidateInvalidIndexPatterns() throws Exception { } public void testParseTemplateWithAliases() throws Exception { - String templateInJSON = "{\"aliases\": {\"log\":{}}, \"index_patterns\": [\"pattern-1\"]}"; + String templateInJSON = """ + {"aliases": {"log":{}}, "index_patterns": ["pattern-1"]}"""; try ( XContentParser parser = XContentHelper.createParser( NamedXContentRegistry.EMPTY, @@ -194,7 +224,8 @@ public void testFromToXContent() throws Exception { templateBuilder.version(between(0, 100)); } if (randomBoolean()) { - templateBuilder.putMapping("doc", "{\"doc\":{\"properties\":{\"type\":\"text\"}}}"); + templateBuilder.putMapping("doc", """ + {"doc":{"properties":{"type":"text"}}}"""); } IndexTemplateMetadata template = templateBuilder.build(); XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.JSON.xContent())); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java index 9f709a231b811..3759a0b8798c1 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java @@ -1088,7 +1088,9 @@ public void testParseMappingsWithTypedTemplateAndTypelessIndexMapping() throws E public void testParseMappingsWithTypedTemplate() throws Exception { IndexTemplateMetadata templateMetadata = addMatchingTemplate(builder -> { try { - builder.putMapping("type", "{\"type\":{\"properties\":{\"field\":{\"type\":\"keyword\"}}}}"); + builder.putMapping("type", """ + {"type":{"properties":{"field":{"type":"keyword"}}}} + """); } catch (IOException e) { ExceptionsHelper.reThrowIfNotNull(e); } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java index ae5b340138cce..8d9041917efc9 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateServiceTests.java @@ -401,11 +401,9 @@ public void testPutGlobalTemplateWithIndexHiddenSetting() throws Exception { public void testAddComponentTemplate() throws Exception { MetadataIndexTemplateService metadataIndexTemplateService = getMetadataIndexTemplateService(); ClusterState state = ClusterState.EMPTY_STATE; - Template template = new Template( - Settings.builder().build(), - new CompressedXContent("{\"properties\":{\"@timestamp\":{\"type\":\"date\"}}}"), - ComponentTemplateTests.randomAliases() - ); + Template template = new Template(Settings.builder().build(), new CompressedXContent(""" + {"properties":{"@timestamp":{"type":"date"}}} + """), ComponentTemplateTests.randomAliases()); ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>()); state = metadataIndexTemplateService.addComponentTemplate(state, false, "foo", componentTemplate); @@ -1101,64 +1099,32 @@ public void testResolveConflictingMappings() throws Exception { final MetadataIndexTemplateService service = getMetadataIndexTemplateService(); ClusterState state = ClusterState.EMPTY_STATE; - ComponentTemplate ct1 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); - ComponentTemplate ct2 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); + ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "keyword" + } + } + }"""), null), null, null); + ComponentTemplate ct2 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "text" + } + } + }"""), null), null, null); state = service.addComponentTemplate(state, true, "ct_high", ct1); state = service.addComponentTemplate(state, true, "ct_low", ct2); - ComposableIndexTemplate it = new ComposableIndexTemplate( - List.of("i*"), - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - List.of("ct_low", "ct_high"), - 0L, - 1L, - null, - null, - null - ); + ComposableIndexTemplate it = new ComposableIndexTemplate(List.of("i*"), new Template(null, new CompressedXContent(""" + { + "properties": { + "field": { + "type": "keyword" + } + } + }"""), null), List.of("ct_low", "ct_high"), 0L, 1L, null, null, null); state = service.addIndexTemplateV2(state, true, "my-template", it); List mappings = MetadataIndexTemplateService.collectMappings(state, "my-template", "my-index"); @@ -1189,64 +1155,32 @@ public void testResolveMappings() throws Exception { final MetadataIndexTemplateService service = getMetadataIndexTemplateService(); ClusterState state = ClusterState.EMPTY_STATE; - ComponentTemplate ct1 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field1\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); - ComponentTemplate ct2 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); + ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field1": { + "type": "keyword" + } + } + }"""), null), null, null); + ComponentTemplate ct2 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "text" + } + } + }"""), null), null, null); state = service.addComponentTemplate(state, true, "ct_high", ct1); state = service.addComponentTemplate(state, true, "ct_low", ct2); - ComposableIndexTemplate it = new ComposableIndexTemplate( - List.of("i*"), - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field3\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - List.of("ct_low", "ct_high"), - 0L, - 1L, - null, - null, - null - ); + ComposableIndexTemplate it = new ComposableIndexTemplate(List.of("i*"), new Template(null, new CompressedXContent(""" + { + "properties": { + "field3": { + "type": "integer" + } + } + }"""), null), List.of("ct_low", "ct_high"), 0L, 1L, null, null, null); state = service.addIndexTemplateV2(state, true, "my-template", it); List mappings = MetadataIndexTemplateService.collectMappings(state, "my-template", "my-index"); @@ -1271,49 +1205,26 @@ public void testDefinedTimestampMappingIsAddedForDataStreamTemplates() throws Ex final MetadataIndexTemplateService service = getMetadataIndexTemplateService(); ClusterState state = ClusterState.EMPTY_STATE; - ComponentTemplate ct1 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field1\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); + ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field1": { + "type": "keyword" + } + } + }"""), null), null, null); state = service.addComponentTemplate(state, true, "ct1", ct1); { - ComposableIndexTemplate it = new ComposableIndexTemplate( - List.of("logs*"), - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - List.of("ct1"), - 0L, - 1L, - null, - new ComposableIndexTemplate.DataStreamTemplate(), - null - ); + ComposableIndexTemplate it = new ComposableIndexTemplate(List.of("logs*"), new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "integer" + } + } + }"""), null), List.of("ct1"), 0L, 1L, null, new ComposableIndexTemplate.DataStreamTemplate(), null); state = service.addIndexTemplateV2(state, true, "logs-data-stream-template", it); List mappings = MetadataIndexTemplateService.collectMappings( @@ -1344,28 +1255,14 @@ public void testDefinedTimestampMappingIsAddedForDataStreamTemplates() throws Ex { // indices matched by templates without the data stream field defined don't get the default @timestamp mapping - ComposableIndexTemplate it = new ComposableIndexTemplate( - List.of("timeseries*"), - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - List.of("ct1"), - 0L, - 1L, - null, - null, - null - ); + ComposableIndexTemplate it = new ComposableIndexTemplate(List.of("timeseries*"), new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "integer" + } + } + }"""), null), List.of("ct1"), 0L, 1L, null, null, null); state = service.addIndexTemplateV2(state, true, "timeseries-template", it); List mappings = MetadataIndexTemplateService.collectMappings(state, "timeseries-template", "timeseries"); @@ -1416,23 +1313,14 @@ public void testUserDefinedMappingTakesPrecedenceOverDefault() throws Exception { // user defines a @timestamp mapping as part of a component template - ComponentTemplate ct1 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date_nanos\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); + ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "@timestamp": { + "type": "date_nanos" + } + } + }"""), null), null, null); state = service.addComponentTemplate(state, true, "ct1", ct1); ComposableIndexTemplate it = new ComposableIndexTemplate( @@ -1476,19 +1364,14 @@ public void testUserDefinedMappingTakesPrecedenceOverDefault() throws Exception { // user defines a @timestamp mapping as part of a composable index template - Template template = new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date_nanos\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ); + Template template = new Template(null, new CompressedXContent(""" + { + "properties": { + "@timestamp": { + "type": "date_nanos" + } + } + }"""), null); ComposableIndexTemplate it = new ComposableIndexTemplate( List.of("timeseries*"), template, @@ -1714,69 +1597,37 @@ public void testIndexTemplateFailsToOverrideComponentTemplateMappingField() thro final MetadataIndexTemplateService service = getMetadataIndexTemplateService(); ClusterState state = ClusterState.EMPTY_STATE; - ComponentTemplate ct1 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"foo\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); - ComponentTemplate ct2 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field1\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); + ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "object", + "properties": { + "foo": { + "type": "integer" + } + } + } + } + }"""), null), null, null); + ComponentTemplate ct2 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field1": { + "type": "text" + } + } + }"""), null), null, null); state = service.addComponentTemplate(state, true, "c1", ct1); state = service.addComponentTemplate(state, true, "c2", ct2); - ComposableIndexTemplate it = new ComposableIndexTemplate( - List.of("i*"), - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - randomBoolean() ? Arrays.asList("c1", "c2") : Arrays.asList("c2", "c1"), - 0L, - 1L, - null, - null, - null - ); + ComposableIndexTemplate it = new ComposableIndexTemplate(List.of("i*"), new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "text" + } + } + }"""), null), randomBoolean() ? Arrays.asList("c1", "c2") : Arrays.asList("c2", "c1"), 0L, 1L, null, null, null); final ClusterState finalState = state; IllegalArgumentException e = expectThrows( @@ -1807,45 +1658,29 @@ public void testUpdateComponentTemplateFailsIfResolvedIndexTemplatesWouldBeInval final MetadataIndexTemplateService service = getMetadataIndexTemplateService(); ClusterState state = ClusterState.EMPTY_STATE; - ComponentTemplate ct1 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"foo\": {\n" - + " \"type\": \"integer\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); - ComponentTemplate ct2 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field1\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); + ComponentTemplate ct1 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "object", + "properties": { + "foo": { + "type": "integer" + } + } + } + } + } + """), null), null, null); + ComponentTemplate ct2 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field1": { + "type": "text" + } + } + } + """), null), null, null); state = service.addComponentTemplate(state, true, "c1", ct1); state = service.addComponentTemplate(state, true, "c2", ct2); ComposableIndexTemplate it = new ComposableIndexTemplate( @@ -1862,23 +1697,15 @@ public void testUpdateComponentTemplateFailsIfResolvedIndexTemplatesWouldBeInval // Great, the templates aren't invalid state = service.addIndexTemplateV2(state, randomBoolean(), "my-template", it); - ComponentTemplate changedCt2 = new ComponentTemplate( - new Template( - null, - new CompressedXContent( - "{\n" - + " \"properties\": {\n" - + " \"field2\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }" - ), - null - ), - null, - null - ); + ComponentTemplate changedCt2 = new ComponentTemplate(new Template(null, new CompressedXContent(""" + { + "properties": { + "field2": { + "type": "text" + } + } + } + """), null), null, null); final ClusterState finalState = state; IllegalArgumentException e = expectThrows( diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMappingServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMappingServiceTests.java index 3cef19c4dbb0f..2cba73d2e3be9 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMappingServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMappingServiceTests.java @@ -39,9 +39,8 @@ public void testMappingClusterStateUpdateDoesntChangeExistingIndices() throws Ex final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class); // TODO - it will be nice to get a random mapping generator - final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest( - "{ \"properties\": { \"field\": { \"type\": \"text\" }}}" - ); + final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest(""" + { "properties": { "field": { "type": "text" }}}"""); request.indices(new Index[] { indexService.index() }); final ClusterStateTaskExecutor.ClusterTasksResult result = mappingService.putMappingExecutor .execute(clusterService.state(), Collections.singletonList(request)); @@ -62,9 +61,8 @@ public void testClusterStateIsNotChangedWithIdenticalMappings() throws Exception final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class); - final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest( - "{ \"properties\" { \"field\": { \"type\": \"text\" }}}" - ); + final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest(""" + { "properties" { "field": { "type": "text" }}}"""); ClusterState result = mappingService.putMappingExecutor.execute( clusterService.state(), Collections.singletonList(request) @@ -82,9 +80,8 @@ public void testMappingVersion() throws Exception { final long previousVersion = indexService.getMetadata().getMappingVersion(); final MetadataMappingService mappingService = getInstanceFromNode(MetadataMappingService.class); final ClusterService clusterService = getInstanceFromNode(ClusterService.class); - final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest( - "{ \"properties\": { \"field\": { \"type\": \"text\" }}}" - ); + final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest(""" + { "properties": { "field": { "type": "text" }}}"""); request.indices(new Index[] { indexService.index() }); final ClusterStateTaskExecutor.ClusterTasksResult result = mappingService.putMappingExecutor .execute(clusterService.state(), Collections.singletonList(request)); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMigrateToDataStreamServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMigrateToDataStreamServiceTests.java index 4afd6ae62a8f7..b4e8acf11d4f5 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMigrateToDataStreamServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataMigrateToDataStreamServiceTests.java @@ -53,7 +53,9 @@ public void testValidateRequestWithNonexistentAlias() { public void testValidateRequestWithFilteredAlias() { String filteredAliasName = "filtered_alias"; - AliasMetadata filteredAlias = AliasMetadata.builder(filteredAliasName).filter("{\"term\":{\"user.id\":\"kimchy\"}}").build(); + AliasMetadata filteredAlias = AliasMetadata.builder(filteredAliasName).filter(""" + {"term":{"user.id":"kimchy"}} + """).build(); ClusterState cs = ClusterState.builder(new ClusterName("dummy")) .metadata( Metadata.builder() diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java index fde75d6f608c1..29eeb6f0dc544 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataTests.java @@ -965,78 +965,77 @@ public static void assertMultiField(Map properties, String field assertLeafs(subFieldsDef, subFields); } - private static final String FIND_MAPPINGS_TEST_ITEM = "{\n" - + " \"_doc\": {\n" - + " \"_routing\": {\n" - + " \"required\":true\n" - + " }," - + " \"_source\": {\n" - + " \"enabled\":false\n" - + " }," - + " \"properties\": {\n" - + " \"name\": {\n" - + " \"properties\": {\n" - + " \"first\": {\n" - + " \"type\": \"keyword\"\n" - + " },\n" - + " \"last\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"birth\": {\n" - + " \"type\": \"date\"\n" - + " },\n" - + " \"age\": {\n" - + " \"type\": \"integer\"\n" - + " },\n" - + " \"ip\": {\n" - + " \"type\": \"ip\"\n" - + " },\n" - + " \"suggest\" : {\n" - + " \"type\": \"completion\"\n" - + " },\n" - + " \"address\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"street\": {\n" - + " \"type\": \"keyword\"\n" - + " },\n" - + " \"location\": {\n" - + " \"type\": \"geo_point\"\n" - + " },\n" - + " \"area\": {\n" - + " \"type\": \"geo_shape\", \n" - + " \"tree\": \"quadtree\",\n" - + " \"precision\": \"1m\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"properties\": {\n" - + " \"type\": \"nested\",\n" - + " \"properties\": {\n" - + " \"key\" : {\n" - + " \"type\": \"text\",\n" - + " \"fields\": {\n" - + " \"keyword\" : {\n" - + " \"type\" : \"keyword\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"value\" : {\n" - + " \"type\": \"text\",\n" - + " \"fields\": {\n" - + " \"keyword\" : {\n" - + " \"type\" : \"keyword\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + private static final String FIND_MAPPINGS_TEST_ITEM = """ + { + "_doc": { + "_routing": { + "required":true + }, "_source": { + "enabled":false + }, "properties": { + "name": { + "properties": { + "first": { + "type": "keyword" + }, + "last": { + "type": "keyword" + } + } + }, + "birth": { + "type": "date" + }, + "age": { + "type": "integer" + }, + "ip": { + "type": "ip" + }, + "suggest" : { + "type": "completion" + }, + "address": { + "type": "object", + "properties": { + "street": { + "type": "keyword" + }, + "location": { + "type": "geo_point" + }, + "area": { + "type": "geo_shape", \s + "tree": "quadtree", + "precision": "1m" + } + } + }, + "properties": { + "type": "nested", + "properties": { + "key" : { + "type": "text", + "fields": { + "keyword" : { + "type" : "keyword" + } + } + }, + "value" : { + "type": "text", + "fields": { + "keyword" : { + "type" : "keyword" + } + } + } + } + } + } + } + } + }"""; public void testTransientSettingsOverridePersistentSettings() { final Setting setting = Setting.simpleString("key"); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/TemplateUpgradeServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/TemplateUpgradeServiceTests.java index aabfcd2beea6c..6cc85a09d4a1d 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/TemplateUpgradeServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/TemplateUpgradeServiceTests.java @@ -191,7 +191,9 @@ public void testUpdateTemplates() { } Map additions = new HashMap<>(additionsCount); for (int i = 0; i < additionsCount; i++) { - additions.put("add_template_" + i, new BytesArray("{\"index_patterns\" : \"*\", \"order\" : " + i + "}")); + additions.put("add_template_" + i, new BytesArray(""" + {"index_patterns" : "*", "order" : %s} + """.formatted(i))); } final TemplateUpgradeService service = new TemplateUpgradeService(mockClient, clusterService, threadPool, Collections.emptyList()); diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetadataTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetadataTests.java index e891aa06e1200..4858ded628233 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetadataTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/ToAndFromJsonMetadataTests.java @@ -183,8 +183,10 @@ public void testSimpleJsonFromAndTo() throws IOException { assertThat(parsedMetadata.dataStreams().get("data-stream2").getIndices(), contains(idx2.getIndex())); } - private static final String MAPPING_SOURCE1 = "{\"mapping1\":{\"text1\":{\"type\":\"string\"}}}"; - private static final String MAPPING_SOURCE2 = "{\"mapping2\":{\"text2\":{\"type\":\"string\"}}}"; + private static final String MAPPING_SOURCE1 = """ + {"mapping1":{"text1":{"type":"string"}}}"""; + private static final String MAPPING_SOURCE2 = """ + {"mapping2":{"text2":{"type":"string"}}}"""; private static final String ALIAS_FILTER1 = "{\"field1\":\"value1\"}"; private static final String ALIAS_FILTER2 = "{\"field2\":\"value2\"}"; @@ -203,57 +205,51 @@ public void testToXContentGateway_FlatSettingTrue_ReduceMappingFalse() throws IO metadata.toXContent(builder, new ToXContent.MapParams(mapParams)); builder.endObject(); - assertEquals( - "{\n" - + " \"meta-data\" : {\n" - + " \"version\" : 0,\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 1,\n" - + " \"last_committed_config\" : [\n" - + " \"commitedConfigurationNodeId\"\n" - + " ],\n" - + " \"last_accepted_config\" : [\n" - + " \"acceptedConfigurationNodeId\"\n" - + " ],\n" - + " \"voting_config_exclusions\" : [\n" - + " {\n" - + " \"node_id\" : \"exlucdedNodeId\",\n" - + " \"node_name\" : \"excludedNodeName\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"settings\" : {\n" - + " \"index.version.created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"templates\" : {\n" - + " \"template\" : {\n" - + " \"order\" : 0,\n" - + " \"index_patterns\" : [\n" - + " \"pattern1\",\n" - + " \"pattern2\"\n" - + " ],\n" - + " \"settings\" : {\n" - + " \"index.version.created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"key1\" : { }\n" - + " },\n" - + " \"aliases\" : { }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "meta-data" : { + "version" : 0, + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 1, + "last_committed_config" : [ + "commitedConfigurationNodeId" + ], + "last_accepted_config" : [ + "acceptedConfigurationNodeId" + ], + "voting_config_exclusions" : [ + { + "node_id" : "exlucdedNodeId", + "node_name" : "excludedNodeName" + } + ] + }, + "settings" : { + "index.version.created" : "%s" + }, + "templates" : { + "template" : { + "order" : 0, + "index_patterns" : [ + "pattern1", + "pattern2" + ], + "settings" : { + "index.version.created" : "%s" + }, + "mappings" : { + "key1" : { } + }, + "aliases" : { } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + } + }""".formatted(Version.CURRENT.id, Version.CURRENT.id), Strings.toString(builder)); } public void testToXContentAPI_SameTypeName() throws IOException { @@ -296,63 +292,59 @@ public void testToXContentAPI_SameTypeName() throws IOException { metadata.toXContent(builder, new ToXContent.MapParams(mapParams)); builder.endObject(); - assertEquals( - "{\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 0,\n" - + " \"last_committed_config\" : [ ],\n" - + " \"last_accepted_config\" : [ ],\n" - + " \"voting_config_exclusions\" : [ ]\n" - + " },\n" - + " \"templates\" : { },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 2,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"number_of_shards\" : \"1\",\n" - + " \"number_of_replicas\" : \"2\",\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"type\" : {\n" - + " \"key\" : \"value\"\n" - + " }\n" - + " },\n" - + " \"aliases\" : [ ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 1\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [ ]\n" - + " },\n" - + " \"rollover_info\" : { },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "metadata" : { + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 0, + "last_committed_config" : [ ], + "last_accepted_config" : [ ], + "voting_config_exclusions" : [ ] + }, + "templates" : { }, + "indices" : { + "index" : { + "version" : 2, + "mapping_version" : 1, + "settings_version" : 1, + "aliases_version" : 1, + "routing_num_shards" : 1, + "state" : "open", + "settings" : { + "index" : { + "number_of_shards" : "1", + "number_of_replicas" : "2", + "version" : { + "created" : "%s" + } + } + }, + "mappings" : { + "type" : { + "key" : "value" + } + }, + "aliases" : [ ], + "primary_terms" : { + "0" : 1 + }, + "in_sync_allocations" : { + "0" : [ ] + }, + "rollover_info" : { }, + "system" : false, + "timestamp_range" : { + "shards" : [ ] + } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + } + }""".formatted(Version.CURRENT.id), Strings.toString(builder)); } public void testToXContentGateway_FlatSettingFalse_ReduceMappingTrue() throws IOException { @@ -370,59 +362,53 @@ public void testToXContentGateway_FlatSettingFalse_ReduceMappingTrue() throws IO metadata.toXContent(builder, new ToXContent.MapParams(mapParams)); builder.endObject(); - assertEquals( - "{\n" - + " \"meta-data\" : {\n" - + " \"version\" : 0,\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 1,\n" - + " \"last_committed_config\" : [\n" - + " \"commitedConfigurationNodeId\"\n" - + " ],\n" - + " \"last_accepted_config\" : [\n" - + " \"acceptedConfigurationNodeId\"\n" - + " ],\n" - + " \"voting_config_exclusions\" : [\n" - + " {\n" - + " \"node_id\" : \"exlucdedNodeId\",\n" - + " \"node_name\" : \"excludedNodeName\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"settings\" : {\n" - + " \"index.version.created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"templates\" : {\n" - + " \"template\" : {\n" - + " \"order\" : 0,\n" - + " \"index_patterns\" : [\n" - + " \"pattern1\",\n" - + " \"pattern2\"\n" - + " ],\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : { },\n" - + " \"aliases\" : { }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "meta-data" : { + "version" : 0, + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 1, + "last_committed_config" : [ + "commitedConfigurationNodeId" + ], + "last_accepted_config" : [ + "acceptedConfigurationNodeId" + ], + "voting_config_exclusions" : [ + { + "node_id" : "exlucdedNodeId", + "node_name" : "excludedNodeName" + } + ] + }, + "settings" : { + "index.version.created" : "%s" + }, + "templates" : { + "template" : { + "order" : 0, + "index_patterns" : [ + "pattern1", + "pattern2" + ], + "settings" : { + "index" : { + "version" : { + "created" : "%s" + } + } + }, + "mappings" : { }, + "aliases" : { } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + } + }""".formatted(Version.CURRENT.id, Version.CURRENT.id), Strings.toString(builder)); } public void testToXContentAPI_FlatSettingTrue_ReduceMappingFalse() throws IOException { @@ -441,96 +427,90 @@ public void testToXContentAPI_FlatSettingTrue_ReduceMappingFalse() throws IOExce metadata.toXContent(builder, new ToXContent.MapParams(mapParams)); builder.endObject(); - assertEquals( - "{\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 1,\n" - + " \"last_committed_config\" : [\n" - + " \"commitedConfigurationNodeId\"\n" - + " ],\n" - + " \"last_accepted_config\" : [\n" - + " \"acceptedConfigurationNodeId\"\n" - + " ],\n" - + " \"voting_config_exclusions\" : [\n" - + " {\n" - + " \"node_id\" : \"exlucdedNodeId\",\n" - + " \"node_name\" : \"excludedNodeName\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"templates\" : {\n" - + " \"template\" : {\n" - + " \"order\" : 0,\n" - + " \"index_patterns\" : [\n" - + " \"pattern1\",\n" - + " \"pattern2\"\n" - + " ],\n" - + " \"settings\" : {\n" - + " \"index.version.created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"key1\" : { }\n" - + " },\n" - + " \"aliases\" : { }\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 2,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index.number_of_replicas\" : \"2\",\n" - + " \"index.number_of_shards\" : \"1\",\n" - + " \"index.version.created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"type\" : {\n" - + " \"type1\" : {\n" - + " \"key\" : \"value\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"aliases\" : [\n" - + " \"alias\"\n" - + " ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 1\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [\n" - + " \"allocationId\"\n" - + " ]\n" - + " },\n" - + " \"rollover_info\" : {\n" - + " \"rolloveAlias\" : {\n" - + " \"met_conditions\" : { },\n" - + " \"time\" : 1\n" - + " }\n" - + " },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "metadata" : { + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 1, + "last_committed_config" : [ + "commitedConfigurationNodeId" + ], + "last_accepted_config" : [ + "acceptedConfigurationNodeId" + ], + "voting_config_exclusions" : [ + { + "node_id" : "exlucdedNodeId", + "node_name" : "excludedNodeName" + } + ] + }, + "templates" : { + "template" : { + "order" : 0, + "index_patterns" : [ + "pattern1", + "pattern2" + ], + "settings" : { + "index.version.created" : "%s" + }, + "mappings" : { + "key1" : { } + }, + "aliases" : { } + } + }, + "indices" : { + "index" : { + "version" : 2, + "mapping_version" : 1, + "settings_version" : 1, + "aliases_version" : 1, + "routing_num_shards" : 1, + "state" : "open", + "settings" : { + "index.number_of_replicas" : "2", + "index.number_of_shards" : "1", + "index.version.created" : "%s" + }, + "mappings" : { + "type" : { + "type1" : { + "key" : "value" + } + } + }, + "aliases" : [ + "alias" + ], + "primary_terms" : { + "0" : 1 + }, + "in_sync_allocations" : { + "0" : [ + "allocationId" + ] + }, + "rollover_info" : { + "rolloveAlias" : { + "met_conditions" : { }, + "time" : 1 + } + }, + "system" : false, + "timestamp_range" : { + "shards" : [ ] + } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + } + }""".formatted(Version.CURRENT.id, Version.CURRENT.id), Strings.toString(builder)); } public void testToXContentAPI_FlatSettingFalse_ReduceMappingTrue() throws IOException { @@ -549,102 +529,96 @@ public void testToXContentAPI_FlatSettingFalse_ReduceMappingTrue() throws IOExce metadata.toXContent(builder, new ToXContent.MapParams(mapParams)); builder.endObject(); - assertEquals( - "{\n" - + " \"metadata\" : {\n" - + " \"cluster_uuid\" : \"clusterUUID\",\n" - + " \"cluster_uuid_committed\" : false,\n" - + " \"cluster_coordination\" : {\n" - + " \"term\" : 1,\n" - + " \"last_committed_config\" : [\n" - + " \"commitedConfigurationNodeId\"\n" - + " ],\n" - + " \"last_accepted_config\" : [\n" - + " \"acceptedConfigurationNodeId\"\n" - + " ],\n" - + " \"voting_config_exclusions\" : [\n" - + " {\n" - + " \"node_id\" : \"exlucdedNodeId\",\n" - + " \"node_name\" : \"excludedNodeName\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"templates\" : {\n" - + " \"template\" : {\n" - + " \"order\" : 0,\n" - + " \"index_patterns\" : [\n" - + " \"pattern1\",\n" - + " \"pattern2\"\n" - + " ],\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : { },\n" - + " \"aliases\" : { }\n" - + " }\n" - + " },\n" - + " \"indices\" : {\n" - + " \"index\" : {\n" - + " \"version\" : 2,\n" - + " \"mapping_version\" : 1,\n" - + " \"settings_version\" : 1,\n" - + " \"aliases_version\" : 1,\n" - + " \"routing_num_shards\" : 1,\n" - + " \"state\" : \"open\",\n" - + " \"settings\" : {\n" - + " \"index\" : {\n" - + " \"number_of_shards\" : \"1\",\n" - + " \"number_of_replicas\" : \"2\",\n" - + " \"version\" : {\n" - + " \"created\" : \"" - + Version.CURRENT.id - + "\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"mappings\" : {\n" - + " \"type\" : {\n" - + " \"type1\" : {\n" - + " \"key\" : \"value\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"aliases\" : [\n" - + " \"alias\"\n" - + " ],\n" - + " \"primary_terms\" : {\n" - + " \"0\" : 1\n" - + " },\n" - + " \"in_sync_allocations\" : {\n" - + " \"0\" : [\n" - + " \"allocationId\"\n" - + " ]\n" - + " },\n" - + " \"rollover_info\" : {\n" - + " \"rolloveAlias\" : {\n" - + " \"met_conditions\" : { },\n" - + " \"time\" : 1\n" - + " }\n" - + " },\n" - + " \"system\" : false,\n" - + " \"timestamp_range\" : {\n" - + " \"shards\" : [ ]\n" - + " }\n" - + " }\n" - + " },\n" - + " \"index-graveyard\" : {\n" - + " \"tombstones\" : [ ]\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "metadata" : { + "cluster_uuid" : "clusterUUID", + "cluster_uuid_committed" : false, + "cluster_coordination" : { + "term" : 1, + "last_committed_config" : [ + "commitedConfigurationNodeId" + ], + "last_accepted_config" : [ + "acceptedConfigurationNodeId" + ], + "voting_config_exclusions" : [ + { + "node_id" : "exlucdedNodeId", + "node_name" : "excludedNodeName" + } + ] + }, + "templates" : { + "template" : { + "order" : 0, + "index_patterns" : [ + "pattern1", + "pattern2" + ], + "settings" : { + "index" : { + "version" : { + "created" : "%s" + } + } + }, + "mappings" : { }, + "aliases" : { } + } + }, + "indices" : { + "index" : { + "version" : 2, + "mapping_version" : 1, + "settings_version" : 1, + "aliases_version" : 1, + "routing_num_shards" : 1, + "state" : "open", + "settings" : { + "index" : { + "number_of_shards" : "1", + "number_of_replicas" : "2", + "version" : { + "created" : "%s" + } + } + }, + "mappings" : { + "type" : { + "type1" : { + "key" : "value" + } + } + }, + "aliases" : [ + "alias" + ], + "primary_terms" : { + "0" : 1 + }, + "in_sync_allocations" : { + "0" : [ + "allocationId" + ] + }, + "rollover_info" : { + "rolloveAlias" : { + "met_conditions" : { }, + "time" : 1 + } + }, + "system" : false, + "timestamp_range" : { + "shards" : [ ] + } + } + }, + "index-graveyard" : { + "tombstones" : [ ] + } + } + }""".formatted(Version.CURRENT.id, Version.CURRENT.id), Strings.toString(builder)); } private Metadata buildMetadata() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java index c6adc7212355b..8512665c07057 100644 --- a/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/routing/allocation/AllocationCommandsTests.java @@ -637,17 +637,51 @@ public void testSerialization() throws Exception { } public void testXContent() throws Exception { - String commands = "{\n" - + " \"commands\" : [\n" - + " {\"allocate_empty_primary\" : {\"index\" : \"test\", \"shard\" : 1," - + " \"node\" : \"node1\", \"accept_data_loss\" : true}}\n" - + " ,{\"allocate_stale_primary\" : {\"index\" : \"test\", \"shard\" : 2," - + " \"node\" : \"node1\", \"accept_data_loss\" : true}}\n" - + " ,{\"allocate_replica\" : {\"index\" : \"test\", \"shard\" : 2, \"node\" : \"node1\"}}\n" - + " ,{\"move\" : {\"index\" : \"test\", \"shard\" : 3, \"from_node\" : \"node2\", \"to_node\" : \"node3\"}} \n" - + " ,{\"cancel\" : {\"index\" : \"test\", \"shard\" : 4, \"node\" : \"node5\", \"allow_primary\" : true}} \n" - + " ]\n" - + "}\n"; + String commands = """ + { + "commands": [ + { + "allocate_empty_primary": { + "index": "test", + "shard": 1, + "node": "node1", + "accept_data_loss": true + } + }, + { + "allocate_stale_primary": { + "index": "test", + "shard": 2, + "node": "node1", + "accept_data_loss": true + } + }, + { + "allocate_replica": { + "index": "test", + "shard": 2, + "node": "node1" + } + }, + { + "move": { + "index": "test", + "shard": 3, + "from_node": "node2", + "to_node": "node3" + } + }, + { + "cancel": { + "index": "test", + "shard": 4, + "node": "node5", + "allow_primary": true + } + } + ] + } + """; XContentParser parser = createParser(JsonXContent.jsonXContent, commands); // move two tokens, parser expected to be "on" `commands` field parser.nextToken(); diff --git a/server/src/test/java/org/elasticsearch/common/NumbersTests.java b/server/src/test/java/org/elasticsearch/common/NumbersTests.java index 02fedf1fdd417..6be3fc23ef2dc 100644 --- a/server/src/test/java/org/elasticsearch/common/NumbersTests.java +++ b/server/src/test/java/org/elasticsearch/common/NumbersTests.java @@ -236,7 +236,8 @@ public void testIsPositiveNumeric() { assertFalse(Numbers.isPositiveNumeric("test")); assertTrue(Numbers.isPositiveNumeric("9223372036854775807000000")); assertEquals( - "Cannot invoke \"String.length()\" because \"string\" is null", + """ + Cannot invoke "String.length()" because "string" is null""", expectThrows(NullPointerException.class, () -> Numbers.isPositiveNumeric(null)).getMessage() ); } diff --git a/server/src/test/java/org/elasticsearch/common/geo/GeometryParserTests.java b/server/src/test/java/org/elasticsearch/common/geo/GeometryParserTests.java index 19ad0ae61546b..3e0b164fab419 100644 --- a/server/src/test/java/org/elasticsearch/common/geo/GeometryParserTests.java +++ b/server/src/test/java/org/elasticsearch/common/geo/GeometryParserTests.java @@ -161,7 +161,8 @@ public void testNullParsing() throws Exception { // if we serialize non-null value - it should be serialized as geojson format.toXContent(new Point(100, 10), newGeoJson, ToXContent.EMPTY_PARAMS); newGeoJson.endObject(); - assertEquals("{\"val\":{\"type\":\"Point\",\"coordinates\":[100.0,10.0]}}", Strings.toString(newGeoJson)); + assertEquals(""" + {"val":{"type":"Point","coordinates":[100.0,10.0]}}""", Strings.toString(newGeoJson)); newGeoJson = XContentFactory.jsonBuilder().startObject().field("val"); format.toXContent(null, newGeoJson, ToXContent.EMPTY_PARAMS); diff --git a/server/src/test/java/org/elasticsearch/common/logging/JsonThrowablePatternConverterTests.java b/server/src/test/java/org/elasticsearch/common/logging/JsonThrowablePatternConverterTests.java index 80c21c7dd14d3..3455288990c8b 100644 --- a/server/src/test/java/org/elasticsearch/common/logging/JsonThrowablePatternConverterTests.java +++ b/server/src/test/java/org/elasticsearch/common/logging/JsonThrowablePatternConverterTests.java @@ -17,11 +17,12 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; +import java.util.regex.Pattern; import static org.hamcrest.Matchers.equalTo; public class JsonThrowablePatternConverterTests extends ESTestCase { - private static final String LINE_SEPARATOR = System.lineSeparator(); + private static final Pattern NEWLINE = Pattern.compile("\\R"); private JsonThrowablePatternConverter converter = JsonThrowablePatternConverter.newInstance(null, null); public void testNoStacktrace() throws IOException { @@ -36,26 +37,18 @@ public void testNoStacktrace() throws IOException { } public void testStacktraceWithJson() throws IOException { - - String json = "{" - + LINE_SEPARATOR - + " \"terms\" : {" - + LINE_SEPARATOR - + " \"user\" : [" - + LINE_SEPARATOR - + " \"u1\"," - + LINE_SEPARATOR - + " \"u2\"," - + LINE_SEPARATOR - + " \"u3\"" - + LINE_SEPARATOR - + " ]," - + LINE_SEPARATOR - + " \"boost\" : 1.0" - + LINE_SEPARATOR - + " }" - + LINE_SEPARATOR - + "}"; + String json = """ + { + "terms": { + "user": [ + "u1", + "u2", + "u3" + ], + "boost": 1.0 + } + }\ + """; Exception thrown = new Exception(json); LogEvent event = Log4jLogEvent.newBuilder().setMessage(new SimpleMessage("message")).setThrown(thrown).build(); @@ -67,7 +60,7 @@ public void testStacktraceWithJson() throws IOException { .findFirst() .orElseThrow(() -> new AssertionError("no logs parsed")); - int jsonLength = json.split(LINE_SEPARATOR).length; + int jsonLength = NEWLINE.split(json).length; int stacktraceLength = thrown.getStackTrace().length; assertThat( "stacktrace should formatted in multiple lines. JsonLogLine= " + jsonLogLine + " result= " + result, @@ -81,10 +74,18 @@ private String format(LogEvent event) { converter.format(event, builder); String jsonStacktraceElement = builder.toString(); - return "{\"type\": \"console\", \"timestamp\": \"2019-01-03T16:30:53,058+0100\", \"level\": \"DEBUG\", " - + "\"component\": \"o.e.a.s.TransportSearchAction\", \"cluster.name\": \"clustername\", \"node.name\": \"node-0\", " - + "\"cluster.uuid\": \"OG5MkvOrR9azuClJhWvy6Q\", \"node.id\": \"VTShUqmcQG6SzeKY5nn7qA\", \"message\": \"msg msg\" " - + jsonStacktraceElement - + "}"; + return """ + { + "type": "console", + "timestamp": "2019-01-03T16:30:53,058+0100", + "level": "DEBUG", + "component": "o.e.a.s.TransportSearchAction", + "cluster.name": "clustername", + "node.name": "node-0", + "cluster.uuid": "OG5MkvOrR9azuClJhWvy6Q", + "node.id": "VTShUqmcQG6SzeKY5nn7qA", + "message": "msg msg" + %s + }""".formatted(jsonStacktraceElement); } } diff --git a/server/src/test/java/org/elasticsearch/common/settings/SettingsTests.java b/server/src/test/java/org/elasticsearch/common/settings/SettingsTests.java index 878bfcd5b812e..b78b455a91aad 100644 --- a/server/src/test/java/org/elasticsearch/common/settings/SettingsTests.java +++ b/server/src/test/java/org/elasticsearch/common/settings/SettingsTests.java @@ -528,20 +528,23 @@ public void testToXContent() throws IOException { builder.startObject(); test.toXContent(builder, new ToXContent.MapParams(Collections.emptyMap())); builder.endObject(); - assertEquals("{\"foo\":{\"bar.baz\":\"test\",\"bar\":[\"1\",\"2\",\"3\"]}}", Strings.toString(builder)); + assertEquals(""" + {"foo":{"bar.baz":"test","bar":["1","2","3"]}}""", Strings.toString(builder)); test = Settings.builder().putList("foo.bar", "1", "2", "3").build(); builder = XContentBuilder.builder(XContentType.JSON.xContent()); builder.startObject(); test.toXContent(builder, new ToXContent.MapParams(Collections.emptyMap())); builder.endObject(); - assertEquals("{\"foo\":{\"bar\":[\"1\",\"2\",\"3\"]}}", Strings.toString(builder)); + assertEquals(""" + {"foo":{"bar":["1","2","3"]}}""", Strings.toString(builder)); builder = XContentBuilder.builder(XContentType.JSON.xContent()); builder.startObject(); test.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "true"))); builder.endObject(); - assertEquals("{\"foo.bar\":[\"1\",\"2\",\"3\"]}", Strings.toString(builder)); + assertEquals(""" + {"foo.bar":["1","2","3"]}""", Strings.toString(builder)); } public void testLoadEmptyStream() throws IOException { @@ -690,14 +693,16 @@ public void testProcessSetting() throws IOException { builder.startObject(); test.toXContent(builder, new ToXContent.MapParams(Collections.emptyMap())); builder.endObject(); - assertEquals("{\"ant.bee\":{\"cat\":\"value2\"},\"ant\":\"value1\",\"bee\":{\"cat\":\"value3\"}}", Strings.toString(builder)); + assertEquals(""" + {"ant.bee":{"cat":"value2"},"ant":"value1","bee":{"cat":"value3"}}""", Strings.toString(builder)); test = Settings.builder().put("ant", "value1").put("ant.bee.cat", "value2").put("ant.bee.cat.dog.ewe", "value3").build(); builder = XContentBuilder.builder(XContentType.JSON.xContent()); builder.startObject(); test.toXContent(builder, new ToXContent.MapParams(Collections.emptyMap())); builder.endObject(); - assertEquals("{\"ant.bee\":{\"cat.dog\":{\"ewe\":\"value3\"},\"cat\":\"value2\"},\"ant\":\"value1\"}", Strings.toString(builder)); + assertEquals(""" + {"ant.bee":{"cat.dog":{"ewe":"value3"},"cat":"value2"},"ant":"value1"}""", Strings.toString(builder)); } } diff --git a/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java b/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java index d7420e4111c76..5f273ed01ea4a 100644 --- a/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/JavaDateMathParserTests.java @@ -371,24 +371,14 @@ private void assertDateEquals(long gotMillis, String original, String expected) long expectedMillis = parser.parse(expected, () -> 0).toEpochMilli(); if (gotMillis != expectedMillis) { ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(gotMillis), ZoneOffset.UTC); - fail( - "Date math not equal\n" - + "Original : " - + original - + "\n" - + "Parsed : " - + formatter.format(zonedDateTime) - + "\n" - + "Expected : " - + expected - + "\n" - + "Expected milliseconds : " - + expectedMillis - + "\n" - + "Actual milliseconds : " - + gotMillis - + "\n" - ); + fail(""" + Date math not equal + Original : %s + Parsed : %s + Expected : %s + Expected milliseconds : %s + Actual milliseconds : %s + """.formatted(original, formatter.format(zonedDateTime), expected, expectedMillis, gotMillis)); } } } diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java b/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java index c93356af6a454..96b93568c66d2 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java @@ -692,18 +692,31 @@ public void testToXContent() throws Exception { builder.endObject(); return builder; }; - assertResult( - "{'root':{" - + "'field':'value'," - + "'array':['1','2','3']," - + "'foo':{'bar':'baz'}" - + "}," - + "'childs':[" - + "{'field':'value','array':['1','2','3'],'foo':{'bar':'baz'}}," - + "{'field':'value','foo':{'bar':'baz'}}" - + "]}", - () -> builder().value(xcontent2) - ); + assertResult(XContentHelper.stripWhitespace(""" + { + "root": { + "field": "value", + "array": [ "1", "2", "3" ], + "foo": { + "bar": "baz" + } + }, + "childs": [ + { + "field": "value", + "array": [ "1", "2", "3" ], + "foo": { + "bar": "baz" + } + }, + { + "field": "value", + "foo": { + "bar": "baz" + } + } + ] + }"""), () -> builder().value(xcontent2)); } public void testMap() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java b/server/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java index 8750aa66854a3..28598c75b3e87 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/builder/XContentBuilderTests.java @@ -96,7 +96,8 @@ public void testRaw() throws IOException { xContentBuilder.startObject(); xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}").streamInput()); xContentBuilder.endObject(); - assertThat(Strings.toString(xContentBuilder), equalTo("{\"foo\":{\"test\":\"value\"}}")); + assertThat(Strings.toString(xContentBuilder), equalTo(""" + {"foo":{"test":"value"}}""")); } { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); @@ -104,7 +105,8 @@ public void testRaw() throws IOException { xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}").streamInput()); xContentBuilder.rawField("foo1", new BytesArray("{\"test\":\"value\"}").streamInput()); xContentBuilder.endObject(); - assertThat(Strings.toString(xContentBuilder), equalTo("{\"foo\":{\"test\":\"value\"},\"foo1\":{\"test\":\"value\"}}")); + assertThat(Strings.toString(xContentBuilder), equalTo(""" + {"foo":{"test":"value"},"foo1":{"test":"value"}}""")); } { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); @@ -112,7 +114,8 @@ public void testRaw() throws IOException { xContentBuilder.field("test", "value"); xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}").streamInput()); xContentBuilder.endObject(); - assertThat(Strings.toString(xContentBuilder), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"}}")); + assertThat(Strings.toString(xContentBuilder), equalTo(""" + {"test":"value","foo":{"test":"value"}}""")); } { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); @@ -121,10 +124,8 @@ public void testRaw() throws IOException { xContentBuilder.rawField("foo", new BytesArray("{\"test\":\"value\"}").streamInput()); xContentBuilder.field("test1", "value1"); xContentBuilder.endObject(); - assertThat( - Strings.toString(xContentBuilder), - equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"test1\":\"value1\"}") - ); + assertThat(Strings.toString(xContentBuilder), equalTo(""" + {"test":"value","foo":{"test":"value"},"test1":"value1"}""")); } { XContentBuilder xContentBuilder = XContentFactory.contentBuilder(XContentType.JSON); @@ -134,10 +135,8 @@ public void testRaw() throws IOException { xContentBuilder.rawField("foo1", new BytesArray("{\"test\":\"value\"}").streamInput()); xContentBuilder.field("test1", "value1"); xContentBuilder.endObject(); - assertThat( - Strings.toString(xContentBuilder), - equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"foo1\":{\"test\":\"value\"},\"test1\":\"value1\"}") - ); + assertThat(Strings.toString(xContentBuilder), equalTo(""" + {"test":"value","foo":{"test":"value"},"foo1":{"test":"value"},"test1":"value1"}""")); } } @@ -170,7 +169,8 @@ public void testWritingBinaryToStream() throws Exception { gen.close(); String sData = bos.bytes().utf8ToString(); - assertThat(sData, equalTo("{\"name\":\"something\", source : { test : \"value\" },\"name2\":\"something2\"}")); + assertThat(sData, equalTo(""" + {"name":"something", source : { test : "value" },"name2":"something2"}""")); } public void testByteConversion() throws Exception { @@ -311,19 +311,36 @@ public void testIndentIsPlatformIndependent() throws IOException { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); builder.startObject().field("test", "foo").startObject("foo").field("foobar", "boom").endObject().endObject(); String string = Strings.toString(builder); - assertEquals("{\n" + " \"test\" : \"foo\",\n" + " \"foo\" : {\n" + " \"foobar\" : \"boom\"\n" + " }\n" + "}", string); + assertEquals(""" + { + "test" : "foo", + "foo" : { + "foobar" : "boom" + } + }""", string); builder = XContentFactory.contentBuilder(XContentType.YAML).prettyPrint(); builder.startObject().field("test", "foo").startObject("foo").field("foobar", "boom").endObject().endObject(); string = Strings.toString(builder); - assertEquals("---\n" + "test: \"foo\"\n" + "foo:\n" + " foobar: \"boom\"\n", string); + assertEquals(""" + --- + test: "foo" + foo: + foobar: "boom" + """, string); } public void testRenderGeoPoint() throws IOException { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint(); builder.startObject().field("foo").value(new GeoPoint(1, 2)).endObject(); String string = Strings.toString(builder); - assertEquals("{\n" + " \"foo\" : {\n" + " \"lat\" : 1.0,\n" + " \"lon\" : 2.0\n" + " }\n" + "}", string.trim()); + assertEquals(""" + { + "foo" : { + "lat" : 1.0, + "lon" : 2.0 + } + }""", string.trim()); } public void testWriteMapWithNullKeys() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java b/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java index 1157ec0ede7ab..f2a7162be013a 100644 --- a/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java +++ b/server/src/test/java/org/elasticsearch/index/get/GetResultTests.java @@ -80,22 +80,35 @@ public void testToXContent() throws IOException { 1, 1, true, - new BytesArray("{ \"field1\" : " + "\"value1\", \"field2\":\"value2\"}"), + new BytesArray(""" + { "field1" : "value1", "field2":"value2"}"""), singletonMap("field1", new DocumentField("field1", singletonList("value1"))), singletonMap("field1", new DocumentField("metafield", singletonList("metavalue"))) ); String output = Strings.toString(getResult); - assertEquals( - "{\"_index\":\"index\",\"_id\":\"id\",\"_version\":1,\"_seq_no\":0,\"_primary_term\":1," - + "\"metafield\":\"metavalue\",\"found\":true,\"_source\":{ \"field1\" : \"value1\", \"field2\":\"value2\"}," - + "\"fields\":{\"field1\":[\"value1\"]}}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_id": "id", + "_version": 1, + "_seq_no": 0, + "_primary_term": 1, + "metafield": "metavalue", + "found": true, + "_source": { + "field1": "value1", + "field2": "value2" + }, + "fields": { + "field1": [ "value1" ] + } + }"""), XContentHelper.stripWhitespace(output)); } { GetResult getResult = new GetResult("index", "id", UNASSIGNED_SEQ_NO, 0, 1, false, null, null, null); String output = Strings.toString(getResult); - assertEquals("{\"_index\":\"index\",\"_id\":\"id\",\"found\":false}", output); + assertEquals(""" + {"_index":"index","_id":"id","found":false}""", output); } } @@ -108,7 +121,8 @@ public void testToCompatibleXContent() throws IOException { 1, 1, true, - new BytesArray("{ \"field1\" : " + "\"value1\", \"field2\":\"value2\"}"), + new BytesArray(""" + { "field1" : "value1", "field2":"value2"}"""), singletonMap("field1", new DocumentField("field1", singletonList("value1"))), singletonMap("field1", new DocumentField("metafield", singletonList("metavalue"))) ); @@ -116,12 +130,24 @@ public void testToCompatibleXContent() throws IOException { try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent, RestApiVersion.V_7)) { getResult.toXContent(builder, ToXContent.EMPTY_PARAMS); String output = Strings.toString(builder); - assertEquals( - "{\"_index\":\"index\",\"_type\":\"_doc\",\"_id\":\"id\",\"_version\":1,\"_seq_no\":0,\"_primary_term\":1," - + "\"metafield\":\"metavalue\",\"found\":true,\"_source\":{ \"field1\" : \"value1\", \"field2\":\"value2\"}," - + "\"fields\":{\"field1\":[\"value1\"]}}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_index": "index", + "_type": "_doc", + "_id": "id", + "_version": 1, + "_seq_no": 0, + "_primary_term": 1, + "metafield": "metavalue", + "found": true, + "_source": { + "field1": "value1", + "field2": "value2" + }, + "fields": { + "field1": [ "value1" ] + } + }"""), XContentHelper.stripWhitespace(output)); } } { @@ -130,7 +156,8 @@ public void testToCompatibleXContent() throws IOException { try (XContentBuilder builder = XContentBuilder.builder(JsonXContent.jsonXContent, RestApiVersion.V_7)) { getResult.toXContent(builder, ToXContent.EMPTY_PARAMS); String output = Strings.toString(builder); - assertEquals("{\"_index\":\"index\",\"_type\":\"_doc\",\"_id\":\"id\",\"found\":false}", output); + assertEquals(""" + {"_index":"index","_type":"_doc","_id":"id","found":false}""", output); } } } @@ -189,11 +216,20 @@ public void testToXContentEmbedded() throws IOException { ); BytesReference originalBytes = toXContentEmbedded(getResult, XContentType.JSON, false); - assertEquals( - "{\"_seq_no\":0,\"_primary_term\":1,\"found\":true,\"_source\":{\"foo\":\"bar\",\"baz\":[\"baz_0\",\"baz_1\"]}," - + "\"fields\":{\"foo\":[\"bar\"],\"baz\":[\"baz_0\",\"baz_1\"]}}", - originalBytes.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_seq_no": 0, + "_primary_term": 1, + "found": true, + "_source": { + "foo": "bar", + "baz": [ "baz_0", "baz_1" ] + }, + "fields": { + "foo": [ "bar" ], + "baz": [ "baz_0", "baz_1" ] + } + }"""), originalBytes.utf8ToString()); } public void testToXContentEmbeddedNotFound() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java index 3dc44a3a73714..382faf8182a08 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java @@ -74,7 +74,8 @@ public void testSerialization() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); mapper.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals("{\"field\":{\"type\":\"boolean\"}}", Strings.toString(builder)); + assertEquals(""" + {"field":{"type":"boolean"}}""", Strings.toString(builder)); // now change some parameters defaultMapper = createDocumentMapper(fieldMapping(b -> { @@ -86,7 +87,8 @@ public void testSerialization() throws IOException { builder = XContentFactory.jsonBuilder().startObject(); mapper.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals("{\"field\":{\"type\":\"boolean\",\"doc_values\":false,\"null_value\":true}}", Strings.toString(builder)); + assertEquals(""" + {"field":{"type":"boolean","doc_values":false,"null_value":true}}""", Strings.toString(builder)); } public void testParsesBooleansStrict() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java index 72a8e0a5bb824..46426c45aeab7 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java @@ -185,11 +185,9 @@ public void testCompletionAnalyzerSettings() throws Exception { assertThat(analyzer.preservePositionIncrements(), equalTo(true)); assertThat(analyzer.preserveSep(), equalTo(false)); - assertEquals( - "{\"field\":{\"type\":\"completion\",\"analyzer\":\"simple\",\"search_analyzer\":\"standard\"," - + "\"preserve_separators\":false,\"preserve_position_increments\":true,\"max_input_length\":50}}", - Strings.toString(fieldMapper) - ); + assertEquals(""" + {"field":{"type":"completion","analyzer":"simple","search_analyzer":"standard",\ + "preserve_separators":false,"preserve_position_increments":true,"max_input_length":50}}""", Strings.toString(fieldMapper)); } @SuppressWarnings("unchecked") diff --git a/server/src/test/java/org/elasticsearch/index/mapper/CompositeRuntimeFieldTests.java b/server/src/test/java/org/elasticsearch/index/mapper/CompositeRuntimeFieldTests.java index 1c1ee5d62723f..cf211bc5fc7b9 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/CompositeRuntimeFieldTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/CompositeRuntimeFieldTests.java @@ -155,11 +155,9 @@ public void testToXContent() throws IOException { b.endObject(); })); assertEquals( - "{\"_doc\":{\"runtime\":{" - + "\"message\":{\"type\":\"composite\"," - + "\"meta\":{\"test-meta\":\"value\"}," - + "\"script\":{\"source\":\"dummy\",\"lang\":\"painless\"}," - + "\"fields\":{\"response\":{\"type\":\"long\"}}}}}}", + """ + {"_doc":{"runtime":{"message":{"type":"composite","meta":{"test-meta":"value"},\ + "script":{"source":"dummy","lang":"painless"},"fields":{"response":{"type":"long"}}}}}}""", Strings.toString(mapperService.mappingLookup().getMapping()) ); } @@ -270,12 +268,9 @@ public void testMappingUpdate() throws IOException { assertEquals(1, mappedFieldTypes.size()); assertSame(doubleSubField, mappedFieldTypes.iterator().next()); - assertEquals( - "{\"obj\":{\"type\":\"composite\"," - + "\"script\":{\"source\":\"dummy2\",\"lang\":\"painless\"}," - + "\"fields\":{\"double-subfield\":{\"type\":\"double\"}}}}", - Strings.toString(rf) - ); + assertEquals(""" + {"obj":{"type":"composite","script":{"source":"dummy2","lang":"painless"},\ + "fields":{"double-subfield":{"type":"double"}}}}""", Strings.toString(rf)); } public void testFieldDefinedTwiceWithSameName() throws IOException { @@ -418,10 +413,8 @@ public void testParseDocumentSubfieldsOutsideRuntimeObject() throws IOException assertNull(doc1.rootDoc().get("obj.long")); assertNotNull(doc1.rootDoc().get("obj.bool")); - assertEquals( - "{\"_doc\":{\"properties\":{\"obj\":{\"properties\":{\"bool\":{\"type\":\"boolean\"}}}}}}", - Strings.toString(doc1.dynamicMappingsUpdate()) - ); + assertEquals(""" + {"_doc":{"properties":{"obj":{"properties":{"bool":{"type":"boolean"}}}}}}""", Strings.toString(doc1.dynamicMappingsUpdate())); MapperService mapperService2 = createMapperService(topMapping(b -> { b.field("dynamic", "runtime"); @@ -439,9 +432,7 @@ public void testParseDocumentSubfieldsOutsideRuntimeObject() throws IOException ParsedDocument doc2 = mapperService2.documentMapper().parse(source(b -> b.field("obj.long", 1L).field("obj.bool", true))); assertNull(doc2.rootDoc().get("obj.long")); assertNull(doc2.rootDoc().get("obj.bool")); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\",\"runtime\":{\"obj.bool\":{\"type\":\"boolean\"}}}}", - Strings.toString(doc2.dynamicMappingsUpdate()) - ); + assertEquals(""" + {"_doc":{"dynamic":"runtime","runtime":{"obj.bool":{"type":"boolean"}}}}""", Strings.toString(doc2.dynamicMappingsUpdate())); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java index bec3b8294686e..a93528357aa7a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java @@ -552,8 +552,8 @@ public void testPropagateDynamicRuntimeWithDynamicMapper() throws Exception { })); assertNull(doc.rootDoc().getField("foo.bar.baz")); assertEquals( - "{\"_doc\":{\"dynamic\":\"false\"," - + "\"runtime\":{\"foo.bar.baz\":{\"type\":\"keyword\"},\"foo.baz\":{\"type\":\"keyword\"}}}}", + """ + {"_doc":{"dynamic":"false","runtime":{"foo.bar.baz":{"type":"keyword"},"foo.baz":{"type":"keyword"}}}}""", Strings.toString(doc.dynamicMappingsUpdate()) ); } @@ -805,7 +805,8 @@ public void testDynamicRuntimeLongArray() throws Exception { ParsedDocument doc = mapper.parse(source(b -> b.startArray("foo").value(0).value(1).endArray())); assertEquals(0, doc.rootDoc().getFields("foo").length); RuntimeField foo = doc.dynamicMappingsUpdate().getRoot().getRuntimeField("foo"); - assertEquals("{\"foo\":{\"type\":\"long\"}}", Strings.toString(foo)); + assertEquals(""" + {"foo":{"type":"long"}}""", Strings.toString(foo)); } public void testDynamicRuntimeDoubleArray() throws Exception { @@ -813,7 +814,8 @@ public void testDynamicRuntimeDoubleArray() throws Exception { ParsedDocument doc = mapper.parse(source(b -> b.startArray("foo").value(0.25).value(1.43).endArray())); assertEquals(0, doc.rootDoc().getFields("foo").length); RuntimeField foo = doc.dynamicMappingsUpdate().getRoot().getRuntimeField("foo"); - assertEquals("{\"foo\":{\"type\":\"double\"}}", Strings.toString(foo)); + assertEquals(""" + {"foo":{"type":"double"}}""", Strings.toString(foo)); } public void testDynamicRuntimeStringArray() throws Exception { @@ -821,7 +823,8 @@ public void testDynamicRuntimeStringArray() throws Exception { ParsedDocument doc = mapper.parse(source(b -> b.startArray("foo").value("test1").value("test2").endArray())); assertEquals(0, doc.rootDoc().getFields("foo").length); RuntimeField foo = doc.dynamicMappingsUpdate().getRoot().getRuntimeField("foo"); - assertEquals("{\"foo\":{\"type\":\"keyword\"}}", Strings.toString(foo)); + assertEquals(""" + {"foo":{"type":"keyword"}}""", Strings.toString(foo)); } public void testDynamicRuntimeBooleanArray() throws Exception { @@ -829,7 +832,8 @@ public void testDynamicRuntimeBooleanArray() throws Exception { ParsedDocument doc = mapper.parse(source(b -> b.startArray("foo").value(true).value(false).endArray())); assertEquals(0, doc.rootDoc().getFields("foo").length); RuntimeField foo = doc.dynamicMappingsUpdate().getRoot().getRuntimeField("foo"); - assertEquals("{\"foo\":{\"type\":\"boolean\"}}", Strings.toString(foo)); + assertEquals(""" + {"foo":{"type":"boolean"}}""", Strings.toString(foo)); } public void testDynamicRuntimeDateArray() throws Exception { @@ -837,7 +841,8 @@ public void testDynamicRuntimeDateArray() throws Exception { ParsedDocument doc = mapper.parse(source(b -> b.startArray("foo").value("2020-12-15").value("2020-12-09").endArray())); assertEquals(0, doc.rootDoc().getFields("foo").length); RuntimeField foo = doc.dynamicMappingsUpdate().getRoot().getRuntimeField("foo"); - assertEquals("{\"foo\":{\"type\":\"date\"}}", Strings.toString(foo)); + assertEquals(""" + {"foo":{"type":"date"}}""", Strings.toString(foo)); } public void testMappedGeoPointArray() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java index b2adcfaeaa289..f7a286f49aea1 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DynamicMappingTests.java @@ -9,6 +9,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -47,11 +48,23 @@ public void testDynamicTrue() throws IOException { assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertThat(doc.rootDoc().get("field2"), equalTo("value2")); - assertEquals( - "{\"_doc\":{\"dynamic\":\"true\",\"" - + "properties\":{\"field2\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "true", + "properties": { + "field2": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicRuntime() throws IOException { @@ -67,10 +80,8 @@ public void testDynamicRuntime() throws IOException { assertThat(doc.rootDoc().get("field1"), equalTo("value1")); assertNull(doc.rootDoc().get("field2")); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\"," + "\"runtime\":{\"field2\":{\"type\":\"keyword\"}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(""" + {"_doc":{"dynamic":"runtime","runtime":{"field2":{"type":"keyword"}}}}""", Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicFalse() throws IOException { @@ -186,11 +197,22 @@ public void testDynamicField() throws Exception { DocumentMapper mapper = createDocumentMapper(mapping(b -> {})); ParsedDocument doc = mapper.parse(source(b -> b.field("foo", "bar"))); assertNotNull(doc.dynamicMappingsUpdate()); - assertEquals( - "{\"_doc\":{\"properties\":{\"foo\":{\"type\":\"text\",\"fields\":" - + "{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "properties": { + "foo": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } @@ -198,21 +220,43 @@ public void testDynamicFieldOnIncorrectDate() throws Exception { DocumentMapper mapper = createDocumentMapper(mapping(b -> {})); ParsedDocument doc = mapper.parse(source(b -> b.field("foo", "2020-01-01T01-01-01Z"))); assertNotNull(doc.dynamicMappingsUpdate()); - assertEquals( - "{\"_doc\":{\"properties\":{\"foo\":{\"type\":\"text\",\"fields\":" - + "{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "properties": { + "foo": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicUpdateWithRuntimeField() throws Exception { MapperService mapperService = createMapperService(runtimeFieldMapping(b -> b.field("type", "keyword"))); ParsedDocument doc = mapperService.documentMapper().parse(source(b -> b.field("test", "value"))); - assertEquals( - "{\"_doc\":{\"properties\":{" - + "\"test\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate().getRoot()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "properties": { + "test": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate().getRoot())); merge(mapperService, dynamicMapping(doc.dynamicMappingsUpdate())); Mapping merged = mapperService.documentMapper().mapping(); assertNotNull(merged.getRoot().getMapper("test")); @@ -296,10 +340,8 @@ public void testObject() throws Exception { })); assertNotNull(doc.dynamicMappingsUpdate()); - assertThat( - Strings.toString(doc.dynamicMappingsUpdate()), - containsString("{\"foo\":{\"properties\":{\"bar\":{\"properties\":{\"baz\":{\"type\":\"text\"") - ); + assertThat(Strings.toString(doc.dynamicMappingsUpdate()), containsString(""" + {"foo":{"properties":{"bar":{"properties":{"baz":{"type":"text\"""")); } public void testDynamicRuntimeFieldWithinObject() throws Exception { @@ -312,10 +354,17 @@ public void testDynamicRuntimeFieldWithinObject() throws Exception { b.endObject(); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\"," + "\"runtime\":{\"foo.bar.baz\":{\"type\":\"long\"}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "runtime", + "runtime": { + "foo.bar.baz": { + "type": "long" + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicRuntimeMappingDynamicObject() throws Exception { @@ -335,13 +384,35 @@ public void testDynamicRuntimeMappingDynamicObject() throws Exception { b.endObject(); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\"," - + "\"runtime\":{\"object.foo.bar.baz\":{\"type\":\"long\"}}," - + "\"properties\":{\"dynamic_object\":{\"dynamic\":\"true\"," - + "\"properties\":{\"foo\":{\"properties\":{\"bar\":{\"properties\":{\"baz\":{\"type\":\"long\"}}}}}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "runtime", + "runtime": { + "object.foo.bar.baz": { + "type": "long" + } + }, + "properties": { + "dynamic_object": { + "dynamic": "true", + "properties": { + "foo": { + "properties": { + "bar": { + "properties": { + "baz": { + "type": "long" + } + } + } + } + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicMappingDynamicRuntimeObject() throws Exception { @@ -361,13 +432,40 @@ public void testDynamicMappingDynamicRuntimeObject() throws Exception { b.endObject(); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"true\",\"" - + "runtime\":{\"runtime_object.foo.bar.baz\":{\"type\":\"keyword\"}}," - + "\"properties\":{\"object\":{\"properties\":{\"foo\":{\"properties\":{\"bar\":{\"properties\":{" - + "\"baz\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "true", + "runtime": { + "runtime_object.foo.bar.baz": { + "type": "keyword" + } + }, + "properties": { + "object": { + "properties": { + "foo": { + "properties": { + "bar": { + "properties": { + "baz": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + } + } + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testArray() throws Exception { @@ -375,7 +473,8 @@ public void testArray() throws Exception { ParsedDocument doc = mapper.parse(source(b -> b.startArray("foo").value("bar").value("baz").endArray())); assertNotNull(doc.dynamicMappingsUpdate()); - assertThat(Strings.toString(doc.dynamicMappingsUpdate()), containsString("{\"foo\":{\"type\":\"text\"")); + assertThat(Strings.toString(doc.dynamicMappingsUpdate()), containsString(""" + {"foo":{"type":"text\"""")); } public void testInnerDynamicMapping() throws Exception { @@ -389,10 +488,8 @@ public void testInnerDynamicMapping() throws Exception { })); assertNotNull(doc.dynamicMappingsUpdate()); - assertThat( - Strings.toString(doc.dynamicMappingsUpdate()), - containsString("{\"field\":{\"properties\":{\"bar\":{\"properties\":{\"baz\":{\"type\":\"text\"") - ); + assertThat(Strings.toString(doc.dynamicMappingsUpdate()), containsString(""" + {"field":{"properties":{"bar":{"properties":{"baz":{"type":"text\"""")); } public void testComplexArray() throws Exception { @@ -406,11 +503,29 @@ public void testComplexArray() throws Exception { b.endArray(); })); assertNotNull(doc.dynamicMappingsUpdate()); - assertEquals( - "{\"_doc\":{\"properties\":{\"foo\":{\"properties\":{\"bar\":{\"type\":\"text\",\"fields\":{" - + "\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}},\"baz\":{\"type\":\"long\"}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "properties": { + "foo": { + "properties": { + "bar": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + }, + "baz": { + "type": "long" + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testReuseExistingMappings() throws Exception { @@ -541,15 +656,29 @@ public void testDynamicRuntimeLeafFields() throws IOException { b.field("boolean", true); b.field("date", "2020-12-15"); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\"," - + "\"runtime\":{\"boolean\":{\"type\":\"boolean\"}," - + "\"date\":{\"type\":\"date\"}," - + "\"double\":{\"type\":\"double\"}," - + "\"long\":{\"type\":\"long\"}," - + "\"string\":{\"type\":\"keyword\"}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "runtime", + "runtime": { + "boolean": { + "type": "boolean" + }, + "date": { + "type": "date" + }, + "double": { + "type": "double" + }, + "long": { + "type": "long" + }, + "string": { + "type": "keyword" + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicRuntimeWithDynamicDateFormats() throws IOException { @@ -561,12 +690,22 @@ public void testDynamicRuntimeWithDynamicDateFormats() throws IOException { b.field("date1", "15/12/2020"); b.field("date2", "15-12-2020"); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\"," - + "\"runtime\":{\"date1\":{\"type\":\"date\",\"format\":\"dd/MM/yyyy\"}," - + "\"date2\":{\"type\":\"date\",\"format\":\"dd-MM-yyyy\"}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "runtime", + "runtime": { + "date1": { + "type": "date", + "format": "dd/MM/yyyy" + }, + "date2": { + "type": "date", + "format": "dd-MM-yyyy" + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicRuntimeWithinObjects() throws IOException { @@ -582,16 +721,49 @@ public void testDynamicRuntimeWithinObjects() throws IOException { b.startObject("dynamic_true").field("field1", "text").startObject("child").field("field2", "text").endObject().endObject(); b.startObject("dynamic_runtime").field("field3", "text").startObject("child").field("field4", "text").endObject().endObject(); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"false\"," - + "\"runtime\":{\"dynamic_runtime.child.field4\":{\"type\":\"keyword\"}," - + "\"dynamic_runtime.field3\":{\"type\":\"keyword\"}}," - + "\"properties\":{" - + "\"dynamic_true\":{\"dynamic\":\"true\",\"properties\":{\"child\":{\"properties\":{" - + "\"field2\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}," - + "\"field1\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}}}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "false", + "runtime": { + "dynamic_runtime.child.field4": { + "type": "keyword" + }, + "dynamic_runtime.field3": { + "type": "keyword" + } + }, + "properties": { + "dynamic_true": { + "dynamic": "true", + "properties": { + "child": { + "properties": { + "field2": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + }, + "field1": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + } + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } public void testDynamicRuntimeDotsInFieldNames() throws IOException { @@ -602,13 +774,25 @@ public void testDynamicRuntimeDotsInFieldNames() throws IOException { b.array("one.two", 1.2, 1.2, 1.2); b.field("one", "one"); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\",\"runtime\":{" - + "\"one\":{\"type\":\"keyword\"}," - + "\"one.two\":{\"type\":\"double\"}," - + "\"one.two.three\":{\"type\":\"long\"}," - + "\"one.two.three.four\":{\"type\":\"keyword\"}}}}", - Strings.toString(doc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "runtime", + "runtime": { + "one": { + "type": "keyword" + }, + "one.two": { + "type": "double" + }, + "one.two.three": { + "type": "long" + }, + "one.two.three.four": { + "type": "keyword" + } + } + } + }"""), Strings.toString(doc.dynamicMappingsUpdate())); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplateTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplateTests.java index 994face978308..6b028b8cf2012 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplateTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplateTests.java @@ -89,7 +89,8 @@ public void testMappingForName() throws IOException { DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef); Map stringObjectMap = template.mappingForName("my_name", "my_type"); assertEquals( - "{\"field1_my_name\":\"my_type\",\"test\":[\"field2_my_name_my_type\"]}", + """ + {"field1_my_name":"my_type","test":["field2_my_name_my_type"]}""", Strings.toString(JsonXContent.contentBuilder().map(stringObjectMap)) ); } @@ -101,7 +102,8 @@ public void testMappingForNameRuntime() throws IOException { DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef); Map stringObjectMap = template.mappingForName("my_name", "my_type"); assertEquals( - "{\"field1_my_name\":\"my_type\",\"test\":[\"field2_my_name_my_type\"]}", + """ + {"field1_my_name":"my_type","test":["field2_my_name_my_type"]}""", Strings.toString(JsonXContent.contentBuilder().map(stringObjectMap)) ); } @@ -285,7 +287,8 @@ public void testSerialization() throws Exception { DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef); XContentBuilder builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"match_mapping_type\":\"string\",\"mapping\":{\"store\":true}}", Strings.toString(builder)); + assertEquals(""" + {"match_mapping_type":"string","mapping":{"store":true}}""", Strings.toString(builder)); // name-based template templateDef = new HashMap<>(); @@ -298,7 +301,8 @@ public void testSerialization() throws Exception { template = DynamicTemplate.parse("my_template", templateDef); builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"match\":\"*name\",\"unmatch\":\"first_name\",\"mapping\":{\"store\":true}}", Strings.toString(builder)); + assertEquals(""" + {"match":"*name","unmatch":"first_name","mapping":{"store":true}}""", Strings.toString(builder)); // path-based template templateDef = new HashMap<>(); @@ -311,7 +315,8 @@ public void testSerialization() throws Exception { template = DynamicTemplate.parse("my_template", templateDef); builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"path_match\":\"*name\",\"path_unmatch\":\"first_name\",\"mapping\":{\"store\":true}}", Strings.toString(builder)); + assertEquals(""" + {"path_match":"*name","path_unmatch":"first_name","mapping":{"store":true}}""", Strings.toString(builder)); // regex matching templateDef = new HashMap<>(); @@ -324,7 +329,8 @@ public void testSerialization() throws Exception { template = DynamicTemplate.parse("my_template", templateDef); builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"match\":\"^a$\",\"match_pattern\":\"regex\",\"mapping\":{\"store\":true}}", Strings.toString(builder)); + assertEquals(""" + {"match":"^a$","match_pattern":"regex","mapping":{"store":true}}""", Strings.toString(builder)); // empty condition templateDef = new HashMap<>(); @@ -332,7 +338,8 @@ public void testSerialization() throws Exception { template = DynamicTemplate.parse("my_template", templateDef); builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat(Strings.toString(builder), equalTo("{\"mapping\":{\"store\":true}}")); + assertThat(Strings.toString(builder), equalTo(""" + {"mapping":{"store":true}}""")); } public void testSerializationRuntimeMappings() throws Exception { @@ -343,7 +350,8 @@ public void testSerializationRuntimeMappings() throws Exception { DynamicTemplate template = DynamicTemplate.parse("my_template", templateDef); XContentBuilder builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"match_mapping_type\":\"string\",\"runtime\":{}}", Strings.toString(builder)); + assertEquals(""" + {"match_mapping_type":"string","runtime":{}}""", Strings.toString(builder)); // name-based template templateDef = new HashMap<>(); @@ -356,7 +364,8 @@ public void testSerializationRuntimeMappings() throws Exception { template = DynamicTemplate.parse("my_template", templateDef); builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"match\":\"*name\",\"unmatch\":\"first_name\",\"runtime\":{\"type\":\"new_type\"}}", Strings.toString(builder)); + assertEquals(""" + {"match":"*name","unmatch":"first_name","runtime":{"type":"new_type"}}""", Strings.toString(builder)); // path-based template templateDef = new HashMap<>(); @@ -369,7 +378,8 @@ public void testSerializationRuntimeMappings() throws Exception { template = DynamicTemplate.parse("my_template", templateDef); builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"path_match\":\"*name\",\"path_unmatch\":\"first_name\",\"runtime\":{}}", Strings.toString(builder)); + assertEquals(""" + {"path_match":"*name","path_unmatch":"first_name","runtime":{}}""", Strings.toString(builder)); // regex matching templateDef = new HashMap<>(); @@ -382,7 +392,8 @@ public void testSerializationRuntimeMappings() throws Exception { template = DynamicTemplate.parse("my_template", templateDef); builder = JsonXContent.contentBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"match\":\"^a$\",\"match_pattern\":\"regex\",\"runtime\":{}}", Strings.toString(builder)); + assertEquals(""" + {"match":"^a$","match_pattern":"regex","runtime":{}}""", Strings.toString(builder)); } public void testMatchTemplateName() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java index 2db5239b483c7..acac3b69a4d35 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -196,11 +197,10 @@ public void testDynamicMapperWithBadMapping() throws IOException { } b.endArray(); })); - assertWarnings( - "dynamic template [test] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{\"badparam\":false}}], " - + "attempted to validate it with the following match_mapping_type: [string], last error: " - + "[unknown parameter [badparam] on mapper [__dynamic__test] of type [null]]" - ); + assertWarnings(""" + dynamic template [test] has invalid content [{"match_mapping_type":"string","mapping":{"badparam":false}}], \ + attempted to validate it with the following match_mapping_type: [string], last error: \ + [unknown parameter [badparam] on mapper [__dynamic__test] of type [null]]"""); mapper.parse(source(b -> b.field("field", "foo"))); assertWarnings( @@ -499,12 +499,10 @@ public void testIllegalDynamicTemplateUnknownAttributeRuntime() throws Exception mapping.endObject(); MapperParsingException e = expectThrows(MapperParsingException.class, () -> createMapperService(mapping)); - assertEquals( - "Failed to parse mapping: dynamic template [my_template] has invalid content [" - + "{\"match_mapping_type\":\"string\",\"runtime\":{\"foo\":\"bar\",\"type\":\"keyword\"}}], " - + "attempted to validate it with the following match_mapping_type: [string]", - e.getMessage() - ); + assertEquals(""" + Failed to parse mapping: dynamic template [my_template] has invalid content \ + [{"match_mapping_type":"string","runtime":{"foo":"bar","type":"keyword"}}], \ + attempted to validate it with the following match_mapping_type: [string]""", e.getMessage()); assertEquals("unknown parameter [foo] on runtime field [__dynamic__my_template] of type [keyword]", e.getRootCause().getMessage()); } @@ -656,11 +654,10 @@ public void testIllegalDynamicTemplate7DotXIndex() throws Exception { Version createdVersion = randomVersionBetween(random(), Version.V_7_0_0, Version.V_7_7_0); MapperService mapperService = createMapperService(createdVersion, mapping); assertThat(mapperService.documentMapper().mappingSource().toString(), containsString("\"type\":\"string\"")); - assertWarnings( - "dynamic template [my_template] has invalid content [{\"match_mapping_type\":\"string\",\"mapping\":{\"type\":" - + "\"string\"}}], attempted to validate it with the following match_mapping_type: [string], " - + "last error: [No mapper found for type [string]]" - ); + assertWarnings(""" + dynamic template [my_template] has invalid content \ + [{"match_mapping_type":"string","mapping":{"type":"string"}}], attempted to validate it \ + with the following match_mapping_type: [string], last error: [No mapper found for type [string]]"""); } public void testTemplateWithoutMatchPredicates() throws Exception { @@ -685,16 +682,11 @@ public void testTemplateWithoutMatchPredicates() throws Exception { } mapping.endObject(); MapperService mapperService = createMapperService(mapping); + final String json = """ + {"foo": "41.12,-71.34", "bar": "41.12,-71.34"} + """; ParsedDocument doc = mapperService.documentMapper() - .parse( - new SourceToParse( - "1", - new BytesArray("{\"foo\": \"41.12,-71.34\", \"bar\": \"41.12,-71.34\"}"), - XContentType.JSON, - null, - Map.of("foo", "geo_point") - ) - ); + .parse(new SourceToParse("1", new BytesArray(json), XContentType.JSON, null, Map.of("foo", "geo_point"))); assertThat(doc.rootDoc().getFields("foo"), arrayWithSize(2)); assertThat(doc.rootDoc().getFields("bar"), arrayWithSize(1)); } @@ -845,7 +837,8 @@ public void testDynamicTemplateRuntimeMatchMappingType() throws Exception { b.field("l", 1); })); assertEquals( - "{\"_doc\":{\"runtime\":{\"s\":{\"type\":\"long\"}},\"properties\":{\"l\":{\"type\":\"long\"}}}}", + """ + {"_doc":{"runtime":{"s":{"type":"long"}},"properties":{"l":{"type":"long"}}}}""", Strings.toString(parsedDoc.dynamicMappingsUpdate()) ); } @@ -884,19 +877,53 @@ public void testDynamicTemplateRuntimeMatch() throws Exception { b.endObject(); b.endArray(); })); - assertEquals( - "{\"_doc\":{\"runtime\":{" - + "\"field_array.field_double\":{\"type\":\"double\"}," - + "\"field_boolean\":{\"type\":\"boolean\"}," - + "\"field_long\":{\"type\":\"long\"}," - + "\"field_object.field_date\":{\"type\":\"date\"}," - + "\"field_string\":{\"type\":\"keyword\"}}," - + "\"properties\":" - + "{\"concrete_string\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}," - + "\"field_array\":{\"properties\":{\"concrete_double\":{\"type\":\"float\"}}}," - + "\"field_object\":{\"properties\":{\"concrete_date\":{\"type\":\"date\"}}}}}}", - Strings.toString(parsedDoc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "runtime": { + "field_array.field_double": { + "type": "double" + }, + "field_boolean": { + "type": "boolean" + }, + "field_long": { + "type": "long" + }, + "field_object.field_date": { + "type": "date" + }, + "field_string": { + "type": "keyword" + } + }, + "properties": { + "concrete_string": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + }, + "field_array": { + "properties": { + "concrete_double": { + "type": "float" + } + } + }, + "field_object": { + "properties": { + "concrete_date": { + "type": "date" + } + } + } + } + } + }"""), Strings.toString(parsedDoc.dynamicMappingsUpdate())); } public void testDynamicTemplateRuntimePathMatch() throws Exception { @@ -929,18 +956,45 @@ public void testDynamicTemplateRuntimePathMatch() throws Exception { b.endObject(); b.startObject("concrete").field("boolean", true).endObject(); })); - assertEquals( - "{\"_doc\":{\"runtime\":{" - + "\"object.date\":{\"type\":\"date\"}," - + "\"object.long\":{\"type\":\"long\"}," - + "\"object.object.string\":{\"type\":\"keyword\"}}," - + "\"properties\":" - + "{" - + "\"concrete\":{\"properties\":{\"boolean\":{\"type\":\"boolean\"}}}," - + "\"double\":{\"type\":\"float\"}," - + "\"object\":{\"properties\":{\"object\":{\"properties\":{\"concrete\":{\"type\":\"boolean\"}}}}}}}}", - Strings.toString(parsedDoc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "runtime": { + "object.date": { + "type": "date" + }, + "object.long": { + "type": "long" + }, + "object.object.string": { + "type": "keyword" + } + }, + "properties": { + "concrete": { + "properties": { + "boolean": { + "type": "boolean" + } + } + }, + "double": { + "type": "float" + }, + "object": { + "properties": { + "object": { + "properties": { + "concrete": { + "type": "boolean" + } + } + } + } + } + } + } + }"""), Strings.toString(parsedDoc.dynamicMappingsUpdate())); } public void testDynamicRuntimeWithDynamicTemplate() throws IOException { @@ -966,13 +1020,22 @@ public void testDynamicRuntimeWithDynamicTemplate() throws IOException { b.field("double", 1.23); b.field("concrete_double", 1.23); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\"," - + "\"runtime\":{" - + "\"double\":{\"type\":\"double\"}}," - + "\"properties\":{\"concrete_double\":{\"type\":\"float\"}}}}", - Strings.toString(parsedDoc.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "runtime", + "runtime": { + "double": { + "type": "double" + } + }, + "properties": { + "concrete_double": { + "type": "float" + } + } + } + }"""), Strings.toString(parsedDoc.dynamicMappingsUpdate())); DocumentMapper documentMapper = createDocumentMapper(topMapping(b -> { b.field("dynamic", ObjectMapper.Dynamic.RUNTIME); @@ -995,11 +1058,21 @@ public void testDynamicRuntimeWithDynamicTemplate() throws IOException { b.field("s", "hello"); b.field("l", 1); })); - assertEquals( - "{\"_doc\":{\"dynamic\":\"runtime\"," - + "\"runtime\":{\"l\":{\"type\":\"long\"}}," - + "\"properties\":{\"s\":{\"type\":\"keyword\"}}}}", - Strings.toString(parsedDoc2.dynamicMappingsUpdate()) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "dynamic": "runtime", + "runtime": { + "l": { + "type": "long" + } + }, + "properties": { + "s": { + "type": "keyword" + } + } + } + }"""), Strings.toString(parsedDoc2.dynamicMappingsUpdate())); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FieldFilterMapperPluginTests.java b/server/src/test/java/org/elasticsearch/index/mapper/FieldFilterMapperPluginTests.java index 4ff57354123bd..40c80c976081f 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FieldFilterMapperPluginTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FieldFilterMapperPluginTests.java @@ -280,71 +280,69 @@ public Function> getFieldFilter() { "properties.value.keyword_visible" ); - private static final String TEST_ITEM = "{\n" - + " \"_doc\": {\n" - + " \"_meta\": {\n" - + " \"version\":0.19\n" - + " }," - + " \"_routing\": {\n" - + " \"required\":true\n" - + " }," - + " \"_source\": {\n" - + " \"enabled\":false\n" - + " }," - + " \"properties\": {\n" - + " \"name\": {\n" - + " \"properties\": {\n" - + " \"first\": {\n" - + " \"type\": \"keyword\"\n" - + " },\n" - + " \"last_visible\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"birth\": {\n" - + " \"type\": \"date\"\n" - + " },\n" - + " \"age_visible\": {\n" - + " \"type\": \"integer\"\n" - + " },\n" - + " \"address\": {\n" - + " \"type\": \"object\",\n" - + " \"properties\": {\n" - + " \"street\": {\n" - + " \"type\": \"keyword\"\n" - + " },\n" - + " \"location\": {\n" - + " \"type\": \"geo_point\"\n" - + " },\n" - + " \"area_visible\": {\n" - + " \"type\": \"geo_shape\"" - + " }\n" - + " }\n" - + " },\n" - + " \"properties\": {\n" - + " \"type\": \"nested\",\n" - + " \"properties\": {\n" - + " \"key_visible\" : {\n" - + " \"type\": \"text\",\n" - + " \"fields\": {\n" - + " \"keyword\" : {\n" - + " \"type\" : \"keyword\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"value\" : {\n" - + " \"type\": \"text\",\n" - + " \"fields\": {\n" - + " \"keyword_visible\" : {\n" - + " \"type\" : \"keyword\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + private static final String TEST_ITEM = """ + { + "_doc": { + "_meta": { + "version":0.19 + }, "_routing": { + "required":true + }, "_source": { + "enabled":false + }, "properties": { + "name": { + "properties": { + "first": { + "type": "keyword" + }, + "last_visible": { + "type": "keyword" + } + } + }, + "birth": { + "type": "date" + }, + "age_visible": { + "type": "integer" + }, + "address": { + "type": "object", + "properties": { + "street": { + "type": "keyword" + }, + "location": { + "type": "geo_point" + }, + "area_visible": { + "type": "geo_shape" + } + } + }, + "properties": { + "type": "nested", + "properties": { + "key_visible" : { + "type": "text", + "fields": { + "keyword" : { + "type" : "keyword" + } + } + }, + "value" : { + "type": "text", + "fields": { + "keyword_visible" : { + "type" : "keyword" + } + } + } + } + } + } + } + } + }"""; } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/MapperServiceTests.java b/server/src/test/java/org/elasticsearch/index/mapper/MapperServiceTests.java index 373a3ed92e7ac..e6b110dee721a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/MapperServiceTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/MapperServiceTests.java @@ -283,7 +283,8 @@ public void testMappingUpdateChecks() throws IOException { // Text fields are not stored by default, so an incoming update that is identical but // just has `stored:false` should not require an update - builder.putMapping("{\"properties\":{\"field\":{\"type\":\"text\",\"store\":\"false\"}}}"); + builder.putMapping(""" + {"properties":{"field":{"type":"text","store":"false"}}}"""); assertTrue(mapperService.assertNoUpdateRequired(builder.build())); } @@ -297,7 +298,8 @@ public void testMappingUpdateChecks() throws IOException { builder.settings(settings); // However, an update that really does need a rebuild will throw an exception - builder.putMapping("{\"properties\":{\"field\":{\"type\":\"text\",\"store\":\"true\"}}}"); + builder.putMapping(""" + {"properties":{"field":{"type":"text","store":"true"}}}"""); Exception e = expectThrows(IllegalStateException.class, () -> mapperService.assertNoUpdateRequired(builder.build())); assertThat(e.getMessage(), containsString("expected current mapping [")); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java index 2e1de1e2699b1..d9677ac35a57f 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java @@ -1141,10 +1141,8 @@ public void testMergeNestedMappingsFromDynamicUpdate() throws IOException { merge(mapperService, Strings.toString(doc.dynamicMappingsUpdate())); merge(mapperService, Strings.toString(doc.dynamicMappingsUpdate())); - assertThat( - Strings.toString(mapperService.documentMapper().mapping()), - containsString("\"properties\":{\"object\":{\"type\":\"nested\",\"include_in_parent\":true}}") - ); + assertThat(Strings.toString(mapperService.documentMapper().mapping()), containsString(""" + "properties":{"object":{"type":"nested","include_in_parent":true}}""")); } public void testFieldNames() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperTests.java index 1129348bfa6b4..67ae6f0eb2e8a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperTests.java @@ -27,27 +27,20 @@ public void testDifferentInnerObjectTokenFailure() throws Exception { DocumentMapper defaultMapper = createDocumentMapper(mapping(b -> {})); IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> defaultMapper.parse( - new SourceToParse( - "1", - new BytesArray( - " {\n" - + " \"object\": {\n" - + " \"array\":[\n" - + " {\n" - + " \"object\": { \"value\": \"value\" }\n" - + " },\n" - + " {\n" - + " \"object\":\"value\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"value\":\"value\"\n" - + " }" - ), - XContentType.JSON - ) - ) + () -> defaultMapper.parse(new SourceToParse("1", new BytesArray(""" + { + "object": { + "array":[ + { + "object": { "value": "value" } + }, + { + "object":"value" + } + ] + }, + "value":"value" + }""".indent(1)), XContentType.JSON)) ); assertTrue(e.getMessage(), e.getMessage().contains("cannot be changed from type")); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/ParametrizedMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/ParametrizedMapperTests.java index b8dd766cf5b15..b5f4e4ed4dc09 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/ParametrizedMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/ParametrizedMapperTests.java @@ -296,7 +296,9 @@ private String toStringWithDefaults(ToXContent value) throws IOException { // defaults - create empty builder config, and serialize with and without defaults public void testDefaults() throws IOException { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","required":"value"}"""; + TestMapper mapper = fromMapping(mapping); assertTrue(mapper.fixed); @@ -304,61 +306,84 @@ public void testDefaults() throws IOException { assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); - assertEquals( - "{\"field\":{\"type\":\"test_mapper\",\"fixed\":true," - + "\"fixed2\":false,\"variable\":\"default\",\"index\":true," - + "\"wrapper\":\"default\",\"int_value\":5,\"analyzer\":\"_keyword\"," - + "\"required\":\"value\",\"restricted\":\"foo\",\"enum_field\":\"name1\"," - + "\"restricted_enum_field\":\"name1\"}}", - toStringWithDefaults(mapper) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "field": { + "type": "test_mapper", + "fixed": true, + "fixed2": false, + "variable": "default", + "index": true, + "wrapper": "default", + "int_value": 5, + "analyzer": "_keyword", + "required": "value", + "restricted": "foo", + "enum_field": "name1", + "restricted_enum_field": "name1" + } + }"""), toStringWithDefaults(mapper)); } // merging - try updating 'fixed' and 'fixed2' should get an error, try updating 'variable' and verify update public void testMerging() { - String mapping = "{\"type\":\"test_mapper\",\"fixed\":false,\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","fixed":false,"required":"value"}"""; TestMapper mapper = fromMapping(mapping); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); - TestMapper badMerge = fromMapping("{\"type\":\"test_mapper\",\"fixed\":true,\"fixed2\":true,\"required\":\"value\"}"); + TestMapper badMerge = fromMapping(""" + {"type":"test_mapper","fixed":true,"fixed2":true,"required":"value"}"""); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> mapper.merge(badMerge)); - String expectedError = "Mapper for [field] conflicts with existing mapper:\n" - + "\tCannot update parameter [fixed] from [false] to [true]\n" - + "\tCannot update parameter [fixed2] from [false] to [true]"; + String expectedError = """ + Mapper for [field] conflicts with existing mapper: + \tCannot update parameter [fixed] from [false] to [true] + \tCannot update parameter [fixed2] from [false] to [true]"""; assertEquals(expectedError, e.getMessage()); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); // original mapping is unaffected // TODO: should we have to include 'fixed' here? Or should updates take as 'defaults' the existing values? - TestMapper goodMerge = fromMapping("{\"type\":\"test_mapper\",\"fixed\":false,\"variable\":\"updated\",\"required\":\"value\"}"); + TestMapper goodMerge = fromMapping(""" + {"type":"test_mapper","fixed":false,"variable":"updated","required":"value"}"""); TestMapper merged = (TestMapper) mapper.merge(goodMerge); - assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); // original mapping is unaffected - assertEquals( - "{\"field\":{\"type\":\"test_mapper\",\"fixed\":false,\"variable\":\"updated\",\"required\":\"value\"}}", - Strings.toString(merged) - ); + assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); // original mapping is unaffected + assertEquals(""" + {"field":{"type":"test_mapper","fixed":false,"variable":"updated","required":"value"}}\ + """, Strings.toString(merged)); } // add multifield, verify, add second multifield, verify, overwrite second multifield - public void testMultifields() { - String mapping = "{\"type\":\"test_mapper\",\"variable\":\"foo\",\"required\":\"value\"," - + "\"fields\":{\"sub\":{\"type\":\"keyword\"}}}"; + public void testMultifields() throws IOException { + String mapping = """ + {"type":"test_mapper","variable":"foo","required":"value","fields":{"sub":{"type":"keyword"}}}"""; TestMapper mapper = fromMapping(mapping); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); - String addSubField = "{\"type\":\"test_mapper\",\"variable\":\"foo\",\"required\":\"value\"" - + ",\"fields\":{\"sub2\":{\"type\":\"keyword\"}}}"; + String addSubField = """ + {"type":"test_mapper","variable":"foo","required":"value","fields":{"sub2":{"type":"keyword"}}}"""; TestMapper toMerge = fromMapping(addSubField); TestMapper merged = (TestMapper) mapper.merge(toMerge); - assertEquals( - "{\"field\":{\"type\":\"test_mapper\",\"variable\":\"foo\",\"required\":\"value\"," - + "\"fields\":{\"sub\":{\"type\":\"keyword\"},\"sub2\":{\"type\":\"keyword\"}}}}", - Strings.toString(merged) - ); - - String badSubField = "{\"type\":\"test_mapper\",\"variable\":\"foo\",\"required\":\"value\"," - + "\"fields\":{\"sub2\":{\"type\":\"binary\"}}}"; + assertEquals(XContentHelper.stripWhitespace(""" + { + "field": { + "type": "test_mapper", + "variable": "foo", + "required": "value", + "fields": { + "sub": { + "type": "keyword" + }, + "sub2": { + "type": "keyword" + } + } + } + }"""), Strings.toString(merged)); + + String badSubField = """ + {"type":"test_mapper","variable":"foo","required":"value","fields":{"sub2":{"type":"binary"}}}"""; TestMapper badToMerge = fromMapping(badSubField); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> merged.merge(badToMerge)); assertEquals("mapper [field.sub2] cannot be changed from type [keyword] to [binary]", e.getMessage()); @@ -366,46 +391,66 @@ public void testMultifields() { // add copy_to, verify public void testCopyTo() { - String mapping = "{\"type\":\"test_mapper\",\"variable\":\"foo\",\"required\":\"value\",\"copy_to\":[\"other\"]}"; + String mapping = """ + {"type":"test_mapper","variable":"foo","required":"value","copy_to":["other"]}"""; TestMapper mapper = fromMapping(mapping); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); // On update, copy_to is completely replaced - TestMapper toMerge = fromMapping( - "{\"type\":\"test_mapper\",\"variable\":\"updated\",\"required\":\"value\"," + "\"copy_to\":[\"foo\",\"bar\"]}" - ); + TestMapper toMerge = fromMapping(""" + {"type":"test_mapper","variable":"updated","required":"value","copy_to":["foo","bar"]}"""); TestMapper merged = (TestMapper) mapper.merge(toMerge); - assertEquals( - "{\"field\":{\"type\":\"test_mapper\",\"variable\":\"updated\",\"required\":\"value\"," + "\"copy_to\":[\"foo\",\"bar\"]}}", - Strings.toString(merged) - ); + assertEquals(""" + {"field":{"type":"test_mapper","variable":"updated","required":"value","copy_to":["foo","bar"]}}""", Strings.toString(merged)); - TestMapper removeCopyTo = fromMapping("{\"type\":\"test_mapper\",\"variable\":\"updated\",\"required\":\"value\"}"); + TestMapper removeCopyTo = fromMapping(""" + {"type":"test_mapper","variable":"updated","required":"value"}"""); TestMapper noCopyTo = (TestMapper) merged.merge(removeCopyTo); - assertEquals("{\"field\":{\"type\":\"test_mapper\",\"variable\":\"updated\",\"required\":\"value\"}}", Strings.toString(noCopyTo)); + assertEquals(""" + {"field":{"type":"test_mapper","variable":"updated","required":"value"}}""", Strings.toString(noCopyTo)); } public void testNullables() { - String mapping = "{\"type\":\"test_mapper\",\"fixed\":null,\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","fixed":null,"required":"value"}"""; MapperParsingException e = expectThrows(MapperParsingException.class, () -> fromMapping(mapping)); assertEquals("[fixed] on mapper [field] of type [test_mapper] must not have a [null] value", e.getMessage()); - String fine = "{\"type\":\"test_mapper\",\"variable\":null,\"required\":\"value\"}"; + String fine = """ + {"type":"test_mapper","variable":null,"required":"value"}"""; TestMapper mapper = fromMapping(fine); assertEquals("{\"field\":" + fine + "}", Strings.toString(mapper)); } public void testObjectSerialization() throws IOException { - - String mapping = "{\"_doc\":{" - + "\"properties\":{" - + "\"actual\":{\"type\":\"double\"}," - + "\"bucket_count\":{\"type\":\"long\"}," - + "\"bucket_influencers\":{\"type\":\"nested\",\"properties\":{" - + "\"anomaly_score\":{\"type\":\"double\"}," - + "\"bucket_span\":{\"type\":\"long\"}," - + "\"is_interim\":{\"type\":\"boolean\"}}}}}}"; + String mapping = XContentHelper.stripWhitespace(""" + { + "_doc": { + "properties": { + "actual": { + "type": "double" + }, + "bucket_count": { + "type": "long" + }, + "bucket_influencers": { + "type": "nested", + "properties": { + "anomaly_score": { + "type": "double" + }, + "bucket_span": { + "type": "long" + }, + "is_interim": { + "type": "boolean" + } + } + } + } + } + }"""); MapperService mapperService = createMapperService(mapping); assertEquals(mapping, Strings.toString(mapperService.documentMapper().mapping())); @@ -416,12 +461,14 @@ public void testObjectSerialization() throws IOException { // test custom serializer public void testCustomSerialization() { - String mapping = "{\"type\":\"test_mapper\",\"wrapper\":\"wrapped value\",\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","wrapper":"wrapped value","required":"value"}"""; TestMapper mapper = fromMapping(mapping); assertEquals("wrapped value", mapper.wrapper.name); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); - String conflict = "{\"type\":\"test_mapper\",\"wrapper\":\"new value\",\"required\":\"value\"}"; + String conflict = """ + {"type":"test_mapper","wrapper":"new value","required":"value"}"""; TestMapper toMerge = fromMapping(conflict); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> mapper.merge(toMerge)); assertEquals( @@ -433,63 +480,67 @@ public void testCustomSerialization() { // test validator public void testParameterValidation() { - String mapping = "{\"type\":\"test_mapper\",\"int_value\":10,\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","int_value":10,"required":"value"}"""; TestMapper mapper = fromMapping(mapping); assertEquals(10, mapper.intValue); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); - IllegalArgumentException e = expectThrows( - IllegalArgumentException.class, - () -> fromMapping("{\"type\":\"test_mapper\",\"int_value\":60,\"required\":\"value\"}") - ); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> fromMapping(""" + {"type":"test_mapper","int_value":60,"required":"value"}""")); assertEquals("Value of [n] cannot be greater than 50", e.getMessage()); - IllegalArgumentException e2 = expectThrows( - IllegalArgumentException.class, - () -> fromMapping("{\"type\":\"test_mapper\",\"int_value\":-60,\"required\":\"value\"}") - ); + IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, () -> fromMapping(""" + {"type":"test_mapper","int_value":-60,"required":"value"}""")); assertEquals("Value of [n] cannot be less than 0", e2.getMessage()); } // test deprecations public void testDeprecatedParameterName() { - String mapping = "{\"type\":\"test_mapper\",\"fixed2_old\":true,\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","fixed2_old":true,"required":"value"}"""; TestMapper mapper = fromMapping(mapping); assertTrue(mapper.fixed2); assertWarnings("Parameter [fixed2_old] on mapper [field] is deprecated, use [fixed2]"); - assertEquals("{\"field\":{\"type\":\"test_mapper\",\"fixed2\":true,\"required\":\"value\"}}", Strings.toString(mapper)); + assertEquals(""" + {"field":{"type":"test_mapper","fixed2":true,"required":"value"}}""", Strings.toString(mapper)); } /** * test parsing mapping from dynamic templates, should ignore unknown parameters for bwc and log warning before 8.0.0 */ public void testBWCunknownParametersfromDynamicTemplates() { - String mapping = "{\"type\":\"test_mapper\",\"some_unknown_parameter\":true,\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","some_unknown_parameter":true,"required":"value"}"""; TestMapper mapper = fromMapping(mapping, VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0), true); assertNotNull(mapper); assertWarnings( "Parameter [some_unknown_parameter] is used in a dynamic template mapping and has no effect on type [test_mapper]. " + "Usage will result in an error in future major versions and should be removed." ); - assertEquals("{\"field\":{\"type\":\"test_mapper\",\"required\":\"value\"}}", Strings.toString(mapper)); + assertEquals(""" + {"field":{"type":"test_mapper","required":"value"}}""", Strings.toString(mapper)); MapperParsingException ex = expectThrows(MapperParsingException.class, () -> fromMapping(mapping, Version.V_8_0_0, true)); assertEquals("unknown parameter [some_unknown_parameter] on mapper [field] of type [test_mapper]", ex.getMessage()); } public void testAnalyzers() { - String mapping = "{\"type\":\"test_mapper\",\"analyzer\":\"_standard\",\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","analyzer":"_standard","required":"value"}"""; TestMapper mapper = fromMapping(mapping); assertEquals(mapper.analyzer, Lucene.STANDARD_ANALYZER); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); - String withDef = "{\"type\":\"test_mapper\",\"analyzer\":\"default\",\"required\":\"value\"}"; + String withDef = """ + {"type":"test_mapper","analyzer":"default","required":"value"}"""; mapper = fromMapping(withDef); assertEquals(mapper.analyzer.name(), "default"); assertThat(mapper.analyzer.analyzer(), instanceOf(StandardAnalyzer.class)); assertEquals("{\"field\":" + withDef + "}", Strings.toString(mapper)); - String badAnalyzer = "{\"type\":\"test_mapper\",\"analyzer\":\"wibble\",\"required\":\"value\"}"; + String badAnalyzer = """ + {"type":"test_mapper","analyzer":"wibble","required":"value"}"""; IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> fromMapping(badAnalyzer)); assertEquals("analyzer [wibble] has not been configured in mappings", e.getMessage()); @@ -504,31 +555,35 @@ public void testAnalyzers() { public void testDeprecatedParameters() { // 'index' is declared explicitly, 'store' is not, but is one of the previously always-accepted params - String mapping = "{\"type\":\"test_mapper\",\"index\":false,\"store\":true,\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","index":false,"store":true,"required":"value"}"""; TestMapper mapper = fromMapping(mapping, Version.V_7_8_0); assertWarnings("Parameter [store] has no effect on type [test_mapper] and will be removed in future"); assertFalse(mapper.index); - assertEquals("{\"field\":{\"type\":\"test_mapper\",\"index\":false,\"required\":\"value\"}}", Strings.toString(mapper)); + assertEquals(""" + {"field":{"type":"test_mapper","index":false,"required":"value"}}""", Strings.toString(mapper)); MapperParsingException e = expectThrows(MapperParsingException.class, () -> fromMapping(mapping, Version.V_8_0_0)); assertEquals("unknown parameter [store] on mapper [field] of type [test_mapper]", e.getMessage()); } public void testLinkedAnalyzers() throws IOException { - String mapping = "{\"type\":\"test_mapper\",\"analyzer\":\"_standard\",\"required\":\"value\"}"; + String mapping = """ + {"type":"test_mapper","analyzer":"_standard","required":"value"}"""; TestMapper mapper = fromMapping(mapping); assertEquals("_standard", mapper.analyzer.name()); assertEquals("_standard", mapper.searchAnalyzer.name()); assertEquals("{\"field\":" + mapping + "}", Strings.toString(mapper)); - String mappingWithSA = "{\"type\":\"test_mapper\",\"search_analyzer\":\"_standard\",\"required\":\"value\"}"; + String mappingWithSA = """ + {"type":"test_mapper","search_analyzer":"_standard","required":"value"}"""; mapper = fromMapping(mappingWithSA); assertEquals("_keyword", mapper.analyzer.name()); assertEquals("_standard", mapper.searchAnalyzer.name()); assertEquals("{\"field\":" + mappingWithSA + "}", Strings.toString(mapper)); - String mappingWithBoth = "{\"type\":\"test_mapper\",\"analyzer\":\"default\"," - + "\"search_analyzer\":\"_standard\",\"required\":\"value\"}"; + String mappingWithBoth = """ + {"type":"test_mapper","analyzer":"default","search_analyzer":"_standard","required":"value"}"""; mapper = fromMapping(mappingWithBoth); assertEquals("default", mapper.analyzer.name()); assertEquals("_standard", mapper.searchAnalyzer.name()); @@ -536,21 +591,31 @@ public void testLinkedAnalyzers() throws IOException { // we've configured things so that search_analyzer is only output when different from // analyzer, no matter what the value of `include_defaults` is - String mappingWithSame = "{\"type\":\"test_mapper\",\"analyzer\":\"default\"," - + "\"search_analyzer\":\"default\",\"required\":\"value\"}"; + String mappingWithSame = """ + {"type":"test_mapper","analyzer":"default","search_analyzer":"default","required":"value"}"""; mapper = fromMapping(mappingWithSame); assertEquals("default", mapper.analyzer.name()); assertEquals("default", mapper.searchAnalyzer.name()); - assertEquals("{\"field\":{\"type\":\"test_mapper\",\"analyzer\":\"default\",\"required\":\"value\"}}", Strings.toString(mapper)); - - assertEquals( - "{\"field\":{\"type\":\"test_mapper\",\"fixed\":true," - + "\"fixed2\":false,\"variable\":\"default\",\"index\":true," - + "\"wrapper\":\"default\",\"int_value\":5,\"analyzer\":\"default\"," - + "\"required\":\"value\",\"restricted\":\"foo\",\"enum_field\":\"name1\"," - + "\"restricted_enum_field\":\"name1\"}}", - toStringWithDefaults(mapper) - ); + assertEquals(""" + {"field":{"type":"test_mapper","analyzer":"default","required":"value"}}""", Strings.toString(mapper)); + + assertEquals(XContentHelper.stripWhitespace(""" + { + "field": { + "type": "test_mapper", + "fixed": true, + "fixed2": false, + "variable": "default", + "index": true, + "wrapper": "default", + "int_value": 5, + "analyzer": "default", + "required": "value", + "restricted": "foo", + "enum_field": "name1", + "restricted_enum_field": "name1" + } + }"""), toStringWithDefaults(mapper)); } public void testRequiredField() { @@ -560,7 +625,8 @@ public void testRequiredField() { assertEquals("field [required] must be specified", iae.getMessage()); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":null}"; + String mapping = """ + {"type":"test_mapper","required":null}"""; MapperParsingException exc = expectThrows(MapperParsingException.class, () -> fromMapping(mapping)); assertEquals("[required] on mapper [field] of type [test_mapper] must not have a [null] value", exc.getMessage()); } @@ -568,17 +634,20 @@ public void testRequiredField() { public void testRestrictedField() { { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\",\"restricted\":\"baz\"}"; + String mapping = """ + {"type":"test_mapper","required":"a","restricted":"baz"}"""; MapperParsingException e = expectThrows(MapperParsingException.class, () -> fromMapping(mapping)); assertEquals("Unknown value [baz] for field [restricted] - accepted values are [foo, bar]", e.getMessage()); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\",\"restricted\":\"bar\"}"; + String mapping = """ + {"type":"test_mapper","required":"a","restricted":"bar"}"""; TestMapper mapper = fromMapping(mapping); assertEquals("bar", mapper.restricted); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\"}"; + String mapping = """ + {"type":"test_mapper","required":"a"}"""; TestMapper mapper = fromMapping(mapping); assertEquals("foo", mapper.restricted); } @@ -586,17 +655,20 @@ public void testRestrictedField() { public void testEnumField() { { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\",\"enum_field\":\"baz\"}"; + String mapping = """ + {"type":"test_mapper","required":"a","enum_field":"baz"}"""; MapperParsingException e = expectThrows(MapperParsingException.class, () -> fromMapping(mapping)); assertEquals("Unknown value [baz] for field [enum_field] - accepted values are [name1, name2, name3]", e.getMessage()); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\",\"enum_field\":\"name3\"}"; + String mapping = """ + {"type":"test_mapper","required":"a","enum_field":"name3"}"""; TestMapper mapper = fromMapping(mapping); assertEquals(DummyEnumType.name3, mapper.enumField); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\"}"; + String mapping = """ + {"type":"test_mapper","required":"a"}"""; TestMapper mapper = fromMapping(mapping); assertEquals(DummyEnumType.name1, mapper.enumField); } @@ -604,22 +676,26 @@ public void testEnumField() { public void testRestrictedEnumField() { { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\",\"restricted_enum_field\":\"baz\"}"; + String mapping = """ + {"type":"test_mapper","required":"a","restricted_enum_field":"baz"}"""; MapperParsingException e = expectThrows(MapperParsingException.class, () -> fromMapping(mapping)); assertEquals("Unknown value [baz] for field [restricted_enum_field] - accepted values are [name1, name2]", e.getMessage()); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\",\"restricted_enum_field\":\"name3\"}"; + String mapping = """ + {"type":"test_mapper","required":"a","restricted_enum_field":"name3"}"""; MapperParsingException e = expectThrows(MapperParsingException.class, () -> fromMapping(mapping)); assertEquals("Unknown value [name3] for field [restricted_enum_field] - accepted values are [name1, name2]", e.getMessage()); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\",\"restricted_enum_field\":\"name2\"}"; + String mapping = """ + {"type":"test_mapper","required":"a","restricted_enum_field":"name2"}"""; TestMapper mapper = fromMapping(mapping); assertEquals(DummyEnumType.name2, mapper.restrictedEnumField); } { - String mapping = "{\"type\":\"test_mapper\",\"required\":\"a\"}"; + String mapping = """ + {"type":"test_mapper","required":"a"}"""; TestMapper mapper = fromMapping(mapping); assertEquals(DummyEnumType.name1, mapper.restrictedEnumField); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java index c3b7a20c30748..ffee28e772b0f 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.index.mapper; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.mapper.MapperService.MergeReason; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -197,10 +198,9 @@ public void testRuntimeSectionRejectedUpdate() throws IOException { MappedFieldType field = mapperService.fieldType("field"); assertThat(field, instanceOf(LongScriptFieldType.class)); assertNull(mapperService.fieldType("another_field")); - assertEquals( - "{\"_doc\":{\"runtime\":{\"field\":{\"type\":\"long\"}},\"properties\":{\"concrete\":{\"type\":\"keyword\"}}}}", - Strings.toString(mapperService.documentMapper().mapping().getRoot()) - ); + assertEquals(""" + {"_doc":{"runtime":{"field":{"type":"long"}},\ + "properties":{"concrete":{"type":"keyword"}}}}""", Strings.toString(mapperService.documentMapper().mapping().getRoot())); } } @@ -252,32 +252,56 @@ public void testRuntimeSectionMerge() throws IOException { { String mapping = Strings.toString(runtimeMapping(builder -> builder.startObject("field3").field("type", "date").endObject())); merge(mapperService, mapping); - assertEquals( - "{\"_doc\":" - + "{\"runtime\":{" - + "\"field\":{\"type\":\"double\"}," - + "\"field2\":{\"type\":\"long\"}," - + "\"field3\":{\"type\":\"date\"}}," - + "\"properties\":{" - + "\"concrete\":{\"type\":\"keyword\"}," - + "\"field\":{\"type\":\"keyword\"}}}}", - mapperService.documentMapper().mappingSource().toString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "runtime": { + "field": { + "type": "double" + }, + "field2": { + "type": "long" + }, + "field3": { + "type": "date" + } + }, + "properties": { + "concrete": { + "type": "keyword" + }, + "field": { + "type": "keyword" + } + } + } + }"""), mapperService.documentMapper().mappingSource().toString()); } { // remove a runtime field String mapping = Strings.toString(runtimeMapping(builder -> builder.nullField("field3"))); merge(mapperService, mapping); - assertEquals( - "{\"_doc\":" - + "{\"runtime\":{" - + "\"field\":{\"type\":\"double\"}," - + "\"field2\":{\"type\":\"long\"}}," - + "\"properties\":{" - + "\"concrete\":{\"type\":\"keyword\"}," - + "\"field\":{\"type\":\"keyword\"}}}}", - mapperService.documentMapper().mappingSource().toString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "runtime": { + "field": { + "type": "double" + }, + "field2": { + "type": "long" + } + }, + "properties": { + "concrete": { + "type": "keyword" + }, + "field": { + "type": "keyword" + } + } + } + }"""), mapperService.documentMapper().mappingSource().toString()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java index 1a4161d4e40b1..8f64d575699c2 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java @@ -245,10 +245,9 @@ public void testBWCSerialization() throws IOException { b.endObject(); })); - assertEquals( - "{\"_doc\":{\"properties\":{\"field\":{\"type\":\"text\",\"fields\":{\"subfield\":{\"type\":\"long\"}},\"fielddata\":true}}}}", - Strings.toString(mapperService.documentMapper().mapping()) - ); + assertEquals(""" + {"_doc":{"properties":{"field":{"type":"text","fields":{"subfield":{"type":"long"}},\ + "fielddata":true}}}}""", Strings.toString(mapperService.documentMapper().mapping())); } public void testEnableStore() throws IOException { @@ -289,9 +288,12 @@ public void testIndexOptions() throws IOException { DocumentMapper mapper = createDocumentMapper(mapping); String serialized = Strings.toString(mapper.mapping()); - assertThat(serialized, containsString("\"offsets\":{\"type\":\"text\",\"index_options\":\"offsets\"}")); - assertThat(serialized, containsString("\"freqs\":{\"type\":\"text\",\"index_options\":\"freqs\"}")); - assertThat(serialized, containsString("\"docs\":{\"type\":\"text\",\"index_options\":\"docs\"}")); + assertThat(serialized, containsString(""" + "offsets":{"type":"text","index_options":"offsets"}""")); + assertThat(serialized, containsString(""" + "freqs":{"type":"text","index_options":"freqs"}""")); + assertThat(serialized, containsString(""" + "docs":{"type":"text","index_options":"docs"}""")); ParsedDocument doc = mapper.parse(source(b -> { for (String option : supportedOptions.keySet()) { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldParserTests.java b/server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldParserTests.java index 02bbcb2f84efd..b09236c3471b2 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldParserTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldParserTests.java @@ -36,7 +36,8 @@ public void setUp() throws Exception { } public void testTextValues() throws Exception { - String input = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; + String input = """ + { "key1": "value1", "key2": "value2" }"""; XContentParser xContentParser = createXContentParser(input); List fields = parser.parse(xContentParser); @@ -92,7 +93,8 @@ public void testBooleanValues() throws Exception { } public void testBasicArrays() throws Exception { - String input = "{ \"key\": [true, false] }"; + String input = """ + { "key": [true, false] }"""; XContentParser xContentParser = createXContentParser(input); List fields = parser.parse(xContentParser); @@ -116,7 +118,8 @@ public void testBasicArrays() throws Exception { } public void testArrayOfArrays() throws Exception { - String input = "{ \"key\": [[true, \"value\"], 3] }"; + String input = """ + { "key": [[true, "value"], 3] }"""; XContentParser xContentParser = createXContentParser(input); List fields = parser.parse(xContentParser); @@ -148,7 +151,11 @@ public void testArrayOfArrays() throws Exception { } public void testArraysOfObjects() throws Exception { - String input = "{ \"key1\": [{ \"key2\": true }, false], \"key4\": \"other\" }"; + String input = """ + { + "key1": [ { "key2": true }, false ], + "key4": "other" + }"""; XContentParser xContentParser = createXContentParser(input); List fields = parser.parse(xContentParser); @@ -180,7 +187,15 @@ public void testArraysOfObjects() throws Exception { } public void testNestedObjects() throws Exception { - String input = "{ \"parent1\": { \"key\" : \"value\" }," + "\"parent2\": { \"key\" : \"value\" }}"; + String input = """ + { + "parent1": { + "key": "value" + }, + "parent2": { + "key": "value" + } + }"""; XContentParser xContentParser = createXContentParser(input); List fields = parser.parse(xContentParser); @@ -209,9 +224,14 @@ public void testNestedObjects() throws Exception { * * The same field name can be specified as a dotted path and using object notation. */ public void testDottedPaths() throws Exception { - String input = "{ \"object1.object2\": \"value1\"," - + "\"object1.object2.object3\": \"value2\"," - + "\"object1\": { \"object2\": \"value3\" }}"; + String input = """ + { + "object1.object2": "value1", + "object1.object2.object3": "value2", + "object1": { + "object2": "value3" + } + }"""; XContentParser xContentParser = createXContentParser(input); List fields = parser.parse(xContentParser); @@ -243,7 +263,13 @@ public void testDottedPaths() throws Exception { } public void testDepthLimit() throws Exception { - String input = "{ \"parent1\": { \"key\" : \"value\" }," + "\"parent2\": [{ \"key\" : { \"key\" : \"value\" }}]}"; + String input = """ + { + "parent1": { + "key": "value" + }, + "parent2": [ { "key": { "key": "value" } } ] + }"""; XContentParser xContentParser = createXContentParser(input); FlattenedFieldParser configuredParser = new FlattenedFieldParser( "field", @@ -259,7 +285,13 @@ public void testDepthLimit() throws Exception { } public void testDepthLimitBoundary() throws Exception { - String input = "{ \"parent1\": { \"key\" : \"value\" }," + "\"parent2\": [{ \"key\" : { \"key\" : \"value\" }}]}"; + String input = """ + { + "parent1": { + "key": "value" + }, + "parent2": [ { "key": { "key": "value" } } ] + }"""; XContentParser xContentParser = createXContentParser(input); FlattenedFieldParser configuredParser = new FlattenedFieldParser( "field", diff --git a/server/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java b/server/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java index f0ad2bab7bff0..a4d98ab36ad2e 100644 --- a/server/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java +++ b/server/src/test/java/org/elasticsearch/index/query/AbstractTermQueryTestCase.java @@ -40,17 +40,12 @@ protected Map getAlternateVersions() { } else { value = testQuery.value(); } - String contentString = "{\n" - + " \"" - + testQuery.getName() - + "\" : {\n" - + " \"" - + testQuery.fieldName() - + "\" : " - + value - + "\n" - + " }\n" - + "}"; + String contentString = """ + { + "%s" : { + "%s" : %s + } + }""".formatted(testQuery.getName(), testQuery.fieldName(), value); alternateVersions.put(contentString, testQuery); return alternateVersions; } diff --git a/server/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java index 06eecb83a2296..eaf2090cad7a6 100644 --- a/server/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java @@ -123,7 +123,10 @@ protected Map getAlternateVersions() { Map alternateVersions = new HashMap<>(); BoolQueryBuilder tempQueryBuilder = createTestQueryBuilder(); BoolQueryBuilder expectedQuery = new BoolQueryBuilder(); - String contentString = "{\n" + " \"bool\" : {\n"; + String contentString = """ + { + "bool" : { + """; if (tempQueryBuilder.must().size() > 0) { QueryBuilder must = tempQueryBuilder.must().get(0); contentString += "\"must\": " + must.toString() + ","; @@ -264,13 +267,15 @@ public void testFromJson() throws IOException { } public void testMinimumShouldMatchNumber() throws IOException { - String query = "{\"bool\" : {\"must\" : { \"term\" : { \"field\" : \"value\" } }, \"minimum_should_match\" : 1 } }"; + String query = """ + {"bool" : {"must" : { "term" : { "field" : "value" } }, "minimum_should_match" : 1 } }"""; BoolQueryBuilder builder = (BoolQueryBuilder) parseQuery(query); assertEquals("1", builder.minimumShouldMatch()); } public void testMinimumShouldMatchNull() throws IOException { - String query = "{\"bool\" : {\"must\" : { \"term\" : { \"field\" : \"value\" } }, \"minimum_should_match\" : null } }"; + String query = """ + {"bool" : {"must" : { "term" : { "field" : "value" } }, "minimum_should_match" : null } }"""; BoolQueryBuilder builder = (BoolQueryBuilder) parseQuery(query); assertEquals(null, builder.minimumShouldMatch()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java index b427d564842d1..0d3bce848f351 100644 --- a/server/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java @@ -53,28 +53,29 @@ public void testIllegalArguments() { } public void testFromJson() throws IOException { - String query = "{\n" - + " \"boosting\" : {\n" - + " \"positive\" : {\n" - + " \"term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 5.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"negative\" : {\n" - + " \"term\" : {\n" - + " \"field2\" : {\n" - + " \"value\" : \"value2\",\n" - + " \"boost\" : 8.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"negative_boost\" : 23.0,\n" - + " \"boost\" : 42.0\n" - + " }\n" - + "}"; + String query = """ + { + "boosting" : { + "positive" : { + "term" : { + "field1" : { + "value" : "value1", + "boost" : 5.0 + } + } + }, + "negative" : { + "term" : { + "field2" : { + "value" : "value2", + "boost" : 8.0 + } + } + }, + "negative_boost" : 23.0, + "boost" : 42.0 + } + }"""; BoostingQueryBuilder queryBuilder = (BoostingQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); diff --git a/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java index 2e179068f8e13..78d70d7c2fcd9 100644 --- a/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java @@ -74,16 +74,17 @@ protected void doAssertLuceneQuery(CombinedFieldsQueryBuilder queryBuilder, Quer } public void testValuesFromXContent() throws IOException { - String json = "{\n" - + " \"combined_fields\" : {\n" - + " \"query\" : \"quick brown fox\",\n" - + " \"fields\" : [ \"abstract^1.0\", \"body^1.0\", \"title^1.0\" ],\n" - + " \"operator\" : \"OR\",\n" - + " \"zero_terms_query\" : \"NONE\",\n" - + " \"auto_generate_synonyms_phrase_query\" : true,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "combined_fields" : { + "query" : "quick brown fox", + "fields" : [ "abstract^1.0", "body^1.0", "title^1.0" ], + "operator" : "OR", + "zero_terms_query" : "NONE", + "auto_generate_synonyms_phrase_query" : true, + "boost" : 2.0 + } + }"""; CombinedFieldsQueryBuilder parsed = (CombinedFieldsQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -102,14 +103,13 @@ public void testMinumumShouldMatchFromXContent() throws IOException { Object[] expectedValues = new Object[] { "2", "2", "2%", null }; int i = 0; for (Object value : testValues) { - String json = "{\n" - + " \"combined_fields\" : {\n" - + " \"query\" : \"quick brown fox\",\n" - + " \"minimum_should_match\" : " - + value - + "\n" - + " }\n" - + "}"; + String json = """ + { + "combined_fields" : { + "query" : "quick brown fox", + "minimum_should_match" : %s + } + }""".formatted(value); CombinedFieldsQueryBuilder parsed = (CombinedFieldsQueryBuilder) parseQuery(json); diff --git a/server/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java index 461863b4d252d..0f2bd8cbde627 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ConstantScoreQueryBuilderTests.java @@ -57,12 +57,12 @@ public void testFilterElement() throws IOException { * test that "filter" does not accept an array of queries, throws {@link ParsingException} */ public void testNoArrayAsFilterElements() throws IOException { - String queryString = "{ \"" - + ConstantScoreQueryBuilder.NAME - + "\" : {\n" - + "\"filter\" : [ { \"term\": { \"foo\": \"a\" } },\n" - + "{ \"term\": { \"foo\": \"x\" } } ]\n" - + "} }"; + String queryString = """ + { + "%s": { + "filter": [ { "term": { "foo": "a" } }, { "term": { "foo": "x" } } ] + } + }""".formatted(ConstantScoreQueryBuilder.NAME); ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(queryString)); assertThat(e.getMessage(), containsString("unexpected token [START_ARRAY]")); } @@ -77,17 +77,18 @@ public void testUnknownField() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"constant_score\" : {\n" - + " \"filter\" : {\n" - + " \"terms\" : {\n" - + " \"user\" : [ \"kimchy\", \"elasticsearch\" ],\n" - + " \"boost\" : 42.0\n" - + " }\n" - + " },\n" - + " \"boost\" : 23.0\n" - + " }\n" - + "}"; + String json = """ + { + "constant_score" : { + "filter" : { + "terms" : { + "user" : [ "kimchy", "elasticsearch" ], + "boost" : 42.0 + } + }, + "boost" : 23.0 + } + }"""; ConstantScoreQueryBuilder parsed = (ConstantScoreQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java index 0bc6a67c6c9b6..eef4d2fcfa1ae 100644 --- a/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java @@ -51,7 +51,12 @@ protected Map getAlternateVersions() { QueryBuilder innerQuery = createTestQueryBuilder().innerQueries().get(0); DisMaxQueryBuilder expectedQuery = new DisMaxQueryBuilder(); expectedQuery.add(innerQuery); - String contentString = "{\n" + " \"dis_max\" : {\n" + " \"queries\" : " + innerQuery.toString() + " }\n" + "}"; + String contentString = """ + { + "dis_max": { + "queries": %s + } + }""".formatted(innerQuery.toString()); alternateVersions.put(contentString, expectedQuery); return alternateVersions; } @@ -62,49 +67,49 @@ public void testIllegalArguments() { } public void testToQueryInnerPrefixQuery() throws Exception { - String queryAsString = "{\n" - + " \"dis_max\":{\n" - + " \"queries\":[\n" - + " {\n" - + " \"prefix\":{\n" - + " \"" - + TEXT_FIELD_NAME - + "\":{\n" - + " \"value\":\"sh\",\n" - + " \"boost\":1.2\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}"; + String queryAsString = """ + { + "dis_max": { + "queries": [ + { + "prefix": { + "%s": { + "value": "sh", + "boost": 1.2 + } + } + } + ] + } + }""".formatted(TEXT_FIELD_NAME); Query query = parseQuery(queryAsString).toQuery(createSearchExecutionContext()); Query expected = new DisjunctionMaxQuery(List.of(new BoostQuery(new PrefixQuery(new Term(TEXT_FIELD_NAME, "sh")), 1.2f)), 0); assertEquals(expected, query); } public void testFromJson() throws IOException { - String json = "{\n" - + " \"dis_max\" : {\n" - + " \"tie_breaker\" : 0.7,\n" - + " \"queries\" : [ {\n" - + " \"term\" : {\n" - + " \"age\" : {\n" - + " \"value\" : 34,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"term\" : {\n" - + " \"age\" : {\n" - + " \"value\" : 35,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"boost\" : 1.2\n" - + " }\n" - + "}"; + String json = """ + { + "dis_max" : { + "tie_breaker" : 0.7, + "queries" : [ { + "term" : { + "age" : { + "value" : 34, + "boost" : 1.0 + } + } + }, { + "term" : { + "age" : { + "value" : 35, + "boost" : 1.0 + } + } + } ], + "boost" : 1.2 + } + }"""; DisMaxQueryBuilder parsed = (DisMaxQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/DistanceFeatureQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/DistanceFeatureQueryBuilderTests.java index 56ba0d87d6bfb..dc3b4dabe971d 100644 --- a/server/src/test/java/org/elasticsearch/index/query/DistanceFeatureQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/DistanceFeatureQueryBuilderTests.java @@ -88,20 +88,15 @@ public void testFromJsonDateFieldType() throws IOException { // origin as string String origin = "2018-01-01T13:10:30Z"; String pivot = "7d"; - String json = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + DATE_FIELD_NAME - + "\",\n" - + " \"origin\": \"" - + origin - + "\",\n" - + " \"pivot\" : \"" - + pivot - + "\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "distance_feature": { + "field": "%s", + "origin": "%s", + "pivot": "%s", + "boost": 1.0 + } + }""".formatted(DATE_FIELD_NAME, origin, pivot); DistanceFeatureQueryBuilder parsed = (DistanceFeatureQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, origin, parsed.origin().origin()); @@ -110,20 +105,15 @@ public void testFromJsonDateFieldType() throws IOException { // origin as long long originLong = 1514812230999L; - json = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + DATE_FIELD_NAME - + "\",\n" - + " \"origin\": " - + originLong - + ",\n" - + " \"pivot\" : \"" - + pivot - + "\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + json = """ + { + "distance_feature": { + "field": "%s", + "origin": %s, + "pivot": "%s", + "boost": 1.0 + } + }""".formatted(DATE_FIELD_NAME, originLong, pivot); parsed = (DistanceFeatureQueryBuilder) parseQuery(json); assertEquals(json, originLong, parsed.origin().origin()); } @@ -132,20 +122,15 @@ public void testFromJsonDateNanosFieldType() throws IOException { // origin as string String origin = "2018-01-01T13:10:30.323456789Z"; String pivot = "100000000nanos"; - String json = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + DATE_NANOS_FIELD_NAME - + "\",\n" - + " \"origin\": \"" - + origin - + "\",\n" - + " \"pivot\" : \"" - + pivot - + "\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "distance_feature": { + "field": "%s", + "origin": "%s", + "pivot": "%s", + "boost": 1.0 + } + }""".formatted(DATE_NANOS_FIELD_NAME, origin, pivot); DistanceFeatureQueryBuilder parsed = (DistanceFeatureQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, origin, parsed.origin().origin()); @@ -154,20 +139,15 @@ public void testFromJsonDateNanosFieldType() throws IOException { // origin as long long originLong = 1514812230999L; - json = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + DATE_NANOS_FIELD_NAME - + "\",\n" - + " \"origin\": " - + originLong - + ",\n" - + " \"pivot\" : \"" - + pivot - + "\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + json = """ + { + "distance_feature": { + "field": "%s", + "origin": %s, + "pivot": "%s", + "boost": 1.0 + } + }""".formatted(DATE_NANOS_FIELD_NAME, originLong, pivot); parsed = (DistanceFeatureQueryBuilder) parseQuery(json); assertEquals(json, originLong, parsed.origin().origin()); } @@ -177,20 +157,15 @@ public void testFromJsonGeoFieldType() throws IOException { final String pivot = "1km"; // origin as string - String json = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + GEO_POINT_FIELD_NAME - + "\",\n" - + " \"origin\": \"" - + origin.toString() - + "\",\n" - + " \"pivot\" : \"" - + pivot - + "\",\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "distance_feature": { + "field": "%s", + "origin": "%s", + "pivot": "%s", + "boost": 2.0 + } + }""".formatted(GEO_POINT_FIELD_NAME, origin.toString(), pivot); DistanceFeatureQueryBuilder parsed = (DistanceFeatureQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, origin.toString(), parsed.origin().origin()); @@ -198,70 +173,58 @@ public void testFromJsonGeoFieldType() throws IOException { assertEquals(json, 2.0, parsed.boost(), 0.0001); // origin as array - json = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + GEO_POINT_FIELD_NAME - + "\",\n" - + " \"origin\": [" - + origin.lon() - + ", " - + origin.lat() - + "],\n" - + " \"pivot\" : \"" - + pivot - + "\",\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + json = """ + { + "distance_feature": { + "field": "%s", + "origin": [ %s, %s ], + "pivot": "%s", + "boost": 2.0 + } + }""".formatted(GEO_POINT_FIELD_NAME, origin.lon(), origin.lat(), pivot); parsed = (DistanceFeatureQueryBuilder) parseQuery(json); assertEquals(json, origin, parsed.origin().origin()); // origin as object - json = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + GEO_POINT_FIELD_NAME - + "\",\n" - + " \"origin\": {" - + "\"lat\":" - + origin.lat() - + ", \"lon\":" - + origin.lon() - + "},\n" - + " \"pivot\" : \"" - + pivot - + "\",\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + json = """ + { + "distance_feature": { + "field": "%s", + "origin": { + "lat": %s, + "lon": %s + }, + "pivot": "%s", + "boost": 2.0 + } + }""".formatted(GEO_POINT_FIELD_NAME, origin.lat(), origin.lon(), pivot); parsed = (DistanceFeatureQueryBuilder) parseQuery(json); assertEquals(json, origin, parsed.origin().origin()); } public void testQueryMatchNoDocsQueryWithUnmappedField() throws IOException { Query expectedQuery = Queries.newMatchNoDocsQuery("Can't run [" + DistanceFeatureQueryBuilder.NAME + "] query on unmapped fields!"); - String queryString = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"random_unmapped_field\",\n" - + " \"origin\": \"random_string\",\n" - + " \"pivot\" : \"random_string\"\n" - + " }\n" - + "}"; + String queryString = """ + { + "distance_feature" : { + "field": "random_unmapped_field", + "origin": "random_string", + "pivot" : "random_string" + } + }"""; Query query = parseQuery(queryString).toQuery(createSearchExecutionContext()); assertEquals(expectedQuery, query); } public void testQueryFailsWithWrongFieldType() { - String query = "{\n" - + " \"distance_feature\" : {\n" - + " \"field\": \"" - + INT_FIELD_NAME - + "\",\n" - + " \"origin\": 40,\n" - + " \"pivot\" : \"random_string\"\n" - + " }\n" - + "}"; + String query = """ + { + "distance_feature": { + "field": "%s", + "origin": 40, + "pivot": "random_string" + } + }""".formatted(INT_FIELD_NAME); IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> parseQuery(query).toQuery(createSearchExecutionContext()) diff --git a/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java index d5bf663b89e8c..191817d126ab4 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java @@ -111,7 +111,13 @@ public void testIllegalArguments() { } public void testFromJson() throws IOException { - String json = "{\n" + " \"exists\" : {\n" + " \"field\" : \"user\",\n" + " \"boost\" : 42.0\n" + " }\n" + "}"; + String json = """ + { + "exists" : { + "field" : "user", + "boost" : 42.0 + } + }"""; ExistsQueryBuilder parsed = (ExistsQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilderTests.java index 1662da679b551..0671a074de4d3 100644 --- a/server/src/test/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/FieldMaskingSpanQueryBuilderTests.java @@ -55,23 +55,22 @@ public void testIllegalArguments() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"" - + NAME.getPreferredName() - + "\" : {\n" - + " \"query\" : {\n" - + " \"span_term\" : {\n" - + " \"value\" : {\n" - + " \"value\" : 0.5,\n" - + " \"boost\" : 0.23\n" - + " }\n" - + " }\n" - + " },\n" - + " \"field\" : \"mapped_geo_shape\",\n" - + " \"boost\" : 42.0,\n" - + " \"_name\" : \"KPI\"\n" - + " }\n" - + "}"; + String json = """ + { + "%s" : { + "query" : { + "span_term" : { + "value" : { + "value" : 0.5, + "boost" : 0.23 + } + } + }, + "field" : "mapped_geo_shape", + "boost" : 42.0, + "_name" : "KPI" + } + }""".formatted(NAME.getPreferredName()); Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat( exception.getMessage(), @@ -80,41 +79,41 @@ public void testFromJson() throws IOException { } public void testJsonWithTopLevelBoost() throws IOException { - String json = "{\n" - + " \"" - + NAME.getPreferredName() - + "\" : {\n" - + " \"query\" : {\n" - + " \"span_term\" : {\n" - + " \"value\" : {\n" - + " \"value\" : \"foo\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"field\" : \"mapped_geo_shape\",\n" - + " \"boost\" : 42.0,\n" - + " \"_name\" : \"KPI\"\n" - + " }\n" - + "}"; + String json = """ + { + "%s" : { + "query" : { + "span_term" : { + "value" : { + "value" : "foo" + } + } + }, + "field" : "mapped_geo_shape", + "boost" : 42.0, + "_name" : "KPI" + } + }""".formatted(NAME.getPreferredName()); Query q = parseQuery(json).toQuery(createSearchExecutionContext()); assertEquals(new BoostQuery(new FieldMaskingSpanQuery(new SpanTermQuery(new Term("value", "foo")), "mapped_geo_shape"), 42.0f), q); } public void testJsonWithDeprecatedName() throws IOException { - String json = "{\n" - + " \"field_masking_span\" : {\n" - + " \"query\" : {\n" - + " \"span_term\" : {\n" - + " \"value\" : {\n" - + " \"value\" : \"foo\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"field\" : \"mapped_geo_shape\",\n" - + " \"boost\" : 42.0,\n" - + " \"_name\" : \"KPI\"\n" - + " }\n" - + "}"; + String json = """ + { + "field_masking_span" : { + "query" : { + "span_term" : { + "value" : { + "value" : "foo" + } + } + }, + "field" : "mapped_geo_shape", + "boost" : 42.0, + "_name" : "KPI" + } + }"""; Query q = parseQuery(json).toQuery(createSearchExecutionContext()); assertWarnings("Deprecated field [field_masking_span] used, expected [" + NAME.getPreferredName() + "] instead"); } diff --git a/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java index c0d3946ffc805..3723a505ff0e3 100644 --- a/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java @@ -52,15 +52,12 @@ protected FuzzyQueryBuilder doCreateTestQueryBuilder() { protected Map getAlternateVersions() { Map alternateVersions = new HashMap<>(); FuzzyQueryBuilder fuzzyQuery = new FuzzyQueryBuilder(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10)); - String contentString = "{\n" - + " \"fuzzy\" : {\n" - + " \"" - + fuzzyQuery.fieldName() - + "\" : \"" - + fuzzyQuery.value() - + "\"\n" - + " }\n" - + "}"; + String contentString = """ + { + "fuzzy" : { + "%s" : "%s" + } + }""".formatted(fuzzyQuery.fieldName(), fuzzyQuery.value()); alternateVersions.put(contentString, fuzzyQuery); return alternateVersions; } @@ -87,18 +84,17 @@ public void testIllegalArguments() { } public void testToQueryWithStringField() throws IOException { - String query = "{\n" - + " \"fuzzy\":{\n" - + " \"" - + TEXT_FIELD_NAME - + "\":{\n" - + " \"value\":\"sh\",\n" - + " \"fuzziness\": \"AUTO\",\n" - + " \"prefix_length\":1,\n" - + " \"boost\":2.0\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "fuzzy":{ + "%s":{ + "value":"sh", + "fuzziness": "AUTO", + "prefix_length":1, + "boost":2.0 + } + } + }""".formatted(TEXT_FIELD_NAME); Query parsedQuery = parseQuery(query).toQuery(createSearchExecutionContext()); assertThat(parsedQuery, instanceOf(BoostQuery.class)); BoostQuery boostQuery = (BoostQuery) parsedQuery; @@ -111,18 +107,17 @@ public void testToQueryWithStringField() throws IOException { } public void testToQueryWithStringFieldDefinedFuzziness() throws IOException { - String query = "{\n" - + " \"fuzzy\":{\n" - + " \"" - + TEXT_FIELD_NAME - + "\":{\n" - + " \"value\":\"sh\",\n" - + " \"fuzziness\": \"AUTO:2,5\",\n" - + " \"prefix_length\":1,\n" - + " \"boost\":2.0\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "fuzzy":{ + "%s":{ + "value":"sh", + "fuzziness": "AUTO:2,5", + "prefix_length":1, + "boost":2.0 + } + } + }""".formatted(TEXT_FIELD_NAME); Query parsedQuery = parseQuery(query).toQuery(createSearchExecutionContext()); assertThat(parsedQuery, instanceOf(BoostQuery.class)); BoostQuery boostQuery = (BoostQuery) parsedQuery; @@ -135,18 +130,17 @@ public void testToQueryWithStringFieldDefinedFuzziness() throws IOException { } public void testToQueryWithStringFieldDefinedWrongFuzziness() throws IOException { - String queryMissingFuzzinessUpLimit = "{\n" - + " \"fuzzy\":{\n" - + " \"" - + TEXT_FIELD_NAME - + "\":{\n" - + " \"value\":\"sh\",\n" - + " \"fuzziness\": \"AUTO:2\",\n" - + " \"prefix_length\":1,\n" - + " \"boost\":2.0\n" - + " }\n" - + " }\n" - + "}"; + String queryMissingFuzzinessUpLimit = """ + { + "fuzzy":{ + "%s":{ + "value":"sh", + "fuzziness": "AUTO:2", + "prefix_length":1, + "boost":2.0 + } + } + }""".formatted(TEXT_FIELD_NAME); ElasticsearchParseException e = expectThrows( ElasticsearchParseException.class, () -> parseQuery(queryMissingFuzzinessUpLimit).toQuery(createSearchExecutionContext()) @@ -154,18 +148,17 @@ public void testToQueryWithStringFieldDefinedWrongFuzziness() throws IOException String msg = "failed to find low and high distance values"; assertTrue(e.getMessage() + " didn't contain: " + msg + " but: " + e.getMessage(), e.getMessage().contains(msg)); - String queryHavingNegativeFuzzinessLowLimit = "{\n" - + " \"fuzzy\":{\n" - + " \"" - + TEXT_FIELD_NAME - + "\":{\n" - + " \"value\":\"sh\",\n" - + " \"fuzziness\": \"AUTO:-1,6\",\n" - + " \"prefix_length\":1,\n" - + " \"boost\":2.0\n" - + " }\n" - + " }\n" - + "}"; + String queryHavingNegativeFuzzinessLowLimit = """ + { + "fuzzy":{ + "%s":{ + "value":"sh", + "fuzziness": "AUTO:-1,6", + "prefix_length":1, + "boost":2.0 + } + } + }""".formatted(TEXT_FIELD_NAME); String msg2 = "fuzziness wrongly configured"; ElasticsearchParseException e2 = expectThrows( ElasticsearchParseException.class, @@ -173,36 +166,34 @@ public void testToQueryWithStringFieldDefinedWrongFuzziness() throws IOException ); assertTrue(e2.getMessage() + " didn't contain: " + msg2 + " but: " + e.getMessage(), e.getMessage().contains(msg)); - String queryMissingFuzzinessUpLimit2 = "{\n" - + " \"fuzzy\":{\n" - + " \"" - + TEXT_FIELD_NAME - + "\":{\n" - + " \"value\":\"sh\",\n" - + " \"fuzziness\": \"AUTO:1,\",\n" - + " \"prefix_length\":1,\n" - + " \"boost\":2.0\n" - + " }\n" - + " }\n" - + "}"; + String queryMissingFuzzinessUpLimit2 = """ + { + "fuzzy":{ + "%s":{ + "value":"sh", + "fuzziness": "AUTO:1,", + "prefix_length":1, + "boost":2.0 + } + } + }""".formatted(TEXT_FIELD_NAME); e = expectThrows( ElasticsearchParseException.class, () -> parseQuery(queryMissingFuzzinessUpLimit2).toQuery(createSearchExecutionContext()) ); assertTrue(e.getMessage() + " didn't contain: " + msg + " but: " + e.getMessage(), e.getMessage().contains(msg)); - String queryMissingFuzzinessLowLimit = "{\n" - + " \"fuzzy\":{\n" - + " \"" - + TEXT_FIELD_NAME - + "\":{\n" - + " \"value\":\"sh\",\n" - + " \"fuzziness\": \"AUTO:,5\",\n" - + " \"prefix_length\":1,\n" - + " \"boost\":2.0\n" - + " }\n" - + " }\n" - + "}"; + String queryMissingFuzzinessLowLimit = """ + { + "fuzzy":{ + "%s":{ + "value":"sh", + "fuzziness": "AUTO:,5", + "prefix_length":1, + "boost":2.0 + } + } + }""".formatted(TEXT_FIELD_NAME); e = expectThrows( ElasticsearchParseException.class, () -> parseQuery(queryMissingFuzzinessLowLimit).toQuery(createSearchExecutionContext()) @@ -212,16 +203,16 @@ public void testToQueryWithStringFieldDefinedWrongFuzziness() throws IOException } public void testToQueryWithNumericField() throws IOException { - String query = "{\n" - + " \"fuzzy\":{\n" - + " \"" - + INT_FIELD_NAME - + "\":{\n" - + " \"value\":12,\n" - + " \"fuzziness\":2\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "fuzzy":{ + "%s":{ + "value":12, + "fuzziness":2 + } + } + } + """.formatted(INT_FIELD_NAME); IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> parseQuery(query).toQuery(createSearchExecutionContext()) @@ -233,18 +224,19 @@ public void testToQueryWithNumericField() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"fuzzy\" : {\n" - + " \"user\" : {\n" - + " \"value\" : \"ki\",\n" - + " \"fuzziness\" : \"2\",\n" - + " \"prefix_length\" : 0,\n" - + " \"max_expansions\" : 100,\n" - + " \"transpositions\" : false,\n" - + " \"boost\" : 42.0\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "fuzzy" : { + "user" : { + "value" : "ki", + "fuzziness" : "2", + "prefix_length" : 0, + "max_expansions" : 100, + "transpositions" : false, + "boost" : 42.0 + } + } + }"""; FuzzyQueryBuilder parsed = (FuzzyQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, 42.0, parsed.boost(), 0.00001); @@ -253,48 +245,52 @@ public void testFromJson() throws IOException { } public void testParseFailsWithMultipleFields() throws IOException { - String json1 = "{\n" - + " \"fuzzy\" : {\n" - + " \"message1\" : {\n" - + " \"value\" : \"this is a test\"\n" - + " }\n" - + " }\n" - + "}"; + String json1 = """ + { + "fuzzy" : { + "message1" : { + "value" : "this is a test" + } + } + }"""; parseQuery(json1); // should be all good - String json2 = "{\n" - + " \"fuzzy\" : {\n" - + " \"message1\" : {\n" - + " \"value\" : \"this is a test\"\n" - + " },\n" - + " \"message2\" : {\n" - + " \"value\" : \"this is a test\"\n" - + " }\n" - + " }\n" - + "}"; + String json2 = """ + { + "fuzzy" : { + "message1" : { + "value" : "this is a test" + }, + "message2" : { + "value" : "this is a test" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json2)); assertEquals("[fuzzy] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); - String shortJson = "{\n" - + " \"fuzzy\" : {\n" - + " \"message1\" : \"this is a test\",\n" - + " \"message2\" : \"value\" : \"this is a test\"\n" - + " }\n" - + "}"; + String shortJson = """ + { + "fuzzy" : { + "message1" : "this is a test", + "message2" : "value" : "this is a test" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[fuzzy] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } public void testParseFailsWithValueArray() { - String query = "{\n" - + " \"fuzzy\" : {\n" - + " \"message1\" : {\n" - + " \"value\" : [ \"one\", \"two\", \"three\"]\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "fuzzy" : { + "message1" : { + "value" : [ "one", "two", "three"] + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(query)); assertEquals("[fuzzy] unexpected token [START_ARRAY] after [value]", e.getMessage()); diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java index 104ab4bf07b4a..14ea14809c867 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java @@ -290,94 +290,94 @@ public void fillIn(double coordinate, GeoBoundingBoxQueryBuilder qb) { } public void testParsingAndToQuery1() throws IOException { - String query = "{\n" - + " \"geo_bounding_box\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"top_left\":[-70, 40],\n" - + " \"bottom_right\":[-80, 30]\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_bounding_box":{ + "%s":{ + "top_left":[-70, 40], + "bottom_right":[-80, 30] + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoBoundingBoxQuery(query); } public void testParsingAndToQuery2() throws IOException { - String query = "{\n" - + " \"geo_bounding_box\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"top_left\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " },\n" - + " \"bottom_right\":{\n" - + " \"lat\":30,\n" - + " \"lon\":-80\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_bounding_box":{ + "%s":{ + "top_left":{ + "lat":40, + "lon":-70 + }, + "bottom_right":{ + "lat":30, + "lon":-80 + } + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoBoundingBoxQuery(query); } public void testParsingAndToQuery3() throws IOException { - String query = "{\n" - + " \"geo_bounding_box\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"top_left\":\"40, -70\",\n" - + " \"bottom_right\":\"30, -80\"\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_bounding_box":{ + "%s":{ + "top_left":"40, -70", + "bottom_right":"30, -80" + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoBoundingBoxQuery(query); } public void testParsingAndToQuery4() throws IOException { - String query = "{\n" - + " \"geo_bounding_box\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"top_left\":\"drn5x1g8cu2y\",\n" - + " \"bottom_right\":\"30, -80\"\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_bounding_box":{ + "%s":{ + "top_left":"drn5x1g8cu2y", + "bottom_right":"30, -80" + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoBoundingBoxQuery(query); } public void testParsingAndToQuery5() throws IOException { - String query = "{\n" - + " \"geo_bounding_box\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"top_right\":\"40, -80\",\n" - + " \"bottom_left\":\"30, -70\"\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_bounding_box":{ + "%s":{ + "top_right":"40, -80", + "bottom_left":"30, -70" + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoBoundingBoxQuery(query); } public void testParsingAndToQuery6() throws IOException { - String query = "{\n" - + " \"geo_bounding_box\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"right\": -80,\n" - + " \"top\": 40,\n" - + " \"left\": -70,\n" - + " \"bottom\": 30\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_bounding_box":{ + "%s":{ + "right": -80, + "top": 40, + "left": -70, + "bottom": 30 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoBoundingBoxQuery(query); } @@ -388,17 +388,18 @@ private void assertGeoBoundingBoxQuery(String query) throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"geo_bounding_box\" : {\n" - + " \"pin.location\" : {\n" - + " \"top_left\" : [ -74.1, 40.73 ],\n" - + " \"bottom_right\" : [ -71.12, 40.01 ]\n" - + " },\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "geo_bounding_box" : { + "pin.location" : { + "top_left" : [ -74.1, 40.73 ], + "bottom_right" : [ -71.12, 40.01 ] + }, + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; GeoBoundingBoxQueryBuilder parsed = (GeoBoundingBoxQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, "pin.location", parsed.fieldName()); @@ -410,30 +411,32 @@ public void testFromJson() throws IOException { } public void testFromWKT() throws IOException { - String wkt = "{\n" - + " \"geo_bounding_box\" : {\n" - + " \"pin.location\" : {\n" - + " \"wkt\" : \"BBOX (-74.1, -71.12, 40.73, 40.01)\"\n" - + " },\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String wkt = """ + { + "geo_bounding_box" : { + "pin.location" : { + "wkt" : "BBOX (-74.1, -71.12, 40.73, 40.01)" + }, + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; // toXContent generates the query in geojson only; for now we need to test against the expected // geojson generated content - String expectedJson = "{\n" - + " \"geo_bounding_box\" : {\n" - + " \"pin.location\" : {\n" - + " \"top_left\" : [ -74.1, 40.73 ],\n" - + " \"bottom_right\" : [ -71.12, 40.01 ]\n" - + " },\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String expectedJson = """ + { + "geo_bounding_box" : { + "pin.location" : { + "top_left" : [ -74.1, 40.73 ], + "bottom_right" : [ -71.12, 40.01 ] + }, + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; // parse with wkt GeoBoundingBoxQueryBuilder parsed = (GeoBoundingBoxQueryBuilder) parseQuery(wkt); @@ -449,29 +452,31 @@ public void testFromWKT() throws IOException { } public void testFromGeohash() throws IOException { - String json = "{\n" - + " \"geo_bounding_box\" : {\n" - + " \"pin.location\" : {\n" - + " \"top_left\" : \"dr\",\n" - + " \"bottom_right\" : \"dq\"\n" - + " },\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; - - String expectedJson = "{\n" - + " \"geo_bounding_box\" : {\n" - + " \"pin.location\" : {\n" - + " \"top_left\" : [ -78.75, 45.0 ],\n" - + " \"bottom_right\" : [ -67.5, 33.75 ]\n" - + " },\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "geo_bounding_box" : { + "pin.location" : { + "top_left" : "dr", + "bottom_right" : "dq" + }, + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; + + String expectedJson = """ + { + "geo_bounding_box" : { + "pin.location" : { + "top_left" : [ -78.75, 45.0 ], + "bottom_right" : [ -67.5, 33.75 ] + }, + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; GeoBoundingBoxQueryBuilder parsed = (GeoBoundingBoxQueryBuilder) parseQuery(json); checkGeneratedJson(expectedJson, parsed); assertEquals(json, "pin.location", parsed.fieldName()); @@ -483,40 +488,41 @@ public void testFromGeohash() throws IOException { } public void testMalformedGeohashes() { - String jsonGeohashAndWkt = "{\n" - + " \"geo_bounding_box\" : {\n" - + " \"pin.location\" : {\n" - + " \"top_left\" : [ -78.75, 45.0 ],\n" - + " \"wkt\" : \"BBOX (-74.1, -71.12, 40.73, 40.01)\"\n" - + " },\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String jsonGeohashAndWkt = """ + { + "geo_bounding_box" : { + "pin.location" : { + "top_left" : [ -78.75, 45.0 ], + "wkt" : "BBOX (-74.1, -71.12, 40.73, 40.01)" + }, + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; ElasticsearchParseException e1 = expectThrows(ElasticsearchParseException.class, () -> parseQuery(jsonGeohashAndWkt)); assertThat(e1.getMessage(), containsString("Conflicting definition found using well-known text and explicit corners.")); } public void testHonorsCoercion() throws IOException { - String query = "{\n" - + " \"geo_bounding_box\": {\n" - + " \"validation_method\": \"COERCE\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\": {\n" - + " \"top_left\": {\n" - + " \"lat\": -15.5,\n" - + " \"lon\": 176.5\n" - + " },\n" - + " \"bottom_right\": {\n" - + " \"lat\": -19.6,\n" - + " \"lon\": 181\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_bounding_box": { + "validation_method": "COERCE", + "%s": { + "top_left": { + "lat": -15.5, + "lon": 176.5 + }, + "bottom_right": { + "lat": -19.6, + "lon": 181 + } + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoBoundingBoxQuery(query); } diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java index a8902a781bc38..c3642cf1422df 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java @@ -151,179 +151,179 @@ protected void doAssertLuceneQuery(GeoDistanceQueryBuilder queryBuilder, Query q } public void testParsingAndToQuery1() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"12mi\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"12mi", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 12, DistanceUnit.MILES); } public void testParsingAndToQuery2() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"12mi\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":[-70, 40]\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"12mi", + "%s":[-70, 40] + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 12, DistanceUnit.MILES); } public void testParsingAndToQuery3() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"12mi\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":\"40, -70\"\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"12mi", + "%s":"40, -70" + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 12, DistanceUnit.MILES); } public void testParsingAndToQuery4() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"12mi\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":\"drn5x1g8cu2y\"\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"12mi", + "%s":"drn5x1g8cu2y" + } + } + """.formatted(GEO_POINT_FIELD_NAME); GeoPoint geoPoint = GeoPoint.fromGeohash("drn5x1g8cu2y"); assertGeoDistanceRangeQuery(query, geoPoint.getLat(), geoPoint.getLon(), 12, DistanceUnit.MILES); } public void testParsingAndToQuery5() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":12,\n" - + " \"unit\":\"mi\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":12, + "unit":"mi", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 12, DistanceUnit.MILES); } public void testParsingAndToQuery6() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"12\",\n" - + " \"unit\":\"mi\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"12", + "unit":"mi", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 12, DistanceUnit.MILES); } public void testParsingAndToQuery7() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"19.312128\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"19.312128", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 19.312128, DistanceUnit.DEFAULT); } public void testParsingAndToQuery8() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":19.312128,\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":19.312128, + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 19.312128, DistanceUnit.DEFAULT); } public void testParsingAndToQuery9() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"19.312128\",\n" - + " \"unit\":\"km\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"19.312128", + "unit":"km", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 19.312128, DistanceUnit.KILOMETERS); } public void testParsingAndToQuery10() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":19.312128,\n" - + " \"unit\":\"km\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":19.312128, + "unit":"km", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 19.312128, DistanceUnit.KILOMETERS); } public void testParsingAndToQuery11() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"19.312128km\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"19.312128km", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 19.312128, DistanceUnit.KILOMETERS); } public void testParsingAndToQuery12() throws IOException { - String query = "{\n" - + " \"geo_distance\":{\n" - + " \"distance\":\"12mi\",\n" - + " \"unit\":\"km\",\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_distance":{ + "distance":"12mi", + "unit":"km", + "%s":{ + "lat":40, + "lon":-70 + } + } + } + """.formatted(GEO_POINT_FIELD_NAME); assertGeoDistanceRangeQuery(query, 40, -70, 12, DistanceUnit.MILES); } @@ -336,16 +336,17 @@ private void assertGeoDistanceRangeQuery(String query, double lat, double lon, d } public void testFromJson() throws IOException { - String json = "{\n" - + " \"geo_distance\" : {\n" - + " \"pin.location\" : [ -70.0, 40.0 ],\n" - + " \"distance\" : 12000.0,\n" - + " \"distance_type\" : \"arc\",\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "geo_distance" : { + "pin.location" : [ -70.0, 40.0 ], + "distance" : 12000.0, + "distance_type" : "arc", + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; GeoDistanceQueryBuilder parsed = (GeoDistanceQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, -70.0, parsed.point().getLon(), 0.0001); @@ -369,16 +370,17 @@ public void testIgnoreUnmapped() throws IOException { } public void testParseFailsWithMultipleFields() throws IOException { - String json = "{\n" - + " \"geo_distance\" : {\n" - + " \"point1\" : {\n" - + " \"lat\" : 30, \"lon\" : 12\n" - + " },\n" - + " \"point2\" : {\n" - + " \"lat\" : 30, \"lon\" : 12\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "geo_distance" : { + "point1" : { + "lat" : 30, "lon" : 12 + }, + "point2" : { + "lat" : 30, "lon" : 12 + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[geo_distance] query doesn't support multiple fields, found [point1] and [point2]", e.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java index 732c69ade3613..a8108175da68b 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java @@ -161,82 +161,57 @@ public void testParsingAndToQueryParsingExceptions() throws IOException { } public void testParsingAndToQuery1() throws IOException { - String query = "{\n" - + " \"geo_polygon\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"points\":[\n" - + " [-70, 40],\n" - + " [-80, 30],\n" - + " [-90, 20]\n" - + " ]\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_polygon": { + "%s": { + "points": [ + [ -70, 40 ], + [ -80, 30 ], + [ -90, 20 ] + ] + } + } + }""".formatted(GEO_POINT_FIELD_NAME); assertGeoPolygonQuery(query); assertDeprecationWarning(); } public void testParsingAndToQuery2() throws IOException { - String query = "{\n" - + " \"geo_polygon\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"points\":[\n" - + " {\n" - + " \"lat\":40,\n" - + " \"lon\":-70\n" - + " },\n" - + " {\n" - + " \"lat\":30,\n" - + " \"lon\":-80\n" - + " },\n" - + " {\n" - + " \"lat\":20,\n" - + " \"lon\":-90\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_polygon": { + "%s": { + "points": [ { "lat": 40, "lon": -70 }, { "lat": 30, "lon": -80 }, { "lat": 20, "lon": -90 } ] + } + } + }""".formatted(GEO_POINT_FIELD_NAME); assertGeoPolygonQuery(query); assertDeprecationWarning(); } public void testParsingAndToQuery3() throws IOException { - String query = "{\n" - + " \"geo_polygon\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"points\":[\n" - + " \"40, -70\",\n" - + " \"30, -80\",\n" - + " \"20, -90\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_polygon": { + "%s": { + "points": [ "40, -70", "30, -80", "20, -90" ] + } + } + }""".formatted(GEO_POINT_FIELD_NAME); assertGeoPolygonQuery(query); assertDeprecationWarning(); } public void testParsingAndToQuery4() throws IOException { - String query = "{\n" - + " \"geo_polygon\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"points\":[\n" - + " \"drn5x1g8cu2y\",\n" - + " \"30, -80\",\n" - + " \"20, -90\"\n" - + " ]\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "geo_polygon": { + "%s": { + "points": [ "drn5x1g8cu2y", "30, -80", "20, -90" ] + } + } + }""".formatted(GEO_POINT_FIELD_NAME); assertGeoPolygonQuery(query); assertDeprecationWarning(); } @@ -248,16 +223,17 @@ private void assertGeoPolygonQuery(String query) throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"geo_polygon\" : {\n" - + " \"person.location\" : {\n" - + " \"points\" : [ [ -70.0, 40.0 ], [ -80.0, 30.0 ], [ -90.0, 20.0 ], [ -70.0, 40.0 ] ]\n" - + " },\n" - + " \"validation_method\" : \"STRICT\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "geo_polygon" : { + "person.location" : { + "points" : [ [ -70.0, 40.0 ], [ -80.0, 30.0 ], [ -90.0, 20.0 ], [ -70.0, 40.0 ] ] + }, + "validation_method" : "STRICT", + "ignore_unmapped" : false, + "boost" : 1.0 + } + }"""; GeoPolygonQueryBuilder parsed = (GeoPolygonQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, 4, parsed.points().size()); @@ -280,36 +256,34 @@ public void testIgnoreUnmapped() throws IOException { public void testPointValidation() throws IOException { SearchExecutionContext context = createSearchExecutionContext(); - String queryInvalidLat = "{\n" - + " \"geo_polygon\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"points\":[\n" - + " [-70, 140],\n" - + " [-80, 30],\n" - + " [-90, 20]\n" - + " ]\n" - + " }\n" - + " }\n" - + "}\n"; + String queryInvalidLat = """ + { + "geo_polygon": { + "%s": { + "points": [ + [ -70, 140 ], + [ -80, 30 ], + [ -90, 20 ] + ] + } + } + }""".formatted(GEO_POINT_FIELD_NAME); QueryShardException e1 = expectThrows(QueryShardException.class, () -> parseQuery(queryInvalidLat).toQuery(context)); assertThat(e1.getMessage(), containsString("illegal latitude value [140.0] for [geo_polygon]")); - String queryInvalidLon = "{\n" - + " \"geo_polygon\":{\n" - + " \"" - + GEO_POINT_FIELD_NAME - + "\":{\n" - + " \"points\":[\n" - + " [-70, 40],\n" - + " [-80, 30],\n" - + " [-190, 20]\n" - + " ]\n" - + " }\n" - + " }\n" - + "}\n"; + String queryInvalidLon = """ + { + "geo_polygon": { + "%s": { + "points": [ + [ -70, 40 ], + [ -80, 30 ], + [ -190, 20 ] + ] + } + } + }""".formatted(GEO_POINT_FIELD_NAME); QueryShardException e2 = expectThrows(QueryShardException.class, () -> parseQuery(queryInvalidLon).toQuery(context)); assertThat(e2.getMessage(), containsString("illegal longitude value [-190.0] for [geo_polygon]")); diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java index 67dbcfbd26ecb..308324acc43c6 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java @@ -129,19 +129,20 @@ public void testThatXContentSerializationInsideOfArrayWorks() throws Exception { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"geo_shape\" : {\n" - + " \"location\" : {\n" - + " \"shape\" : {\n" - + " \"type\" : \"Envelope\",\n" - + " \"coordinates\" : [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ]\n" - + " },\n" - + " \"relation\" : \"intersects\"\n" - + " },\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 42.0\n" - + " }\n" - + "}"; + String json = """ + { + "geo_shape" : { + "location" : { + "shape" : { + "type" : "Envelope", + "coordinates" : [ [ 13.0, 53.0 ], [ 14.0, 52.0 ] ] + }, + "relation" : "intersects" + }, + "ignore_unmapped" : false, + "boost" : 42.0 + } + }"""; GeoShapeQueryBuilder parsed = (GeoShapeQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, 42.0, parsed.boost(), 0.0001); diff --git a/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java index 6e7ed1a8c77fb..ddc06e520d203 100644 --- a/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java @@ -58,13 +58,25 @@ public void testIdsQueryWithInvalidValues() throws Exception { } public void testFromJson() throws IOException { - String json = "{\n" + " \"ids\" : {\n" + " \"values\" : [ \"1\", \"100\", \"4\" ],\n" + " \"boost\" : 1.0\n" + " }\n" + "}"; + String json = """ + { + "ids" : { + "values" : [ "1", "100", "4" ], + "boost" : 1.0 + } + }"""; IdsQueryBuilder parsed = (IdsQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertThat(parsed.ids(), contains("1", "100", "4")); // check that type that is not an array and also ids that are numbers are parsed - json = "{\n" + " \"ids\" : {\n" + " \"values\" : [ 1, 100, 4 ],\n" + " \"boost\" : 1.0\n" + " }\n" + "}"; + json = """ + { + "ids" : { + "values" : [ 1, 100, 4 ], + "boost" : 1.0 + } + }"""; parsed = (IdsQueryBuilder) parseQuery(json); assertThat(parsed.ids(), contains("1", "100", "4")); } diff --git a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java index 43427842b4642..d055c75dac817 100644 --- a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java @@ -185,20 +185,33 @@ public IntervalQueryBuilder mutateInstance(IntervalQueryBuilder instance) throws public void testMatchInterval() throws IOException { - String json = "{ \"intervals\" : " + "{ \"" + TEXT_FIELD_NAME + "\" : { \"match\" : { \"query\" : \"Hello world\" } } } }"; + String json = """ + { + "intervals": { + "%s": { + "match": { + "query": "Hello world" + } + } + } + }""".formatted(TEXT_FIELD_NAME); IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); Query expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.unordered(Intervals.term("hello"), Intervals.term("world"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - json = "{ \"intervals\" : " - + "{ \"" - + TEXT_FIELD_NAME - + "\" : { " - + " \"match\" : { " - + " \"query\" : \"Hello world\"," - + " \"max_gaps\" : 40 } } } }"; + json = """ + { + "intervals": { + "%s": { + "match": { + "query": "Hello world", + "max_gaps": 40 + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json); expected = new IntervalQuery( @@ -207,14 +220,18 @@ public void testMatchInterval() throws IOException { ); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - json = "{ \"intervals\" : " - + "{ \"" - + TEXT_FIELD_NAME - + "\" : { " - + " \"match\" : { " - + " \"query\" : \"Hello world\"," - + " \"ordered\" : true }," - + " \"boost\" : 2 } } }"; + json = """ + { + "intervals": { + "%s": { + "match": { + "query": "Hello world", + "ordered": true + }, + "boost": 2 + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json); expected = new BoostQuery( @@ -223,15 +240,19 @@ public void testMatchInterval() throws IOException { ); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - json = "{ \"intervals\" : " - + "{ \"" - + TEXT_FIELD_NAME - + "\" : { " - + " \"match\" : { " - + " \"query\" : \"Hello world\"," - + " \"max_gaps\" : 10," - + " \"analyzer\" : \"whitespace\"," - + " \"ordered\" : true } } } }"; + json = """ + { + "intervals": { + "%s": { + "match": { + "query": "Hello world", + "max_gaps": 10, + "analyzer": "whitespace", + "ordered": true + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json); expected = new IntervalQuery( @@ -240,18 +261,20 @@ public void testMatchInterval() throws IOException { ); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - json = "{ \"intervals\" : " - + "{ \"" - + TEXT_FIELD_NAME - + "\" : { " - + " \"match\" : { " - + " \"query\" : \"Hello world\"," - + " \"max_gaps\" : 10," - + " \"analyzer\" : \"whitespace\"," - + " \"use_field\" : \"" - + MASKED_FIELD - + "\"," - + " \"ordered\" : true } } } }"; + json = """ + { + "intervals": { + "%s": { + "match": { + "query": "Hello world", + "max_gaps": 10, + "analyzer": "whitespace", + "use_field": "%s", + "ordered": true + } + } + } + }""".formatted(TEXT_FIELD_NAME, MASKED_FIELD); builder = (IntervalQueryBuilder) parseQuery(json); expected = new IntervalQuery( @@ -260,18 +283,26 @@ public void testMatchInterval() throws IOException { ); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - json = "{ \"intervals\" : " - + "{ \"" - + TEXT_FIELD_NAME - + "\" : { " - + " \"match\" : { " - + " \"query\" : \"Hello world\"," - + " \"max_gaps\" : 10," - + " \"analyzer\" : \"whitespace\"," - + " \"ordered\" : true," - + " \"filter\" : {" - + " \"containing\" : {" - + " \"match\" : { \"query\" : \"blah\" } } } } } } }"; + json = """ + { + "intervals": { + "%s": { + "match": { + "query": "Hello world", + "max_gaps": 10, + "analyzer": "whitespace", + "ordered": true, + "filter": { + "containing": { + "match": { + "query": "blah" + } + } + } + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json); expected = new IntervalQuery( @@ -286,26 +317,59 @@ public void testMatchInterval() throws IOException { public void testOrInterval() throws IOException { - String json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": {" - + " \"any_of\" : { " - + " \"intervals\" : [" - + " { \"match\" : { \"query\" : \"one\" } }," - + " { \"match\" : { \"query\" : \"two\" } } ] } } } }"; + String json = """ + { + "intervals": { + "%s": { + "any_of": { + "intervals": [ + { + "match": { + "query": "one" + } + }, + { + "match": { + "query": "two" + } + } + ] + } + } + } + }""".formatted(TEXT_FIELD_NAME); IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); Query expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.or(Intervals.term("one"), Intervals.term("two"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": {" - + " \"any_of\" : { " - + " \"intervals\" : [" - + " { \"match\" : { \"query\" : \"one\" } }," - + " { \"match\" : { \"query\" : \"two\" } } ]," - + " \"filter\" : {" - + " \"not_containing\" : { \"match\" : { \"query\" : \"three\" } } } } } } }"; + json = """ + { + "intervals": { + "%s": { + "any_of": { + "intervals": [ + { + "match": { + "query": "one" + } + }, + { + "match": { + "query": "two" + } + } + ], + "filter": { + "not_containing": { + "match": { + "query": "three" + } + } + } + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json); expected = new IntervalQuery( TEXT_FIELD_NAME, @@ -316,25 +380,39 @@ public void testOrInterval() throws IOException { public void testCombineInterval() throws IOException { - String json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": {" - + " \"all_of\" : {" - + " \"ordered\" : true," - + " \"intervals\" : [" - + " { \"match\" : { \"query\" : \"one\" } }," - + " { \"all_of\" : { " - + " \"ordered\" : false," - + " \"intervals\" : [" - + " { \"match\" : { \"query\" : \"two\" } }," - + " { \"match\" : { \"query\" : \"three\" } } ] } } ]," - + " \"max_gaps\" : 30," - + " \"filter\" : { " - + " \"contained_by\" : { " - + " \"match\" : { " - + " \"query\" : \"SENTENCE\"," - + " \"analyzer\" : \"keyword\" } } } }," - + " \"boost\" : 1.5 } } }"; + String json = """ + { + "intervals": { + "%s": { + "all_of": { + "ordered": true, + "intervals": [ + { + "match": { + "query": "one" + } + }, + { + "all_of": { + "ordered": false, + "intervals": [ { "match": { "query": "two" } }, { "match": { "query": "three" } } ] + } + } + ], + "max_gaps": 30, + "filter": { + "contained_by": { + "match": { + "query": "SENTENCE", + "analyzer": "keyword" + } + } + } + }, + "boost": 1.5 + } + } + }""".formatted(TEXT_FIELD_NAME); IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); Query expected = new BoostQuery( new IntervalQuery( @@ -354,22 +432,47 @@ public void testCombineInterval() throws IOException { } public void testCombineDisjunctionInterval() throws IOException { - String json = "{ \"intervals\" : " - + "{ \"" - + TEXT_FIELD_NAME - + "\": { " - + " \"all_of\" : {" - + " \"ordered\" : true," - + " \"intervals\" : [" - + " { \"match\" : { \"query\" : \"atmosphere\" } }," - + " { \"any_of\" : {" - + " \"intervals\" : [" - + " { \"match\" : { \"query\" : \"cold\" } }," - + " { \"match\" : { \"query\" : \"outside\" } } ] } } ]," - + " \"max_gaps\" : 30," - + " \"filter\" : { " - + " \"not_contained_by\" : { " - + " \"match\" : { \"query\" : \"freeze\" } } } } } } }"; + String json = """ + { + "intervals": { + "%s": { + "all_of": { + "ordered": true, + "intervals": [ + { + "match": { + "query": "atmosphere" + } + }, + { + "any_of": { + "intervals": [ + { + "match": { + "query": "cold" + } + }, + { + "match": { + "query": "outside" + } + } + ] + } + } + ], + "max_gaps": 30, + "filter": { + "not_contained_by": { + "match": { + "query": "freeze" + } + } + } + } + } + } + }""".formatted(TEXT_FIELD_NAME); IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); Query expected = new IntervalQuery( @@ -405,18 +508,20 @@ public void testNonIndexedFields() throws IOException { }); assertThat(e.getMessage(), equalTo("Cannot create intervals over field [" + NO_POSITIONS_FIELD + "] with no positions indexed")); - String json = "{ \"intervals\" : " - + "{ \"" - + TEXT_FIELD_NAME - + "\" : { " - + " \"match\" : { " - + " \"query\" : \"Hello world\"," - + " \"max_gaps\" : 10," - + " \"analyzer\" : \"whitespace\"," - + " \"use_field\" : \"" - + NO_POSITIONS_FIELD - + "\"," - + " \"ordered\" : true } } } }"; + String json = """ + { + "intervals": { + "%s": { + "match": { + "query": "Hello world", + "max_gaps": 10, + "analyzer": "whitespace", + "use_field": "%s", + "ordered": true + } + } + } + }""".formatted(TEXT_FIELD_NAME, NO_POSITIONS_FIELD); e = expectThrows(IllegalArgumentException.class, () -> { IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); @@ -426,12 +531,24 @@ public void testNonIndexedFields() throws IOException { } public void testMultipleProviders() { - String json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"boost\" : 1," - + "\"match\" : { \"query\" : \"term1\" }," - + "\"all_of\" : { \"intervals\" : [ { \"query\" : \"term2\" } ] } }"; + String json = """ + { + "intervals": { + "%s": { + "boost": 1, + "match": { + "query": "term1" + }, + "all_of": { + "intervals": [ + { + "query": "term2" + } + ] + } + } + } + }""".formatted(TEXT_FIELD_NAME); ParsingException e = expectThrows(ParsingException.class, () -> { parseQuery(json); }); assertThat(e.getMessage(), equalTo("Only one interval rule can be specified, found [match] and [all_of]")); @@ -454,14 +571,21 @@ public FactoryType compile(Script script, ScriptContext 3\" } } } } } }"; + String json = """ + { + "intervals": { + "%s": { + "match": { + "query": "term1", + "filter": { + "script": { + "source": "interval.start > 3" + } + } + } + } + } + }""".formatted(TEXT_FIELD_NAME); IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); Query q = builder.toQuery(context); @@ -475,37 +599,75 @@ public FactoryType compile(Script script, ScriptContext { IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(no_positions_json); builder1.toQuery(createSearchExecutionContext()); }); - String no_positions_fixed_field_json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"prefix\" : { \"prefix\" : \"term\", \"use_field\" : \"" - + NO_POSITIONS_FIELD - + "\" } } } }"; + String no_positions_fixed_field_json = """ + { + "intervals": { + "%s": { + "prefix": { + "prefix": "term", + "use_field": "%s" + } + } + } + }""".formatted(TEXT_FIELD_NAME, NO_POSITIONS_FIELD); expectThrows(IllegalArgumentException.class, () -> { IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(no_positions_fixed_field_json); builder1.toQuery(createSearchExecutionContext()); }); - String prefix_json = "{ \"intervals\" : { \"" + PREFIXED_FIELD + "\": { " + "\"prefix\" : { \"prefix\" : \"term\" } } } }"; + String prefix_json = """ + { + "intervals": { + "%s": { + "prefix": { + "prefix": "term" + } + } + } + }""".formatted(PREFIXED_FIELD); builder = (IntervalQueryBuilder) parseQuery(prefix_json); expected = new IntervalQuery(PREFIXED_FIELD, Intervals.fixField(PREFIXED_FIELD + "._index_prefix", Intervals.term("term"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String short_prefix_json = "{ \"intervals\" : { \"" + PREFIXED_FIELD + "\": { " + "\"prefix\" : { \"prefix\" : \"t\" } } } }"; + String short_prefix_json = """ + { + "intervals": { + "%s": { + "prefix": { + "prefix": "t" + } + } + } + }""".formatted(PREFIXED_FIELD); builder = (IntervalQueryBuilder) parseQuery(short_prefix_json); expected = new IntervalQuery( PREFIXED_FIELD, @@ -513,12 +675,17 @@ public void testPrefixes() throws IOException { ); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String fix_field_prefix_json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"prefix\" : { \"prefix\" : \"term\", \"use_field\" : \"" - + PREFIXED_FIELD - + "\" } } } }"; + String fix_field_prefix_json = """ + { + "intervals": { + "%s": { + "prefix": { + "prefix": "term", + "use_field": "%s" + } + } + } + }""".formatted(TEXT_FIELD_NAME, PREFIXED_FIELD); builder = (IntervalQueryBuilder) parseQuery(fix_field_prefix_json); // This looks weird, but it's fine, because the innermost fixField wins expected = new IntervalQuery( @@ -527,20 +694,34 @@ public void testPrefixes() throws IOException { ); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String keyword_json = "{ \"intervals\" : { \"" - + PREFIXED_FIELD - + "\": { " - + "\"prefix\" : { \"prefix\" : \"Term\", \"analyzer\" : \"keyword\" } } } }"; + String keyword_json = """ + { + "intervals": { + "%s": { + "prefix": { + "prefix": "Term", + "analyzer": "keyword" + } + } + } + }""".formatted(PREFIXED_FIELD); builder = (IntervalQueryBuilder) parseQuery(keyword_json); expected = new IntervalQuery(PREFIXED_FIELD, Intervals.fixField(PREFIXED_FIELD + "._index_prefix", Intervals.term("Term"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String keyword_fix_field_json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"prefix\" : { \"prefix\" : \"Term\", \"analyzer\" : \"keyword\", \"use_field\" : \"" - + PREFIXED_FIELD - + "\" } } } }"; + String keyword_fix_field_json = """ + { + "intervals": { + "%s": { + "prefix": { + "prefix": "Term", + "analyzer": "keyword", + "use_field": "%s" + } + } + } + } + """.formatted(TEXT_FIELD_NAME, PREFIXED_FIELD); builder = (IntervalQueryBuilder) parseQuery(keyword_fix_field_json); expected = new IntervalQuery( TEXT_FIELD_NAME, @@ -551,54 +732,97 @@ public void testPrefixes() throws IOException { public void testWildcard() throws IOException { - String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"wildcard\" : { \"pattern\" : \"Te?m\" } } } }"; + String json = """ + { + "intervals": { + "%s": { + "wildcard": { + "pattern": "Te?m" + } + } + } + }""".formatted(TEXT_FIELD_NAME); IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); Query expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.wildcard(new BytesRef("te?m"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String no_positions_json = "{ \"intervals\" : { \"" - + NO_POSITIONS_FIELD - + "\": { " - + "\"wildcard\" : { \"pattern\" : \"term\" } } } }"; + String no_positions_json = """ + { + "intervals": { + "%s": { + "wildcard": { + "pattern": "term" + } + } + } + } + """.formatted(NO_POSITIONS_FIELD); expectThrows(IllegalArgumentException.class, () -> { IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(no_positions_json); builder1.toQuery(createSearchExecutionContext()); }); - String keyword_json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"analyzer\" : \"keyword\" } } } }"; + String keyword_json = """ + { + "intervals": { + "%s": { + "wildcard": { + "pattern": "Te?m", + "analyzer": "keyword" + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(keyword_json); expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.wildcard(new BytesRef("Te?m"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String fixed_field_json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"use_field\" : \"masked_field\" } } } }"; + String fixed_field_json = """ + { + "intervals": { + "%s": { + "wildcard": { + "pattern": "Te?m", + "use_field": "masked_field" + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(fixed_field_json); expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(MASKED_FIELD, Intervals.wildcard(new BytesRef("te?m")))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String fixed_field_json_no_positions = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"use_field\" : \"" - + NO_POSITIONS_FIELD - + "\" } } } }"; + String fixed_field_json_no_positions = """ + { + "intervals": { + "%s": { + "wildcard": { + "pattern": "Te?m", + "use_field": "%s" + } + } + } + }""".formatted(TEXT_FIELD_NAME, NO_POSITIONS_FIELD); expectThrows(IllegalArgumentException.class, () -> { IntervalQueryBuilder builder1 = (IntervalQueryBuilder) parseQuery(fixed_field_json_no_positions); builder1.toQuery(createSearchExecutionContext()); }); - String fixed_field_analyzer_json = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"wildcard\" : { \"pattern\" : \"Te?m\", \"use_field\" : \"masked_field\", \"analyzer\" : \"keyword\" } } } }"; + String fixed_field_analyzer_json = """ + { + "intervals": { + "%s": { + "wildcard": { + "pattern": "Te?m", + "use_field": "masked_field", + "analyzer": "keyword" + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(fixed_field_analyzer_json); expected = new IntervalQuery(TEXT_FIELD_NAME, Intervals.fixField(MASKED_FIELD, Intervals.wildcard(new BytesRef("Te?m")))); @@ -612,7 +836,16 @@ private static IntervalsSource buildFuzzySource(String term, String label, int p public void testFuzzy() throws IOException { - String json = "{ \"intervals\" : { \"" + TEXT_FIELD_NAME + "\": { " + "\"fuzzy\" : { \"term\" : \"Term\" } } } }"; + String json = """ + { + "intervals": { + "%s": { + "fuzzy": { + "term": "Term" + } + } + } + }""".formatted(TEXT_FIELD_NAME); IntervalQueryBuilder builder = (IntervalQueryBuilder) parseQuery(json); Query expected = new IntervalQuery( @@ -621,45 +854,82 @@ public void testFuzzy() throws IOException { ); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String json_with_prefix = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2 } } } }"; + String json_with_prefix = """ + { + "intervals": { + "%s": { + "fuzzy": { + "term": "Term", + "prefix_length": 2 + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json_with_prefix); expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("term", "term", 2, true, Fuzziness.AUTO.asDistance("term"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String json_with_fuzziness = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"fuzziness\" : \"1\" } } } }"; + String json_with_fuzziness = """ + { + "intervals": { + "%s": { + "fuzzy": { + "term": "Term", + "prefix_length": 2, + "fuzziness": "1" + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json_with_fuzziness); expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("term", "term", 2, true, Fuzziness.ONE.asDistance("term"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String json_no_transpositions = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"transpositions\" : false } } } }"; + String json_no_transpositions = """ + { + "intervals": { + "%s": { + "fuzzy": { + "term": "Term", + "prefix_length": 2, + "transpositions": false + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json_no_transpositions); expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("term", "term", 2, false, Fuzziness.AUTO.asDistance("term"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String json_with_analyzer = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"analyzer\" : \"keyword\" } } } }"; + String json_with_analyzer = """ + { + "intervals": { + "%s": { + "fuzzy": { + "term": "Term", + "prefix_length": 2, + "analyzer": "keyword" + } + } + } + }""".formatted(TEXT_FIELD_NAME); builder = (IntervalQueryBuilder) parseQuery(json_with_analyzer); expected = new IntervalQuery(TEXT_FIELD_NAME, buildFuzzySource("Term", "Term", 2, true, Fuzziness.AUTO.asDistance("term"))); assertEquals(expected, builder.toQuery(createSearchExecutionContext())); - String json_with_fixfield = "{ \"intervals\" : { \"" - + TEXT_FIELD_NAME - + "\": { " - + "\"fuzzy\" : { \"term\" : \"Term\", \"prefix_length\" : 2, \"fuzziness\" : \"1\", " - + "\"use_field\" : \"" - + MASKED_FIELD - + "\" } } } }"; + String json_with_fixfield = """ + { + "intervals": { + "%s": { + "fuzzy": { + "term": "Term", + "prefix_length": 2, + "fuzziness": "1", + "use_field": "%s" + } + } + } + }""".formatted(TEXT_FIELD_NAME, MASKED_FIELD); builder = (IntervalQueryBuilder) parseQuery(json_with_fixfield); expected = new IntervalQuery( TEXT_FIELD_NAME, diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchAllQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchAllQueryBuilderTests.java index e211db32960b4..555bd353726e8 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchAllQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchAllQueryBuilderTests.java @@ -29,7 +29,12 @@ protected void doAssertLuceneQuery(MatchAllQueryBuilder queryBuilder, Query quer } public void testFromJson() throws IOException { - String json = "{\n" + " \"match_all\" : {\n" + " \"boost\" : 1.2\n" + " }\n" + "}"; + String json = """ + { + "match_all" : { + "boost" : 1.2 + } + }"""; MatchAllQueryBuilder parsed = (MatchAllQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, 1.2, parsed.boost(), 0.0001); diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java index 77eb3ac38b917..d1cf813f458a4 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java @@ -156,41 +156,44 @@ public void testIllegalValues() { } public void testFromSimpleJson() throws IOException { - final String simple = "{" + "\"match_bool_prefix\": {" + "\"fieldName\": \"fieldValue\"" + "}" + "}"; - final String expected = "{" - + "\"match_bool_prefix\": {" - + "\"fieldName\": {" - + "\"query\": \"fieldValue\"," - + "\"operator\": \"OR\"," - + "\"prefix_length\": 0," - + "\"max_expansions\": 50," - + "\"fuzzy_transpositions\": true," - + "\"boost\": 1.0" - + "}" - + "}" - + "}"; + final String simple = """ + {"match_bool_prefix": {"fieldName": "fieldValue"}}"""; + final String expected = """ + { + "match_bool_prefix": { + "fieldName": { + "query": "fieldValue", + "operator": "OR", + "prefix_length": 0, + "max_expansions": 50, + "fuzzy_transpositions": true, + "boost": 1.0 + } + } + }"""; final MatchBoolPrefixQueryBuilder builder = (MatchBoolPrefixQueryBuilder) parseQuery(simple); checkGeneratedJson(expected, builder); } public void testFromJson() throws IOException { - final String expected = "{" - + "\"match_bool_prefix\": {" - + "\"fieldName\": {" - + "\"query\": \"fieldValue\"," - + "\"analyzer\": \"simple\"," - + "\"operator\": \"AND\"," - + "\"minimum_should_match\": \"2\"," - + "\"fuzziness\": \"1\"," - + "\"prefix_length\": 1," - + "\"max_expansions\": 10," - + "\"fuzzy_transpositions\": false," - + "\"fuzzy_rewrite\": \"constant_score\"," - + "\"boost\": 2.0" - + "}" - + "}" - + "}"; + final String expected = """ + { + "match_bool_prefix": { + "fieldName": { + "query": "fieldValue", + "analyzer": "simple", + "operator": "AND", + "minimum_should_match": "2", + "fuzziness": "1", + "prefix_length": 1, + "max_expansions": 10, + "fuzzy_transpositions": false, + "fuzzy_rewrite": "constant_score", + "boost": 2.0 + } + } + }"""; final MatchBoolPrefixQueryBuilder builder = (MatchBoolPrefixQueryBuilder) parseQuery(expected); checkGeneratedJson(expected, builder); @@ -198,16 +201,17 @@ public void testFromJson() throws IOException { public void testParseFailsWithMultipleFields() { { - final String json = "{" - + "\"match_bool_prefix\" : {" - + "\"field_name_1\" : {" - + "\"query\" : \"foo\"" - + "}," - + "\"field_name_2\" : {" - + "\"query\" : \"foo\"\n" - + "}" - + "}" - + "}"; + final String json = """ + { + "match_bool_prefix": { + "field_name_1": { + "query": "foo" + }, + "field_name_2": { + "query": "foo" + } + } + }"""; final ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals( "[match_bool_prefix] query doesn't support multiple fields, found [field_name_1] and [field_name_2]", @@ -216,12 +220,8 @@ public void testParseFailsWithMultipleFields() { } { - final String simpleJson = "{" - + "\"match_bool_prefix\" : {" - + "\"field_name_1\" : \"foo\"," - + "\"field_name_2\" : \"foo\"" - + "}" - + "}"; + final String simpleJson = """ + {"match_bool_prefix" : {"field_name_1" : "foo","field_name_2" : "foo"}}"""; final ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(simpleJson)); assertEquals( "[match_bool_prefix] query doesn't support multiple fields, found [field_name_1] and [field_name_2]", diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java index 778375945a35f..e5c44193d02f4 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java @@ -29,7 +29,12 @@ protected void doAssertLuceneQuery(MatchNoneQueryBuilder queryBuilder, Query que } public void testFromJson() throws IOException { - String json = "{\n" + " \"match_none\" : {\n" + " \"boost\" : 1.2\n" + " }\n" + "}"; + String json = """ + { + "match_none" : { + "boost" : 1.2 + } + }"""; MatchNoneQueryBuilder parsed = (MatchNoneQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, 1.2, parsed.boost(), 0.0001); diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java index e87d51152500b..00de0a11bb0ce 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java @@ -68,15 +68,12 @@ protected Map getAlternateVersions() { randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10) ); - String contentString = "{\n" - + " \"match_phrase_prefix\" : {\n" - + " \"" - + matchPhrasePrefixQuery.fieldName() - + "\" : \"" - + matchPhrasePrefixQuery.value() - + "\"\n" - + " }\n" - + "}"; + String contentString = """ + { + "match_phrase_prefix" : { + "%s" : "%s" + } + }""".formatted(matchPhrasePrefixQuery.fieldName(), matchPhrasePrefixQuery.value()); alternateVersions.put(contentString, matchPhrasePrefixQuery); return alternateVersions; } @@ -133,65 +130,75 @@ public void testPhrasePrefixZeroTermsQuery() throws IOException { } public void testPhrasePrefixMatchQuery() throws IOException { - String json1 = "{\n" + " \"match_phrase_prefix\" : {\n" + " \"message\" : \"this is a test\"\n" + " }\n" + "}"; - - String expected = "{\n" - + " \"match_phrase_prefix\" : {\n" - + " \"message\" : {\n" - + " \"query\" : \"this is a test\",\n" - + " \"slop\" : 0,\n" - + " \"max_expansions\" : 50,\n" - + " \"zero_terms_query\" : \"NONE\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json1 = """ + { + "match_phrase_prefix" : { + "message" : "this is a test" + } + }"""; + + String expected = """ + { + "match_phrase_prefix" : { + "message" : { + "query" : "this is a test", + "slop" : 0, + "max_expansions" : 50, + "zero_terms_query" : "NONE", + "boost" : 1.0 + } + } + }"""; MatchPhrasePrefixQueryBuilder qb = (MatchPhrasePrefixQueryBuilder) parseQuery(json1); checkGeneratedJson(expected, qb); - String json3 = "{\n" - + " \"match_phrase_prefix\" : {\n" - + " \"message\" : {\n" - + " \"query\" : \"this is a test\",\n" - + " \"max_expansions\" : 10\n" - + " }\n" - + " }\n" - + "}"; - expected = "{\n" - + " \"match_phrase_prefix\" : {\n" - + " \"message\" : {\n" - + " \"query\" : \"this is a test\",\n" - + " \"slop\" : 0,\n" - + " \"max_expansions\" : 10,\n" - + " \"zero_terms_query\" : \"NONE\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json3 = """ + { + "match_phrase_prefix" : { + "message" : { + "query" : "this is a test", + "max_expansions" : 10 + } + } + }"""; + expected = """ + { + "match_phrase_prefix" : { + "message" : { + "query" : "this is a test", + "slop" : 0, + "max_expansions" : 10, + "zero_terms_query" : "NONE", + "boost" : 1.0 + } + } + }"""; qb = (MatchPhrasePrefixQueryBuilder) parseQuery(json3); checkGeneratedJson(expected, qb); } public void testParseFailsWithMultipleFields() throws IOException { - String json = "{\n" - + " \"match_phrase_prefix\" : {\n" - + " \"message1\" : {\n" - + " \"query\" : \"this is a test\"\n" - + " },\n" - + " \"message2\" : {\n" - + " \"query\" : \"this is a test\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "match_phrase_prefix" : { + "message1" : { + "query" : "this is a test" + }, + "message2" : { + "query" : "this is a test" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[match_phrase_prefix] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); - String shortJson = "{\n" - + " \"match_phrase_prefix\" : {\n" - + " \"message1\" : \"this is a test\",\n" - + " \"message2\" : \"this is a test\"\n" - + " }\n" - + "}"; + String shortJson = """ + { + "match_phrase_prefix" : { + "message1" : "this is a test", + "message2" : "this is a test" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[match_phrase_prefix] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java index e988a69c22e8f..bc3047dbc4ce1 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java @@ -76,15 +76,12 @@ protected Map getAlternateVersions() { randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10) ); - String contentString = "{\n" - + " \"match_phrase\" : {\n" - + " \"" - + matchPhraseQuery.fieldName() - + "\" : \"" - + matchPhraseQuery.value() - + "\"\n" - + " }\n" - + "}"; + String contentString = """ + { + "match_phrase" : { + "%s" : "%s" + } + }""".formatted(matchPhraseQuery.fieldName(), matchPhraseQuery.value()); alternateVersions.put(contentString, matchPhraseQuery); return alternateVersions; } @@ -125,33 +122,40 @@ public void testBadAnalyzer() throws IOException { } public void testFromSimpleJson() throws IOException { - String json1 = "{\n" + " \"match_phrase\" : {\n" + " \"message\" : \"this is a test\"\n" + " }\n" + "}"; - - String expected = "{\n" - + " \"match_phrase\" : {\n" - + " \"message\" : {\n" - + " \"query\" : \"this is a test\",\n" - + " \"slop\" : 0,\n" - + " \"zero_terms_query\" : \"NONE\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json1 = """ + { + "match_phrase" : { + "message" : "this is a test" + } + }"""; + + String expected = """ + { + "match_phrase" : { + "message" : { + "query" : "this is a test", + "slop" : 0, + "zero_terms_query" : "NONE", + "boost" : 1.0 + } + } + }"""; MatchPhraseQueryBuilder qb = (MatchPhraseQueryBuilder) parseQuery(json1); checkGeneratedJson(expected, qb); } public void testFromJson() throws IOException { - String json = "{\n" - + " \"match_phrase\" : {\n" - + " \"message\" : {\n" - + " \"query\" : \"this is a test\",\n" - + " \"slop\" : 2,\n" - + " \"zero_terms_query\" : \"ALL\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "match_phrase" : { + "message" : { + "query" : "this is a test", + "slop" : 2, + "zero_terms_query" : "ALL", + "boost" : 1.0 + } + } + }"""; MatchPhraseQueryBuilder parsed = (MatchPhraseQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -162,25 +166,27 @@ public void testFromJson() throws IOException { } public void testParseFailsWithMultipleFields() throws IOException { - String json = "{\n" - + " \"match_phrase\" : {\n" - + " \"message1\" : {\n" - + " \"query\" : \"this is a test\"\n" - + " },\n" - + " \"message2\" : {\n" - + " \"query\" : \"this is a test\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "match_phrase" : { + "message1" : { + "query" : "this is a test" + }, + "message2" : { + "query" : "this is a test" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[match_phrase] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); - String shortJson = "{\n" - + " \"match_phrase\" : {\n" - + " \"message1\" : \"this is a test\",\n" - + " \"message2\" : \"this is a test\"\n" - + " }\n" - + "}"; + String shortJson = """ + { + "match_phrase" : { + "message1" : "this is a test", + "message2" : "this is a test" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[match_phrase] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index 9c67d13a51c9f..5ded8d024a75f 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -127,15 +127,12 @@ protected MatchQueryBuilder doCreateTestQueryBuilder() { protected Map getAlternateVersions() { Map alternateVersions = new HashMap<>(); MatchQueryBuilder matchQuery = new MatchQueryBuilder(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLengthBetween(1, 10)); - String contentString = "{\n" - + " \"match\" : {\n" - + " \"" - + matchQuery.fieldName() - + "\" : \"" - + matchQuery.value() - + "\"\n" - + " }\n" - + "}"; + String contentString = """ + { + "match" : { + "%s" : "%s" + } + }""".formatted(matchQuery.fieldName(), matchQuery.value()); alternateVersions.put(contentString, matchQuery); return alternateVersions; } @@ -245,21 +242,22 @@ public void testIllegalValues() { } public void testSimpleMatchQuery() throws IOException { - String json = "{\n" - + " \"match\" : {\n" - + " \"message\" : {\n" - + " \"query\" : \"to be or not to be\",\n" - + " \"operator\" : \"AND\",\n" - + " \"prefix_length\" : 0,\n" - + " \"max_expansions\" : 50,\n" - + " \"fuzzy_transpositions\" : true,\n" - + " \"lenient\" : false,\n" - + " \"zero_terms_query\" : \"ALL\",\n" - + " \"auto_generate_synonyms_phrase_query\" : true,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "match" : { + "message" : { + "query" : "to be or not to be", + "operator" : "AND", + "prefix_length" : 0, + "max_expansions" : 50, + "fuzzy_transpositions" : true, + "lenient" : false, + "zero_terms_query" : "ALL", + "auto_generate_synonyms_phrase_query" : true, + "boost" : 1.0 + } + } + }"""; MatchQueryBuilder qb = (MatchQueryBuilder) parseQuery(json); checkGeneratedJson(json, qb); @@ -309,40 +307,48 @@ public void testLenientFlag() throws Exception { } public void testParseFailsWithMultipleFields() { - String json = "{\n" - + " \"match\" : {\n" - + " \"message1\" : {\n" - + " \"query\" : \"this is a test\"\n" - + " },\n" - + " \"message2\" : {\n" - + " \"query\" : \"this is a test\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "match" : { + "message1" : { + "query" : "this is a test" + }, + "message2" : { + "query" : "this is a test" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[match] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); - String shortJson = "{\n" - + " \"match\" : {\n" - + " \"message1\" : \"this is a test\",\n" - + " \"message2\" : \"this is a test\"\n" - + " }\n" - + "}"; + String shortJson = """ + { + "match" : { + "message1" : "this is a test", + "message2" : "this is a test" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[match] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } public void testParseFailsWithTermsArray() { - String json1 = "{\n" - + " \"match\" : {\n" - + " \"message1\" : {\n" - + " \"query\" : [\"term1\", \"term2\"]\n" - + " }\n" - + " }\n" - + "}"; + String json1 = """ + { + "match" : { + "message1" : { + "query" : ["term1", "term2"] + } + } + }"""; expectThrows(ParsingException.class, () -> parseQuery(json1)); - String json2 = "{\n" + " \"match\" : {\n" + " \"message1\" : [\"term1\", \"term2\"]\n" + " }\n" + "}"; + String json2 = """ + { + "match" : { + "message1" : ["term1", "term2"] + } + }"""; expectThrows(IllegalStateException.class, () -> parseQuery(json2)); } diff --git a/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java index f11142d2d1598..743de8ea7d884 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java @@ -427,29 +427,30 @@ public void testCacheability() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"more_like_this\" : {\n" - + " \"fields\" : [ \"title\", \"description\" ],\n" - + " \"like\" : [ \"and potentially some more text here as well\", {\n" - + " \"_index\" : \"imdb\",\n" - + " \"_id\" : \"1\"\n" - + " }, {\n" - + " \"_index\" : \"imdb\",\n" - + " \"_id\" : \"2\"\n" - + " } ],\n" - + " \"max_query_terms\" : 12,\n" - + " \"min_term_freq\" : 1,\n" - + " \"min_doc_freq\" : 5,\n" - + " \"max_doc_freq\" : 2147483647,\n" - + " \"min_word_length\" : 0,\n" - + " \"max_word_length\" : 0,\n" - + " \"minimum_should_match\" : \"30%\",\n" - + " \"boost_terms\" : 0.0,\n" - + " \"include\" : false,\n" - + " \"fail_on_unsupported_field\" : true,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "more_like_this" : { + "fields" : [ "title", "description" ], + "like" : [ "and potentially some more text here as well", { + "_index" : "imdb", + "_id" : "1" + }, { + "_index" : "imdb", + "_id" : "2" + } ], + "max_query_terms" : 12, + "min_term_freq" : 1, + "min_doc_freq" : 5, + "max_doc_freq" : 2147483647, + "min_word_length" : 0, + "max_word_length" : 0, + "minimum_should_match" : "30%", + "boost_terms" : 0.0, + "include" : false, + "fail_on_unsupported_field" : true, + "boost" : 1.0 + } + }"""; MoreLikeThisQueryBuilder parsed = (MoreLikeThisQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java index b99b16323df01..6e4a7be09035c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java @@ -142,12 +142,13 @@ && randomBoolean() @Override protected Map getAlternateVersions() { Map alternateVersions = new HashMap<>(); - String query = "{\n" - + " \"multi_match\": {\n" - + " \"query\": \"foo bar\",\n" - + " \"fields\": \"myField\"\n" - + " }\n" - + "}"; + String query = """ + { + "multi_match": { + "query": "foo bar", + "fields": "myField" + } + }"""; alternateVersions.put(query, new MultiMatchQueryBuilder("foo bar", "myField")); return alternateVersions; } @@ -281,22 +282,23 @@ public void testToQueryBooleanPrefixMultipleFields() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"multi_match\" : {\n" - + " \"query\" : \"quick brown fox\",\n" - + " \"fields\" : [ \"title^1.0\", \"title.original^1.0\", \"title.shingles^1.0\" ],\n" - + " \"type\" : \"most_fields\",\n" - + " \"operator\" : \"OR\",\n" - + " \"slop\" : 0,\n" - + " \"prefix_length\" : 0,\n" - + " \"max_expansions\" : 50,\n" - + " \"lenient\" : false,\n" - + " \"zero_terms_query\" : \"NONE\",\n" - + " \"auto_generate_synonyms_phrase_query\" : true,\n" - + " \"fuzzy_transpositions\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "multi_match" : { + "query" : "quick brown fox", + "fields" : [ "title^1.0", "title.original^1.0", "title.shingles^1.0" ], + "type" : "most_fields", + "operator" : "OR", + "slop" : 0, + "prefix_length" : 0, + "max_expansions" : 50, + "lenient" : false, + "zero_terms_query" : "NONE", + "auto_generate_synonyms_phrase_query" : true, + "fuzzy_transpositions" : false, + "boost" : 1.0 + } + }"""; MultiMatchQueryBuilder parsed = (MultiMatchQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -317,16 +319,15 @@ public void testFuzzinessNotAllowedTypes() throws IOException { Type.PHRASE.parseField().getPreferredName(), Type.PHRASE_PREFIX.parseField().getPreferredName() }; for (String type : notAllowedTypes) { - String json = "{\n" - + " \"multi_match\" : {\n" - + " \"query\" : \"quick brown fox\",\n" - + " \"fields\" : [ \"title^1.0\", \"title.original^1.0\", \"title.shingles^1.0\" ],\n" - + " \"type\" : \"" - + type - + "\",\n" - + " \"fuzziness\" : 1" - + " }\n" - + "}"; + String json = """ + { + "multi_match": { + "query": "quick brown fox", + "fields": [ "title^1.0", "title.original^1.0", "title.shingles^1.0" ], + "type": "%s", + "fuzziness": 1 + } + }""".formatted(type); ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("Fuzziness not allowed for type [" + type + "]", e.getMessage()); @@ -334,12 +335,12 @@ public void testFuzzinessNotAllowedTypes() throws IOException { } public void testQueryParameterArrayException() { - String json = "{\n" - + " \"multi_match\" : {\n" - + " \"query\" : [\"quick\", \"brown\", \"fox\"]\n" - + " \"fields\" : [ \"title^1.0\", \"title.original^1.0\", \"title.shingles^1.0\" ]" - + " }\n" - + "}"; + String json = """ + { + "multi_match" : { + "query" : ["quick", "brown", "fox"] + "fields" : [ "title^1.0", "title.original^1.0", "title.shingles^1.0" ] } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[multi_match] unknown token [START_ARRAY] after [query]", e.getMessage()); diff --git a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 622b7db857ac9..b9712286560c0 100644 --- a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -139,41 +139,42 @@ public void testValidate() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"nested\" : {\n" - + " \"query\" : {\n" - + " \"bool\" : {\n" - + " \"must\" : [ {\n" - + " \"match\" : {\n" - + " \"obj1.name\" : {\n" - + " \"query\" : \"blue\",\n" - + " \"operator\" : \"OR\",\n" - + " \"prefix_length\" : 0,\n" - + " \"max_expansions\" : 50,\n" - + " \"fuzzy_transpositions\" : true,\n" - + " \"lenient\" : false,\n" - + " \"zero_terms_query\" : \"NONE\",\n" - + " \"auto_generate_synonyms_phrase_query\" : true,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"range\" : {\n" - + " \"obj1.count\" : {\n" - + " \"gt\" : 5,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " },\n" - + " \"path\" : \"obj1\",\n" - + " \"ignore_unmapped\" : false,\n" - + " \"score_mode\" : \"avg\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "nested" : { + "query" : { + "bool" : { + "must" : [ { + "match" : { + "obj1.name" : { + "query" : "blue", + "operator" : "OR", + "prefix_length" : 0, + "max_expansions" : 50, + "fuzzy_transpositions" : true, + "lenient" : false, + "zero_terms_query" : "NONE", + "auto_generate_synonyms_phrase_query" : true, + "boost" : 1.0 + } + } + }, { + "range" : { + "obj1.count" : { + "gt" : 5, + "boost" : 1.0 + } + } + } ], + "boost" : 1.0 + } + }, + "path" : "obj1", + "ignore_unmapped" : false, + "score_mode" : "avg", + "boost" : 1.0 + } + }"""; NestedQueryBuilder parsed = (NestedQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java index 4e5d10a30092e..b38a0b59a0c61 100644 --- a/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java @@ -41,15 +41,12 @@ protected PrefixQueryBuilder doCreateTestQueryBuilder() { protected Map getAlternateVersions() { Map alternateVersions = new HashMap<>(); PrefixQueryBuilder prefixQuery = randomPrefixQuery(); - String contentString = "{\n" - + " \"prefix\" : {\n" - + " \"" - + prefixQuery.fieldName() - + "\" : \"" - + prefixQuery.value() - + "\"\n" - + " }\n" - + "}"; + String contentString = """ + { + "prefix" : { + "%s" : "%s" + } + }""".formatted(prefixQuery.fieldName(), prefixQuery.value()); alternateVersions.put(contentString, prefixQuery); return alternateVersions; } @@ -96,10 +93,17 @@ public void testBlendedRewriteMethod() throws IOException { } public void testFromJson() throws IOException { - String json = "{ \"prefix\" : { \"user\" : { \"value\" : \"ki\",\n" - + " \"case_insensitive\" : true,\n" - + " \"boost\" : 2.0" - + "} }}"; + String json = """ + { + "prefix": { + "user": { + "value": "ki", + "case_insensitive": true, + "boost": 2.0 + } + } + } + """; PrefixQueryBuilder parsed = (PrefixQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -120,20 +124,27 @@ public void testNumeric() throws Exception { } public void testParseFailsWithMultipleFields() throws IOException { - String json = "{\n" - + " \"prefix\": {\n" - + " \"user1\": {\n" - + " \"value\": \"ki\"\n" - + " },\n" - + " \"user2\": {\n" - + " \"value\": \"ki\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "prefix": { + "user1": { + "value": "ki" + }, + "user2": { + "value": "ki" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[prefix] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); - String shortJson = "{\n" + " \"prefix\": {\n" + " \"user1\": \"ki\",\n" + " \"user2\": \"ki\"\n" + " }\n" + "}"; + String shortJson = """ + { + "prefix": { + "user1": "ki", + "user2": "ki" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[prefix] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index 288bd6b2339d4..601aa7af12315 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -885,27 +885,25 @@ public void testLenientFlag() throws Exception { } public void testTimezone() throws Exception { - String queryAsString = "{\n" - + " \"query_string\":{\n" - + " \"time_zone\":\"Europe/Paris\",\n" - + " \"query\":\"" - + DATE_FIELD_NAME - + ":[2012 TO 2014]\"\n" - + " }\n" - + "}"; + String queryAsString = """ + { + "query_string":{ + "time_zone":"Europe/Paris", + "query":"%s:[2012 TO 2014]" + } + }""".formatted(DATE_FIELD_NAME); QueryBuilder queryBuilder = parseQuery(queryAsString); assertThat(queryBuilder, instanceOf(QueryStringQueryBuilder.class)); QueryStringQueryBuilder queryStringQueryBuilder = (QueryStringQueryBuilder) queryBuilder; assertThat(queryStringQueryBuilder.timeZone(), equalTo(ZoneId.of("Europe/Paris"))); - String invalidQueryAsString = "{\n" - + " \"query_string\":{\n" - + " \"time_zone\":\"This timezone does not exist\",\n" - + " \"query\":\"" - + DATE_FIELD_NAME - + ":[2012 TO 2014]\"\n" - + " }\n" - + "}"; + String invalidQueryAsString = """ + { + "query_string":{ + "time_zone":"This timezone does not exist", + "query":"%s:[2012 TO 2014]" + } + }""".formatted(DATE_FIELD_NAME); expectThrows(DateTimeException.class, () -> parseQuery(invalidQueryAsString)); } @@ -1086,26 +1084,27 @@ public void testExistsFieldQuery() throws Exception { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"query_string\" : {\n" - + " \"query\" : \"this AND that OR thus\",\n" - + " \"default_field\" : \"content\",\n" - + " \"fields\" : [ ],\n" - + " \"type\" : \"best_fields\",\n" - + " \"tie_breaker\" : 0.0,\n" - + " \"default_operator\" : \"or\",\n" - + " \"max_determinized_states\" : 10000,\n" - + " \"enable_position_increments\" : true,\n" - + " \"fuzziness\" : \"AUTO\",\n" - + " \"fuzzy_prefix_length\" : 0,\n" - + " \"fuzzy_max_expansions\" : 50,\n" - + " \"phrase_slop\" : 0,\n" - + " \"escape\" : false,\n" - + " \"auto_generate_synonyms_phrase_query\" : true,\n" - + " \"fuzzy_transpositions\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "query_string" : { + "query" : "this AND that OR thus", + "default_field" : "content", + "fields" : [ ], + "type" : "best_fields", + "tie_breaker" : 0.0, + "default_operator" : "or", + "max_determinized_states" : 10000, + "enable_position_increments" : true, + "fuzziness" : "AUTO", + "fuzzy_prefix_length" : 0, + "fuzzy_max_expansions" : 50, + "phrase_slop" : 0, + "escape" : false, + "auto_generate_synonyms_phrase_query" : true, + "fuzzy_transpositions" : false, + "boost" : 1.0 + } + }"""; QueryStringQueryBuilder parsed = (QueryStringQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index 7aaf368e19896..7451de234ef0c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -125,26 +125,23 @@ protected Map getAlternateVersions() { rangeQueryBuilder.to(randomIntBetween(101, 200)); } - String query = "{\n" - + " \"range\":{\n" - + " \"" - + INT_FIELD_NAME - + "\": {\n" - + " \"include_lower\":" - + rangeQueryBuilder.includeLower() - + ",\n" - + " \"include_upper\":" - + rangeQueryBuilder.includeUpper() - + ",\n" - + " \"from\":" - + rangeQueryBuilder.from() - + ",\n" - + " \"to\":" - + rangeQueryBuilder.to() - + "\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "range":{ + "%s": { + "include_lower":%s, + "include_upper":%s, + "from":%s, + "to":%s + } + } + }""".formatted( + INT_FIELD_NAME, + rangeQueryBuilder.includeLower(), + rangeQueryBuilder.includeUpper(), + rangeQueryBuilder.from(), + rangeQueryBuilder.to() + ); alternateVersions.put(query, rangeQueryBuilder); return alternateVersions; } @@ -278,17 +275,16 @@ public void testToQueryNumericField() throws IOException { public void testDateRangeQueryFormat() throws IOException { // We test 01/01/2012 from gte and 2030 for lt - String query = "{\n" - + " \"range\" : {\n" - + " \"" - + DATE_FIELD_NAME - + "\" : {\n" - + " \"gte\": \"01/01/2012\",\n" - + " \"lt\": \"2030\",\n" - + " \"format\": \"dd/MM/yyyy||yyyy\"\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "range" : { + "%s" : { + "gte": "01/01/2012", + "lt": "2030", + "format": "dd/MM/yyyy||yyyy" + } + } + }""".formatted(DATE_FIELD_NAME); Query parsedQuery = parseQuery(query).toQuery(createSearchExecutionContext()); assertThat(parsedQuery, instanceOf(IndexOrDocValuesQuery.class)); parsedQuery = ((IndexOrDocValuesQuery) parsedQuery).getIndexQuery(); @@ -304,31 +300,30 @@ public void testDateRangeQueryFormat() throws IOException { ); // Test Invalid format - final String invalidQuery = "{\n" - + " \"range\" : {\n" - + " \"" - + DATE_FIELD_NAME - + "\" : {\n" - + " \"gte\": \"01/01/2012\",\n" - + " \"lt\": \"2030\",\n" - + " \"format\": \"yyyy\"\n" - + " }\n" - + " }\n" - + "}"; + final String invalidQuery = """ + { + "range" : { + "%s" : { + "gte": "01/01/2012", + "lt": "2030", + "format": "yyyy" + } + } + }""".formatted(DATE_FIELD_NAME); expectThrows(ElasticsearchParseException.class, () -> parseQuery(invalidQuery).toQuery(createSearchExecutionContext())); } public void testDateRangeBoundaries() throws IOException { - String query = "{\n" - + " \"range\" : {\n" - + " \"" - + DATE_FIELD_NAME - + "\" : {\n" - + " \"gte\": \"2014-11-05||/M\",\n" - + " \"lte\": \"2014-12-08||/d\"\n" - + " }\n" - + " }\n" - + "}\n"; + String query = """ + { + "range" : { + "%s" : { + "gte": "2014-11-05||/M", + "lte": "2014-12-08||/d" + } + } + } + """.formatted(DATE_FIELD_NAME); Query parsedQuery = parseQuery(query).toQuery(createSearchExecutionContext()); assertThat(parsedQuery, instanceOf(IndexOrDocValuesQuery.class)); parsedQuery = ((IndexOrDocValuesQuery) parsedQuery).getIndexQuery(); @@ -342,16 +337,15 @@ public void testDateRangeBoundaries() throws IOException { parsedQuery ); - query = "{\n" - + " \"range\" : {\n" - + " \"" - + DATE_FIELD_NAME - + "\" : {\n" - + " \"gt\": \"2014-11-05||/M\",\n" - + " \"lt\": \"2014-12-08||/d\"\n" - + " }\n" - + " }\n" - + "}"; + query = """ + { + "range" : { + "%s" : { + "gt": "2014-11-05||/M", + "lt": "2014-12-08||/d" + } + } + }""".formatted(DATE_FIELD_NAME); parsedQuery = parseQuery(query).toQuery(createSearchExecutionContext()); assertThat(parsedQuery, instanceOf(IndexOrDocValuesQuery.class)); parsedQuery = ((IndexOrDocValuesQuery) parsedQuery).getIndexQuery(); @@ -367,17 +361,16 @@ public void testDateRangeBoundaries() throws IOException { } public void testDateRangeQueryTimezone() throws IOException { - String query = "{\n" - + " \"range\" : {\n" - + " \"" - + DATE_FIELD_NAME - + "\" : {\n" - + " \"gte\": \"2012-01-01\",\n" - + " \"lte\": \"now\",\n" - + " \"time_zone\": \"+01:00\"\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "range" : { + "%s" : { + "gte": "2012-01-01", + "lte": "now", + "time_zone": "+01:00" + } + } + }""".formatted(DATE_FIELD_NAME); SearchExecutionContext context = createSearchExecutionContext(); Query parsedQuery = parseQuery(query).toQuery(context); assertThat(parsedQuery, instanceOf(DateRangeIncludingNowQuery.class)); @@ -387,32 +380,32 @@ public void testDateRangeQueryTimezone() throws IOException { assertThat(parsedQuery, instanceOf(PointRangeQuery.class)); // TODO what else can we assert - query = "{\n" - + " \"range\" : {\n" - + " \"" - + INT_FIELD_NAME - + "\" : {\n" - + " \"gte\": \"0\",\n" - + " \"lte\": \"100\",\n" - + " \"time_zone\": \"-01:00\"\n" - + " }\n" - + " }\n" - + "}"; + query = """ + { + "range" : { + "%s" : { + "gte": "0", + "lte": "100", + "time_zone": "-01:00" + } + } + }""".formatted(INT_FIELD_NAME); QueryBuilder queryBuilder = parseQuery(query); queryBuilder.toQuery(createSearchExecutionContext()); // no exception } public void testFromJson() throws IOException { - String json = "{\n" - + " \"range\" : {\n" - + " \"timestamp\" : {\n" - + " \"gte\" : \"2015-01-01 00:00:00\",\n" - + " \"lte\" : \"now\",\n" - + " \"time_zone\" : \"+01:00\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "range" : { + "timestamp" : { + "gte" : "2015-01-01 00:00:00", + "lte" : "now", + "time_zone" : "+01:00", + "boost" : 1.0 + } + } + }"""; RangeQueryBuilder parsed = (RangeQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -422,16 +415,17 @@ public void testFromJson() throws IOException { } public void testNamedQueryParsing() throws IOException { - String json = "{\n" - + " \"range\" : {\n" - + " \"timestamp\" : {\n" - + " \"from\" : \"2015-01-01 00:00:00\",\n" - + " \"to\" : \"now\",\n" - + " \"boost\" : 1.0,\n" - + " \"_name\" : \"my_range\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "range" : { + "timestamp" : { + "from" : "2015-01-01 00:00:00", + "to" : "now", + "boost" : 1.0, + "_name" : "my_range" + } + } + }"""; assertNotNull(parseQuery(json)); } @@ -543,50 +537,51 @@ protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteC } public void testParseFailsWithMultipleFields() { - String json = "{\n" - + " \"range\": {\n" - + " \"age\": {\n" - + " \"gte\": 30,\n" - + " \"lte\": 40\n" - + " },\n" - + " \"price\": {\n" - + " \"gte\": 10,\n" - + " \"lte\": 30\n" - + " }\n" - + " }\n" - + " }"; + String json = """ + { + "range": { + "age": { + "gte": 30, + "lte": 40 + }, + "price": { + "gte": 10, + "lte": 30 + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[range] query doesn't support multiple fields, found [age] and [price]", e.getMessage()); } public void testParseFailsWithMultipleFieldsWhenOneIsDate() { - String json = "{\n" - + " \"range\": {\n" - + " \"age\": {\n" - + " \"gte\": 30,\n" - + " \"lte\": 40\n" - + " },\n" - + " \"" - + DATE_FIELD_NAME - + "\": {\n" - + " \"gte\": \"2016-09-13 05:01:14\"\n" - + " }\n" - + " }\n" - + " }"; + String json = """ + { + "range": { + "age": { + "gte": 30, + "lte": 40 + }, + "%s": { + "gte": "2016-09-13 05:01:14" + } + } + }""".formatted(DATE_FIELD_NAME); ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[range] query doesn't support multiple fields, found [age] and [" + DATE_FIELD_NAME + "]", e.getMessage()); } public void testParseRelation() { - String json = "{\n" - + " \"range\": {\n" - + " \"age\": {\n" - + " \"gte\": 30,\n" - + " \"lte\": 40,\n" - + " \"relation\": \"disjoint\"\n" - + " }" - + " }\n" - + " }"; + String json = """ + { + "range": { + "age": { + "gte": 30, + "lte": 40, + "relation": "disjoint" + } + } + }"""; String fieldName = randomAlphaOfLengthBetween(1, 20); IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, () -> parseQuery(json)); assertEquals("[range] query does not support relation [disjoint]", e1.getMessage()); diff --git a/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java index 2994f2f5804e1..8d1bc23091f9c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java @@ -51,15 +51,12 @@ protected RegexpQueryBuilder doCreateTestQueryBuilder() { protected Map getAlternateVersions() { Map alternateVersions = new HashMap<>(); RegexpQueryBuilder regexpQuery = randomRegexpQuery(); - String contentString = "{\n" - + " \"regexp\" : {\n" - + " \"" - + regexpQuery.fieldName() - + "\" : \"" - + regexpQuery.value() - + "\"\n" - + " }\n" - + "}"; + String contentString = """ + { + "regexp" : { + "%s" : "%s" + } + }""".formatted(regexpQuery.fieldName(), regexpQuery.value()); alternateVersions.put(contentString, regexpQuery); return alternateVersions; } @@ -91,17 +88,18 @@ public void testIllegalArguments() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"regexp\" : {\n" - + " \"name.first\" : {\n" - + " \"value\" : \"s.*y\",\n" - + " \"flags_value\" : 7,\n" - + " \"case_insensitive\" : true,\n" - + " \"max_determinized_states\" : 20000,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "regexp" : { + "name.first" : { + "value" : "s.*y", + "flags_value" : 7, + "case_insensitive" : true, + "max_determinized_states" : 20000, + "boost" : 1.0 + } + } + }"""; RegexpQueryBuilder parsed = (RegexpQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -121,20 +119,27 @@ public void testNumeric() throws Exception { } public void testParseFailsWithMultipleFields() throws IOException { - String json = "{\n" - + " \"regexp\": {\n" - + " \"user1\": {\n" - + " \"value\": \"k.*y\"\n" - + " },\n" - + " \"user2\": {\n" - + " \"value\": \"k.*y\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "regexp": { + "user1": { + "value": "k.*y" + }, + "user2": { + "value": "k.*y" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[regexp] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); - String shortJson = "{\n" + " \"regexp\": {\n" + " \"user1\": \"k.*y\",\n" + " \"user2\": \"k.*y\"\n" + " }\n" + "}"; + String shortJson = """ + { + "regexp": { + "user1": "k.*y", + "user2": "k.*y" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[regexp] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index 6633d015eee05..7a98d1f71a0fe 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -49,16 +49,17 @@ public void testIllegalConstructorArg() { } public void testFromJsonVerbose() throws IOException { - String json = "{\n" - + " \"script\" : {\n" - + " \"script\" : {\n" - + " \"source\" : \"5\",\n" - + " \"lang\" : \"mockscript\"\n" - + " },\n" - + " \"boost\" : 1.0,\n" - + " \"_name\" : \"PcKdEyPOmR\"\n" - + " }\n" - + "}"; + String json = """ + { + "script" : { + "script" : { + "source" : "5", + "lang" : "mockscript" + }, + "boost" : 1.0, + "_name" : "PcKdEyPOmR" + } + }"""; ScriptQueryBuilder parsed = (ScriptQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -67,31 +68,32 @@ public void testFromJsonVerbose() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"script\" : {\n" - + " \"script\" : \"5\"," - + " \"boost\" : 1.0,\n" - + " \"_name\" : \"PcKdEyPOmR\"\n" - + " }\n" - + "}"; + String json = """ + { + "script" : { + "script" : "5", "boost" : 1.0, + "_name" : "PcKdEyPOmR" + } + }"""; ScriptQueryBuilder parsed = (ScriptQueryBuilder) parseQuery(json); assertEquals(json, "5", parsed.script().getIdOrCode()); } public void testArrayOfScriptsException() { - String json = "{\n" - + " \"script\" : {\n" - + " \"script\" : [ {\n" - + " \"source\" : \"5\",\n" - + " \"lang\" : \"mockscript\"\n" - + " },\n" - + " {\n" - + " \"source\" : \"6\",\n" - + " \"lang\" : \"mockscript\"\n" - + " }\n ]" - + " }\n" - + "}"; + String json = """ + { + "script" : { + "script" : [ { + "source" : "5", + "lang" : "mockscript" + }, + { + "source" : "6", + "lang" : "mockscript" + } + ] } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(e.getMessage(), containsString("does not support an array of scripts")); diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java index 5b60cb157bbdd..50bba3fbcf226 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java @@ -59,15 +59,16 @@ protected void doAssertLuceneQuery(ScriptScoreQueryBuilder queryBuilder, Query q } public void testFromJson() throws IOException { - String json = "{\n" - + " \"script_score\" : {\n" - + " \"query\" : { \"match_all\" : {} },\n" - + " \"script\" : {\n" - + " \"source\" : \"doc['field'].value\" \n" - + " },\n" - + " \"min_score\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "script_score" : { + "query" : { "match_all" : {} }, + "script" : { + "source" : "doc['field'].value" + }, + "min_score" : 2.0 + } + }"""; ScriptScoreQueryBuilder parsed = (ScriptScoreQueryBuilder) parseQuery(json); assertEquals(json, 2, parsed.getMinScore(), 0.0001); diff --git a/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java index a99ede620a888..a2cac642aa7f5 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java @@ -250,7 +250,12 @@ public void testFieldsCannotBeSetToNull() { public void testDefaultFieldParsing() throws IOException { String query = randomAlphaOfLengthBetween(1, 10).toLowerCase(Locale.ROOT); - String contentString = "{\n" + " \"simple_query_string\" : {\n" + " \"query\" : \"" + query + "\"" + " }\n" + "}"; + String contentString = """ + { + "simple_query_string": { + "query": "%s" + } + }""".formatted(query); SimpleQueryStringBuilder queryBuilder = (SimpleQueryStringBuilder) parseQuery(contentString); assertThat(queryBuilder.value(), equalTo(query)); assertThat(queryBuilder.fields(), notNullValue()); @@ -359,23 +364,24 @@ public void testNegativeFlags() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"simple_query_string\" : {\n" - + " \"query\" : \"\\\"fried eggs\\\" +(eggplant | potato) -frittata\",\n" - + " \"fields\" : [ \"body^5.0\" ],\n" - + " \"analyzer\" : \"snowball\",\n" - + " \"flags\" : -1,\n" - + " \"default_operator\" : \"and\",\n" - + " \"lenient\" : false,\n" - + " \"analyze_wildcard\" : false,\n" - + " \"quote_field_suffix\" : \".quote\",\n" - + " \"auto_generate_synonyms_phrase_query\" : true,\n" - + " \"fuzzy_prefix_length\" : 1,\n" - + " \"fuzzy_max_expansions\" : 5,\n" - + " \"fuzzy_transpositions\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "simple_query_string" : { + "query" : "\\"fried eggs\\" +(eggplant | potato) -frittata", + "fields" : [ "body^5.0" ], + "analyzer" : "snowball", + "flags" : -1, + "default_operator" : "and", + "lenient" : false, + "analyze_wildcard" : false, + "quote_field_suffix" : ".quote", + "auto_generate_synonyms_phrase_query" : true, + "fuzzy_prefix_length" : 1, + "fuzzy_max_expansions" : 5, + "fuzzy_transpositions" : false, + "boost" : 1.0 + } + }"""; SimpleQueryStringBuilder parsed = (SimpleQueryStringBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanContainingQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanContainingQueryBuilderTests.java index 67e2ebd95f939..6378d1d2b2d6c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanContainingQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanContainingQueryBuilderTests.java @@ -37,41 +37,42 @@ public void testIllegalArguments() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_containing\" : {\n" - + " \"big\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"bar\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"baz\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 5,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " },\n" - + " \"little\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"foo\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_containing" : { + "big" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "bar", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "baz", + "boost" : 1.0 + } + } + } ], + "slop" : 5, + "in_order" : true, + "boost" : 1.0 + } + }, + "little" : { + "span_term" : { + "field1" : { + "value" : "foo", + "boost" : 1.0 + } + } + }, + "boost" : 2.0 + } + }"""; SpanContainingQueryBuilder parsed = (SpanContainingQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -82,41 +83,42 @@ public void testFromJson() throws IOException { } public void testFromJsoWithNonDefaultBoostInBigQuery() { - String json = "{\n" - + " \"span_containing\" : {\n" - + " \"big\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"bar\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"baz\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 5,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " },\n" - + " \"little\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"foo\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_containing" : { + "big" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "bar", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "baz", + "boost" : 1.0 + } + } + } ], + "slop" : 5, + "in_order" : true, + "boost" : 2.0 + } + }, + "little" : { + "span_term" : { + "field1" : { + "value" : "foo", + "boost" : 1.0 + } + } + }, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat( @@ -126,41 +128,42 @@ public void testFromJsoWithNonDefaultBoostInBigQuery() { } public void testFromJsonWithNonDefaultBoostInLittleQuery() { - String json = "{\n" - + " \"span_containing\" : {\n" - + " \"little\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"bar\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"baz\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 5,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " },\n" - + " \"big\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"foo\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_containing" : { + "little" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "bar", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "baz", + "boost" : 1.0 + } + } + } ], + "slop" : 5, + "in_order" : true, + "boost" : 2.0 + } + }, + "big" : { + "span_term" : { + "field1" : { + "value" : "foo", + "boost" : 1.0 + } + } + }, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat( diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanFirstQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanFirstQueryBuilderTests.java index fca9a61732f14..11fc2835f1782 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanFirstQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanFirstQueryBuilderTests.java @@ -64,20 +64,21 @@ public void testParseEnd() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_first\" : {\n" - + " \"match\" : {\n" - + " \"span_term\" : {\n" - + " \"user\" : {\n" - + " \"value\" : \"kimchy\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"end\" : 3,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_first" : { + "match" : { + "span_term" : { + "user" : { + "value" : "kimchy", + "boost" : 1.0 + } + } + }, + "end" : 3, + "boost" : 1.0 + } + }"""; SpanFirstQueryBuilder parsed = (SpanFirstQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -87,20 +88,21 @@ public void testFromJson() throws IOException { } public void testFromJsonWithNonDefaultBoostInMatchQuery() { - String json = "{\n" - + " \"span_first\" : {\n" - + " \"match\" : {\n" - + " \"span_term\" : {\n" - + " \"user\" : {\n" - + " \"value\" : \"kimchy\",\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"end\" : 3,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_first" : { + "match" : { + "span_term" : { + "user" : { + "value" : "kimchy", + "boost" : 2.0 + } + } + }, + "end" : 3, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(exception.getMessage(), equalTo("span_first [match] as a nested span clause can't have non-default boost value [2.0]")); diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanGapQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanGapQueryBuilderTests.java index f72a0057f6281..647783549b078 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanGapQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanGapQueryBuilderTests.java @@ -74,32 +74,32 @@ public void testIllegalArguments() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_gap\" : {\n" - + " \"field\" : 2" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value3\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 12,\n" - + " \"in_order\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field" : { + "value" : "value1", + "boost" : 1.0 + } + } + }, { + "span_gap" : { + "field" : 2 } + }, { + "span_term" : { + "field" : { + "value" : "value3", + "boost" : 1.0 + } + } + } ], + "slop" : 12, + "in_order" : false, + "boost" : 1.0 + } + }"""; SpanNearQueryBuilder parsed = (SpanNearQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java index 181b9c7dd3dc8..6f5ba88b02a32 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanMultiTermQueryBuilderTests.java @@ -220,19 +220,20 @@ public void testToQueryInnerTermQuery() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_multi\" : {\n" - + " \"match\" : {\n" - + " \"prefix\" : {\n" - + " \"user\" : {\n" - + " \"value\" : \"ki\",\n" - + " \"boost\" : 1.08\n" - + " }\n" - + " }\n" - + " },\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_multi" : { + "match" : { + "prefix" : { + "user" : { + "value" : "ki", + "boost" : 1.08 + } + } + }, + "boost" : 1.0 + } + }"""; SpanMultiTermQueryBuilder parsed = (SpanMultiTermQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanNearQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanNearQueryBuilderTests.java index 93cfb4d3f17aa..4a9ee980decb7 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanNearQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanNearQueryBuilderTests.java @@ -77,35 +77,36 @@ public void testClausesUnmodifiable() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value2\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value3\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 12,\n" - + " \"in_order\" : false,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field" : { + "value" : "value1", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value2", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value3", + "boost" : 1.0 + } + } + } ], + "slop" : 12, + "in_order" : false, + "boost" : 2.0 + } + }"""; SpanNearQueryBuilder parsed = (SpanNearQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -117,18 +118,19 @@ public void testFromJson() throws IOException { } public void testParsingSlopDefault() throws IOException { - String json = "{\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }]\n" - + " }\n" - + "}"; + String json = """ + { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field" : { + "value" : "value1", + "boost" : 1.0 + } + } + }] + } + }"""; SpanNearQueryBuilder parsed = (SpanNearQueryBuilder) parseQuery(json); assertEquals(json, 1, parsed.clauses().size()); @@ -138,71 +140,73 @@ public void testParsingSlopDefault() throws IOException { } public void testCollectPayloadsNoLongerSupported() throws Exception { - String json = "{\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value2\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value3\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 12,\n" - + " \"in_order\" : false,\n" - + " \"collect_payloads\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field" : { + "value" : "value1", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value2", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value3", + "boost" : 1.0 + } + } + } ], + "slop" : 12, + "in_order" : false, + "collect_payloads" : false, + "boost" : 1.0 + } + }"""; final ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(e.getMessage(), containsString("[span_near] query does not support [collect_payloads]")); } public void testFromJsonWithNonDefaultBoostInInnerQuery() { - String json = "{\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value2\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value3\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 12,\n" - + " \"in_order\" : false,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field" : { + "value" : "value1", + "boost" : 2.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value2", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value3", + "boost" : 1.0 + } + } + } ], + "slop" : 12, + "in_order" : false, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(exception.getMessage(), equalTo("span_near [clauses] as a nested span clause can't have non-default boost value [2.0]")); diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanNotQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanNotQueryBuilderTests.java index 76ef40f0a3c66..a1426d3783fe3 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanNotQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanNotQueryBuilderTests.java @@ -162,43 +162,44 @@ public void testParserExceptions() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_not\" : {\n" - + " \"include\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"hoya\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"exclude\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"la\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"hoya\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 0,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " },\n" - + " \"pre\" : 0,\n" - + " \"post\" : 0,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_not" : { + "include" : { + "span_term" : { + "field1" : { + "value" : "hoya", + "boost" : 1.0 + } + } + }, + "exclude" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "la", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "hoya", + "boost" : 1.0 + } + } + } ], + "slop" : 0, + "in_order" : true, + "boost" : 1.0 + } + }, + "pre" : 0, + "post" : 0, + "boost" : 2.0 + } + }"""; SpanNotQueryBuilder parsed = (SpanNotQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -209,86 +210,88 @@ public void testFromJson() throws IOException { } public void testFromJsonWithNonDefaultBoostInIncludeQuery() { - String json = "{\n" - + " \"span_not\" : {\n" - + " \"exclude\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"hoya\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"include\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"la\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"hoya\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 0,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " },\n" - + " \"pre\" : 0,\n" - + " \"post\" : 0,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_not" : { + "exclude" : { + "span_term" : { + "field1" : { + "value" : "hoya", + "boost" : 1.0 + } + } + }, + "include" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "la", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "hoya", + "boost" : 1.0 + } + } + } ], + "slop" : 0, + "in_order" : true, + "boost" : 2.0 + } + }, + "pre" : 0, + "post" : 0, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(exception.getMessage(), equalTo("span_not [include] as a nested span clause can't have non-default boost value [2.0]")); } public void testFromJsonWithNonDefaultBoostInExcludeQuery() { - String json = "{\n" - + " \"span_not\" : {\n" - + " \"include\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"hoya\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"exclude\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"la\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"hoya\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 0,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " },\n" - + " \"pre\" : 0,\n" - + " \"post\" : 0,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_not" : { + "include" : { + "span_term" : { + "field1" : { + "value" : "hoya", + "boost" : 1.0 + } + } + }, + "exclude" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "la", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "hoya", + "boost" : 1.0 + } + } + } ], + "slop" : 0, + "in_order" : true, + "boost" : 2.0 + } + }, + "pre" : 0, + "post" : 0, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(exception.getMessage(), equalTo("span_not [exclude] as a nested span clause can't have non-default boost value [2.0]")); diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java index 74a95829b39ac..6390dde48cdad 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanOrQueryBuilderTests.java @@ -60,33 +60,34 @@ public void testClausesUnmodifiable() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_or\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value2\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value3\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_or" : { + "clauses" : [ { + "span_term" : { + "field" : { + "value" : "value1", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value2", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field" : { + "value" : "value3", + "boost" : 1.0 + } + } + } ], + "boost" : 2.0 + } + }"""; SpanOrQueryBuilder parsed = (SpanOrQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -96,19 +97,20 @@ public void testFromJson() throws IOException { } public void testFromJsonWithNonDefaultBoostInInnerQuery() { - String json = "{\n" - + " \"span_or\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field\" : {\n" - + " \"value\" : \"value1\",\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_or" : { + "clauses" : [ { + "span_term" : { + "field" : { + "value" : "value1", + "boost" : 2.0 + } + } + } ], + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(exception.getMessage(), equalTo("span_or [clauses] as a nested span clause can't have non-default boost value [2.0]")); diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java index 7f84537ab15b7..cab43d2800fe9 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanTermQueryBuilderTests.java @@ -90,25 +90,27 @@ public void testFromJson() throws IOException { } public void testParseFailsWithMultipleFields() throws IOException { - String json = "{\n" - + " \"span_term\" : {\n" - + " \"message1\" : {\n" - + " \"term\" : \"this\"\n" - + " },\n" - + " \"message2\" : {\n" - + " \"term\" : \"this\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "span_term" : { + "message1" : { + "term" : "this" + }, + "message2" : { + "term" : "this" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[span_term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); - String shortJson = "{\n" - + " \"span_term\" : {\n" - + " \"message1\" : \"this\",\n" - + " \"message2\" : \"this\"\n" - + " }\n" - + "}"; + String shortJson = """ + { + "span_term" : { + "message1" : "this", + "message2" : "this" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[span_term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/SpanWithinQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SpanWithinQueryBuilderTests.java index f8aaf8dac47ad..b3f08b5436914 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SpanWithinQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SpanWithinQueryBuilderTests.java @@ -37,41 +37,42 @@ public void testIllegalArguments() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"span_within\" : {\n" - + " \"big\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"bar\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"baz\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 5,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " },\n" - + " \"little\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"foo\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"boost\" : 2.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_within" : { + "big" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "bar", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "baz", + "boost" : 1.0 + } + } + } ], + "slop" : 5, + "in_order" : true, + "boost" : 1.0 + } + }, + "little" : { + "span_term" : { + "field1" : { + "value" : "foo", + "boost" : 1.0 + } + } + }, + "boost" : 2.0 + } + }"""; SpanWithinQueryBuilder parsed = (SpanWithinQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -82,82 +83,84 @@ public void testFromJson() throws IOException { } public void testFromJsonWithNonDefaultBoostInBigQuery() { - String json = "{\n" - + " \"span_within\" : {\n" - + " \"big\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"bar\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"baz\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 5,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " },\n" - + " \"little\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"foo\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_within" : { + "big" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "bar", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "baz", + "boost" : 1.0 + } + } + } ], + "slop" : 5, + "in_order" : true, + "boost" : 2.0 + } + }, + "little" : { + "span_term" : { + "field1" : { + "value" : "foo", + "boost" : 1.0 + } + } + }, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat(exception.getMessage(), equalTo("span_within [big] as a nested span clause can't have non-default boost value [2.0]")); } public void testFromJsonWithNonDefaultBoostInLittleQuery() { - String json = "{\n" - + " \"span_within\" : {\n" - + " \"little\" : {\n" - + " \"span_near\" : {\n" - + " \"clauses\" : [ {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"bar\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " }, {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"baz\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " } ],\n" - + " \"slop\" : 5,\n" - + " \"in_order\" : true,\n" - + " \"boost\" : 2.0\n" - + " }\n" - + " },\n" - + " \"big\" : {\n" - + " \"span_term\" : {\n" - + " \"field1\" : {\n" - + " \"value\" : \"foo\",\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + " },\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "span_within" : { + "little" : { + "span_near" : { + "clauses" : [ { + "span_term" : { + "field1" : { + "value" : "bar", + "boost" : 1.0 + } + } + }, { + "span_term" : { + "field1" : { + "value" : "baz", + "boost" : 1.0 + } + } + } ], + "slop" : 5, + "in_order" : true, + "boost" : 2.0 + } + }, + "big" : { + "span_term" : { + "field1" : { + "value" : "foo", + "boost" : 1.0 + } + } + }, + "boost" : 1.0 + } + }"""; Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json)); assertThat( diff --git a/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java index a80eb7811eaf8..ff969fb481bb5 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java @@ -113,21 +113,27 @@ private Query termQuery(MappedFieldType mapper, Object value, boolean caseInsens } public void testTermArray() throws IOException { - String queryAsString = "{\n" + " \"term\": {\n" + " \"age\": [34, 35]\n" + " }\n" + "}"; + String queryAsString = """ + { + "term": { + "age": [34, 35] + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(queryAsString)); assertEquals("[term] query does not support array of values", e.getMessage()); } public void testFromJson() throws IOException { - String json = "{\n" - + " \"term\" : {\n" - + " \"exact_value\" : {\n" - + " \"value\" : \"Quick Foxes!\",\n" - + " \"case_insensitive\" : true,\n" - + " \"boost\" : 1.0\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "term" : { + "exact_value" : { + "value" : "Quick Foxes!", + "case_insensitive" : true, + "boost" : 1.0 + } + } + }"""; TermQueryBuilder parsed = (TermQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -145,32 +151,40 @@ public void testGeo() throws Exception { } public void testParseFailsWithMultipleFields() { - String json = "{\n" - + " \"term\" : {\n" - + " \"message1\" : {\n" - + " \"value\" : \"this\"\n" - + " },\n" - + " \"message2\" : {\n" - + " \"value\" : \"this\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "term" : { + "message1" : { + "value" : "this" + }, + "message2" : { + "value" : "this" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); - String shortJson = "{\n" + " \"term\" : {\n" + " \"message1\" : \"this\",\n" + " \"message2\" : \"this\"\n" + " }\n" + "}"; + String shortJson = """ + { + "term" : { + "message1" : "this", + "message2" : "this" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage()); } public void testParseAndSerializeBigInteger() throws IOException { - String json = "{\n" - + " \"term\" : {\n" - + " \"foo\" : {\n" - + " \"value\" : 80315953321748200608\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "term" : { + "foo" : { + "value" : 80315953321748200608 + } + } + }"""; QueryBuilder parsedQuery = parseQuery(json); assertSerialization(parsedQuery); } diff --git a/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java index 1102ed89713e3..4ba2c9d8e8cf3 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java @@ -170,20 +170,21 @@ public void testNullValues() { } public void testBothValuesAndLookupSet() throws IOException { - String query = "{\n" - + " \"terms\": {\n" - + " \"field\": [\n" - + " \"blue\",\n" - + " \"pill\"\n" - + " ],\n" - + " \"field_lookup\": {\n" - + " \"index\": \"pills\",\n" - + " \"type\": \"red\",\n" - + " \"id\": \"3\",\n" - + " \"path\": \"white rabbit\"\n" - + " }\n" - + " }\n" - + "}"; + String query = """ + { + "terms": { + "field": [ + "blue", + "pill" + ], + "field_lookup": { + "index": "pills", + "type": "red", + "id": "3", + "path": "white rabbit" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(query)); assertThat(e.getMessage(), containsString("[" + TermsQueryBuilder.NAME + "] query does not support more than one field.")); @@ -240,12 +241,13 @@ public void testTermsQueryWithMultipleFields() throws IOException { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"terms\" : {\n" - + " \"user\" : [ \"kimchy\", \"elasticsearch\" ],\n" - + " \"boost\" : 1.0\n" - + " }\n" - + "}"; + String json = """ + { + "terms" : { + "user" : [ "kimchy", "elasticsearch" ], + "boost" : 1.0 + } + }"""; TermsQueryBuilder parsed = (TermsQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); diff --git a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java index 157e3e294ed21..9554b2a7101f9 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java @@ -38,15 +38,12 @@ protected WildcardQueryBuilder doCreateTestQueryBuilder() { protected Map getAlternateVersions() { Map alternateVersions = new HashMap<>(); WildcardQueryBuilder wildcardQuery = randomWildcardQuery(); - String contentString = "{\n" - + " \"wildcard\" : {\n" - + " \"" - + wildcardQuery.fieldName() - + "\" : \"" - + wildcardQuery.value() - + "\"\n" - + " }\n" - + "}"; + String contentString = """ + { + "wildcard" : { + "%s" : "%s" + } + }""".formatted(wildcardQuery.fieldName(), wildcardQuery.value()); alternateVersions.put(contentString, wildcardQuery); return alternateVersions; } @@ -99,10 +96,16 @@ public void testEmptyValue() throws IOException { } public void testFromJson() throws IOException { - String json = "{ \"wildcard\" : { \"user\" : { \"wildcard\" : \"ki*y\"," - + " \"case_insensitive\" : true,\n" - + " \"boost\" : 2.0" - + " } }}"; + String json = """ + { + "wildcard": { + "user": { + "wildcard": "ki*y", + "case_insensitive": true, + "boost": 2.0 + } + } + }"""; WildcardQueryBuilder parsed = (WildcardQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, "ki*y", parsed.value()); @@ -110,25 +113,27 @@ public void testFromJson() throws IOException { } public void testParseFailsWithMultipleFields() throws IOException { - String json = "{\n" - + " \"wildcard\": {\n" - + " \"user1\": {\n" - + " \"wildcard\": \"ki*y\"\n" - + " },\n" - + " \"user2\": {\n" - + " \"wildcard\": \"ki*y\"\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "wildcard": { + "user1": { + "wildcard": "ki*y" + }, + "user2": { + "wildcard": "ki*y" + } + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); assertEquals("[wildcard] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); - String shortJson = "{\n" - + " \"wildcard\": {\n" - + " \"user1\": \"ki*y\",\n" - + " \"user2\": \"ki*y\"\n" - + " }\n" - + "}"; + String shortJson = """ + { + "wildcard": { + "user1": "ki*y", + "user2": "ki*y" + } + }"""; e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); assertEquals("[wildcard] query doesn't support multiple fields, found [user1] and [user2]", e.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java index be7a8e9814891..f8440b01cc351 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java @@ -93,7 +93,12 @@ public void testUnknownField() { } public void testFromJson() throws IOException { - String json = "{\n" + " \"wrapper\" : {\n" + " \"query\" : \"e30=\"\n" + " }\n" + "}"; + String json = """ + { + "wrapper" : { + "query" : "e30=" + } + }"""; WrapperQueryBuilder parsed = (WrapperQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); @@ -119,10 +124,12 @@ public void testMustRewrite() throws IOException { } public void testRewriteWithInnerName() throws IOException { - QueryBuilder builder = new WrapperQueryBuilder("{ \"match_all\" : {\"_name\" : \"foobar\"}}"); + QueryBuilder builder = new WrapperQueryBuilder(""" + { "match_all" : {"_name" : "foobar"}}"""); SearchExecutionContext searchExecutionContext = createSearchExecutionContext(); assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(searchExecutionContext)); - builder = new WrapperQueryBuilder("{ \"match_all\" : {\"_name\" : \"foobar\"}}").queryName("outer"); + builder = new WrapperQueryBuilder(""" + { "match_all" : {"_name" : "foobar"}}""").queryName("outer"); assertEquals( new BoolQueryBuilder().must(new MatchAllQueryBuilder().queryName("foobar")).queryName("outer"), builder.rewrite(searchExecutionContext) diff --git a/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index b0d60ac6537b1..16ade358d5773 100644 --- a/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -298,48 +298,49 @@ public void testIllegalArguments() { } public void testParseFunctionsArray() throws IOException { - String functionScoreQuery = "{\n" - + " \"function_score\":{\n" - + " \"query\":{\n" - + " \"term\":{\n" - + " \"field1\":\"value1\"\n" - + " }\n" - + " },\n" - + " \"functions\": [\n" - + " {\n" - + " \"random_score\": {\n" - + " \"seed\":123456\n" - + " },\n" - + " \"weight\": 3,\n" - + " \"filter\": {\n" - + " \"term\":{\n" - + " \"field2\":\"value2\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"filter\": {\n" - + " \"term\":{\n" - + " \"field3\":\"value3\"\n" - + " }\n" - + " },\n" - + " \"weight\": 9\n" - + " },\n" - + " {\n" - + " \"gauss\": {\n" - + " \"field_name\": {\n" - + " \"origin\":0.5,\n" - + " \"scale\":0.6\n" - + " }\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"boost\" : 3,\n" - + " \"score_mode\" : \"avg\",\n" - + " \"boost_mode\" : \"replace\",\n" - + " \"max_boost\" : 10\n" - + " }\n" - + "}"; + String functionScoreQuery = """ + { + "function_score":{ + "query":{ + "term":{ + "field1":"value1" + } + }, + "functions": [ + { + "random_score": { + "seed":123456 + }, + "weight": 3, + "filter": { + "term":{ + "field2":"value2" + } + } + }, + { + "filter": { + "term":{ + "field3":"value3" + } + }, + "weight": 9 + }, + { + "gauss": { + "field_name": { + "origin":0.5, + "scale":0.6 + } + } + } + ], + "boost" : 3, + "score_mode" : "avg", + "boost_mode" : "replace", + "max_boost" : 10 + } + }"""; QueryBuilder queryBuilder = parseQuery(functionScoreQuery); /* @@ -393,25 +394,26 @@ public void testParseFunctionsArray() throws IOException { } public void testParseSingleFunction() throws IOException { - String functionScoreQuery = "{\n" - + " \"function_score\":{\n" - + " \"query\":{\n" - + " \"term\":{\n" - + " \"field1\":\"value1\"\n" - + " }\n" - + " },\n" - + " \"gauss\": {\n" - + " \"field_name\": {\n" - + " \"origin\":0.5,\n" - + " \"scale\":0.6\n" - + " }\n" - + " },\n" - + " \"boost\" : 3,\n" - + " \"score_mode\" : \"avg\",\n" - + " \"boost_mode\" : \"replace\",\n" - + " \"max_boost\" : 10\n" - + " }\n" - + "}"; + String functionScoreQuery = """ + { + "function_score":{ + "query":{ + "term":{ + "field1":"value1" + } + }, + "gauss": { + "field_name": { + "origin":0.5, + "scale":0.6 + } + }, + "boost" : 3, + "score_mode" : "avg", + "boost_mode" : "replace", + "max_boost" : 10 + } + }"""; QueryBuilder queryBuilder = parseQuery(functionScoreQuery); /* @@ -448,39 +450,41 @@ public void testParseSingleFunction() throws IOException { public void testProperErrorMessageWhenTwoFunctionsDefinedInQueryBody() throws IOException { // without a functions array, we support only a single function, weight can't be associated with the function either. - String functionScoreQuery = "{\n" - + " \"function_score\": {\n" - + " \"script_score\": {\n" - + " \"script\": \"5\"\n" - + " },\n" - + " \"weight\": 2\n" - + " }\n" - + "}"; + String functionScoreQuery = """ + { + "function_score": { + "script_score": { + "script": "5" + }, + "weight": 2 + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(functionScoreQuery)); assertThat(e.getMessage(), containsString("use [functions] array if you want to define several functions.")); } public void testProperErrorMessageWhenTwoFunctionsDefinedInFunctionsArray() throws IOException { - String functionScoreQuery = "{\n" - + " \"function_score\":{\n" - + " \"functions\": [\n" - + " {\n" - + " \"random_score\": {\n" - + " \"seed\":123456\n" - + " },\n" - + " \"weight\": 3,\n" - + " \"script_score\": {\n" - + " \"script\": \"_index['text']['foo'].tf()\"\n" - + " },\n" - + " \"filter\": {\n" - + " \"term\":{\n" - + " \"field2\":\"value2\"\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}"; + String functionScoreQuery = """ + { + "function_score":{ + "functions": [ + { + "random_score": { + "seed":123456 + }, + "weight": 3, + "script_score": { + "script": "_index['text']['foo'].tf()" + }, + "filter": { + "term":{ + "field2":"value2" + } + } + } + ] + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(functionScoreQuery)); assertThat( e.getMessage(), @@ -489,19 +493,20 @@ public void testProperErrorMessageWhenTwoFunctionsDefinedInFunctionsArray() thro } public void testProperErrorMessageWhenMissingFunction() throws IOException { - String functionScoreQuery = "{\n" - + " \"function_score\":{\n" - + " \"functions\": [\n" - + " {\n" - + " \"filter\": {\n" - + " \"term\":{\n" - + " \"field2\":\"value2\"\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}"; + String functionScoreQuery = """ + { + "function_score":{ + "functions": [ + { + "filter": { + "term":{ + "field2":"value2" + } + } + } + ] + } + }"""; ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(functionScoreQuery)); assertThat(e.getMessage(), containsString("an entry in functions list is missing a function.")); } @@ -585,20 +590,21 @@ public void testProperErrorMessagesForMisplacedWeightsAndFunctions() throws IOEx } public void testMalformedThrowsException() throws IOException { - String json = "{\n" - + " \"function_score\":{\n" - + " \"query\":{\n" - + " \"term\":{\n" - + " \"name.last\":\"banon\"\n" - + " }\n" - + " },\n" - + " \"functions\": [\n" - + " {\n" - + " {\n" - + " }\n" - + " ]\n" - + " }\n" - + "}"; + String json = """ + { + "function_score":{ + "query":{ + "term":{ + "name.last":"banon" + } + }, + "functions": [ + { + { + } + ] + } + }"""; JsonParseException e = expectThrows(JsonParseException.class, () -> parseQuery(json)); assertThat(e.getMessage(), containsString("Unexpected character ('{")); } @@ -624,64 +630,69 @@ public void testCustomWeightFactorQueryBuilderWithFunctionScoreWithoutQueryGiven public void testFieldValueFactorFactorArray() throws IOException { // don't permit an array of factors - String querySource = "{" - + " \"function_score\": {" - + " \"query\": {" - + " \"match\": {\"name\": \"foo\"}" - + " }," - + " \"functions\": [" - + " {" - + " \"field_value_factor\": {" - + " \"field\": \"test\"," - + " \"factor\": [1.2,2]" - + " }" - + " }" - + " ]" - + " }" - + "}"; + String querySource = """ + { + "function_score": { + "query": { + "match": { + "name": "foo" + } + }, + "functions": [ + { + "field_value_factor": { + "field": "test", + "factor": [ 1.2, 2 ] + } + } + ] + } + }"""; expectParsingException(querySource, containsString("[field_value_factor] field 'factor' does not support lists or objects")); } public void testFromJson() throws IOException { - String json = "{\n" - + " \"function_score\" : {\n" - + " \"query\" : { \"match_all\" : {} },\n" - + " \"functions\" : [ {\n" - + " \"filter\" : { \"match_all\" : {}},\n" - + " \"weight\" : 23.0,\n" - + " \"random_score\" : { }\n" - + " }, {\n" - + " \"filter\" : { \"match_all\" : {}},\n" - + " \"weight\" : 5.0\n" - + " } ],\n" - + " \"score_mode\" : \"multiply\",\n" - + " \"boost_mode\" : \"multiply\",\n" - + " \"max_boost\" : 100.0,\n" - + " \"min_score\" : 1.0,\n" - + " \"boost\" : 42.0\n" - + " }\n" - + "}"; + String json = """ + { + "function_score" : { + "query" : { "match_all" : {} }, + "functions" : [ { + "filter" : { "match_all" : {}}, + "weight" : 23.0, + "random_score" : { } + }, { + "filter" : { "match_all" : {}}, + "weight" : 5.0 + } ], + "score_mode" : "multiply", + "boost_mode" : "multiply", + "max_boost" : 100.0, + "min_score" : 1.0, + "boost" : 42.0 + } + }"""; FunctionScoreQueryBuilder parsed = (FunctionScoreQueryBuilder) parseQuery(json); // this should be equivalent to the same with a match_all query - String expected = "{\n" - + " \"function_score\" : {\n" - + " \"query\" : { \"match_all\" : {} },\n" - + " \"functions\" : [ {\n" - + " \"filter\" : { \"match_all\" : {}},\n" - + " \"weight\" : 23.0,\n" - + " \"random_score\" : { }\n" - + " }, {\n" - + " \"filter\" : { \"match_all\" : {}},\n" - + " \"weight\" : 5.0\n" - + " } ],\n" - + " \"score_mode\" : \"multiply\",\n" - + " \"boost_mode\" : \"multiply\",\n" - + " \"max_boost\" : 100.0,\n" - + " \"min_score\" : 1.0,\n" - + " \"boost\" : 42.0\n" - + " }\n" - + "}"; + String expected = """ + { + "function_score" : { + "query" : { "match_all" : {} }, + "functions" : [ { + "filter" : { "match_all" : {}}, + "weight" : 23.0, + "random_score" : { } + }, { + "filter" : { "match_all" : {}}, + "weight" : 5.0 + } ], + "score_mode" : "multiply", + "boost_mode" : "multiply", + "max_boost" : 100.0, + "min_score" : 1.0, + "boost" : 42.0 + } + }"""; FunctionScoreQueryBuilder expectedParsed = (FunctionScoreQueryBuilder) parseQuery(expected); assertEquals(expectedParsed, parsed); @@ -761,44 +772,56 @@ public void testSingleScriptFunction() throws IOException { } public void testQueryMalformedArrayNotSupported() throws IOException { - String json = "{\n" + " \"function_score\" : {\n" + " \"not_supported\" : []\n" + " }\n" + "}"; + String json = """ + { + "function_score" : { + "not_supported" : [] + } + }"""; expectParsingException(json, "array [not_supported] is not supported"); } public void testQueryMalformedFieldNotSupported() throws IOException { - String json = "{\n" + " \"function_score\" : {\n" + " \"not_supported\" : \"value\"\n" + " }\n" + "}"; + String json = """ + { + "function_score" : { + "not_supported" : "value" + } + }"""; expectParsingException(json, "field [not_supported] is not supported"); } public void testMalformedQueryFunctionFieldNotSupported() throws IOException { - String json = "{\n" - + " \"function_score\" : {\n" - + " \"functions\" : [ {\n" - + " \"not_supported\" : 23.0\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "function_score" : { + "functions" : [ { + "not_supported" : 23.0 + } + } + }"""; expectParsingException(json, "field [not_supported] is not supported"); } public void testMalformedQueryMultipleQueryObjects() throws IOException { // verify that an error is thrown rather than setting the query twice (https://github.com/elastic/elasticsearch/issues/16583) - String json = "{\n" - + " \"function_score\":{\n" - + " \"query\":{\n" - + " \"bool\":{\n" - + " \"must\":{\"match\":{\"field\":\"value\"}}" - + " },\n" - + " \"ignored_field_name\": {\n" - + " {\"match\":{\"field\":\"value\"}}\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String json = """ + { + "function_score":{ + "query":{ + "bool":{ + "must":{"match":{"field":"value"}} + }, + "ignored_field_name": { + {"match":{"field":"value"}} + } + } + } + } + }"""; expectParsingException(json, equalTo("[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]")); } diff --git a/server/src/test/java/org/elasticsearch/index/reindex/ReindexRequestTests.java b/server/src/test/java/org/elasticsearch/index/reindex/ReindexRequestTests.java index a9f77cbed63f7..339a3712587f7 100644 --- a/server/src/test/java/org/elasticsearch/index/reindex/ReindexRequestTests.java +++ b/server/src/test/java/org/elasticsearch/index/reindex/ReindexRequestTests.java @@ -259,7 +259,10 @@ public void testBuildRemoteInfoFullyLoaded() throws IOException { assertEquals("https", remoteInfo.getScheme()); assertEquals("example.com", remoteInfo.getHost()); assertEquals(9200, remoteInfo.getPort()); - assertEquals("{\n \"a\" : \"b\"\n}", remoteInfo.getQuery().utf8ToString()); + assertEquals(""" + { + "a" : "b" + }""", remoteInfo.getQuery().utf8ToString()); assertEquals("testuser", remoteInfo.getUsername()); assertEquals("testpass", remoteInfo.getPassword()); assertEquals(headers, remoteInfo.getHeaders()); diff --git a/server/src/test/java/org/elasticsearch/index/replication/IndexLevelReplicationTests.java b/server/src/test/java/org/elasticsearch/index/replication/IndexLevelReplicationTests.java index 789e743421374..ca5dc38109431 100644 --- a/server/src/test/java/org/elasticsearch/index/replication/IndexLevelReplicationTests.java +++ b/server/src/test/java/org/elasticsearch/index/replication/IndexLevelReplicationTests.java @@ -292,7 +292,9 @@ public void testCheckpointsAdvance() throws Exception { } public void testConflictingOpsOnReplica() throws Exception { - String mappings = "{ \"_doc\": { \"properties\": { \"f\": { \"type\": \"keyword\"} }}}"; + String mappings = """ + { "_doc": { "properties": { "f": { "type": "keyword"} }}} + """; try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(2, mappings))) { shards.startAll(); List replicas = shards.getReplicas(); @@ -319,7 +321,8 @@ public void testConflictingOpsOnReplica() throws Exception { } public void testReplicaTermIncrementWithConcurrentPrimaryPromotion() throws Exception { - String mappings = "{ \"_doc\": { \"properties\": { \"f\": { \"type\": \"keyword\"} }}}"; + String mappings = """ + { "_doc": { "properties": { "f": { "type": "keyword"} }}}"""; try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(2, mappings))) { shards.startAll(); long primaryPrimaryTerm = shards.getPrimary().getPendingPrimaryTerm(); @@ -369,7 +372,8 @@ public void testReplicaTermIncrementWithConcurrentPrimaryPromotion() throws Exce } public void testReplicaOperationWithConcurrentPrimaryPromotion() throws Exception { - String mappings = "{ \"_doc\": { \"properties\": { \"f\": { \"type\": \"keyword\"} }}}"; + String mappings = """ + { "_doc": { "properties": { "f": { "type": "keyword"} }}}"""; try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(1, mappings))) { shards.startAll(); long primaryPrimaryTerm = shards.getPrimary().getPendingPrimaryTerm(); diff --git a/server/src/test/java/org/elasticsearch/index/replication/RecoveryDuringReplicationTests.java b/server/src/test/java/org/elasticsearch/index/replication/RecoveryDuringReplicationTests.java index 95114f3ef7ff1..5124de5fcccf9 100644 --- a/server/src/test/java/org/elasticsearch/index/replication/RecoveryDuringReplicationTests.java +++ b/server/src/test/java/org/elasticsearch/index/replication/RecoveryDuringReplicationTests.java @@ -329,7 +329,8 @@ public void testReplicaRollbackStaleDocumentsInPeerRecovery() throws Exception { } public void testResyncAfterPrimaryPromotion() throws Exception { - String mappings = "{ \"_doc\": { \"properties\": { \"f\": { \"type\": \"keyword\"} }}}"; + String mappings = """ + { "_doc": { "properties": { "f": { "type": "keyword"} }}}"""; try (ReplicationGroup shards = new ReplicationGroup(buildIndexMetadata(2, mappings))) { shards.startAll(); int initialDocs = randomInt(10); diff --git a/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryParserTests.java b/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryParserTests.java index e72e7e1d7d723..0d1c5f9606370 100644 --- a/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryParserTests.java +++ b/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryParserTests.java @@ -65,28 +65,30 @@ public void setup() throws IOException { Settings settings = Settings.builder().build(); IndexService indexService = createIndex("test", settings); MapperService mapperService = indexService.mapperService(); - String mapping = "{\n" - + " \"person\":{\n" - + " \"properties\":{\n" - + " \"name\":{\n" - + " \"properties\":{\n" - + " \"first\": {\n" - + " \"type\":\"text\",\n" - + " \"analyzer\":\"standard\"\n" - + " }," - + " \"last\": {\n" - + " \"type\":\"text\",\n" - + " \"analyzer\":\"standard\"\n" - + " }," - + " \"nickname\": {\n" - + " \"type\":\"text\",\n" - + " \"analyzer\":\"whitespace\"\n" - + " }" - + " }" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String mapping = """ + { + "person": { + "properties": { + "name": { + "properties": { + "first": { + "type": "text", + "analyzer": "standard" + }, + "last": { + "type": "text", + "analyzer": "standard" + }, + "nickname": { + "type": "text", + "analyzer": "whitespace" + } + } + } + } + } + } + """; mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); this.indexService = indexService; } diff --git a/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java b/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java index c8a8f2a6b08d7..aa4720e31b86f 100644 --- a/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java +++ b/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java @@ -2644,11 +2644,8 @@ public void testReaderWrapperWorksWithGlobalOrdinals() throws IOException { .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\", \"fielddata\": true }}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text", "fielddata": true }}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard shard = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, wrapper); recoverShardFromStore(shard); indexDoc(shard, "_doc", "0", "{\"foo\" : \"bar\"}"); @@ -2782,11 +2779,8 @@ public void testTranslogRecoverySyncsTranslog() throws IOException { .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(primary); @@ -2828,7 +2822,8 @@ public void testRecoverFromTranslog() throws IOException { .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") + .putMapping(""" + { "properties": { "foo": { "type": "text"}}}""") .settings(settings) .primaryTerm(0, randomLongBetween(1, Long.MAX_VALUE)) .build(); @@ -2910,11 +2905,8 @@ public void testShardActiveDuringPeerRecovery() throws IOException { .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(primary); @@ -2960,11 +2952,8 @@ public void testRefreshListenersDuringPeerRecovery() throws IOException { .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(primary); @@ -3109,7 +3098,8 @@ public void testRecoverFromLocalShard() throws IOException { } assertThat(requestedMappingUpdates, hasKey("_doc")); - assertThat(requestedMappingUpdates.get("_doc").source().string(), equalTo("{\"properties\":{\"foo\":{\"type\":\"text\"}}}")); + assertThat(requestedMappingUpdates.get("_doc").source().string(), equalTo(""" + {"properties":{"foo":{"type":"text"}}}""")); closeShards(sourceShard, targetShard); } @@ -3701,11 +3691,8 @@ public void testIsSearchIdle() throws Exception { .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(primary); indexDoc(primary, "_doc", "0", "{\"foo\" : \"bar\"}"); @@ -3751,11 +3738,8 @@ public void testScheduledRefresh() throws Exception { .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(primary); indexDoc(primary, "_doc", "0", "{\"foo\" : \"bar\"}"); @@ -3825,11 +3809,8 @@ public void testRefreshIsNeededWithRefreshListeners() throws IOException, Interr .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(primary); indexDoc(primary, "_doc", "0", "{\"foo\" : \"bar\"}"); diff --git a/server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java b/server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java index d347fd0e7ddf0..b9aa0d33704c5 100644 --- a/server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java +++ b/server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java @@ -36,11 +36,8 @@ public void testGetForUpdate() throws IOException { .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(primary); LongSupplier translogInMemorySegmentCount = ((InternalEngine) primary.getEngine()).translogInMemorySegmentsCount::get; @@ -90,14 +87,18 @@ public void testGetForUpdate() throws IOException { } public void testGetFromTranslogWithStringSourceMappingOptionsAndStoredFields() throws IOException { - String docToIndex = "{\"foo\" : \"foo\", \"bar\" : \"bar\"}"; + String docToIndex = """ + {"foo" : "foo", "bar" : "bar"} + """; boolean noSource = randomBoolean(); String sourceOptions = noSource ? "\"enabled\": false" : randomBoolean() ? "\"excludes\": [\"fo*\"]" : "\"includes\": [\"ba*\"]"; runGetFromTranslogWithOptions(docToIndex, sourceOptions, noSource ? "" : "{\"bar\":\"bar\"}", "\"text\"", "foo"); } public void testGetFromTranslogWithLongSourceMappingOptionsAndStoredFields() throws IOException { - String docToIndex = "{\"foo\" : 7, \"bar\" : 42}"; + String docToIndex = """ + {"foo" : 7, "bar" : 42} + """; boolean noSource = randomBoolean(); String sourceOptions = noSource ? "\"enabled\": false" : randomBoolean() ? "\"excludes\": [\"fo*\"]" : "\"includes\": [\"ba*\"]"; runGetFromTranslogWithOptions(docToIndex, sourceOptions, noSource ? "" : "{\"bar\":42}", "\"long\"", 7L); @@ -116,20 +117,18 @@ private void runGetFromTranslogWithOptions( .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("test") - .putMapping( - "{ \"properties\": { \"foo\": { \"type\": " - + fieldType - + ", \"store\": true }, " - + "\"bar\": { \"type\": " - + fieldType - + "}}, \"_source\": { " - + sourceOptions - + "}}}" - ) - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("test").putMapping(""" + { + "properties": { + "foo": { + "type": %s, + "store": true + }, + "bar": { "type": %s } + }, + "_source": { %s } + } + }""".formatted(fieldType, fieldType, sourceOptions)).settings(settings).primaryTerm(0, 1).build(); IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, EngineTestCase.randomReaderWrapper()); recoverShardFromStore(primary); LongSupplier translogInMemorySegmentCount = ((InternalEngine) primary.getEngine()).translogInMemorySegmentsCount::get; @@ -191,11 +190,8 @@ public void testTypelessGetForUpdate() throws IOException { .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) .build(); - IndexMetadata metadata = IndexMetadata.builder("index") - .putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\"}}}") - .settings(settings) - .primaryTerm(0, 1) - .build(); + IndexMetadata metadata = IndexMetadata.builder("index").putMapping(""" + { "properties": { "foo": { "type": "text"}}}""").settings(settings).primaryTerm(0, 1).build(); IndexShard shard = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null); recoverShardFromStore(shard); Engine.IndexResult indexResult = indexDoc(shard, "some_type", "0", "{\"foo\" : \"bar\"}"); diff --git a/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java b/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java index 45eaba540a3cb..9408a17ad3b2b 100644 --- a/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java +++ b/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java @@ -44,6 +44,7 @@ import org.elasticsearch.common.util.concurrent.AbstractRunnable; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.common.util.concurrent.ReleasableLock; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.Tuple; import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.index.IndexSettings; @@ -486,18 +487,16 @@ public void testStats() throws IOException { builder.startObject(); copy.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertThat( - Strings.toString(builder), - equalTo( - "{\"translog\":{\"operations\":4,\"size_in_bytes\":" - + 326 - + ",\"uncommitted_operations\":4,\"uncommitted_size_in_bytes\":" - + 271 - + ",\"earliest_last_modified_age\":" - + stats.getEarliestLastModifiedAge() - + "}}" - ) - ); + assertThat(Strings.toString(builder), equalTo(XContentHelper.stripWhitespace(""" + { + "translog": { + "operations": 4, + "size_in_bytes": 326, + "uncommitted_operations": 4, + "uncommitted_size_in_bytes": 271, + "earliest_last_modified_age": %s + } + }""".formatted(stats.getEarliestLastModifiedAge())))); } } translog.getDeletionPolicy().setLocalCheckpointOfSafeCommit(randomLongBetween(3, Long.MAX_VALUE)); diff --git a/server/src/test/java/org/elasticsearch/indices/SystemIndexDescriptorTests.java b/server/src/test/java/org/elasticsearch/indices/SystemIndexDescriptorTests.java index 7720304196dce..8c95ce138f9d8 100644 --- a/server/src/test/java/org/elasticsearch/indices/SystemIndexDescriptorTests.java +++ b/server/src/test/java/org/elasticsearch/indices/SystemIndexDescriptorTests.java @@ -95,16 +95,17 @@ public void testValidation() { * Check that a system index descriptor correctly identifies the presence of a dynamic mapping when once is present. */ public void testFindDynamicMappingsWithDynamicMapping() { - String json = "{" - + " \"foo\": {" - + " \"bar\": {" - + " \"dynamic\": false" - + " }," - + " \"baz\": {" - + " \"dynamic\": true" - + " }" - + " }" - + "}"; + String json = """ + { + "foo": { + "bar": { + "dynamic": false + }, + "baz": { + "dynamic": true + } + } + }"""; final Map mappings = XContentHelper.convertToMap(JsonXContent.jsonXContent, json, false); @@ -115,7 +116,8 @@ public void testFindDynamicMappingsWithDynamicMapping() { * Check that a system index descriptor correctly identifies the absence of a dynamic mapping when none are present. */ public void testFindDynamicMappingsWithoutDynamicMapping() { - String json = "{ \"foo\": { \"bar\": { \"dynamic\": false } } }"; + String json = """ + { "foo": { "bar": { "dynamic": false } } }"""; final Map mappings = XContentHelper.convertToMap(JsonXContent.jsonXContent, json, false); diff --git a/server/src/test/java/org/elasticsearch/indices/TermsLookupTests.java b/server/src/test/java/org/elasticsearch/indices/TermsLookupTests.java index 1e9c5f922b8e2..c9a9b99d49b03 100644 --- a/server/src/test/java/org/elasticsearch/indices/TermsLookupTests.java +++ b/server/src/test/java/org/elasticsearch/indices/TermsLookupTests.java @@ -70,10 +70,8 @@ public void testSerialization() throws IOException { } public void testXContentParsing() throws IOException { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{ \"index\" : \"index\", \"id\" : \"id\", \"path\" : \"path\", \"routing\" : \"routing\" }" - ); + XContentParser parser = createParser(JsonXContent.jsonXContent, """ + { "index" : "index", "id" : "id", "path" : "path", "routing" : "routing" }"""); TermsLookup tl = TermsLookup.parseTermsLookup(parser); assertEquals("index", tl.index()); diff --git a/server/src/test/java/org/elasticsearch/ingest/IngestMetadataTests.java b/server/src/test/java/org/elasticsearch/ingest/IngestMetadataTests.java index 49c7fd128bfbb..87a8b277a3423 100644 --- a/server/src/test/java/org/elasticsearch/ingest/IngestMetadataTests.java +++ b/server/src/test/java/org/elasticsearch/ingest/IngestMetadataTests.java @@ -30,16 +30,10 @@ public class IngestMetadataTests extends ESTestCase { public void testFromXContent() throws IOException { - PipelineConfiguration pipeline = new PipelineConfiguration( - "1", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); - PipelineConfiguration pipeline2 = new PipelineConfiguration( - "2", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field1\", \"value\": \"_value1\"}}]}"), - XContentType.JSON - ); + PipelineConfiguration pipeline = new PipelineConfiguration("1", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); + PipelineConfiguration pipeline2 = new PipelineConfiguration("2", new BytesArray(""" + {"processors": [{"set" : {"field": "_field1", "value": "_value1"}}]}"""), XContentType.JSON); Map map = new HashMap<>(); map.put(pipeline.getId(), pipeline); map.put(pipeline2.getId(), pipeline2); diff --git a/server/src/test/java/org/elasticsearch/ingest/IngestServiceTests.java b/server/src/test/java/org/elasticsearch/ingest/IngestServiceTests.java index c52e29a516f5b..c51fa84e0cd9c 100644 --- a/server/src/test/java/org/elasticsearch/ingest/IngestServiceTests.java +++ b/server/src/test/java/org/elasticsearch/ingest/IngestServiceTests.java @@ -209,11 +209,8 @@ public void testUpdatePipelines() { ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); assertThat(ingestService.pipelines().size(), is(0)); - PipelineConfiguration pipeline = new PipelineConfiguration( - "_id", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); + PipelineConfiguration pipeline = new PipelineConfiguration("_id", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); IngestMetadata ingestMetadata = new IngestMetadata(Collections.singletonMap("_id", pipeline)); clusterState = ClusterState.builder(clusterState) .metadata(Metadata.builder().putCustom(IngestMetadata.TYPE, ingestMetadata)) @@ -269,11 +266,8 @@ public void testInnerUpdatePipelines() { assertThat(ingestService.pipelines().get("_id3").pipeline.getId(), equalTo("_id3")); assertThat(ingestService.pipelines().get("_id3").pipeline.getProcessors().size(), equalTo(0)); - pipeline3 = new PipelineConfiguration( - "_id3", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); + pipeline3 = new PipelineConfiguration("_id3", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); ingestMetadata = new IngestMetadata(Map.of("_id1", pipeline1, "_id3", pipeline3)); ingestService.innerUpdatePipelines(ingestMetadata); @@ -292,11 +286,8 @@ public void testInnerUpdatePipelines() { public void testDelete() { IngestService ingestService = createWithProcessors(); - PipelineConfiguration config = new PipelineConfiguration( - "_id", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); + PipelineConfiguration config = new PipelineConfiguration("_id", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); IngestMetadata ingestMetadata = new IngestMetadata(Collections.singletonMap("_id", config)); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); ClusterState previousClusterState = clusterState; @@ -324,11 +315,8 @@ public void testDelete() { public void testValidateNoIngestInfo() throws Exception { IngestService ingestService = createWithProcessors(); - PutPipelineRequest putRequest = new PutPipelineRequest( - "_id", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); + PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); var pipelineConfig = XContentHelper.convertToMap(putRequest.getSource(), false, putRequest.getXContentType()).v2(); Exception e = expectThrows( @@ -422,14 +410,24 @@ public void testGetProcessorsInPipeline() throws Exception { assertThat(pipeline, nullValue()); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty - PutPipelineRequest putRequest = new PutPipelineRequest( - "_id", - new BytesArray( - "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}," - + "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}" - ), - XContentType.JSON - ); + PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray(""" + { + "processors": [ + { + "set": { + "field": "_field", + "value": "_value", + "tag": "tag1" + } + }, + { + "remove": { + "field": "_field", + "tag": "tag2" + } + } + ] + }"""), XContentType.JSON); ClusterState previousClusterState = clusterState; clusterState = IngestService.innerPut(putRequest, clusterState); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); @@ -455,20 +453,27 @@ public void testGetPipelineWithProcessorType() throws Exception { ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); ClusterState previousClusterState = clusterState; - PutPipelineRequest putRequest1 = new PutPipelineRequest( - "_id1", - new BytesArray( - "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}," - + "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}" - ), - XContentType.JSON - ); + PutPipelineRequest putRequest1 = new PutPipelineRequest("_id1", new BytesArray(""" + { + "processors": [ + { + "set": { + "field": "_field", + "value": "_value", + "tag": "tag1" + } + }, + { + "remove": { + "field": "_field", + "tag": "tag2" + } + } + ] + }"""), XContentType.JSON); clusterState = IngestService.innerPut(putRequest1, clusterState); - PutPipelineRequest putRequest2 = new PutPipelineRequest( - "_id2", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag2\"}}]}"), - XContentType.JSON - ); + PutPipelineRequest putRequest2 = new PutPipelineRequest("_id2", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value", "tag": "tag2"}}]}"""), XContentType.JSON); clusterState = IngestService.innerPut(putRequest2, clusterState); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); @@ -505,11 +510,8 @@ public String getType() { ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); ClusterState previousClusterState = clusterState; - PutPipelineRequest putRequest1 = new PutPipelineRequest( - "_id1", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}]}"), - XContentType.JSON - ); + PutPipelineRequest putRequest1 = new PutPipelineRequest("_id1", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value", "tag": "tag1"}}]}"""), XContentType.JSON); clusterState = IngestService.innerPut(putRequest1, clusterState); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); @@ -576,11 +578,8 @@ public void testGetProcessorsInPipelineComplexConditional() throws Exception { assertThat(pipeline, nullValue()); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty - PutPipelineRequest putRequest = new PutPipelineRequest( - id, - new BytesArray("{\"processors\": [{\"complexSet\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); + PutPipelineRequest putRequest = new PutPipelineRequest(id, new BytesArray(""" + {"processors": [{"complexSet" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); ClusterState previousClusterState = clusterState; clusterState = IngestService.innerPut(putRequest, clusterState); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); @@ -602,11 +601,8 @@ public void testCrud() throws Exception { assertThat(pipeline, nullValue()); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty - PutPipelineRequest putRequest = new PutPipelineRequest( - id, - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); + PutPipelineRequest putRequest = new PutPipelineRequest(id, new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); ClusterState previousClusterState = clusterState; clusterState = IngestService.innerPut(putRequest, clusterState); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); @@ -644,11 +640,8 @@ public void testPut() { assertThat(pipeline.getProcessors().size(), equalTo(0)); // overwrite existing pipeline: - putRequest = new PutPipelineRequest( - id, - new BytesArray("{\"processors\": [], \"description\": \"_description\"}"), - XContentType.JSON - ); + putRequest = new PutPipelineRequest(id, new BytesArray(""" + {"processors": [], "description": "_description"}"""), XContentType.JSON); previousClusterState = clusterState; clusterState = IngestService.innerPut(putRequest, clusterState); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); @@ -707,7 +700,8 @@ public void testPutWithErrorResponse() throws IllegalAccessException { public void testDeleteUsingWildcard() { IngestService ingestService = createWithProcessors(); HashMap pipelines = new HashMap<>(); - BytesArray definition = new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"); + BytesArray definition = new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""); pipelines.put("p1", new PipelineConfiguration("p1", definition, XContentType.JSON)); pipelines.put("p2", new PipelineConfiguration("p2", definition, XContentType.JSON)); pipelines.put("q1", new PipelineConfiguration("q1", definition, XContentType.JSON)); @@ -755,7 +749,8 @@ public void testDeleteUsingWildcard() { public void testDeleteWithExistingUnmatchedPipelines() { IngestService ingestService = createWithProcessors(); HashMap pipelines = new HashMap<>(); - BytesArray definition = new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"); + BytesArray definition = new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""); pipelines.put("p1", new PipelineConfiguration("p1", definition, XContentType.JSON)); IngestMetadata ingestMetadata = new IngestMetadata(pipelines); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); @@ -777,11 +772,8 @@ public void testDeleteWithExistingUnmatchedPipelines() { public void testDeleteWithIndexUsePipeline() { IngestService ingestService = createWithProcessors(); - PipelineConfiguration config = new PipelineConfiguration( - "_id", - new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), - XContentType.JSON - ); + PipelineConfiguration config = new PipelineConfiguration("_id", new BytesArray(""" + {"processors": [{"set" : {"field": "_field", "value": "_value"}}]}"""), XContentType.JSON); IngestMetadata ingestMetadata = new IngestMetadata(Collections.singletonMap("_id", config)); Metadata.Builder builder = Metadata.builder(); for (int i = 0; i < randomIntBetween(2, 10); i++) { @@ -881,14 +873,24 @@ public void testGetPipelines() { public void testValidate() throws Exception { IngestService ingestService = createWithProcessors(); - PutPipelineRequest putRequest = new PutPipelineRequest( - "_id", - new BytesArray( - "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}," - + "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}" - ), - XContentType.JSON - ); + PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray(""" + { + "processors": [ + { + "set": { + "field": "_field", + "value": "_value", + "tag": "tag1" + } + }, + { + "remove": { + "field": "_field", + "tag": "tag2" + } + } + ] + }"""), XContentType.JSON); var pipelineConfig = XContentHelper.convertToMap(putRequest.getSource(), false, putRequest.getXContentType()).v2(); DiscoveryNode node1 = new DiscoveryNode("_node_id1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT); @@ -1091,11 +1093,8 @@ public void testDynamicTemplates() throws Exception { public void testExecuteEmptyPipeline() throws Exception { IngestService ingestService = createWithProcessors(emptyMap()); - PutPipelineRequest putRequest = new PutPipelineRequest( - "_id", - new BytesArray("{\"processors\": [], \"description\": \"_description\"}"), - XContentType.JSON - ); + PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray(""" + {"processors": [], "description": "_description"}"""), XContentType.JSON); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); // Start empty ClusterState previousClusterState = clusterState; clusterState = IngestService.innerPut(putRequest, clusterState); @@ -1425,11 +1424,8 @@ public void testBulkRequestExecution() throws Exception { map.put("mock", (factories, tag, description, config) -> processor); IngestService ingestService = createWithProcessors(map); - PutPipelineRequest putRequest = new PutPipelineRequest( - "_id", - new BytesArray("{\"processors\": [{\"mock\": {}}], \"description\": \"_description\"}"), - XContentType.JSON - ); + PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray(""" + {"processors": [{"mock": {}}], "description": "_description"}"""), XContentType.JSON); ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build(); ClusterState previousClusterState = clusterState; clusterState = IngestService.innerPut(putRequest, clusterState); @@ -1584,11 +1580,8 @@ public void testStats() throws Exception { assertProcessorStats(0, afterThirdRequestStats, "_id2", 1, 0, 0); // test a failure, and that the processor stats are added from the old stats - putRequest = new PutPipelineRequest( - "_id1", - new BytesArray("{\"processors\": [{\"failure-mock\" : { \"on_failure\": [{\"mock\" : {}}]}}, {\"mock\" : {}}]}"), - XContentType.JSON - ); + putRequest = new PutPipelineRequest("_id1", new BytesArray(""" + {"processors": [{"failure-mock" : { "on_failure": [{"mock" : {}}]}}, {"mock" : {}}]}"""), XContentType.JSON); previousClusterState = clusterState; clusterState = IngestService.innerPut(putRequest, clusterState); ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState)); @@ -1942,7 +1935,9 @@ public void testUpdatingRandomPipelineWithoutChangesIsNoOp() throws Exception { public void testUpdatingPipelineWithoutChangesIsNoOp() throws Exception { var value = randomAlphaOfLength(5); - var pipelineString = "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"" + value + "\"}}]}"; + var pipelineString = """ + {"processors": [{"set" : {"field": "_field", "value": "%s"}}]} + """.formatted(value); testUpdatingPipeline(pipelineString); } diff --git a/server/src/test/java/org/elasticsearch/ingest/PipelineConfigurationTests.java b/server/src/test/java/org/elasticsearch/ingest/PipelineConfigurationTests.java index 63d4487c1e739..97676f7837c09 100644 --- a/server/src/test/java/org/elasticsearch/ingest/PipelineConfigurationTests.java +++ b/server/src/test/java/org/elasticsearch/ingest/PipelineConfigurationTests.java @@ -47,7 +47,8 @@ public void testSerialization() throws IOException { } public void testMetaSerialization() throws IOException { - String configJson = "{\"description\": \"blah\", \"_meta\" : {\"foo\": \"bar\"}}"; + String configJson = """ + {"description": "blah", "_meta" : {"foo": "bar"}}"""; PipelineConfiguration configuration = new PipelineConfiguration( "1", new BytesArray(configJson.getBytes(StandardCharsets.UTF_8)), @@ -85,7 +86,8 @@ public void testParser() throws IOException { public void testGetVersion() { { // missing version - String configJson = "{\"description\": \"blah\", \"_meta\" : {\"foo\": \"bar\"}}"; + String configJson = """ + {"description": "blah", "_meta" : {"foo": "bar"}}"""; PipelineConfiguration configuration = new PipelineConfiguration( "1", new BytesArray(configJson.getBytes(StandardCharsets.UTF_8)), @@ -96,7 +98,9 @@ public void testGetVersion() { { // null version int version = randomInt(); - String configJson = "{\"version\": " + version + ", \"description\": \"blah\", \"_meta\" : {\"foo\": \"bar\"}}"; + String configJson = """ + {"version": %d, "description": "blah", "_meta" : {"foo": "bar"}} + """.formatted(version); PipelineConfiguration configuration = new PipelineConfiguration( "1", new BytesArray(configJson.getBytes(StandardCharsets.UTF_8)), diff --git a/server/src/test/java/org/elasticsearch/rest/BytesRestResponseTests.java b/server/src/test/java/org/elasticsearch/rest/BytesRestResponseTests.java index 6927cebb01c44..eff3032e95424 100644 --- a/server/src/test/java/org/elasticsearch/rest/BytesRestResponseTests.java +++ b/server/src/test/java/org/elasticsearch/rest/BytesRestResponseTests.java @@ -18,6 +18,7 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ESTestCase; @@ -78,8 +79,10 @@ public void testDetailedExceptionMessage() throws Exception { Exception t = new ElasticsearchException("an error occurred reading data", new FileNotFoundException("/foo/bar")); BytesRestResponse response = new BytesRestResponse(channel, t); String text = response.content().utf8ToString(); - assertThat(text, containsString("{\"type\":\"exception\",\"reason\":\"an error occurred reading data\"}")); - assertThat(text, containsString("{\"type\":\"file_not_found_exception\",\"reason\":\"/foo/bar\"}")); + assertThat(text, containsString(""" + {"type":"exception","reason":"an error occurred reading data"}""")); + assertThat(text, containsString(""" + {"type":"file_not_found_exception","reason":"/foo/bar"}""")); } public void testNonElasticsearchExceptionIsNotShownAsSimpleMessage() throws Exception { @@ -103,9 +106,12 @@ public void testErrorTrace() throws Exception { Exception t = new UnknownException("an error occurred reading data", new FileNotFoundException("/foo/bar")); BytesRestResponse response = new BytesRestResponse(channel, t); String text = response.content().utf8ToString(); - assertThat(text, containsString("\"type\":\"unknown_exception\",\"reason\":\"an error occurred reading data\"")); - assertThat(text, containsString("{\"type\":\"file_not_found_exception\"")); - assertThat(text, containsString("\"stack_trace\":\"org.elasticsearch.ElasticsearchException$1: an error occurred reading data")); + assertThat(text, containsString(""" + "type":"unknown_exception","reason":"an error occurred reading data\"""")); + assertThat(text, containsString(""" + {"type":"file_not_found_exception\"""")); + assertThat(text, containsString(""" + "stack_trace":"org.elasticsearch.ElasticsearchException$1: an error occurred reading data""")); } public void testGuessRootCause() throws IOException { @@ -114,13 +120,15 @@ public void testGuessRootCause() throws IOException { Exception e = new ElasticsearchException("an error occurred reading data", new FileNotFoundException("/foo/bar")); BytesRestResponse response = new BytesRestResponse(new DetailedExceptionRestChannel(request), e); String text = response.content().utf8ToString(); - assertThat(text, containsString("{\"root_cause\":[{\"type\":\"exception\",\"reason\":\"an error occurred reading data\"}]")); + assertThat(text, containsString(""" + {"root_cause":[{"type":"exception","reason":"an error occurred reading data"}]""")); } { Exception e = new FileNotFoundException("/foo/bar"); BytesRestResponse response = new BytesRestResponse(new DetailedExceptionRestChannel(request), e); String text = response.content().utf8ToString(); - assertThat(text, containsString("{\"root_cause\":[{\"type\":\"file_not_found_exception\",\"reason\":\"/foo/bar\"}]")); + assertThat(text, containsString(""" + {"root_cause":[{"type":"file_not_found_exception","reason":"/foo/bar"}]""")); } } @@ -152,11 +160,31 @@ public void testConvert() throws IOException { ); BytesRestResponse response = new BytesRestResponse(channel, new RemoteTransportException("foo", ex)); String text = response.content().utf8ToString(); - String expected = "{\"error\":{\"root_cause\":[{\"type\":\"parsing_exception\",\"reason\":\"foobar\",\"line\":1,\"col\":2}]," - + "\"type\":\"search_phase_execution_exception\",\"reason\":\"all shards failed\",\"phase\":\"search\",\"grouped\":true," - + "\"failed_shards\":[{\"shard\":1,\"index\":\"foo\",\"node\":\"node_1\",\"reason\":{\"type\":\"parsing_exception\"," - + "\"reason\":\"foobar\",\"line\":1,\"col\":2}}]},\"status\":400}"; - assertEquals(expected.trim(), text.trim()); + String expected = """ + { + "error": { + "root_cause": [ { "type": "parsing_exception", "reason": "foobar", "line": 1, "col": 2 } ], + "type": "search_phase_execution_exception", + "reason": "all shards failed", + "phase": "search", + "grouped": true, + "failed_shards": [ + { + "shard": 1, + "index": "foo", + "node": "node_1", + "reason": { + "type": "parsing_exception", + "reason": "foobar", + "line": 1, + "col": 2 + } + } + ] + }, + "status": 400 + }"""; + assertEquals(XContentHelper.stripWhitespace(expected), XContentHelper.stripWhitespace(text)); String stackTrace = ExceptionsHelper.stackTrace(ex); assertThat(stackTrace, containsString("org.elasticsearch.common.ParsingException: foobar")); } diff --git a/server/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java b/server/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java index 1dc8250ee42a9..dbf976eaee09c 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java @@ -113,72 +113,68 @@ public void testBuildBroadcastShardsHeader() throws IOException { builder.startObject(); RestActions.buildBroadcastShardsHeader(builder, ToXContent.EMPTY_PARAMS, 12, 3, 0, 9, failures); builder.endObject(); - assertThat( - Strings.toString(builder), - equalTo( - "{\n" - + " \"_shards\" : {\n" - + " \"total\" : 12,\n" - + " \"successful\" : 3,\n" - + " \"skipped\" : 0,\n" - + " \"failed\" : 9,\n" - + " \"failures\" : [\n" - + " {\n" - + " \"shard\" : 0,\n" - + " \"index\" : \"index\",\n" - + " \"node\" : \"node0\",\n" - + " \"reason\" : {\n" - + " \"type\" : \"parsing_exception\",\n" - + " \"reason\" : \"error\",\n" - + " \"index_uuid\" : \"_na_\",\n" - + " \"index\" : \"index\",\n" - + " \"line\" : 0,\n" - + " \"col\" : 0,\n" - + " \"caused_by\" : {\n" - + " \"type\" : \"illegal_argument_exception\",\n" - + " \"reason\" : \"some bad argument\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"shard\" : 0,\n" - + " \"index\" : \"cluster1:index\",\n" - + " \"node\" : \"node0\",\n" - + " \"reason\" : {\n" - + " \"type\" : \"parsing_exception\",\n" - + " \"reason\" : \"error\",\n" - + " \"index_uuid\" : \"_na_\",\n" - + " \"index\" : \"index\",\n" - + " \"line\" : 0,\n" - + " \"col\" : 0,\n" - + " \"caused_by\" : {\n" - + " \"type\" : \"illegal_argument_exception\",\n" - + " \"reason\" : \"some bad argument\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"shard\" : 0,\n" - + " \"index\" : \"cluster2:index\",\n" - + " \"node\" : \"node0\",\n" - + " \"reason\" : {\n" - + " \"type\" : \"parsing_exception\",\n" - + " \"reason\" : \"error\",\n" - + " \"index_uuid\" : \"_na_\",\n" - + " \"index\" : \"index\",\n" - + " \"line\" : 0,\n" - + " \"col\" : 0,\n" - + " \"caused_by\" : {\n" - + " \"type\" : \"illegal_argument_exception\",\n" - + " \"reason\" : \"some bad argument\"\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}" - ) - ); + assertThat(Strings.toString(builder), equalTo(""" + { + "_shards" : { + "total" : 12, + "successful" : 3, + "skipped" : 0, + "failed" : 9, + "failures" : [ + { + "shard" : 0, + "index" : "index", + "node" : "node0", + "reason" : { + "type" : "parsing_exception", + "reason" : "error", + "index_uuid" : "_na_", + "index" : "index", + "line" : 0, + "col" : 0, + "caused_by" : { + "type" : "illegal_argument_exception", + "reason" : "some bad argument" + } + } + }, + { + "shard" : 0, + "index" : "cluster1:index", + "node" : "node0", + "reason" : { + "type" : "parsing_exception", + "reason" : "error", + "index_uuid" : "_na_", + "index" : "index", + "line" : 0, + "col" : 0, + "caused_by" : { + "type" : "illegal_argument_exception", + "reason" : "some bad argument" + } + } + }, + { + "shard" : 0, + "index" : "cluster2:index", + "node" : "node0", + "reason" : { + "type" : "parsing_exception", + "reason" : "error", + "index_uuid" : "_na_", + "index" : "index", + "line" : 0, + "col" : 0, + "caused_by" : { + "type" : "illegal_argument_exception", + "reason" : "some bad argument" + } + } + } + ] + } + }""")); } private static ShardSearchFailure createShardFailureParsingException(String nodeId, int shardId, String clusterAlias) { diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestReloadSecureSettingsActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestReloadSecureSettingsActionTests.java index 8c8a3b372703d..badeb7598e839 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestReloadSecureSettingsActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/cluster/RestReloadSecureSettingsActionTests.java @@ -20,7 +20,8 @@ public class RestReloadSecureSettingsActionTests extends ESTestCase { public void testParserWithPassword() throws Exception { - final String request = "{" + "\"secure_settings_password\": \"secure_settings_password_string\"" + "}"; + final String request = """ + {"secure_settings_password": "secure_settings_password_string"}"""; try ( XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java index 398daa7743fb5..3d70b2a6f5ad4 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestCreateIndexActionTests.java @@ -125,17 +125,18 @@ public void testTypeInMapping() throws IOException { List contentTypeHeader = Collections.singletonList(compatibleMediaType(XContentType.VND_JSON, RestApiVersion.V_7)); - String content = "{\n" - + " \"mappings\": {\n" - + " \"some_type\": {\n" - + " \"properties\": {\n" - + " \"field1\": {\n" - + " \"type\": \"text\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String content = """ + { + "mappings": { + "some_type": { + "properties": { + "field1": { + "type": "text" + } + } + } + } + }"""; Map params = new HashMap<>(); params.put(RestCreateIndexAction.INCLUDE_TYPE_NAME_PARAMETER, "true"); @@ -148,7 +149,8 @@ public void testTypeInMapping() throws IOException { CreateIndexRequest createIndexRequest = action.prepareRequestV7(request); // some_type is replaced with _doc - assertThat(createIndexRequest.mappings(), equalTo("{\"_doc\":{\"properties\":{\"field1\":{\"type\":\"text\"}}}}")); + assertThat(createIndexRequest.mappings(), equalTo(""" + {"_doc":{"properties":{"field1":{"type":"text"}}}}""")); assertCriticalWarnings(RestCreateIndexAction.TYPES_DEPRECATION_MESSAGE); } } diff --git a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java index f0600efd8c664..e694a4e236be3 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/admin/indices/RestValidateQueryActionTests.java @@ -104,7 +104,9 @@ public void terminateThreadPool() { public void testRestValidateQueryAction() throws Exception { // GIVEN a valid query - final String content = "{\"query\":{\"bool\":{\"must\":{\"term\":{\"user\":\"kimchy\"}}}}}"; + final String content = """ + {"query":{"bool":{"must":{"term":{"user":"kimchy"}}}}} + """; final RestRequest request = createRestRequest(content); final FakeRestChannel channel = new FakeRestChannel(request, true, 0); diff --git a/server/src/test/java/org/elasticsearch/rest/action/cat/RestTableTests.java b/server/src/test/java/org/elasticsearch/rest/action/cat/RestTableTests.java index 8c1cdec6f9e38..0856c5c787e64 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/cat/RestTableTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/cat/RestTableTests.java @@ -9,6 +9,7 @@ package org.elasticsearch.rest.action.cat; import org.elasticsearch.common.Table; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.rest.AbstractRestChannel; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.test.ESTestCase; @@ -40,18 +41,30 @@ public class RestTableTests extends ESTestCase { private static final String ACCEPT = "Accept"; private static final String TEXT_PLAIN = "text/plain; charset=UTF-8"; private static final String TEXT_TABLE_BODY = "foo foo foo foo foo foo foo foo\n"; - private static final String JSON_TABLE_BODY = "[{\"bulk.foo\":\"foo\",\"bulk.bar\":\"foo\",\"aliasedBulk\":\"foo\"," - + "\"aliasedSecondBulk\":\"foo\",\"unmatched\":\"foo\"," - + "\"invalidAliasesBulk\":\"foo\",\"timestamp\":\"foo\",\"epoch\":\"foo\"}]"; - private static final String YAML_TABLE_BODY = "---\n" - + "- bulk.foo: \"foo\"\n" - + " bulk.bar: \"foo\"\n" - + " aliasedBulk: \"foo\"\n" - + " aliasedSecondBulk: \"foo\"\n" - + " unmatched: \"foo\"\n" - + " invalidAliasesBulk: \"foo\"\n" - + " timestamp: \"foo\"\n" - + " epoch: \"foo\"\n"; + private static final String JSON_TABLE_BODY = """ + [ + { + "bulk.foo": "foo", + "bulk.bar": "foo", + "aliasedBulk": "foo", + "aliasedSecondBulk": "foo", + "unmatched": "foo", + "invalidAliasesBulk": "foo", + "timestamp": "foo", + "epoch": "foo" + } + ]"""; + private static final String YAML_TABLE_BODY = """ + --- + - bulk.foo: "foo" + bulk.bar: "foo" + aliasedBulk: "foo" + aliasedSecondBulk: "foo" + unmatched: "foo" + invalidAliasesBulk: "foo" + timestamp: "foo" + epoch: "foo" + """; private Table table; private FakeRestRequest restRequest; @@ -94,7 +107,11 @@ public void testThatDisplayHeadersAreNotAddedTwice() throws Exception { } public void testThatWeUseTheAcceptHeaderJson() throws Exception { - assertResponse(Collections.singletonMap(ACCEPT, Collections.singletonList(APPLICATION_JSON)), APPLICATION_JSON, JSON_TABLE_BODY); + assertResponse( + Collections.singletonMap(ACCEPT, Collections.singletonList(APPLICATION_JSON)), + APPLICATION_JSON, + XContentHelper.stripWhitespace(JSON_TABLE_BODY) + ); } public void testThatWeUseTheAcceptHeaderYaml() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/rest/action/document/RestBulkActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/document/RestBulkActionTests.java index 8341d3b18e9d0..e0c81493746a5 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/document/RestBulkActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/document/RestBulkActionTests.java @@ -49,19 +49,12 @@ public void bulk(BulkRequest request, ActionListener listener) { final Map params = new HashMap<>(); params.put("pipeline", "timestamps"); new RestBulkAction(settings(Version.CURRENT).build()).handleRequest( - new FakeRestRequest.Builder(xContentRegistry()).withPath("my_index/_bulk") - .withParams(params) - .withContent( - new BytesArray( - "{\"index\":{\"_id\":\"1\"}}\n" - + "{\"field1\":\"val1\"}\n" - + "{\"update\":{\"_id\":\"2\"}}\n" - + "{\"script\":{\"source\":\"ctx._source.counter++;\"},\"upsert\":{\"field1\":\"upserted_val\"}}\n" - ), - XContentType.JSON - ) - .withMethod(RestRequest.Method.POST) - .build(), + new FakeRestRequest.Builder(xContentRegistry()).withPath("my_index/_bulk").withParams(params).withContent(new BytesArray(""" + {"index":{"_id":"1"}} + {"field1":"val1"} + {"update":{"_id":"2"}} + {"script":{"source":"ctx._source.counter++;"},"upsert":{"field1":"upserted_val"}} + """), XContentType.JSON).withMethod(RestRequest.Method.POST).build(), mock(RestChannel.class), verifyingClient ); diff --git a/server/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionTests.java b/server/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionTests.java index a055059e809c2..fa0e25726bf29 100644 --- a/server/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionTests.java +++ b/server/src/test/java/org/elasticsearch/rest/action/document/RestUpdateActionTests.java @@ -52,7 +52,12 @@ public void testUpdateDocVersion() { } else { params.put("version_type", randomFrom(VersionType.values()).name()); } - String content = "{\n" + " \"doc\" : {\n" + " \"name\" : \"new_name\"\n" + " }\n" + "}"; + String content = """ + { + "doc" : { + "name" : "new_name" + } + }"""; FakeRestRequest updateRequest = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST) .withPath("test/_update/1") .withParams(params) diff --git a/server/src/test/java/org/elasticsearch/script/ScriptContextInfoTests.java b/server/src/test/java/org/elasticsearch/script/ScriptContextInfoTests.java index 6b0b537f17a4b..c828293b90e28 100644 --- a/server/src/test/java/org/elasticsearch/script/ScriptContextInfoTests.java +++ b/server/src/test/java/org/elasticsearch/script/ScriptContextInfoTests.java @@ -283,8 +283,21 @@ public void testParameterInfoParser() throws IOException { } public void testScriptMethodInfoParser() throws IOException { - String json = "{\"name\": \"fooFunc\", \"return_type\": \"int\", \"params\": [{\"type\": \"int\", \"name\": \"fooParam\"}, " - + "{\"type\": \"java.util.Map\", \"name\": \"barParam\"}]}"; + String json = """ + { + "name": "fooFunc", + "return_type": "int", + "params": [ + { + "type": "int", + "name": "fooParam" + }, + { + "type": "java.util.Map", + "name": "barParam" + } + ] + }"""; XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(json).streamInput()); ScriptContextInfo.ScriptMethodInfo info = ScriptContextInfo.ScriptMethodInfo.fromXContent(parser); @@ -304,52 +317,53 @@ public void testScriptMethodInfoParser() throws IOException { } public void testScriptContextInfoParser() throws IOException { - String json = "{" - + " \"name\": \"similarity\"," - + " \"methods\": [" - + " {" - + " \"name\": \"execute\"," - + " \"return_type\": \"double\"," - + " \"params\": [" - + " {" - + " \"type\": \"double\"," - + " \"name\": \"weight\"" - + " }," - + " {" - + " \"type\": \"org.elasticsearch.index.similarity.ScriptedSimilarity$Query\"," - + " \"name\": \"query\"" - + " }," - + " {" - + " \"type\": \"org.elasticsearch.index.similarity.ScriptedSimilarity$Field\"," - + " \"name\": \"field\"" - + " }," - + " {" - + " \"type\": \"org.elasticsearch.index.similarity.ScriptedSimilarity$Term\"," - + " \"name\": \"term\"" - + " }," - + " {" - + " \"type\": \"org.elasticsearch.index.similarity.ScriptedSimilarity$Doc\"," - + " \"name\": \"doc\"" - + " }" - + " ]" - + " }," - + " {" - + " \"name\": \"getParams\"," - + " \"return_type\": \"java.util.Map\"," - + " \"params\": []" - + " }," - + " {" - + " \"name\": \"getDoc\"," - + " \"return_type\": \"java.util.Map\"," - + " \"params\": []" - + " }," - + " {" - + " \"name\": \"get_score\"," - + " \"return_type\": \"double\"," - + " \"params\": []" - + " }" - + " ]" - + "}"; + String json = """ + { + "name": "similarity", + "methods": [ + { + "name": "execute", + "return_type": "double", + "params": [ + { + "type": "double", + "name": "weight" + }, + { + "type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Query", + "name": "query" + }, + { + "type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Field", + "name": "field" + }, + { + "type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Term", + "name": "term" + }, + { + "type": "org.elasticsearch.index.similarity.ScriptedSimilarity$Doc", + "name": "doc" + } + ] + }, + { + "name": "getParams", + "return_type": "java.util.Map", + "params": [] + }, + { + "name": "getDoc", + "return_type": "java.util.Map", + "params": [] + }, + { + "name": "get_score", + "return_type": "double", + "params": [] + } + ] + }"""; XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, new BytesArray(json).streamInput()); ScriptContextInfo parsed = ScriptContextInfo.fromXContent(parser); diff --git a/server/src/test/java/org/elasticsearch/script/ScriptMetadataTests.java b/server/src/test/java/org/elasticsearch/script/ScriptMetadataTests.java index 5d9ac3bcda7fe..68c57be9c4be8 100644 --- a/server/src/test/java/org/elasticsearch/script/ScriptMetadataTests.java +++ b/server/src/test/java/org/elasticsearch/script/ScriptMetadataTests.java @@ -49,33 +49,20 @@ public void testGetScript() throws Exception { public void testDiff() throws Exception { ScriptMetadata.Builder builder = new ScriptMetadata.Builder(null); - builder.storeScript( - "1", - StoredScriptSource.parse(new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"abc\"}}}"), XContentType.JSON) - ); - builder.storeScript( - "2", - StoredScriptSource.parse(new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"def\"}}}"), XContentType.JSON) - ); - builder.storeScript( - "3", - StoredScriptSource.parse(new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"ghi\"}}}"), XContentType.JSON) - ); + builder.storeScript("1", StoredScriptSource.parse(new BytesArray(""" + {"script":{"lang":"mustache","source":{"foo":"abc"}}}"""), XContentType.JSON)); + builder.storeScript("2", StoredScriptSource.parse(new BytesArray(""" + {"script":{"lang":"mustache","source":{"foo":"def"}}}"""), XContentType.JSON)); + builder.storeScript("3", StoredScriptSource.parse(new BytesArray(""" + {"script":{"lang":"mustache","source":{"foo":"ghi"}}}"""), XContentType.JSON)); ScriptMetadata scriptMetadata1 = builder.build(); builder = new ScriptMetadata.Builder(scriptMetadata1); - builder.storeScript( - "2", - StoredScriptSource.parse( - new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"changed\"}}}"), - XContentType.JSON - ) - ); + builder.storeScript("2", StoredScriptSource.parse(new BytesArray(""" + {"script":{"lang":"mustache","source":{"foo":"changed"}}}"""), XContentType.JSON)); builder.deleteScript("3"); - builder.storeScript( - "4", - StoredScriptSource.parse(new BytesArray("{\"script\":{\"lang\":\"mustache\",\"source\":{\"foo\":\"jkl\"}}}"), XContentType.JSON) - ); + builder.storeScript("4", StoredScriptSource.parse(new BytesArray(""" + {"script":{"lang":"mustache","source":{"foo":"jkl"}}}"""), XContentType.JSON)); ScriptMetadata scriptMetadata2 = builder.build(); ScriptMetadata.ScriptMetadataDiff diff = (ScriptMetadata.ScriptMetadataDiff) scriptMetadata2.diff(scriptMetadata1); @@ -94,10 +81,8 @@ public void testDiff() throws Exception { public void testBuilder() { ScriptMetadata.Builder builder = new ScriptMetadata.Builder(null); - builder.storeScript( - "_id", - StoredScriptSource.parse(new BytesArray("{\"script\": {\"lang\": \"painless\", \"source\": \"1 + 1\"} }"), XContentType.JSON) - ); + builder.storeScript("_id", StoredScriptSource.parse(new BytesArray(""" + {"script": {"lang": "painless", "source": "1 + 1"} }"""), XContentType.JSON)); ScriptMetadata result = builder.build(); assertEquals("1 + 1", result.getStoredScript("_id").getSource()); diff --git a/server/src/test/java/org/elasticsearch/script/ScriptServiceTests.java b/server/src/test/java/org/elasticsearch/script/ScriptServiceTests.java index 806d9cc6c8420..d5d04af0c5976 100644 --- a/server/src/test/java/org/elasticsearch/script/ScriptServiceTests.java +++ b/server/src/test/java/org/elasticsearch/script/ScriptServiceTests.java @@ -432,11 +432,8 @@ public void testStoreScript() throws Exception { } public void testDeleteScript() throws Exception { - ScriptMetadata scriptMetadata = ScriptMetadata.putStoredScript( - null, - "_id", - StoredScriptSource.parse(new BytesArray("{\"script\": {\"lang\": \"_lang\", \"source\": \"abc\"} }"), XContentType.JSON) - ); + ScriptMetadata scriptMetadata = ScriptMetadata.putStoredScript(null, "_id", StoredScriptSource.parse(new BytesArray(""" + {"script": {"lang": "_lang", "source": "abc"} }"""), XContentType.JSON)); scriptMetadata = ScriptMetadata.deleteStoredScript(scriptMetadata, "_id"); assertNotNull(scriptMetadata); assertNull(scriptMetadata.getStoredScript("_id")); @@ -456,13 +453,8 @@ public void testGetStoredScript() throws Exception { Metadata.builder() .putCustom( ScriptMetadata.TYPE, - new ScriptMetadata.Builder(null).storeScript( - "_id", - StoredScriptSource.parse( - new BytesArray("{\"script\": {\"lang\": \"_lang\", \"source\": \"abc\"} }"), - XContentType.JSON - ) - ).build() + new ScriptMetadata.Builder(null).storeScript("_id", StoredScriptSource.parse(new BytesArray(""" + {"script": {"lang": "_lang", "source": "abc"} }"""), XContentType.JSON)).build() ) ) .build(); diff --git a/server/src/test/java/org/elasticsearch/script/ScriptStatsTests.java b/server/src/test/java/org/elasticsearch/script/ScriptStatsTests.java index 4bf3b74be7d49..96018f0cd16f4 100644 --- a/server/src/test/java/org/elasticsearch/script/ScriptStatsTests.java +++ b/server/src/test/java/org/elasticsearch/script/ScriptStatsTests.java @@ -37,37 +37,38 @@ public void testXContent() throws IOException { stats.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - String expected = "{\n" - + " \"script\" : {\n" - + " \"compilations\" : 1100,\n" - + " \"cache_evictions\" : 2211,\n" - + " \"compilation_limit_triggered\" : 3322,\n" - + " \"contexts\" : [\n" - + " {\n" - + " \"context\" : \"contextA\",\n" - + " \"compilations\" : 1000,\n" - + " \"cache_evictions\" : 2010,\n" - + " \"compilation_limit_triggered\" : 3020\n" - + " },\n" - + " {\n" - + " \"context\" : \"contextB\",\n" - + " \"compilations\" : 100,\n" - + " \"compilations_history\" : {\n" - + " \"5m\" : 1000,\n" - + " \"15m\" : 1001,\n" - + " \"24h\" : 1002\n" - + " },\n" - + " \"cache_evictions\" : 201,\n" - + " \"cache_evictions_history\" : {\n" - + " \"5m\" : 2000,\n" - + " \"15m\" : 2001,\n" - + " \"24h\" : 2002\n" - + " },\n" - + " \"compilation_limit_triggered\" : 302\n" - + " }\n" - + " ]\n" - + " }\n" - + "}"; + String expected = """ + { + "script" : { + "compilations" : 1100, + "cache_evictions" : 2211, + "compilation_limit_triggered" : 3322, + "contexts" : [ + { + "context" : "contextA", + "compilations" : 1000, + "cache_evictions" : 2010, + "compilation_limit_triggered" : 3020 + }, + { + "context" : "contextB", + "compilations" : 100, + "compilations_history" : { + "5m" : 1000, + "15m" : 1001, + "24h" : 1002 + }, + "cache_evictions" : 201, + "cache_evictions_history" : { + "5m" : 2000, + "15m" : 2001, + "24h" : 2002 + }, + "compilation_limit_triggered" : 302 + } + ] + } + }"""; assertThat(Strings.toString(builder), equalTo(expected)); } @@ -77,12 +78,13 @@ public void testSerializeEmptyTimeSeries() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); stats.toXContent(builder, ToXContent.EMPTY_PARAMS); - String expected = "{\n" - + " \"context\" : \"c\",\n" - + " \"compilations\" : 1111,\n" - + " \"cache_evictions\" : 2222,\n" - + " \"compilation_limit_triggered\" : 3333\n" - + "}"; + String expected = """ + { + "context" : "c", + "compilations" : 1111, + "cache_evictions" : 2222, + "compilation_limit_triggered" : 3333 + }"""; assertThat(Strings.toString(builder), equalTo(expected)); } @@ -91,17 +93,18 @@ public void testSerializeTimeSeries() throws IOException { Function mkContextStats = (ts) -> new ScriptContextStats("c", 3333, new TimeSeries(1111), ts); TimeSeries series = new TimeSeries(0, 0, 5, 2222); - String format = "{\n" - + " \"context\" : \"c\",\n" - + " \"compilations\" : 1111,\n" - + " \"cache_evictions\" : %d,\n" - + " \"cache_evictions_history\" : {\n" - + " \"5m\" : %d,\n" - + " \"15m\" : %d,\n" - + " \"24h\" : %d\n" - + " },\n" - + " \"compilation_limit_triggered\" : 3333\n" - + "}"; + String format = """ + { + "context" : "c", + "compilations" : 1111, + "cache_evictions" : %s, + "cache_evictions_history" : { + "5m" : %s, + "15m" : %s, + "24h" : %s + }, + "compilation_limit_triggered" : 3333 + }"""; XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); mkContextStats.apply(series).toXContent(builder, ToXContent.EMPTY_PARAMS); diff --git a/server/src/test/java/org/elasticsearch/search/NestedIdentityTests.java b/server/src/test/java/org/elasticsearch/search/NestedIdentityTests.java index 13ad7fa09fa08..8bca9481529d9 100644 --- a/server/src/test/java/org/elasticsearch/search/NestedIdentityTests.java +++ b/server/src/test/java/org/elasticsearch/search/NestedIdentityTests.java @@ -61,10 +61,13 @@ public void testToXContent() throws IOException { builder.startObject(); nestedIdentity.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals( - "{\n" + " \"_nested\" : {\n" + " \"field\" : \"foo\",\n" + " \"offset\" : 5\n" + " }\n" + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "_nested" : { + "field" : "foo", + "offset" : 5 + } + }""", Strings.toString(builder)); nestedIdentity = new NestedIdentity("foo", 5, new NestedIdentity("bar", 3, null)); builder = JsonXContent.contentBuilder(); @@ -72,19 +75,17 @@ public void testToXContent() throws IOException { builder.startObject(); nestedIdentity.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals( - "{\n" - + " \"_nested\" : {\n" - + " \"field\" : \"foo\",\n" - + " \"offset\" : 5,\n" - + " \"_nested\" : {\n" - + " \"field\" : \"bar\",\n" - + " \"offset\" : 3\n" - + " }\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "_nested" : { + "field" : "foo", + "offset" : 5, + "_nested" : { + "field" : "bar", + "offset" : 3 + } + } + }""", Strings.toString(builder)); } /** diff --git a/server/src/test/java/org/elasticsearch/search/SearchHitTests.java b/server/src/test/java/org/elasticsearch/search/SearchHitTests.java index 0301c032f6fa7..0e5a8187ec470 100644 --- a/server/src/test/java/org/elasticsearch/search/SearchHitTests.java +++ b/server/src/test/java/org/elasticsearch/search/SearchHitTests.java @@ -215,7 +215,8 @@ public void testToXContent() throws IOException { searchHit.score(1.5f); XContentBuilder builder = JsonXContent.contentBuilder(); searchHit.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals("{\"_id\":\"id1\",\"_score\":1.5}", Strings.toString(builder)); + assertEquals(""" + {"_id":"id1","_score":1.5}""", Strings.toString(builder)); } public void testSerializeShardTarget() throws Exception { @@ -288,17 +289,15 @@ public void testHasSource() { public void testWeirdScriptFields() throws Exception { { - XContentParser parser = createParser( - XContentType.JSON.xContent(), - "{\n" - + " \"_index\": \"twitter\",\n" - + " \"_id\": \"1\",\n" - + " \"_score\": 1.0,\n" - + " \"fields\": {\n" - + " \"result\": [null]\n" - + " }\n" - + "}" - ); + XContentParser parser = createParser(XContentType.JSON.xContent(), """ + { + "_index": "twitter", + "_id": "1", + "_score": 1.0, + "fields": { + "result": [null] + } + }"""); SearchHit searchHit = SearchHit.fromXContent(parser); Map fields = searchHit.getFields(); assertEquals(1, fields.size()); @@ -308,17 +307,15 @@ public void testWeirdScriptFields() throws Exception { assertNull(result.getValues().get(0)); } { - XContentParser parser = createParser( - XContentType.JSON.xContent(), - "{\n" - + " \"_index\": \"twitter\",\n" - + " \"_id\": \"1\",\n" - + " \"_score\": 1.0,\n" - + " \"fields\": {\n" - + " \"result\": [{}]\n" - + " }\n" - + "}" - ); + XContentParser parser = createParser(XContentType.JSON.xContent(), """ + { + "_index": "twitter", + "_id": "1", + "_score": 1.0, + "fields": { + "result": [{}] + } + }"""); SearchHit searchHit = SearchHit.fromXContent(parser); Map fields = searchHit.getFields(); @@ -332,19 +329,17 @@ public void testWeirdScriptFields() throws Exception { assertEquals(0, map.size()); } { - XContentParser parser = createParser( - JsonXContent.jsonXContent, - "{\n" - + " \"_index\": \"twitter\",\n" - + " \"_id\": \"1\",\n" - + " \"_score\": 1.0,\n" - + " \"fields\": {\n" - + " \"result\": [\n" - + " []\n" - + " ]\n" - + " }\n" - + "}" - ); + XContentParser parser = createParser(JsonXContent.jsonXContent, """ + { + "_index": "twitter", + "_id": "1", + "_score": 1.0, + "fields": { + "result": [ + [] + ] + } + }"""); SearchHit searchHit = SearchHit.fromXContent(parser); Map fields = searchHit.getFields(); diff --git a/server/src/test/java/org/elasticsearch/search/SearchHitsTests.java b/server/src/test/java/org/elasticsearch/search/SearchHitsTests.java index 8ac2efd128c9f..ddf7efb5f1f91 100644 --- a/server/src/test/java/org/elasticsearch/search/SearchHitsTests.java +++ b/server/src/test/java/org/elasticsearch/search/SearchHitsTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.lucene.LuceneTests; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.test.AbstractSerializingTestCase; @@ -237,12 +238,17 @@ public void testToXContent() throws IOException { builder.startObject(); searchHits.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals( - "{\"hits\":{\"total\":{\"value\":1000,\"relation\":\"eq\"},\"max_score\":1.5," - + "\"hits\":[{\"_id\":\"id1\",\"_score\":null}," - + "{\"_id\":\"id2\",\"_score\":null}]}}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "hits": { + "total": { + "value": 1000, + "relation": "eq" + }, + "max_score": 1.5, + "hits": [ { "_id": "id1", "_score": null }, { "_id": "id2", "_score": null } ] + } + }"""), Strings.toString(builder)); } public void testFromXContentWithShards() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java b/server/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java index d9b7c5d951f72..06c65bea5b7da 100644 --- a/server/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java +++ b/server/src/test/java/org/elasticsearch/search/SearchSortValuesTests.java @@ -105,7 +105,8 @@ public void testToXContent() throws IOException { builder.startObject(); sortValues.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals("{\"sort\":[1,\"foo\",3.0]}", Strings.toString(builder)); + assertEquals(""" + {"sort":[1,"foo",3.0]}""", Strings.toString(builder)); } { SearchSortValues sortValues = new SearchSortValues(new Object[0]); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java index f053977fcd712..f51615c6f41a0 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeTests.java @@ -62,13 +62,14 @@ protected DateRangeAggregationBuilder createTestAggregatorBuilder() { } public void testParsingRangeStrict() throws IOException { - final String rangeAggregation = "{\n" - + "\"field\" : \"date\",\n" - + "\"format\" : \"yyyy-MM-dd\",\n" - + "\"ranges\" : [\n" - + " { \"from\" : \"2017-01-01\", \"to\" : \"2017-01-02\", \"badField\" : \"abcd\" }\n" - + "]\n" - + "}"; + final String rangeAggregation = """ + { + "field" : "date", + "format" : "yyyy-MM-dd", + "ranges" : [ + { "from" : "2017-01-01", "to" : "2017-01-02", "badField" : "abcd" } + ] + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation); XContentParseException ex = expectThrows( XContentParseException.class, diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GeoDistanceRangeTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GeoDistanceRangeTests.java index 249b477fa4ba5..8bde97345c44a 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GeoDistanceRangeTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GeoDistanceRangeTests.java @@ -63,14 +63,15 @@ protected GeoDistanceAggregationBuilder createTestAggregatorBuilder() { } public void testParsingRangeStrict() throws IOException { - final String rangeAggregation = "{\n" - + "\"field\" : \"location\",\n" - + "\"origin\" : \"52.3760, 4.894\",\n" - + "\"unit\" : \"m\",\n" - + "\"ranges\" : [\n" - + " { \"from\" : 10000, \"to\" : 20000, \"badField\" : \"abcd\" }\n" - + "]\n" - + "}"; + final String rangeAggregation = """ + { + "field" : "location", + "origin" : "52.3760, 4.894", + "unit" : "m", + "ranges" : [ + { "from" : 10000, "to" : 20000, "badField" : "abcd" } + ] + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation); XContentParseException ex = expectThrows( XContentParseException.class, @@ -84,14 +85,15 @@ public void testParsingRangeStrict() throws IOException { * We never render "null" values to xContent, but we should test that we can parse them (and they return correct defaults) */ public void testParsingNull() throws IOException { - final String rangeAggregation = "{\n" - + "\"field\" : \"location\",\n" - + "\"origin\" : \"52.3760, 4.894\",\n" - + "\"unit\" : \"m\",\n" - + "\"ranges\" : [\n" - + " { \"from\" : null, \"to\" : null }\n" - + "]\n" - + "}"; + final String rangeAggregation = """ + { + "field" : "location", + "origin" : "52.3760, 4.894", + "unit" : "m", + "ranges" : [ + { "from" : null, "to" : null } + ] + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation); GeoDistanceAggregationBuilder aggregationBuilder = (GeoDistanceAggregationBuilder) GeoDistanceAggregationBuilder.parse( "aggregationName", diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java index 0f550c1b302b0..0244cf523eea8 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/RangeTests.java @@ -59,12 +59,13 @@ protected RangeAggregationBuilder createTestAggregatorBuilder() { } public void testParsingRangeStrict() throws IOException { - final String rangeAggregation = "{\n" - + "\"field\" : \"price\",\n" - + "\"ranges\" : [\n" - + " { \"from\" : 50, \"to\" : 100, \"badField\" : \"abcd\" }\n" - + "]\n" - + "}"; + final String rangeAggregation = """ + { + "field" : "price", + "ranges" : [ + { "from" : 50, "to" : 100, "badField" : "abcd" } + ] + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation); XContentParseException ex = expectThrows( XContentParseException.class, @@ -78,12 +79,13 @@ public void testParsingRangeStrict() throws IOException { * We never render "null" values to xContent, but we should test that we can parse them (and they return correct defaults) */ public void testParsingNull() throws IOException { - final String rangeAggregation = "{\n" - + "\"field\" : \"price\",\n" - + "\"ranges\" : [\n" - + " { \"from\" : null, \"to\" : null }\n" - + "]\n" - + "}"; + final String rangeAggregation = """ + { + "field" : "price", + "ranges" : [ + { "from" : null, "to" : null } + ] + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, rangeAggregation); RangeAggregationBuilder aggregationBuilder = RangeAggregationBuilder.PARSER.parse(parser, "aggregationName"); assertEquals(1, aggregationBuilder.ranges().size()); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java index 910a375d635dd..414fab480d412 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java @@ -23,10 +23,8 @@ public class GeoHashGridParserTests extends ESTestCase { public void testParseValidFromInts() throws Exception { int precision = randomIntBetween(1, 12); - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\":" + precision + ", \"size\": 500, \"shard_size\": 550}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision":%s, "size": 500, "shard_size": 550}""".formatted(precision)); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); // can create a factory @@ -35,10 +33,9 @@ public void testParseValidFromInts() throws Exception { public void testParseValidFromStrings() throws Exception { int precision = randomIntBetween(1, 12); - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\":\"" + precision + "\", \"size\": \"500\", \"shard_size\": \"550\"}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision":"%s", "size": "500", "shard_size": "550"} + """.formatted(precision)); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); // can create a factory @@ -52,10 +49,9 @@ public void testParseDistanceUnitPrecision() throws Exception { distance = 5600 + randomDouble(); // 5.6cm is approx. smallest distance represented by precision 12 } String distanceString = distance + unit.toString(); - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\": \"" + distanceString + "\", \"size\": \"500\", \"shard_size\": \"550\"}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision": "%s", "size": "500", "shard_size": "550"} + """.formatted(distanceString)); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); // can create a factory @@ -66,10 +62,8 @@ public void testParseDistanceUnitPrecision() throws Exception { } public void testParseInvalidUnitPrecision() throws Exception { - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\": \"10kg\", \"size\": \"500\", \"shard_size\": \"550\"}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision": "10kg", "size": "500", "shard_size": "550"}"""); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); XContentParseException ex = expectThrows( @@ -82,10 +76,8 @@ public void testParseInvalidUnitPrecision() throws Exception { } public void testParseDistanceUnitPrecisionTooSmall() throws Exception { - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\": \"1cm\", \"size\": \"500\", \"shard_size\": \"550\"}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision": "1cm", "size": "500", "shard_size": "550"}"""); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); XContentParseException ex = expectThrows( @@ -109,7 +101,8 @@ public void testParseErrorOnBooleanPrecision() throws Exception { } public void testParseErrorOnPrecisionOutOfRange() throws Exception { - XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"13\"}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision":"13"}"""); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); try { @@ -123,24 +116,19 @@ public void testParseErrorOnPrecisionOutOfRange() throws Exception { public void testParseValidBounds() throws Exception { Rectangle bbox = GeometryTestUtils.randomRectangle(); - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\": 5, \"size\": 500, \"shard_size\": 550," - + "\"bounds\": { " - + "\"top\": " - + bbox.getMaxY() - + "," - + "\"bottom\": " - + bbox.getMinY() - + "," - + "\"left\": " - + bbox.getMinX() - + "," - + "\"right\": " - + bbox.getMaxX() - + "}" - + "}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + { + "field": "my_loc", + "precision": 5, + "size": 500, + "shard_size": 550, + "bounds": { + "top": %s, + "bottom": %s, + "left": %s, + "right": %s + } + }""".formatted(bbox.getMaxY(), bbox.getMinY(), bbox.getMinX(), bbox.getMaxX())); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); // can create a factory diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileGridParserTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileGridParserTests.java index 1a12a331bfce0..a637ed0c081a5 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileGridParserTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileGridParserTests.java @@ -21,10 +21,8 @@ public class GeoTileGridParserTests extends ESTestCase { public void testParseValidFromInts() throws Exception { int precision = randomIntBetween(0, GeoTileUtils.MAX_ZOOM); - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\":" + precision + ", \"size\": 500, \"shard_size\": 550}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision":%s, "size": 500, "shard_size": 550}""".formatted(precision)); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); // can create a factory @@ -33,10 +31,9 @@ public void testParseValidFromInts() throws Exception { public void testParseValidFromStrings() throws Exception { int precision = randomIntBetween(0, GeoTileUtils.MAX_ZOOM); - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\":\"" + precision + "\", \"size\": \"500\", \"shard_size\": \"550\"}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision":"%s", "size": "500", "shard_size": "550"} + """.formatted(precision)); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); // can create a factory @@ -44,7 +41,8 @@ public void testParseValidFromStrings() throws Exception { } public void testParseErrorOnBooleanPrecision() throws Exception { - XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":false}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision":false}"""); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); XContentParseException e = expectThrows( @@ -58,7 +56,8 @@ public void testParseErrorOnBooleanPrecision() throws Exception { } public void testParseErrorOnPrecisionOutOfRange() throws Exception { - XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"30\"}"); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + {"field":"my_loc", "precision":"30"}"""); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); try { @@ -72,24 +71,19 @@ public void testParseErrorOnPrecisionOutOfRange() throws Exception { public void testParseValidBounds() throws Exception { Rectangle bbox = GeometryTestUtils.randomRectangle(); - XContentParser stParser = createParser( - JsonXContent.jsonXContent, - "{\"field\":\"my_loc\", \"precision\": 5, \"size\": 500, \"shard_size\": 550," - + "\"bounds\": { " - + "\"top\": " - + bbox.getMaxY() - + "," - + "\"bottom\": " - + bbox.getMinY() - + "," - + "\"left\": " - + bbox.getMinX() - + "," - + "\"right\": " - + bbox.getMaxX() - + "}" - + "}" - ); + XContentParser stParser = createParser(JsonXContent.jsonXContent, """ + { + "field": "my_loc", + "precision": 5, + "size": 500, + "shard_size": 550, + "bounds": { + "top": %s, + "bottom": %s, + "left": %s, + "right": %s + } + }""".formatted(bbox.getMaxY(), bbox.getMinY(), bbox.getMinX(), bbox.getMaxX())); XContentParser.Token token = stParser.nextToken(); assertSame(XContentParser.Token.START_OBJECT, token); // can create a factory diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilderTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilderTests.java index 68a2b6f35b3b5..d4cca6bed8c29 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilderTests.java @@ -99,9 +99,8 @@ protected RangeAggregationBuilder mutateInstance(RangeAggregationBuilder builder } public void testNumericKeys() throws IOException { - RangeAggregationBuilder builder = doParseInstance( - createParser(JsonXContent.jsonXContent, "{\"test\":{\"range\":{\"field\":\"f\",\"ranges\":[{\"key\":1,\"to\":0}]}}}") - ); + RangeAggregationBuilder builder = doParseInstance(createParser(JsonXContent.jsonXContent, """ + {"test":{"range":{"field":"f","ranges":[{"key":1,"to":0}]}}}""")); assertThat(builder.getName(), equalTo("test")); assertThat(builder.field(), equalTo("f")); assertThat(builder.ranges, equalTo(List.of(new RangeAggregator.Range("1", null, 0d)))); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java index a6c1adcc6f9e9..32286c2b501d7 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java @@ -589,7 +589,8 @@ private void addMixedTextDocs(IndexWriter w) throws IOException { } doc.add(new Field("text", text.toString(), TextFieldMapper.Defaults.FIELD_TYPE)); - String json = "{ \"text\" : \"" + text.toString() + "\" }"; + String json = """ + { "text" : "%s" }""".formatted(text.toString()); doc.add(new StoredField("_source", new BytesRef(json))); w.addDocument(doc); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java index dcdd6436c4cad..e2bc0b16a2f51 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java @@ -315,7 +315,8 @@ public void testSignificanceOnTextArrays() throws IOException { for (int i = 0; i < 10; i++) { Document doc = new Document(); doc.add(new Field("text", "foo", TextFieldMapper.Defaults.FIELD_TYPE)); - String json = "{ \"text\" : [\"foo\",\"foo\"], \"title\" : [\"foo\", \"foo\"]}"; + String json = """ + { "text" : ["foo","foo"], "title" : ["foo", "foo"]}"""; doc.add(new StoredField("_source", new BytesRef(json))); w.addDocument(doc); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesTestCase.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesTestCase.java index 773e5f5881cc6..accaf2838717e 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesTestCase.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AbstractPercentilesTestCase.java @@ -112,30 +112,32 @@ public void testEmptyRanksXContent() throws IOException { builder.endObject(); String expected; if (keyed) { - expected = "{\n" - + " \"values\" : {\n" - + " \"1.0\" : null,\n" - + " \"2.0\" : null,\n" - + " \"3.0\" : null\n" - + " }\n" - + "}"; + expected = """ + { + "values" : { + "1.0" : null, + "2.0" : null, + "3.0" : null + } + }"""; } else { - expected = "{\n" - + " \"values\" : [\n" - + " {\n" - + " \"key\" : 1.0,\n" - + " \"value\" : null\n" - + " },\n" - + " {\n" - + " \"key\" : 2.0,\n" - + " \"value\" : null\n" - + " },\n" - + " {\n" - + " \"key\" : 3.0,\n" - + " \"value\" : null\n" - + " }\n" - + " ]\n" - + "}"; + expected = """ + { + "values" : [ + { + "key" : 1.0, + "value" : null + }, + { + "key" : 2.0, + "value" : null + }, + { + "key" : 3.0, + "value" : null + } + ] + }"""; } assertThat(Strings.toString(builder), equalTo(expected)); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java index 59587468b55bc..9219dc88ce886 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.search.aggregations.metrics; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.ParsedAggregation; @@ -207,43 +208,41 @@ public void testDoXContentBody() throws IOException { int count = randomIntBetween(1, 10); DocValueFormat format = randomNumericDocValueFormat(); InternalStats internalStats = createInstance("stats", count, sum, min, max, format, null); - XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); + XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); internalStats.doXContentBody(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - String expected = "{\n" - + " \"count\" : " - + count - + ",\n" - + " \"min\" : " - + min - + ",\n" - + " \"max\" : " - + max - + ",\n" - + " \"avg\" : " - + internalStats.getAvg() - + ",\n" - + " \"sum\" : " - + sum; - if (format != DocValueFormat.RAW) { - expected += ",\n" - + " \"min_as_string\" : \"" - + format.format(internalStats.getMin()) - + "\",\n" - + " \"max_as_string\" : \"" - + format.format(internalStats.getMax()) - + "\",\n" - + " \"avg_as_string\" : \"" - + format.format(internalStats.getAvg()) - + "\",\n" - + " \"sum_as_string\" : \"" - + format.format(internalStats.getSum()) - + "\""; - } - expected += "\n}"; - assertEquals(expected, Strings.toString(builder)); + String expected = """ + { + "count" : %s, + "min" : %s, + "max" : %s, + "avg" : %s, + "sum" : %s + %s + }""".formatted( + count, + min, + max, + internalStats.getAvg(), + sum, + format != DocValueFormat.RAW + ? """ + , + "min_as_string" : "%s", + "max_as_string" : "%s", + "avg_as_string" : "%s", + "sum_as_string" : "%s" + """.formatted( + format.format(internalStats.getMin()), + format.format(internalStats.getMax()), + format.format(internalStats.getAvg()), + format.format(internalStats.getSum()) + ) + : "" + ); + assertEquals(XContentHelper.stripWhitespace(expected), Strings.toString(builder)); // count is zero format = randomNumericDocValueFormat(); @@ -252,21 +251,19 @@ public void testDoXContentBody() throws IOException { sum = 0.0; count = 0; internalStats = createInstance("stats", count, sum, min, max, format, null); - builder = JsonXContent.contentBuilder().prettyPrint(); + builder = JsonXContent.contentBuilder(); builder.startObject(); internalStats.doXContentBody(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals( - "{\n" - + " \"count\" : 0,\n" - + " \"min\" : null,\n" - + " \"max\" : null,\n" - + " \"avg\" : null,\n" - + " \"sum\" : 0.0\n" - + "}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "count" : 0, + "min" : null, + "max" : null, + "avg" : null, + "sum" : 0.0 + }"""), Strings.toString(builder)); } public void testIterator() { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/PercentilesTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/PercentilesTests.java index 2798d6d83310e..4a986c374f9cf 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/PercentilesTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/PercentilesTests.java @@ -74,18 +74,19 @@ public void testDuplicatePercentilesThrows() throws IOException { } public void testExceptionMultipleMethods() throws IOException { - final String illegalAgg = "{\n" - + " \"percentiles\": {\n" - + " \"field\": \"load_time\",\n" - + " \"percents\": [99],\n" - + " \"tdigest\": {\n" - + " \"compression\": 200\n" - + " },\n" - + " \"hdr\": {\n" - + " \"number_of_significant_value_digits\": 3\n" - + " }\n" - + " }\n" - + "}"; + final String illegalAgg = """ + { + "percentiles": { + "field": "load_time", + "percents": [99], + "tdigest": { + "compression": 200 + }, + "hdr": { + "number_of_significant_value_digits": 3 + } + } + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, illegalAgg); assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java index ef35f3630f122..7ace162b26b7a 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TopHitsTests.java @@ -173,25 +173,26 @@ protected final TopHitsAggregationBuilder createTestAggregatorBuilder() { } public void testFailWithSubAgg() throws Exception { - String source = "{\n" - + " \"top-tags\": {\n" - + " \"terms\": {\n" - + " \"field\": \"tags\"\n" - + " },\n" - + " \"aggs\": {\n" - + " \"top_tags_hits\": {\n" - + " \"top_hits\": {},\n" - + " \"aggs\": {\n" - + " \"max\": {\n" - + " \"max\": {\n" - + " \"field\": \"age\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String source = """ + { + "top-tags": { + "terms": { + "field": "tags" + }, + "aggs": { + "top_tags_hits": { + "top_hits": {}, + "aggs": { + "max": { + "max": { + "field": "age" + } + } + } + } + } + } + }"""; XContentParser parser = createParser(JsonXContent.jsonXContent, source); assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); Exception e = expectThrows(AggregationInitializationException.class, () -> AggregatorFactories.parseAggregators(parser)); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalPercentilesBucketTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalPercentilesBucketTests.java index 547536e0c32c7..96fa55216beda 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalPercentilesBucketTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/InternalPercentilesBucketTests.java @@ -151,30 +151,32 @@ public void testEmptyRanksXContent() throws IOException { builder.endObject(); String expected; if (keyed) { - expected = "{\n" - + " \"values\" : {\n" - + " \"1.0\" : null,\n" - + " \"2.0\" : null,\n" - + " \"3.0\" : null\n" - + " }\n" - + "}"; + expected = """ + { + "values" : { + "1.0" : null, + "2.0" : null, + "3.0" : null + } + }"""; } else { - expected = "{\n" - + " \"values\" : [\n" - + " {\n" - + " \"key\" : 1.0,\n" - + " \"value\" : null\n" - + " },\n" - + " {\n" - + " \"key\" : 2.0,\n" - + " \"value\" : null\n" - + " },\n" - + " {\n" - + " \"key\" : 3.0,\n" - + " \"value\" : null\n" - + " }\n" - + " ]\n" - + "}"; + expected = """ + { + "values" : [ + { + "key" : 1.0, + "value" : null + }, + { + "key" : 2.0, + "value" : null + }, + { + "key" : 3.0, + "value" : null + } + ] + }"""; } assertThat(Strings.toString(builder), equalTo(expected)); diff --git a/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java b/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java index 73e6feb35c9e2..8d33ef9e8a74c 100644 --- a/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/builder/SearchSourceBuilderTests.java @@ -120,7 +120,9 @@ private SearchSourceBuilder copyBuilder(SearchSourceBuilder original, Version ve public void testParseIncludeExclude() throws IOException { { - String restContent = " { \"_source\": { \"includes\": \"include\", \"excludes\": \"*.field2\"}}"; + String restContent = """ + { "_source": { "includes": "include", "excludes": "*.field2"}} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); assertArrayEquals(new String[] { "*.field2" }, searchSourceBuilder.fetchSource().excludes()); @@ -139,17 +141,18 @@ public void testParseIncludeExclude() throws IOException { } public void testMultipleQueryObjectsAreRejected() throws Exception { - String restContent = " { \"query\": {\n" - + " \"multi_match\": {\n" - + " \"query\": \"workd\",\n" - + " \"fields\": [\"title^5\", \"plain_body\"]\n" - + " },\n" - + " \"filters\": {\n" - + " \"terms\": {\n" - + " \"status\": [ 3 ]\n" - + " }\n" - + " }\n" - + " } }"; + String restContent = """ + { "query": { + "multi_match": { + "query": "workd", + "fields": ["title^5", "plain_body"] + }, + "filters": { + "terms": { + "status": [ 3 ] + } + } + } }""".indent(1); try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { ParsingException e = expectThrows(ParsingException.class, () -> SearchSourceBuilder.fromXContent(parser)); assertEquals("[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]", e.getMessage()); @@ -157,44 +160,45 @@ public void testMultipleQueryObjectsAreRejected() throws Exception { } public void testParseAndRewrite() throws IOException { - String restContent = "{\n" - + " \"query\": {\n" - + " \"bool\": {\n" - + " \"must\": {\n" - + " \"match_none\": {}\n" - + " }\n" - + " }\n" - + " },\n" - + " \"rescore\": {\n" - + " \"window_size\": 50,\n" - + " \"query\": {\n" - + " \"rescore_query\": {\n" - + " \"bool\": {\n" - + " \"must\": {\n" - + " \"match_none\": {}\n" - + " }\n" - + " }\n" - + " },\n" - + " \"rescore_query_weight\": 10\n" - + " }\n" - + " },\n" - + " \"highlight\": {\n" - + " \"order\": \"score\",\n" - + " \"fields\": {\n" - + " \"content\": {\n" - + " \"fragment_size\": 150,\n" - + " \"number_of_fragments\": 3,\n" - + " \"highlight_query\": {\n" - + " \"bool\": {\n" - + " \"must\": {\n" - + " \"match_none\": {}\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String restContent = """ + { + "query": { + "bool": { + "must": { + "match_none": {} + } + } + }, + "rescore": { + "window_size": 50, + "query": { + "rescore_query": { + "bool": { + "must": { + "match_none": {} + } + } + }, + "rescore_query_weight": 10 + } + }, + "highlight": { + "order": "score", + "fields": { + "content": { + "fragment_size": 150, + "number_of_fragments": 3, + "highlight_query": { + "bool": { + "must": { + "match_none": {} + } + } + } + } + } + } + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); assertThat(searchSourceBuilder.query(), instanceOf(BoolQueryBuilder.class)); @@ -232,13 +236,14 @@ public void testParseSort() throws IOException { } { - String restContent = "{\"sort\" : [\n" - + " { \"post_date\" : {\"order\" : \"asc\"}},\n" - + " \"user\",\n" - + " { \"name\" : \"desc\" },\n" - + " { \"age\" : \"desc\" },\n" - + " \"_score\"\n" - + " ]}"; + String restContent = """ + {"sort" : [ + { "post_date" : {"order" : "asc"}}, + "user", + { "name" : "desc" }, + { "age" : "desc" }, + "_score" + ]}"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); searchSourceBuilder = rewrite(searchSourceBuilder); @@ -254,17 +259,16 @@ public void testParseSort() throws IOException { public void testAggsParsing() throws IOException { { - String restContent = "{\n" - + " " - + "\"aggs\": {" - + " \"test_agg\": {\n" - + " " - + "\"terms\" : {\n" - + " \"field\": \"foo\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n"; + String restContent = """ + { + "aggs": { "test_agg": { + "terms" : { + "field": "foo" + } + } + } + } + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); searchSourceBuilder = rewrite(searchSourceBuilder); @@ -272,15 +276,16 @@ public void testAggsParsing() throws IOException { } } { - String restContent = "{\n" - + " \"aggregations\": {" - + " \"test_agg\": {\n" - + " \"terms\" : {\n" - + " \"field\": \"foo\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n"; + String restContent = """ + { + "aggregations": { "test_agg": { + "terms" : { + "field": "foo" + } + } + } + } + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); searchSourceBuilder = rewrite(searchSourceBuilder); @@ -294,19 +299,21 @@ public void testAggsParsing() throws IOException { */ public void testParseRescore() throws IOException { { - String restContent = "{\n" - + " \"query\" : {\n" - + " \"match\": { \"content\": { \"query\": \"foo bar\" }}\n" - + " },\n" - + " \"rescore\": {" - + " \"window_size\": 50,\n" - + " \"query\": {\n" - + " \"rescore_query\" : {\n" - + " \"match\": { \"content\": { \"query\": \"baz\" } }\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n"; + String restContent = """ + { + "query" : { + "match": { "content": { "query": "foo bar" }} + }, + "rescore": { + "window_size": 50, + "query": { + "rescore_query" : { + "match": { "content": { "query": "baz" } } + } + } + } + } + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); searchSourceBuilder = rewrite(searchSourceBuilder); @@ -319,19 +326,21 @@ public void testParseRescore() throws IOException { } { - String restContent = "{\n" - + " \"query\" : {\n" - + " \"match\": { \"content\": { \"query\": \"foo bar\" }}\n" - + " },\n" - + " \"rescore\": [ {" - + " \"window_size\": 50,\n" - + " \"query\": {\n" - + " \"rescore_query\" : {\n" - + " \"match\": { \"content\": { \"query\": \"baz\" } }\n" - + " }\n" - + " }\n" - + " } ]\n" - + "}\n"; + String restContent = """ + { + "query" : { + "match": { "content": { "query": "foo bar" }} + }, + "rescore": [ { + "window_size": 50, + "query": { + "rescore_query" : { + "match": { "content": { "query": "baz" } } + } + } + } ] + } + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); searchSourceBuilder = rewrite(searchSourceBuilder); @@ -346,7 +355,9 @@ public void testParseRescore() throws IOException { public void testTimeoutWithUnits() throws IOException { final String timeout = randomTimeValue(); - final String query = "{ \"query\": { \"match_all\": {}}, \"timeout\": \"" + timeout + "\"}"; + final String query = """ + { "query": { "match_all": {}}, "timeout": "%s"} + """.formatted(timeout); try (XContentParser parser = createParser(JsonXContent.jsonXContent, query)) { final SearchSourceBuilder builder = SearchSourceBuilder.fromXContent(parser); assertThat(builder.timeout(), equalTo(TimeValue.parseTimeValue(timeout, null, "timeout"))); @@ -355,7 +366,9 @@ public void testTimeoutWithUnits() throws IOException { public void testTimeoutWithoutUnits() throws IOException { final int timeout = randomIntBetween(1, 1024); - final String query = "{ \"query\": { \"match_all\": {}}, \"timeout\": \"" + timeout + "\"}"; + final String query = """ + { "query": { "match_all": {}}, "timeout": "%s"} + """.formatted(timeout); try (XContentParser parser = createParser(JsonXContent.jsonXContent, query)) { final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> SearchSourceBuilder.fromXContent(parser)); assertThat(e, hasToString(containsString("unit is missing or unrecognized"))); @@ -409,7 +422,8 @@ public void testToXContentWithPointInTime() throws IOException { public void testParseIndicesBoost() throws IOException { { - String restContent = " { \"indices_boost\": {\"foo\": 1.0, \"bar\": 2.0}}"; + String restContent = """ + { "indices_boost": {"foo": 1.0, "bar": 2.0}}"""; try (XContentParser parser = createParserWithCompatibilityFor(JsonXContent.jsonXContent, restContent, RestApiVersion.V_7)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); assertEquals(2, searchSourceBuilder.indexBoosts().size()); @@ -420,12 +434,10 @@ public void testParseIndicesBoost() throws IOException { } { - String restContent = "{" - + " \"indices_boost\" : [\n" - + " { \"foo\" : 1.0 },\n" - + " { \"bar\" : 2.0 },\n" - + " { \"baz\" : 3.0 }\n" - + " ]}"; + String restContent = """ + { + "indices_boost": [ { "foo": 1 }, { "bar": 2 }, { "baz": 3 } ] + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, restContent)) { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.fromXContent(parser); assertEquals(3, searchSourceBuilder.indexBoosts().size()); @@ -436,29 +448,41 @@ public void testParseIndicesBoost() throws IOException { } { - String restContent = "{" + " \"indices_boost\" : [\n" + " { \"foo\" : 1.0, \"bar\": 2.0}\n" + // invalid format - " ]}"; + // invalid format + String restContent = """ + { + "indices_boost": [ { "foo": 1, "bar": 2 } ] + }"""; assertIndicesBoostParseErrorMessage(restContent, "Expected [END_OBJECT] in [indices_boost] but found [FIELD_NAME]"); } { - String restContent = "{" + " \"indices_boost\" : [\n" + " {}\n" + // invalid format - " ]}"; + // invalid format + String restContent = """ + { + "indices_boost": [ {} ] + }"""; assertIndicesBoostParseErrorMessage(restContent, "Expected [FIELD_NAME] in [indices_boost] but found [END_OBJECT]"); } { - String restContent = "{" + " \"indices_boost\" : [\n" + " { \"foo\" : \"bar\"}\n" + // invalid format - " ]}"; + // invalid format + String restContent = """ + { + "indices_boost": [ { "foo": "bar" } ] + }"""; assertIndicesBoostParseErrorMessage(restContent, "Expected [VALUE_NUMBER] in [indices_boost] but found [VALUE_STRING]"); } { - String restContent = "{" + " \"indices_boost\" : [\n" + " { \"foo\" : {\"bar\": 1}}\n" + // invalid format - " ]}"; + // invalid format + String restContent = """ + { + "indices_boost": [ { "foo": { "bar": 1 } } ] + }"""; assertIndicesBoostParseErrorMessage(restContent, "Expected [VALUE_NUMBER] in [indices_boost] but found [START_OBJECT]"); } diff --git a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java index 128ba3c03b835..3b2ee31d68b6b 100644 --- a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java @@ -149,24 +149,24 @@ public void testFromXContent() throws IOException { */ public void testUnknownArrayNameExpection() throws IOException { { - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" + " \"bad_fieldname\" : [ \"field1\" 1 \"field2\" ]\n" + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "bad_fieldname" : [ "field1" 1 "field2" ] + } + """); assertEquals("[2:5] [highlight] unknown field [bad_fieldname]", e.getMessage()); } { - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" - + " \"fields\" : {\n" - + " \"body\" : {\n" - + " \"bad_fieldname\" : [ \"field1\" , \"field2\" ]\n" - + " }\n" - + " }\n" - + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "fields" : { + "body" : { + "bad_fieldname" : [ "field1" , "field2" ] + } + } + } + """); assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]")); assertThat(e.getCause().getMessage(), containsString("[fields] failed to parse field [body]")); assertEquals("[4:9] [highlight_field] unknown field [bad_fieldname]", e.getCause().getCause().getMessage()); @@ -184,24 +184,24 @@ private T expectParseThrows(Class exceptionClass, Strin */ public void testUnknownFieldnameExpection() throws IOException { { - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" + " \"bad_fieldname\" : \"value\"\n" + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "bad_fieldname" : "value" + } + """); assertEquals("[2:5] [highlight] unknown field [bad_fieldname]", e.getMessage()); } { - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" - + " \"fields\" : {\n" - + " \"body\" : {\n" - + " \"bad_fieldname\" : \"value\"\n" - + " }\n" - + " }\n" - + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "fields" : { + "body" : { + "bad_fieldname" : "value" + } + } + } + """); assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]")); assertThat(e.getCause().getMessage(), containsString("[fields] failed to parse field [body]")); assertEquals("[4:9] [highlight_field] unknown field [bad_fieldname]", e.getCause().getCause().getMessage()); @@ -213,24 +213,24 @@ public void testUnknownFieldnameExpection() throws IOException { */ public void testUnknownObjectFieldnameExpection() throws IOException { { - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" + " \"bad_fieldname\" : { \"field\" : \"value\" }\n \n" + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "bad_fieldname" : { "field" : "value" } + } + """); assertEquals("[2:5] [highlight] unknown field [bad_fieldname]", e.getMessage()); } { - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" - + " \"fields\" : {\n" - + " \"body\" : {\n" - + " \"bad_fieldname\" : { \"field\" : \"value\" }\n" - + " }\n" - + " }\n" - + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "fields" : { + "body" : { + "bad_fieldname" : { "field" : "value" } + } + } + } + """); assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]")); assertThat(e.getCause().getMessage(), containsString("[fields] failed to parse field [body]")); assertEquals("[4:9] [highlight_field] unknown field [bad_fieldname]", e.getCause().getCause().getMessage()); @@ -250,7 +250,11 @@ public void testStringInFieldsArray() throws IOException { } public void testNoFieldsInObjectInFieldsArray() throws IOException { - XContentParseException e = expectParseThrows(XContentParseException.class, "{\n" + " \"fields\" : [ {\n" + " }] \n" + "}\n"); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "fields" : [ { } ] + } + """); assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]")); assertThat( e.getCause().getMessage(), @@ -262,10 +266,14 @@ public void testNoFieldsInObjectInFieldsArray() throws IOException { } public void testTwoFieldsInObjectInFieldsArray() throws IOException { - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" + " \"fields\" : [ {\n" + " \"body\" : {},\n" + " \"nope\" : {}\n" + " }] \n" + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "fields" : [ { + "body" : {}, + "nope" : {} + }]\s + } + """); assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]")); assertThat( e.getCause().getMessage(), @@ -425,7 +433,11 @@ private static Field getFieldBuilderByName(HighlightBuilder highlightBuilder, St */ public void testParsingTagsSchema() throws IOException { - String highlightElement = "{\n" + " \"tags_schema\" : \"styled\"\n" + "}\n"; + String highlightElement = """ + { + "tags_schema" : "styled" + } + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement)) { HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(parser); @@ -440,7 +452,11 @@ public void testParsingTagsSchema() throws IOException { highlightBuilder.postTags() ); - highlightElement = "{\n" + " \"tags_schema\" : \"default\"\n" + "}\n"; + highlightElement = """ + { + "tags_schema" : "default" + } + """; } try (XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement)) { @@ -456,10 +472,11 @@ public void testParsingTagsSchema() throws IOException { highlightBuilder.postTags() ); - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" + " \"tags_schema\" : \"somthing_else\"\n" + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "tags_schema" : "somthing_else" + } + """); assertThat(e.getMessage(), containsString("[highlight] failed to parse field [tags_schema]")); assertEquals("Unknown tag schema [somthing_else]", e.getCause().getMessage()); } @@ -489,13 +506,22 @@ public void testParsingEmptyStructure() throws IOException { } public void testPreTagsWithoutPostTags() throws IOException { - ParsingException err = expectParseThrows(ParsingException.class, "{\n" + " \"pre_tags\" : [\"\"]\n" + "}\n"); + ParsingException err = expectParseThrows(ParsingException.class, """ + { + "pre_tags" : [""] + } + """); assertEquals("pre_tags are set but post_tags are not set", err.getMessage()); - XContentParseException e = expectParseThrows( - XContentParseException.class, - "{\n" + " \"fields\" : {\n" + " \"body\" : {\n" + " \"pre_tags\" : [\"\"]\n" + " }\n" + " }\n" + "}\n" - ); + XContentParseException e = expectParseThrows(XContentParseException.class, """ + { + "fields" : { + "body" : { + "pre_tags" : [""] + } + } + } + """); assertThat(e.getMessage(), containsString("[highlight] failed to parse field [fields]")); assertThat(e.getCause().getMessage(), containsString("[fields] failed to parse field [body]")); assertEquals("pre_tags are set but post_tags are not set", e.getCause().getCause().getMessage()); diff --git a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java index 14e0b5d00f813..f174ae9180522 100644 --- a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java +++ b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightFieldTests.java @@ -72,7 +72,13 @@ public void testToXContent() throws IOException { builder.startObject(); field.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals("{\n" + " \"foo\" : [\n" + " \"bar\",\n" + " \"baz\"\n" + " ]\n" + "}", Strings.toString(builder)); + assertEquals(""" + { + "foo" : [ + "bar", + "baz" + ] + }""", Strings.toString(builder)); field = new HighlightField("foo", null); builder = JsonXContent.contentBuilder(); @@ -80,7 +86,10 @@ public void testToXContent() throws IOException { builder.startObject(); field.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); - assertEquals("{\n" + " \"foo\" : null\n" + "}", Strings.toString(builder)); + assertEquals(""" + { + "foo" : null + }""", Strings.toString(builder)); } /** diff --git a/server/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java b/server/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java index 73fc207d4dff3..b65ecc6eb0be5 100644 --- a/server/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java +++ b/server/src/test/java/org/elasticsearch/search/profile/ProfileResultTests.java @@ -87,111 +87,103 @@ public void testToXContent() throws IOException { ProfileResult result = new ProfileResult("someType", "some description", breakdown, debug, 223456L, children); XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"type\" : \"someType\",\n" - + " \"description\" : \"some description\",\n" - + " \"time_in_nanos\" : 223456,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 123456,\n" - + " \"stuff\" : 10000\n" - + " },\n" - + " \"debug\" : {\n" - + " \"a\" : \"foo\",\n" - + " \"b\" : \"bar\"\n" - + " },\n" - + " \"children\" : [\n" - + " {\n" - + " \"type\" : \"child1\",\n" - + " \"description\" : \"desc1\",\n" - + " \"time_in_nanos\" : 100,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 100\n" - + " }\n" - + " },\n" - + " {\n" - + " \"type\" : \"child2\",\n" - + " \"description\" : \"desc2\",\n" - + " \"time_in_nanos\" : 123356,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 123356\n" - + " }\n" - + " }\n" - + " ]\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "type" : "someType", + "description" : "some description", + "time_in_nanos" : 223456, + "breakdown" : { + "key1" : 123456, + "stuff" : 10000 + }, + "debug" : { + "a" : "foo", + "b" : "bar" + }, + "children" : [ + { + "type" : "child1", + "description" : "desc1", + "time_in_nanos" : 100, + "breakdown" : { + "key1" : 100 + } + }, + { + "type" : "child2", + "description" : "desc2", + "time_in_nanos" : 123356, + "breakdown" : { + "key1" : 123356 + } + } + ] + }""", Strings.toString(builder)); builder = XContentFactory.jsonBuilder().prettyPrint().humanReadable(true); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"type\" : \"someType\",\n" - + " \"description\" : \"some description\",\n" - + " \"time\" : \"223.4micros\",\n" - + " \"time_in_nanos\" : 223456,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 123456,\n" - + " \"stuff\" : 10000\n" - + " },\n" - + " \"debug\" : {\n" - + " \"a\" : \"foo\",\n" - + " \"b\" : \"bar\"\n" - + " },\n" - + " \"children\" : [\n" - + " {\n" - + " \"type\" : \"child1\",\n" - + " \"description\" : \"desc1\",\n" - + " \"time\" : \"100nanos\",\n" - + " \"time_in_nanos\" : 100,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 100\n" - + " }\n" - + " },\n" - + " {\n" - + " \"type\" : \"child2\",\n" - + " \"description\" : \"desc2\",\n" - + " \"time\" : \"123.3micros\",\n" - + " \"time_in_nanos\" : 123356,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 123356\n" - + " }\n" - + " }\n" - + " ]\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "type" : "someType", + "description" : "some description", + "time" : "223.4micros", + "time_in_nanos" : 223456, + "breakdown" : { + "key1" : 123456, + "stuff" : 10000 + }, + "debug" : { + "a" : "foo", + "b" : "bar" + }, + "children" : [ + { + "type" : "child1", + "description" : "desc1", + "time" : "100nanos", + "time_in_nanos" : 100, + "breakdown" : { + "key1" : 100 + } + }, + { + "type" : "child2", + "description" : "desc2", + "time" : "123.3micros", + "time_in_nanos" : 123356, + "breakdown" : { + "key1" : 123356 + } + } + ] + }""", Strings.toString(builder)); result = new ProfileResult("profileName", "some description", Map.of("key1", 12345678L), Map.of(), 12345678L, List.of()); builder = XContentFactory.jsonBuilder().prettyPrint().humanReadable(true); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"type\" : \"profileName\",\n" - + " \"description\" : \"some description\",\n" - + " \"time\" : \"12.3ms\",\n" - + " \"time_in_nanos\" : 12345678,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 12345678\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "type" : "profileName", + "description" : "some description", + "time" : "12.3ms", + "time_in_nanos" : 12345678, + "breakdown" : { + "key1" : 12345678 + } + }""", Strings.toString(builder)); result = new ProfileResult("profileName", "some description", Map.of("key1", 1234567890L), Map.of(), 1234567890L, List.of()); builder = XContentFactory.jsonBuilder().prettyPrint().humanReadable(true); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"type\" : \"profileName\",\n" - + " \"description\" : \"some description\",\n" - + " \"time\" : \"1.2s\",\n" - + " \"time_in_nanos\" : 1234567890,\n" - + " \"breakdown\" : {\n" - + " \"key1\" : 1234567890\n" - + " }\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "type" : "profileName", + "description" : "some description", + "time" : "1.2s", + "time_in_nanos" : 1234567890, + "breakdown" : { + "key1" : 1234567890 + } + }""", Strings.toString(builder)); } } diff --git a/server/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java b/server/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java index b608da9239952..30fc5beab0541 100644 --- a/server/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java +++ b/server/src/test/java/org/elasticsearch/search/profile/aggregation/AggregationProfileShardResultTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.Writeable.Reader; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.search.profile.ProfileResult; import org.elasticsearch.search.profile.ProfileResultTests; @@ -76,31 +77,45 @@ public void testToXContent() throws IOException { profileResults.add(profileResult); AggregationProfileShardResult aggProfileResults = new AggregationProfileShardResult(profileResults); BytesReference xContent = toXContent(aggProfileResults, XContentType.JSON, false); - assertEquals( - "{\"aggregations\":[" - + "{\"type\":\"someType\"," - + "\"description\":\"someDescription\"," - + "\"time_in_nanos\":6000," - + "\"breakdown\":{\"timing1\":2000,\"timing2\":4000}," - + "\"debug\":{\"stuff\":\"stuff\",\"other_stuff\":[\"foo\",\"bar\"]}" - + "}" - + "]}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "aggregations": [ + { + "type": "someType", + "description": "someDescription", + "time_in_nanos": 6000, + "breakdown": { + "timing1": 2000, + "timing2": 4000 + }, + "debug": { + "stuff": "stuff", + "other_stuff": [ "foo", "bar" ] + } + } + ] + }"""), xContent.utf8ToString()); xContent = toXContent(aggProfileResults, XContentType.JSON, true); - assertEquals( - "{\"aggregations\":[" - + "{\"type\":\"someType\"," - + "\"description\":\"someDescription\"," - + "\"time\":\"6micros\"," - + "\"time_in_nanos\":6000," - + "\"breakdown\":{\"timing1\":2000,\"timing2\":4000}," - + "\"debug\":{\"stuff\":\"stuff\",\"other_stuff\":[\"foo\",\"bar\"]}" - + "}" - + "]}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "aggregations": [ + { + "type": "someType", + "description": "someDescription", + "time": "6micros", + "time_in_nanos": 6000, + "breakdown": { + "timing1": 2000, + "timing2": 4000 + }, + "debug": { + "stuff": "stuff", + "other_stuff": [ "foo", "bar" ] + } + } + ] + }"""), xContent.utf8ToString()); } } diff --git a/server/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java b/server/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java index ddf1c582aec79..d394c95d2db4d 100644 --- a/server/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java +++ b/server/src/test/java/org/elasticsearch/search/profile/query/CollectorResultTests.java @@ -67,77 +67,69 @@ public void testToXContent() throws IOException { CollectorResult result = new CollectorResult("collectorName", "some reason", 123456L, children); XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"name\" : \"collectorName\",\n" - + " \"reason\" : \"some reason\",\n" - + " \"time_in_nanos\" : 123456,\n" - + " \"children\" : [\n" - + " {\n" - + " \"name\" : \"child1\",\n" - + " \"reason\" : \"reason1\",\n" - + " \"time_in_nanos\" : 100\n" - + " },\n" - + " {\n" - + " \"name\" : \"child2\",\n" - + " \"reason\" : \"reason1\",\n" - + " \"time_in_nanos\" : 123356\n" - + " }\n" - + " ]\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "name" : "collectorName", + "reason" : "some reason", + "time_in_nanos" : 123456, + "children" : [ + { + "name" : "child1", + "reason" : "reason1", + "time_in_nanos" : 100 + }, + { + "name" : "child2", + "reason" : "reason1", + "time_in_nanos" : 123356 + } + ] + }""", Strings.toString(builder)); builder = XContentFactory.jsonBuilder().prettyPrint().humanReadable(true); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"name\" : \"collectorName\",\n" - + " \"reason\" : \"some reason\",\n" - + " \"time\" : \"123.4micros\",\n" - + " \"time_in_nanos\" : 123456,\n" - + " \"children\" : [\n" - + " {\n" - + " \"name\" : \"child1\",\n" - + " \"reason\" : \"reason1\",\n" - + " \"time\" : \"100nanos\",\n" - + " \"time_in_nanos\" : 100\n" - + " },\n" - + " {\n" - + " \"name\" : \"child2\",\n" - + " \"reason\" : \"reason1\",\n" - + " \"time\" : \"123.3micros\",\n" - + " \"time_in_nanos\" : 123356\n" - + " }\n" - + " ]\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "name" : "collectorName", + "reason" : "some reason", + "time" : "123.4micros", + "time_in_nanos" : 123456, + "children" : [ + { + "name" : "child1", + "reason" : "reason1", + "time" : "100nanos", + "time_in_nanos" : 100 + }, + { + "name" : "child2", + "reason" : "reason1", + "time" : "123.3micros", + "time_in_nanos" : 123356 + } + ] + }""", Strings.toString(builder)); result = new CollectorResult("collectorName", "some reason", 12345678L, Collections.emptyList()); builder = XContentFactory.jsonBuilder().prettyPrint().humanReadable(true); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"name\" : \"collectorName\",\n" - + " \"reason\" : \"some reason\",\n" - + " \"time\" : \"12.3ms\",\n" - + " \"time_in_nanos\" : 12345678\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "name" : "collectorName", + "reason" : "some reason", + "time" : "12.3ms", + "time_in_nanos" : 12345678 + }""", Strings.toString(builder)); result = new CollectorResult("collectorName", "some reason", 1234567890L, Collections.emptyList()); builder = XContentFactory.jsonBuilder().prettyPrint().humanReadable(true); result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"name\" : \"collectorName\",\n" - + " \"reason\" : \"some reason\",\n" - + " \"time\" : \"1.2s\",\n" - + " \"time_in_nanos\" : 1234567890\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "name" : "collectorName", + "reason" : "some reason", + "time" : "1.2s", + "time_in_nanos" : 1234567890 + }""", Strings.toString(builder)); } } diff --git a/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java b/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java index 9cdd4edadb73a..c51e4ce260d4f 100644 --- a/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java @@ -243,18 +243,32 @@ public MappedFieldType getFieldType(String name) { */ public void testUnknownFieldsExpection() throws IOException { - String rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"bad_rescorer_name\" : { }\n" + "}\n"; + String rescoreElement = """ + { + "window_size" : 20, + "bad_rescorer_name" : { } + } + """; try (XContentParser parser = createParser(rescoreElement)) { Exception e = expectThrows(NamedObjectNotFoundException.class, () -> RescorerBuilder.parseFromXContent(parser)); assertEquals("[3:27] unknown field [bad_rescorer_name]", e.getMessage()); } - rescoreElement = "{\n" + " \"bad_fieldName\" : 20\n" + "}\n"; + rescoreElement = """ + { + "bad_fieldName" : 20 + } + """; try (XContentParser parser = createParser(rescoreElement)) { Exception e = expectThrows(ParsingException.class, () -> RescorerBuilder.parseFromXContent(parser)); assertEquals("rescore doesn't support [bad_fieldName]", e.getMessage()); } - rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"query\" : [ ]\n" + "}\n"; + rescoreElement = """ + { + "window_size" : 20, + "query" : [ ] + } + """; try (XContentParser parser = createParser(rescoreElement)) { Exception e = expectThrows(ParsingException.class, () -> RescorerBuilder.parseFromXContent(parser)); assertEquals("unexpected token [START_ARRAY] after [query]", e.getMessage()); @@ -266,25 +280,34 @@ public void testUnknownFieldsExpection() throws IOException { assertEquals("missing rescore type", e.getMessage()); } - rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"query\" : { \"bad_fieldname\" : 1.0 } \n" + "}\n"; + rescoreElement = """ + { + "window_size" : 20, + "query" : { "bad_fieldname" : 1.0 }\s + } + """; try (XContentParser parser = createParser(rescoreElement)) { XContentParseException e = expectThrows(XContentParseException.class, () -> RescorerBuilder.parseFromXContent(parser)); assertEquals("[3:17] [query] unknown field [bad_fieldname]", e.getMessage()); } - rescoreElement = "{\n" - + " \"window_size\" : 20,\n" - + " \"query\" : { \"rescore_query\" : { \"unknown_queryname\" : { } } } \n" - + "}\n"; + rescoreElement = """ + { + "window_size" : 20, + "query" : { "rescore_query" : { "unknown_queryname" : { } } }\s + } + """; try (XContentParser parser = createParser(rescoreElement)) { Exception e = expectThrows(XContentParseException.class, () -> RescorerBuilder.parseFromXContent(parser)); assertThat(e.getMessage(), containsString("[query] failed to parse field [rescore_query]")); } - rescoreElement = "{\n" - + " \"window_size\" : 20,\n" - + " \"query\" : { \"rescore_query\" : { \"match_all\" : { } } } \n" - + "}\n"; + rescoreElement = """ + { + "window_size" : 20, + "query" : { "rescore_query" : { "match_all" : { } } }\s + } + """; try (XContentParser parser = createParser(rescoreElement)) { RescorerBuilder.parseFromXContent(parser); } diff --git a/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index 4a1e5d341756d..6e266264da742 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -301,7 +301,9 @@ public void testBuildNested() throws IOException { } public void testUnknownOptionFails() throws IOException { - String json = "{ \"post_date\" : {\"reverse\" : true} },\n"; + String json = """ + { "post_date" : {"reverse" : true} }, + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { // need to skip until parser is located on second START_OBJECT diff --git a/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index e5feed2641c56..2096c863ca140 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -190,15 +190,16 @@ public void testSortModeSumIsRejectedInSetter() { } public void testSortModeSumIsRejectedInJSON() throws IOException { - String json = "{\n" - + " \"testname\" : [ {\n" - + " \"lat\" : -6.046997540714173,\n" - + " \"lon\" : -51.94128329747579\n" - + " } ],\n" - + " \"unit\" : \"m\",\n" - + " \"distance_type\" : \"arc\",\n" - + " \"mode\" : \"SUM\"\n" - + "}"; + String json = """ + { + "testname" : [ { + "lat" : -6.046997540714173, + "lon" : -51.94128329747579 + } ], + "unit" : "m", + "distance_type" : "arc", + "mode" : "SUM" + }"""; try (XContentParser itemParser = createParser(JsonXContent.jsonXContent, json)) { itemParser.nextToken(); @@ -211,22 +212,22 @@ public void testSortModeSumIsRejectedInJSON() throws IOException { } public void testGeoDistanceSortCanBeParsedFromGeoHash() throws IOException { - String json = "{\n" - + " \"VDcvDuFjE\" : [ \"7umzzv8eychg\", \"dmdgmt5z13uw\", " - + " \"ezu09wxw6v4c\", \"kc7s3515p6k6\", \"jgeuvjwrmfzn\", \"kcpcfj7ruyf8\" ],\n" - + " \"unit\" : \"m\",\n" - + " \"distance_type\" : \"arc\",\n" - + " \"mode\" : \"MAX\",\n" - + " \"nested\" : {\n" - + " \"filter\" : {\n" - + " \"ids\" : {\n" - + " \"values\" : [ ],\n" - + " \"boost\" : 5.711116\n" - + " }\n" - + " }\n" - + " },\n" - + " \"validation_method\" : \"STRICT\"\n" - + " }"; + String json = """ + { + "VDcvDuFjE" : [ "7umzzv8eychg", "dmdgmt5z13uw", "ezu09wxw6v4c", "kc7s3515p6k6", "jgeuvjwrmfzn", "kcpcfj7ruyf8" ], + "unit" : "m", + "distance_type" : "arc", + "mode" : "MAX", + "nested" : { + "filter" : { + "ids" : { + "values" : [ ], + "boost" : 5.711116 + } + } + }, + "validation_method" : "STRICT" + }"""; try (XContentParser itemParser = createParser(JsonXContent.jsonXContent, json)) { itemParser.nextToken(); diff --git a/server/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java b/server/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java index 39021ee225371..64da8ad9fa06b 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java @@ -47,7 +47,9 @@ public void testIllegalOrder() { */ public void testParseOrder() throws IOException { SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC; - String scoreSortString = "{ \"_score\": { \"order\": \"" + order.toString() + "\" }}"; + String scoreSortString = """ + { "_score": { "order": "%s" }} + """.formatted(order.toString()); XContentParser parser = createParser(JsonXContent.jsonXContent, scoreSortString); // need to skip until parser is located on second START_OBJECT parser.nextToken(); @@ -59,7 +61,8 @@ public void testParseOrder() throws IOException { } public void testReverseOptionFails() throws IOException { - String json = "{ \"_score\": { \"reverse\": true }}"; + String json = """ + { "_score": { "reverse": true }}"""; XContentParser parser = createParser(JsonXContent.jsonXContent, json); // need to skip until parser is located on second START_OBJECT parser.nextToken(); 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 10ac553389e90..e2d5ad60738c6 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -153,19 +153,20 @@ public void testScriptSortTypeIllegalArgument() { } public void testParseJson() throws IOException { - String scriptSort = "{" - + " \"_script\": {" - + " \"type\": \"number\"," - + " \"script\": {" - + " \"source\": \"doc['field_name'].value * factor\"," - + " \"params\": {" - + " \"factor\": 1.1" - + " }" - + " }," - + " \"mode\": \"max\"," - + " \"order\": \"asc\"" - + " }" - + "}"; + 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(); @@ -184,13 +185,15 @@ public void testParseJson() throws IOException { } public void testParseJson_simple() throws IOException { - String scriptSort = "{\n" - + "\"_script\" : {\n" - + "\"type\" : \"number\",\n" - + "\"script\" : \"doc['field_name'].value\",\n" - + "\"mode\" : \"max\",\n" - + "\"order\" : \"asc\"\n" - + "} }\n"; + String scriptSort = """ + { + "_script": { + "type": "number", + "script": "doc['field_name'].value", + "mode": "max", + "order": "asc" + } + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort)) { parser.nextToken(); parser.nextToken(); @@ -209,7 +212,8 @@ public void testParseJson_simple() throws IOException { } public void testParseBadFieldNameExceptions() throws IOException { - String scriptSort = "{\"_script\" : {" + "\"bad_field\" : \"number\"" + "} }"; + String scriptSort = """ + {"_script" : {"bad_field" : "number"} }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort)) { parser.nextToken(); parser.nextToken(); @@ -221,8 +225,8 @@ public void testParseBadFieldNameExceptions() throws IOException { } public void testParseBadFieldNameExceptionsOnStartObject() throws IOException { - - String scriptSort = "{\"_script\" : {" + "\"bad_field\" : { \"order\" : \"asc\" } } }"; + String scriptSort = """ + {"_script" : {"bad_field" : { "order" : "asc" } } }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort)) { parser.nextToken(); parser.nextToken(); @@ -234,7 +238,8 @@ public void testParseBadFieldNameExceptionsOnStartObject() throws IOException { } public void testParseUnexpectedToken() throws IOException { - String scriptSort = "{\"_script\" : {" + "\"script\" : [ \"order\" : \"asc\" ] } }"; + String scriptSort = """ + {"_script" : {"script" : [ "order" : "asc" ] } }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort)) { parser.nextToken(); parser.nextToken(); diff --git a/server/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java b/server/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java index 004f499957cec..aff1bc895551c 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/SortBuilderTests.java @@ -54,7 +54,7 @@ public static void afterClass() throws Exception { */ public void testSingleFieldSort() throws IOException { SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC; - String json = "{ \"sort\" : { \"field1\" : \"" + order + "\" }}"; + String json = "{ \"sort\" : { \"field1\" : \"%s\" }}".formatted(order); List> result = parseSort(json); assertEquals(1, result.size()); SortBuilder sortBuilder = result.get(0); @@ -98,13 +98,15 @@ public void testSingleFieldSort() throws IOException { assertEquals(new ScoreSortBuilder(), sortBuilder); // test two spellings for _geo_disctance - json = "{ \"sort\" : [" + "{\"_geoDistance\" : {" + "\"pin.location\" : \"40,-70\" } }" + "] }"; + json = """ + { "sort" : [{"_geoDistance" : {"pin.location" : "40,-70" } }] }"""; result = parseSort(json); assertEquals(1, result.size()); sortBuilder = result.get(0); assertEquals(new GeoDistanceSortBuilder("pin.location", 40, -70), sortBuilder); - json = "{ \"sort\" : [" + "{\"_geo_distance\" : {" + "\"pin.location\" : \"40,-70\" } }" + "] }"; + json = """ + { "sort" : [{"_geo_distance" : {"pin.location" : "40,-70" } }] }"""; result = parseSort(json); assertEquals(1, result.size()); sortBuilder = result.get(0); @@ -204,16 +206,29 @@ public static List> randomSortBuilderList() { * - "sort" : [ "fieldname", { "fieldname2" : "asc" }, ...] */ public void testMultiFieldSort() throws IOException { - String json = "{ \"sort\" : [" - + "{ \"post_date\" : {\"order\" : \"asc\"}}," - + "\"user\"," - + "{ \"name\" : \"desc\" }," - + "{ \"age\" : \"desc\" }," - + "{" - + "\"_geo_distance\" : {" - + "\"pin.location\" : \"40,-70\" } }," - + "\"_score\"" - + "] }"; + String json = """ + { + "sort": [ + { + "post_date": { + "order": "asc" + } + }, + "user", + { + "name": "desc" + }, + { + "age": "desc" + }, + { + "_geo_distance": { + "pin.location": "40,-70" + } + }, + "_score" + ] + }"""; List> result = parseSort(json); assertEquals(6, result.size()); assertEquals(new FieldSortBuilder("post_date").order(SortOrder.ASC), result.get(0)); diff --git a/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java b/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java index 2b8687221fb8c..8a82ae8ce7268 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestionOptionTests.java @@ -103,6 +103,7 @@ public void testToXContent() throws IOException { Map> contexts = Collections.singletonMap("key", Collections.singleton("value")); CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(1, new Text("someText"), 1.3f, contexts); BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean()); - assertEquals("{\"text\":\"someText\",\"score\":1.3,\"contexts\":{\"key\":[\"value\"]}}", xContent.utf8ToString()); + assertEquals(""" + {"text":"someText","score":1.3,"contexts":{"key":["value"]}}""", xContent.utf8ToString()); } } 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 c0845211ba684..6bd90b72072fb 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java @@ -139,30 +139,26 @@ public void testToXContent() throws IOException { suggestion.addTerm(entry); Suggest suggest = new Suggest(Collections.singletonList(suggestion)); BytesReference xContent = toXContent(suggest, XContentType.JSON, randomBoolean()); - assertEquals( - stripWhitespace( - "{" - + " \"suggest\": {" - + " \"suggestionName\": [" - + " {" - + " \"text\": \"entryText\"," - + " \"offset\": 42," - + " \"length\": 313," - + " \"options\": [" - + " {" - + " \"text\": \"someText\"," - + " \"highlighted\": \"somethingHighlighted\"," - + " \"score\": 1.3," - + " \"collate_match\": true" - + " }" - + " ]" - + " }" - + " ]" - + " }" - + "}" - ), - xContent.utf8ToString() - ); + assertEquals(stripWhitespace(""" + { + "suggest": { + "suggestionName": [ + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ + { + "text": "someText", + "highlighted": "somethingHighlighted", + "score": 1.3, + "collate_match": true + } + ] + } + ] + } + }"""), xContent.utf8ToString()); } public void testFilter() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java index 19f71de3bce40..fc1589066a9fc 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionEntryTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.text.Text; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry; import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option; import org.elasticsearch.search.suggest.completion.CompletionSuggestion; @@ -134,34 +135,32 @@ public void testToXContent() throws IOException { PhraseSuggestion.Entry phraseEntry = new PhraseSuggestion.Entry(new Text("entryText"), 42, 313); phraseEntry.addOption(phraseOption); BytesReference xContent = toXContent(phraseEntry, XContentType.JSON, randomBoolean()); - assertEquals( - "{\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[" - + "{\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}" - + "]}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ + { + "text": "someText", + "highlighted": "somethingHighlighted", + "score": 1.3, + "collate_match": true + } + ] + }"""), xContent.utf8ToString()); TermSuggestion.Entry.Option termOption = new TermSuggestion.Entry.Option(new Text("termSuggestOption"), 42, 3.13f); TermSuggestion.Entry termEntry = new TermSuggestion.Entry(new Text("entryText"), 42, 313); termEntry.addOption(termOption); xContent = toXContent(termEntry, XContentType.JSON, randomBoolean()); - assertEquals( - "{\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[" - + "{\"text\":\"termSuggestOption\"," - + "\"score\":3.13," - + "\"freq\":42}" - + "]}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ { "text": "termSuggestOption", "score": 3.13, "freq": 42 } ] + }"""), xContent.utf8ToString()); CompletionSuggestion.Entry.Option completionOption = new CompletionSuggestion.Entry.Option( -1, @@ -172,18 +171,21 @@ public void testToXContent() throws IOException { CompletionSuggestion.Entry completionEntry = new CompletionSuggestion.Entry(new Text("entryText"), 42, 313); completionEntry.addOption(completionOption); xContent = toXContent(completionEntry, XContentType.JSON, randomBoolean()); - assertEquals( - "{\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[" - + "{\"text\":\"completionOption\"," - + "\"score\":3.13," - + "\"contexts\":{\"key\":[\"value\"]}" - + "}" - + "]}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ + { + "text": "completionOption", + "score": 3.13, + "contexts": { + "key": [ "value" ] + } + } + ] + }"""), xContent.utf8ToString()); } } 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 e44622f0c43a6..0261ab623ee8f 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java @@ -70,14 +70,12 @@ 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" - + "}").replaceAll("\\s+", ""), - xContent.utf8ToString() - ); + assertEquals((""" + { + "text": "someText", + "highlighted": "somethingHighlighted", + "score": 1.3, + "collate_match": true + }""").replaceAll("\\s+", ""), xContent.utf8ToString()); } } diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java index 74d81b86d9663..c3e65edf982b9 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java @@ -160,23 +160,24 @@ 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" - + " }" - + " ]" - + " }" - + " ]" - + "}").replaceAll("\\s+", ""); + String suggestionString = (""" + { + "unknownType#suggestionName": [ + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ + { + "text": "someText", + "highlighted": "somethingHighlighted", + "score": 1.3, + "collate_match": true + } + ] + } + ] + }""").replaceAll("\\s+", ""); try ( XContentParser parser = xContent.createParser( xContentRegistry(), @@ -206,26 +207,24 @@ public void testToXContent() throws IOException { PhraseSuggestion suggestion = new PhraseSuggestion("suggestionName", 5); suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); - assertEquals( - ("{" - + " \"phrase#suggestionName\": [" - + " {" - + " \"text\": \"entryText\"," - + " \"offset\": 42," - + " \"length\": 313," - + " \"options\": [" - + " {" - + " \"text\": \"someText\"," - + " \"highlighted\": \"somethingHighlighted\"," - + " \"score\": 1.3," - + " \"collate_match\": true" - + " }" - + " ]" - + " }" - + " ]" - + "}").replaceAll("\\s+", ""), - xContent.utf8ToString() - ); + assertEquals(""" + { + "phrase#suggestionName": [ + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ + { + "text": "someText", + "highlighted": "somethingHighlighted", + "score": 1.3, + "collate_match": true + } + ] + } + ] + }""".replaceAll("\\s+", ""), xContent.utf8ToString()); } { PhraseSuggestion.Entry.Option option = new PhraseSuggestion.Entry.Option( @@ -239,26 +238,24 @@ public void testToXContent() throws IOException { PhraseSuggestion suggestion = new PhraseSuggestion("suggestionName", 5); suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); - assertEquals( - ("{" - + " \"phrase#suggestionName\": [" - + " {" - + " \"text\": \"entryText\"," - + " \"offset\": 42," - + " \"length\": 313," - + " \"options\": [" - + " {" - + " \"text\": \"someText\"," - + " \"highlighted\": \"somethingHighlighted\"," - + " \"score\": 1.3," - + " \"collate_match\": true" - + " }" - + " ]" - + " }" - + " ]" - + "}").replaceAll("\\s+", ""), - xContent.utf8ToString() - ); + assertEquals(""" + { + "phrase#suggestionName": [ + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ + { + "text": "someText", + "highlighted": "somethingHighlighted", + "score": 1.3, + "collate_match": true + } + ] + } + ] + }""".replaceAll("\\s+", ""), xContent.utf8ToString()); } { TermSuggestion.Entry.Option option = new TermSuggestion.Entry.Option(new Text("someText"), 10, 1.3f); @@ -267,25 +264,17 @@ public void testToXContent() throws IOException { TermSuggestion suggestion = new TermSuggestion("suggestionName", 5, SortBy.SCORE); 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" - + " }" - + " ]" - + " }" - + " ]" - + "}").replaceAll("\\s+", ""), - xContent.utf8ToString() - ); + assertEquals(""" + { + "term#suggestionName": [ + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ { "text": "someText", "score": 1.3, "freq": 10 } ] + } + ] + }""".replaceAll("\\s+", ""), xContent.utf8ToString()); } { Map> contexts = Collections.singletonMap("key", Collections.singleton("value")); @@ -295,29 +284,25 @@ public void testToXContent() throws IOException { CompletionSuggestion suggestion = new CompletionSuggestion("suggestionName", 5, randomBoolean()); 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\"" - + " ]" - + " }" - + " }" - + " ]" - + " }" - + " ]" - + "}").replaceAll("\\s+", ""), - xContent.utf8ToString() - ); + assertEquals(""" + { + "completion#suggestionName": [ + { + "text": "entryText", + "offset": 42, + "length": 313, + "options": [ + { + "text": "someText", + "score": 1.3, + "contexts": { + "key": [ "value" ] + } + } + ] + } + ] + }""".replaceAll("\\s+", ""), xContent.utf8ToString()); } } } diff --git a/server/src/test/java/org/elasticsearch/search/suggest/TermSuggestionOptionTests.java b/server/src/test/java/org/elasticsearch/search/suggest/TermSuggestionOptionTests.java index 887acdcf7e8a5..aa5e9fcc3edbf 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/TermSuggestionOptionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/TermSuggestionOptionTests.java @@ -67,7 +67,8 @@ private void doTestFromXContent(boolean addRandomFields) throws IOException { public void testToXContent() throws IOException { Option option = new Option(new Text("someText"), 100, 1.3f); BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean()); - assertEquals("{\"text\":\"someText\",\"score\":1.3,\"freq\":100}", xContent.utf8ToString()); + assertEquals(""" + {"text":"someText","score":1.3,"freq":100}""", 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 f22b3d79357c0..ece102ddef562 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 @@ -212,16 +212,15 @@ public void testDefaultValuesSet() { public void testMalformedJson() { final String field = RandomStrings.randomAsciiOfLength(random(), 10).toLowerCase(Locale.ROOT); - String suggest = "{\n" - + " \"bad-payload\" : {\n" - + " \"text\" : \"the amsterdma meetpu\",\n" - + " \"term\" : {\n" - + " \"field\" : { \"" - + field - + "\" : \"bad-object\" }\n" - + " }\n" - + " }\n" - + "}"; + String suggest = """ + { + "bad-payload" : { + "text" : "the amsterdma meetpu", + "term" : { + "field" : { "%s" : "bad-object" } + } + } + }""".formatted(field); 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/server/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java b/server/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java index b24b9810f07ec..7a08233542724 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SnapshotsInProgressSerializationTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.unit.ByteSizeValue; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.index.Index; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.repositories.IndexId; @@ -417,30 +418,103 @@ public void testXContent() throws IOException { assertThat( json, anyOf( - equalTo( - "{\"snapshots\":[{\"repository\":\"repo\",\"snapshot\":\"name\",\"uuid\":\"uuid\"," - + "\"include_global_state\":true,\"partial\":true,\"state\":\"SUCCESS\"," - + "\"indices\":[{\"name\":\"index\",\"id\":\"uuid\"}],\"start_time\":\"1970-01-01T00:20:34.567Z\"," - + "\"start_time_millis\":1234567,\"repository_state_id\":0,\"shards\":[" - + "{\"index\":{\"index_name\":\"index\",\"index_uuid\":\"uuid\"},\"shard\":0,\"state\":\"SUCCESS\"," - + "\"generation\":\"shardgen\",\"node\":\"nodeId\"," - + "\"result\":{\"generation\":\"shardgen\",\"size\":\"1b\",\"size_in_bytes\":1,\"segments\":1}}," - + "{\"index\":{\"index_name\":\"index\",\"index_uuid\":\"uuid\"},\"shard\":1,\"state\":\"FAILED\"," - + "\"generation\":\"fail-gen\",\"node\":\"nodeId\",\"reason\":\"failure-reason\"}" - + "],\"feature_states\":[],\"data_streams\":[]}]}" - ), // or the shards might be in the other order: - equalTo( - "{\"snapshots\":[{\"repository\":\"repo\",\"snapshot\":\"name\",\"uuid\":\"uuid\"," - + "\"include_global_state\":true,\"partial\":true,\"state\":\"SUCCESS\"," - + "\"indices\":[{\"name\":\"index\",\"id\":\"uuid\"}],\"start_time\":\"1970-01-01T00:20:34.567Z\"," - + "\"start_time_millis\":1234567,\"repository_state_id\":0,\"shards\":[" - + "{\"index\":{\"index_name\":\"index\",\"index_uuid\":\"uuid\"},\"shard\":1,\"state\":\"FAILED\"," - + "\"generation\":\"fail-gen\",\"node\":\"nodeId\",\"reason\":\"failure-reason\"}," - + "{\"index\":{\"index_name\":\"index\",\"index_uuid\":\"uuid\"},\"shard\":0,\"state\":\"SUCCESS\"," - + "\"generation\":\"shardgen\",\"node\":\"nodeId\"," - + "\"result\":{\"generation\":\"shardgen\",\"size\":\"1b\",\"size_in_bytes\":1,\"segments\":1}}" - + "],\"feature_states\":[],\"data_streams\":[]}]}" - ) + equalTo(XContentHelper.stripWhitespace(""" + { + "snapshots": [ + { + "repository": "repo", + "snapshot": "name", + "uuid": "uuid", + "include_global_state": true, + "partial": true, + "state": "SUCCESS", + "indices": [ { "name": "index", "id": "uuid" } ], + "start_time": "1970-01-01T00:20:34.567Z", + "start_time_millis": 1234567, + "repository_state_id": 0, + "shards": [ + { + "index": { + "index_name": "index", + "index_uuid": "uuid" + }, + "shard": 0, + "state": "SUCCESS", + "generation": "shardgen", + "node": "nodeId", + "result": { + "generation": "shardgen", + "size": "1b", + "size_in_bytes": 1, + "segments": 1 + } + }, + { + "index": { + "index_name": "index", + "index_uuid": "uuid" + }, + "shard": 1, + "state": "FAILED", + "generation": "fail-gen", + "node": "nodeId", + "reason": "failure-reason" + } + ], + "feature_states": [], + "data_streams": [] + } + ] + }""")), + // or the shards might be in the other order: + equalTo(XContentHelper.stripWhitespace(""" + { + "snapshots": [ + { + "repository": "repo", + "snapshot": "name", + "uuid": "uuid", + "include_global_state": true, + "partial": true, + "state": "SUCCESS", + "indices": [ { "name": "index", "id": "uuid" } ], + "start_time": "1970-01-01T00:20:34.567Z", + "start_time_millis": 1234567, + "repository_state_id": 0, + "shards": [ + { + "index": { + "index_name": "index", + "index_uuid": "uuid" + }, + "shard": 1, + "state": "FAILED", + "generation": "fail-gen", + "node": "nodeId", + "reason": "failure-reason" + }, + { + "index": { + "index_name": "index", + "index_uuid": "uuid" + }, + "shard": 0, + "state": "SUCCESS", + "generation": "shardgen", + "node": "nodeId", + "result": { + "generation": "shardgen", + "size": "1b", + "size_in_bytes": 1, + "segments": 1 + } + } + ], + "feature_states": [], + "data_streams": [] + } + ] + }""")) ) ); } diff --git a/server/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java b/server/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java index fa4759f47d6ee..49d01f75a4d09 100644 --- a/server/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java +++ b/server/src/test/java/org/elasticsearch/tasks/ListTasksResponseTests.java @@ -33,7 +33,10 @@ public class ListTasksResponseTests extends AbstractXContentTestCase { public void testEmptyToString() { - assertEquals("{\n" + " \"tasks\" : [ ]\n" + "}", new ListTasksResponse(null, null, null).toString()); + assertEquals(""" + { + "tasks" : [ ] + }""", new ListTasksResponse(null, null, null).toString()); } public void testNonEmptyToString() { @@ -51,30 +54,28 @@ public void testNonEmptyToString() { Collections.singletonMap("foo", "bar") ); ListTasksResponse tasksResponse = new ListTasksResponse(singletonList(info), emptyList(), emptyList()); - assertEquals( - "{\n" - + " \"tasks\" : [\n" - + " {\n" - + " \"node\" : \"node1\",\n" - + " \"id\" : 1,\n" - + " \"type\" : \"dummy-type\",\n" - + " \"action\" : \"dummy-action\",\n" - + " \"description\" : \"dummy-description\",\n" - + " \"start_time\" : \"1970-01-01T00:00:00.000Z\",\n" - + " \"start_time_in_millis\" : 0,\n" - + " \"running_time\" : \"1nanos\",\n" - + " \"running_time_in_nanos\" : 1,\n" - + " \"cancellable\" : true,\n" - + " \"cancelled\" : false,\n" - + " \"parent_task_id\" : \"node1:0\",\n" - + " \"headers\" : {\n" - + " \"foo\" : \"bar\"\n" - + " }\n" - + " }\n" - + " ]\n" - + "}", - tasksResponse.toString() - ); + assertEquals(""" + { + "tasks" : [ + { + "node" : "node1", + "id" : 1, + "type" : "dummy-type", + "action" : "dummy-action", + "description" : "dummy-description", + "start_time" : "1970-01-01T00:00:00.000Z", + "start_time_in_millis" : 0, + "running_time" : "1nanos", + "running_time_in_nanos" : 1, + "cancellable" : true, + "cancelled" : false, + "parent_task_id" : "node1:0", + "headers" : { + "foo" : "bar" + } + } + ] + }""", tasksResponse.toString()); } @Override diff --git a/server/src/test/java/org/elasticsearch/transport/ProxyConnectionStrategyTests.java b/server/src/test/java/org/elasticsearch/transport/ProxyConnectionStrategyTests.java index 3c2392eeab087..ec1ef2b5aab09 100644 --- a/server/src/test/java/org/elasticsearch/transport/ProxyConnectionStrategyTests.java +++ b/server/src/test/java/org/elasticsearch/transport/ProxyConnectionStrategyTests.java @@ -416,10 +416,10 @@ public void testModeSettingsCannotBeUsedWhenInDifferentMode() { Setting concreteSetting = restrictedSetting.v1().getConcreteSettingForNamespace(clusterName); Settings invalid = Settings.builder().put(settings).put(concreteSetting.getKey(), restrictedSetting.v2()).build(); IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> service.validate(invalid, true)); - String expected = "Setting \"" - + concreteSetting.getKey() - + "\" cannot be used with the configured " - + "\"cluster.remote.cluster_name.mode\" [required=PROXY, configured=SNIFF]"; + String expected = """ + Setting "%s" cannot be used with the configured "cluster.remote.cluster_name.mode" \ + [required=PROXY, configured=SNIFF]\ + """.formatted(concreteSetting.getKey()); assertEquals(expected, iae.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java b/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java index 30f4a9596d386..26c2c6d0301ed 100644 --- a/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java +++ b/server/src/test/java/org/elasticsearch/transport/RemoteClusterConnectionTests.java @@ -32,6 +32,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.SuppressForbidden; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.internal.io.IOUtils; @@ -437,19 +438,32 @@ public void testRenderConnectionInfoXContent() throws IOException { builder.endObject(); if (sniff) { - assertEquals( - "{\"test_cluster\":{\"connected\":true,\"mode\":\"sniff\",\"seeds\":[\"seed:1\",\"seed:2\"]," - + "\"num_nodes_connected\":2,\"max_connections_per_cluster\":3,\"initial_connect_timeout\":\"30m\"," - + "\"skip_unavailable\":true}}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "test_cluster": { + "connected": true, + "mode": "sniff", + "seeds": [ "seed:1", "seed:2" ], + "num_nodes_connected": 2, + "max_connections_per_cluster": 3, + "initial_connect_timeout": "30m", + "skip_unavailable": true + } + }"""), Strings.toString(builder)); } else { - assertEquals( - "{\"test_cluster\":{\"connected\":true,\"mode\":\"proxy\",\"proxy_address\":\"seed:1\"," - + "\"server_name\":\"the_server_name\",\"num_proxy_sockets_connected\":16,\"max_proxy_socket_connections\":18," - + "\"initial_connect_timeout\":\"30m\",\"skip_unavailable\":true}}", - Strings.toString(builder) - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "test_cluster": { + "connected": true, + "mode": "proxy", + "proxy_address": "seed:1", + "server_name": "the_server_name", + "num_proxy_sockets_connected": 16, + "max_proxy_socket_connections": 18, + "initial_connect_timeout": "30m", + "skip_unavailable": true + } + }"""), Strings.toString(builder)); } } diff --git a/server/src/test/java/org/elasticsearch/transport/SniffConnectionStrategyTests.java b/server/src/test/java/org/elasticsearch/transport/SniffConnectionStrategyTests.java index e6b60d7d89e6f..02c1fd9f6603e 100644 --- a/server/src/test/java/org/elasticsearch/transport/SniffConnectionStrategyTests.java +++ b/server/src/test/java/org/elasticsearch/transport/SniffConnectionStrategyTests.java @@ -894,10 +894,10 @@ public void testModeSettingsCannotBeUsedWhenInDifferentMode() { Setting concreteSetting = restrictedSetting.v1().getConcreteSettingForNamespace(clusterName); Settings invalid = Settings.builder().put(settings).put(concreteSetting.getKey(), restrictedSetting.v2()).build(); IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> service.validate(invalid, true)); - String expected = "Setting \"" - + concreteSetting.getKey() - + "\" cannot be used with the configured " - + "\"cluster.remote.cluster_name.mode\" [required=SNIFF, configured=PROXY]"; + String expected = """ + Setting "%s" cannot be used with the configured "cluster.remote.cluster_name.mode" \ + [required=SNIFF, configured=PROXY]\ + """.formatted(concreteSetting.getKey()); assertEquals(expected, iae.getMessage()); } } diff --git a/test/fixtures/azure-fixture/src/main/java/fixture/azure/AzureHttpHandler.java b/test/fixtures/azure-fixture/src/main/java/fixture/azure/AzureHttpHandler.java index 595a58d5e3c1a..a8b92384d68fe 100644 --- a/test/fixtures/azure-fixture/src/main/java/fixture/azure/AzureHttpHandler.java +++ b/test/fixtures/azure-fixture/src/main/java/fixture/azure/AzureHttpHandler.java @@ -154,8 +154,9 @@ public void handle(final HttpExchange exchange) throws IOException { RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params); final StringBuilder list = new StringBuilder(); - list.append(""); - list.append(""); + list.append(""" + + """); final String prefix = params.get("prefix"); final Set blobPrefixes = new HashSet<>(); final String delimiter = params.get("delimiter"); @@ -176,17 +177,22 @@ public void handle(final HttpExchange exchange) throws IOException { continue; } } - list.append("").append(blobPath).append(""); - list.append("").append(blob.getValue().length()).append(""); - list.append("BlockBlob"); + list.append(""" + + %s + + %s + BlockBlob + + """.formatted(blobPath, blob.getValue().length())); } if (blobPrefixes.isEmpty() == false) { blobPrefixes.forEach(p -> list.append("").append(p).append("")); - } - list.append(""); - list.append(""); - list.append(""); + list.append(""" + + + """); byte[] response = list.toString().getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/xml"); @@ -223,11 +229,12 @@ public static void sendError(final HttpExchange exchange, final RestStatus statu if ("HEAD".equals(exchange.getRequestMethod())) { exchange.sendResponseHeaders(status.getStatus(), -1L); } else { - final byte[] response = ("" - + errorCode - + "" - + status - + "").getBytes(StandardCharsets.UTF_8); + final byte[] response = (""" + + + %s + %s + """.formatted(errorCode, status)).getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(status.getStatus(), response.length); exchange.getResponseBody().write(response); } diff --git a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/FakeOAuth2HttpHandler.java b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/FakeOAuth2HttpHandler.java index 0ea298c49e0e1..5abeb44b57adf 100644 --- a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/FakeOAuth2HttpHandler.java +++ b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/FakeOAuth2HttpHandler.java @@ -27,7 +27,8 @@ public void handle(final HttpExchange exchange) throws IOException { try { while (exchange.getRequestBody().read(BUFFER) >= 0) { } - byte[] response = ("{\"access_token\":\"foo\",\"token_type\":\"Bearer\",\"expires_in\":3600}").getBytes(UTF_8); + byte[] response = """ + {"access_token":"foo","token_type":"Bearer","expires_in":3600}""".getBytes(UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/json"); exchange.getResponseHeaders().add("Metadata-Flavor", "Google"); exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length); diff --git a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java index 98d55f5cec5cf..9e8ee570177e7 100644 --- a/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java +++ b/test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java @@ -114,11 +114,9 @@ public void handle(final HttpExchange exchange) throws IOException { } } - byte[] response = ("{\"kind\":\"storage#objects\",\"items\":[" - + String.join(",", listOfBlobs) - + "],\"prefixes\":[" - + String.join(",", prefixes) - + "]}").getBytes(UTF_8); + byte[] response = (""" + {"kind":"storage#objects","items":[%s],"prefixes":[%s]}\ + """.formatted(String.join(",", listOfBlobs), String.join(",", prefixes))).getBytes(UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/json; charset=utf-8"); exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length); @@ -185,7 +183,9 @@ public void handle(final HttpExchange exchange) throws IOException { if (content.isPresent()) { blobs.put(content.get().v1(), content.get().v2()); - byte[] response = ("{\"bucket\":\"" + bucket + "\",\"name\":\"" + content.get().v1() + "\"}").getBytes(UTF_8); + byte[] response = """ + {"bucket":"%s","name":"%s"} + """.formatted(bucket, content.get().v1()).getBytes(UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/json"); exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length); exchange.getResponseBody().write(response); @@ -266,20 +266,9 @@ public void handle(final HttpExchange exchange) throws IOException { } private String buildBlobInfoJson(String blobName, int size) { - return "{\"kind\":\"storage#object\"," - + "\"bucket\":\"" - + bucket - + "\"," - + "\"name\":\"" - + blobName - + "\"," - + "\"id\":\"" - + blobName - + "\"," - + "\"size\":\"" - + size - + "\"" - + "}"; + return """ + {"kind":"storage#object","bucket":"%s","name":"%s","id":"%s","size":"%s"} + """.formatted(bucket, blobName, blobName, size); } public Map blobs() { diff --git a/test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpFixtureWithEC2.java b/test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpFixtureWithEC2.java index 1de7683c4b984..3817e2d3b1bf4 100644 --- a/test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpFixtureWithEC2.java +++ b/test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpFixtureWithEC2.java @@ -65,17 +65,14 @@ protected HttpHandler createHandler(final String[] args) { } protected String buildCredentialResponse(final String ec2AccessKey, final String ec2SessionToken) { - return "{" - + "\"AccessKeyId\": \"" - + ec2AccessKey - + "\",\"Expiration\": \"" - + ZonedDateTime.now().plusDays(1L).format(DateTimeFormatter.ISO_DATE_TIME) - + "\",\"RoleArn\": \"arn\"," - + "\"SecretAccessKey\": \"secret_access_key\"," - + "\"Token\": \"" - + ec2SessionToken - + "\"" - + "}"; + return """ + { + "AccessKeyId": "%s", + "Expiration": "%s", + "RoleArn": "arn", + "SecretAccessKey": "secret_access_key", + "Token": "%s" + }""".formatted(ec2AccessKey, ZonedDateTime.now().plusDays(1L).format(DateTimeFormatter.ISO_DATE_TIME), ec2SessionToken); } public static void main(final String[] args) throws Exception { diff --git a/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java b/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java index e348df6b474e0..384373d66f0c4 100644 --- a/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java +++ b/test/framework/src/main/java/org/elasticsearch/cluster/metadata/DataStreamTestHelper.java @@ -99,17 +99,16 @@ public static DataStream.TimestampField createTimestampField(String fieldName) { } public static String generateMapping(String timestampFieldName) { - return "{" - + " \"_doc\":{\n" - + " \"properties\": {\n" - + " \"" - + timestampFieldName - + "\": {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + " }" - + "}"; + return """ + { + "_doc":{ + "properties": { + "%s": { + "type": "date" + } + } + } + }""".formatted(timestampFieldName); } public static String generateMapping(String timestampFieldName, String type) { diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperScriptTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperScriptTestCase.java index 7b0640cc7ae98..19cf9207bd097 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperScriptTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperScriptTestCase.java @@ -60,7 +60,8 @@ public void testToXContent() throws IOException { b.field("script", "serializer_test"); b.endObject(); })); - assertThat(Strings.toString(mapper.mapping()), containsString("\"script\":{\"source\":\"serializer_test\",\"lang\":\"painless\"}")); + assertThat(Strings.toString(mapper.mapping()), containsString(""" + "script":{"source":"serializer_test","lang":"painless"}""")); } public void testCannotIndexDirectlyIntoScriptMapper() throws IOException { diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java index 415ae22809f2c..bcc059888bd71 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java @@ -1110,21 +1110,15 @@ private void writeTestDoc(MappedFieldType fieldType, String fieldName, RandomInd final RangeFieldMapper.Range range = new RangeFieldMapper.Range(rangeType, start, end, true, true); doc.add(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(Collections.singleton(range)))); - json = "{ \"" - + fieldName - + "\" : { \n" - + " \"gte\" : \"" - + start - + "\",\n" - + " \"lte\" : \"" - + end - + "\"\n" - + " }}"; + json = """ + { "%s" : { "gte" : "%s", "lte" : "%s" } } + """.formatted(fieldName, start, end); } else if (vst.equals(CoreValuesSourceType.GEOPOINT)) { double lat = randomDouble(); double lon = randomDouble(); doc.add(new LatLonDocValuesField(fieldName, lat, lon)); - json = "{ \"" + fieldName + "\" : \"[" + lon + "," + lat + "]\" }"; + json = """ + { "%s" : "[%s,%s]" }""".formatted(fieldName, lon, lat); } else { throw new IllegalStateException("Unknown field type [" + typeName + "]"); } diff --git a/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeIntegTestCase.java index 273f77ae18379..4ff6efad0acad 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeIntegTestCase.java @@ -212,12 +212,13 @@ public void testIndexShapeRouting() throws Exception { assertAcked(prepareCreate("test").setMapping(mapping).setSettings(settings(randomSupportedVersion()).build())); ensureGreen(); - String source = "{\n" - + " \"shape\" : {\n" - + " \"type\" : \"bbox\",\n" - + " \"coordinates\" : [[-45.0, 45.0], [45.0, -45.0]]\n" - + " }\n" - + "}"; + String source = """ + { + "shape" : { + "type" : "bbox", + "coordinates" : [[-45.0, 45.0], [45.0, -45.0]] + } + }"""; indexRandom(true, client().prepareIndex("test").setId("0").setSource(source, XContentType.JSON).setRouting("ABC")); @@ -244,7 +245,10 @@ public void testIndexPolygonDateLine() throws Exception { ); ensureGreen(); - String source = "{\n" + " \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\"" + "}"; + String source = """ + { + "shape": "POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))" + }"""; indexRandom(true, client().prepareIndex("test").setId("0").setSource(source, XContentType.JSON)); @@ -277,12 +281,13 @@ public void testDisallowExpensiveQueries() throws InterruptedException, IOExcept ); ensureGreen(); - String source = "{\n" - + " \"shape\" : {\n" - + " \"type\" : \"bbox\",\n" - + " \"coordinates\" : [[-45.0, 45.0], [45.0, -45.0]]\n" - + " }\n" - + "}"; + String source = """ + { + "shape" : { + "type" : "bbox", + "coordinates" : [[-45.0, 45.0], [45.0, -45.0]] + } + }"""; indexRandom(true, client().prepareIndex("test").setId("0").setSource(source, XContentType.JSON)); refresh(); diff --git a/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryTestCase.java index ac7a0aaf886ab..572650deafd91 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryTestCase.java @@ -37,7 +37,6 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.Locale; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; import static org.elasticsearch.index.query.QueryBuilders.geoIntersectionQuery; @@ -55,16 +54,12 @@ public void testShapeFetchingPath() throws Exception { createMapping(defaultIndexName, defaultGeoFieldName); ensureGreen(); - String geo = "\"geo\" : {\"type\":\"polygon\", \"coordinates\":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}"; + String geo = """ + "geo" : {"type":"polygon", "coordinates":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}"""; - client().prepareIndex("shapes") - .setId("1") - .setSource( - String.format(Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", geo, geo, geo, geo), - XContentType.JSON - ) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("shapes").setId("1").setSource(""" + { %s, "1" : { %s, "2" : { %s, "3" : { %s } }} } + """.formatted(geo, geo, geo, geo), XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); client().prepareIndex(defaultIndexName) .setId("1") .setSource( @@ -202,31 +197,31 @@ public void testEnvelopeSpanningDateline() throws Exception { createMapping(defaultIndexName, defaultGeoFieldName); ensureGreen(); - String doc1 = "{\"geo\": {\r\n" - + "\"coordinates\": [\r\n" - + "-33.918711,\r\n" - + "18.847685\r\n" - + "],\r\n" - + "\"type\": \"Point\"\r\n" - + "}}"; + String doc1 = """ + { + "geo": { + "coordinates": [ -33.918711, 18.847685 ], + "type": "Point" + } + }"""; client().index(new IndexRequest(defaultIndexName).id("1").source(doc1, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet(); - String doc2 = "{\"geo\": {\r\n" - + "\"coordinates\": [\r\n" - + "-49.0,\r\n" - + "18.847685\r\n" - + "],\r\n" - + "\"type\": \"Point\"\r\n" - + "}}"; + String doc2 = """ + { + "geo": { + "coordinates": [ -49, 18.847685 ], + "type": "Point" + } + }"""; client().index(new IndexRequest(defaultIndexName).id("2").source(doc2, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet(); - String doc3 = "{\"geo\": {\r\n" - + "\"coordinates\": [\r\n" - + "49.0,\r\n" - + "18.847685\r\n" - + "],\r\n" - + "\"type\": \"Point\"\r\n" - + "}}"; + String doc3 = """ + { + "geo": { + "coordinates": [ 49, 18.847685 ], + "type": "Point" + } + }"""; client().index(new IndexRequest(defaultIndexName).id("3").source(doc3, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet(); @SuppressWarnings("unchecked") diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java index 5346f8ad7bd79..5f720eededf02 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractBroadcastResponseTestCase.java @@ -124,9 +124,10 @@ public void testFailuresDeduplication() throws IOException { assertThat(parsedFailures[1].getCause().getMessage(), containsString("fizz")); } - public void testToXContent() { + public void testToXContent() throws IOException { T response = createTestInstance(10, 10, 0, null); String output = Strings.toString(response); - assertEquals("{\"_shards\":{\"total\":10,\"successful\":10,\"failed\":0}}", output); + assertEquals(""" + {"_shards":{"total":10,"successful":10,"failed":0}}""", output); } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java index f96eaec044941..170bda224d3f2 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java @@ -463,20 +463,22 @@ public void onRemoval(ShardId shardId, Accountable accountable) { MapperService.MergeReason.MAPPING_UPDATE ); // also add mappings for two inner field in the object field - mapperService.merge( - "_doc", - new CompressedXContent( - "{\"properties\":{\"" - + OBJECT_FIELD_NAME - + "\":{\"type\":\"object\"," - + "\"properties\":{\"" - + DATE_FIELD_NAME - + "\":{\"type\":\"date\"},\"" - + INT_FIELD_NAME - + "\":{\"type\":\"integer\"}}}}}" - ), - MapperService.MergeReason.MAPPING_UPDATE - ); + mapperService.merge("_doc", new CompressedXContent(""" + { + "properties": { + "%s": { + "type": "object", + "properties": { + "%s": { + "type": "date" + }, + "%s": { + "type": "integer" + } + } + } + } + }""".formatted(OBJECT_FIELD_NAME, DATE_FIELD_NAME, INT_FIELD_NAME)), MapperService.MergeReason.MAPPING_UPDATE); testCase.initializeAdditionalMappings(mapperService); } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/disruption/LongGCDisruption.java b/test/framework/src/main/java/org/elasticsearch/test/disruption/LongGCDisruption.java index db41724b4a662..a5404e38480eb 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/disruption/LongGCDisruption.java +++ b/test/framework/src/main/java/org/elasticsearch/test/disruption/LongGCDisruption.java @@ -106,8 +106,11 @@ protected void doRun() throws Exception { } if (suspendingThread.isAlive()) { logger.warn( - "failed to suspend node [{}]'s threads within [{}] millis. Suspending thread stack trace:\n {}" - + "\nThreads that weren't suspended:\n {}", + """ + failed to suspend node [{}]'s threads within [{}] millis. Suspending thread stack trace: + {} + Threads that weren't suspended: + {}""", disruptedNode, getSuspendingTimeoutInMillis(), stackTrace(suspendingThread.getStackTrace()), diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuite.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuite.java index a42fdef02210f..fc7d505d89329 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuite.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuite.java @@ -167,13 +167,10 @@ private static Stream validateExecutableSections( .map(section -> (DoSection) section) .filter(section -> false == section.getExpectedWarningHeaders().isEmpty()) .filter(section -> false == hasSkipFeature("warnings", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [do] with a [warnings] section " - + "without a corresponding [\"skip\": \"features\": \"warnings\"] so runners that do not support the [warnings] " - + "section can skip the test at line [" - + section.getLocation().lineNumber - + "]" - ); + .map(section -> """ + attempted to add a [do] with a [warnings] section without a corresponding ["skip": "features": "warnings"] \ + so runners that do not support the [warnings] section can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)); errors = Stream.concat( errors, @@ -182,14 +179,11 @@ private static Stream validateExecutableSections( .map(section -> (DoSection) section) .filter(section -> false == section.getExpectedWarningHeadersRegex().isEmpty()) .filter(section -> false == hasSkipFeature("warnings_regex", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [do] with a [warnings_regex] section " - + "without a corresponding [\"skip\": \"features\": \"warnings_regex\"] so runners that do not " - + "support the [warnings_regex] " - + "section can skip the test at line [" - + section.getLocation().lineNumber - + "]" - ) + .map(section -> """ + attempted to add a [do] with a [warnings_regex] section without a corresponding \ + ["skip": "features": "warnings_regex"] so runners that do not support the [warnings_regex] \ + section can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)) ); errors = Stream.concat( @@ -199,13 +193,11 @@ private static Stream validateExecutableSections( .map(section -> (DoSection) section) .filter(section -> false == section.getAllowedWarningHeaders().isEmpty()) .filter(section -> false == hasSkipFeature("allowed_warnings", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [do] with a [allowed_warnings] section " - + "without a corresponding [\"skip\": \"features\": \"allowed_warnings\"] so runners that do not " - + "support the [allowed_warnings] section can skip the test at line [" - + section.getLocation().lineNumber - + "]" - ) + .map(section -> """ + attempted to add a [do] with a [allowed_warnings] section without a corresponding \ + ["skip": "features": "allowed_warnings"] so runners that do not support the [allowed_warnings] \ + section can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)) ); errors = Stream.concat( @@ -215,13 +207,11 @@ private static Stream validateExecutableSections( .map(section -> (DoSection) section) .filter(section -> false == section.getAllowedWarningHeadersRegex().isEmpty()) .filter(section -> false == hasSkipFeature("allowed_warnings_regex", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [do] with a [allowed_warnings_regex] section " - + "without a corresponding [\"skip\": \"features\": \"allowed_warnings_regex\"] so runners that do not " - + "support the [allowed_warnings_regex] section can skip the test at line [" - + section.getLocation().lineNumber - + "]" - ) + .map(section -> """ + attempted to add a [do] with a [allowed_warnings_regex] section without a corresponding \ + ["skip": "features": "allowed_warnings_regex"] so runners that do not support the [allowed_warnings_regex] \ + section can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)) ); errors = Stream.concat( @@ -231,13 +221,11 @@ private static Stream validateExecutableSections( .map(section -> (DoSection) section) .filter(section -> NodeSelector.ANY != section.getApiCallSection().getNodeSelector()) .filter(section -> false == hasSkipFeature("node_selector", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [do] with a [node_selector] " - + "section without a corresponding [\"skip\": \"features\": \"node_selector\"] so runners that do not support the " - + "[node_selector] section can skip the test at line [" - + section.getLocation().lineNumber - + "]" - ) + .map(section -> """ + attempted to add a [do] with a [node_selector] section without a corresponding \ + ["skip": "features": "node_selector"] so runners that do not support the [node_selector] section \ + can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)) ); errors = Stream.concat( @@ -245,13 +233,10 @@ private static Stream validateExecutableSections( sections.stream() .filter(section -> section instanceof ContainsAssertion) .filter(section -> false == hasSkipFeature("contains", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [contains] assertion " - + "without a corresponding [\"skip\": \"features\": \"contains\"] so runners that do not support the " - + "[contains] assertion can skip the test at line [" - + section.getLocation().lineNumber - + "]" - ) + .map(section -> """ + attempted to add a [contains] assertion without a corresponding ["skip": "features": "contains"] \ + so runners that do not support the [contains] assertion can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)) ); errors = Stream.concat( @@ -261,13 +246,10 @@ private static Stream validateExecutableSections( .map(section -> (DoSection) section) .filter(section -> false == section.getApiCallSection().getHeaders().isEmpty()) .filter(section -> false == hasSkipFeature("headers", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [do] with a [headers] section without a corresponding " - + "[\"skip\": \"features\": \"headers\"] so runners that do not support the [headers] section can skip the test at " - + "line [" - + section.getLocation().lineNumber - + "]" - ) + .map(section -> """ + attempted to add a [do] with a [headers] section without a corresponding ["skip": "features": "headers"] \ + so runners that do not support the [headers] section can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)) ); errors = Stream.concat( @@ -275,13 +257,10 @@ private static Stream validateExecutableSections( sections.stream() .filter(section -> section instanceof CloseToAssertion) .filter(section -> false == hasSkipFeature("close_to", testSection, setupSection, teardownSection)) - .map( - section -> "attempted to add a [close_to] assertion " - + "without a corresponding [\"skip\": \"features\": \"close_to\"] so runners that do not support the " - + "[close_to] assertion can skip the test at line [" - + section.getLocation().lineNumber - + "]" - ) + .map(section -> """ + attempted to add a [close_to] assertion without a corresponding ["skip": "features": "close_to"] \ + so runners that do not support the [close_to] assertion can skip the test at line [%d]\ + """.formatted(section.getLocation().lineNumber)) ); return errors; diff --git a/test/framework/src/test/java/org/elasticsearch/test/AbstractQueryTestCaseTests.java b/test/framework/src/test/java/org/elasticsearch/test/AbstractQueryTestCaseTests.java index 889fbdbdc810a..8fc23dcf33d0c 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/AbstractQueryTestCaseTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/AbstractQueryTestCaseTests.java @@ -35,113 +35,56 @@ public void testAlterateQueries() throws IOException { List> alterations = alterateQueries(singleton("{\"field\": \"value\"}"), null); assertAlterations(alterations, allOf(notNullValue(), hasEntry("{\"newField\":{\"field\":\"value\"}}", STANDARD_ERROR))); - alterations = alterateQueries(singleton("{\"term\":{\"field\": \"value\"}}"), null); - assertAlterations( - alterations, - allOf( - hasEntry("{\"newField\":{\"term\":{\"field\":\"value\"}}}", STANDARD_ERROR), - hasEntry("{\"term\":{\"newField\":{\"field\":\"value\"}}}", STANDARD_ERROR) - ) - ); + alterations = alterateQueries(singleton(""" + {"term":{"field": "value"}}"""), null); + assertAlterations(alterations, allOf(hasEntry(""" + {"newField":{"term":{"field":"value"}}}""", STANDARD_ERROR), hasEntry(""" + {"term":{"newField":{"field":"value"}}}""", STANDARD_ERROR))); - alterations = alterateQueries(singleton("{\"bool\":{\"must\": [{\"match\":{\"field\":\"value\"}}]}}"), null); - assertAlterations( - alterations, - allOf( - hasEntry("{\"newField\":{\"bool\":{\"must\":[{\"match\":{\"field\":\"value\"}}]}}}", STANDARD_ERROR), - hasEntry("{\"bool\":{\"newField\":{\"must\":[{\"match\":{\"field\":\"value\"}}]}}}", STANDARD_ERROR), - hasEntry("{\"bool\":{\"must\":[{\"newField\":{\"match\":{\"field\":\"value\"}}}]}}", STANDARD_ERROR), - hasEntry("{\"bool\":{\"must\":[{\"match\":{\"newField\":{\"field\":\"value\"}}}]}}", STANDARD_ERROR) - ) - ); + alterations = alterateQueries(singleton(""" + {"bool":{"must": [{"match":{"field":"value"}}]}}"""), null); + assertAlterations(alterations, allOf(hasEntry(""" + {"newField":{"bool":{"must":[{"match":{"field":"value"}}]}}}""", STANDARD_ERROR), hasEntry(""" + {"bool":{"newField":{"must":[{"match":{"field":"value"}}]}}}""", STANDARD_ERROR), hasEntry(""" + {"bool":{"must":[{"newField":{"match":{"field":"value"}}}]}}""", STANDARD_ERROR), hasEntry(""" + {"bool":{"must":[{"match":{"newField":{"field":"value"}}}]}}""", STANDARD_ERROR))); - alterations = alterateQueries( - singleton( - "{\"function_score\":" - + "{\"query\": {\"term\":{\"foo\": \"bar\"}}, \"script_score\": {\"script\":\"a + 1\", \"params\": {\"a\":0}}}}" - ), - null - ); - assertAlterations( - alterations, - allOf( - hasEntry( - "{\"newField\":{\"function_score\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"script\":\"a + " - + "1\",\"params\":{\"a\":0}}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"function_score\":{\"newField\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"script\":\"a + " - + "1\",\"params\":{\"a\":0}}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"function_score\":{\"query\":{\"newField\":{\"term\":{\"foo\":\"bar\"}}},\"script_score\":{\"script\":\"a + " - + "1\",\"params\":{\"a\":0}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"function_score\":{\"query\":{\"term\":{\"newField\":{\"foo\":\"bar\"}}},\"script_score\":{\"script\":\"a + " - + "1\",\"params\":{\"a\":0}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"function_score\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"newField\":{\"script\":\"a + " - + "1\",\"params\":{\"a\":0}}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"function_score\":{\"query\":{\"term\":{\"foo\":\"bar\"}},\"script_score\":{\"script\":\"a + 1\"," - + "\"params\":{\"newField\":{\"a\":0}}}}}", - STANDARD_ERROR - ) - ) - ); + alterations = alterateQueries(singleton(""" + {"function_score":{"query": {"term":{"foo": "bar"}}, "script_score": {"script":"a + 1", "params": {"a":0}}}}"""), null); + assertAlterations(alterations, allOf(hasEntry(""" + {"newField":{"function_score":{"query":{"term":{"foo":"bar"}},\ + "script_score":{"script":"a + 1","params":{"a":0}}}}}""", STANDARD_ERROR), hasEntry(""" + {"function_score":{"newField":{"query":{"term":{"foo":"bar"}},\ + "script_score":{"script":"a + 1","params":{"a":0}}}}}""", STANDARD_ERROR), hasEntry(""" + {"function_score":{"query":{"newField":{"term":{"foo":"bar"}}},\ + "script_score":{"script":"a + 1","params":{"a":0}}}}""", STANDARD_ERROR), hasEntry(""" + {"function_score":{"query":{"term":{"newField":{"foo":"bar"}}},\ + "script_score":{"script":"a + 1","params":{"a":0}}}}""", STANDARD_ERROR), hasEntry(""" + {"function_score":{"query":{"term":{"foo":"bar"}},\ + "script_score":{"newField":{"script":"a + 1","params":{"a":0}}}}}""", STANDARD_ERROR), hasEntry(""" + {"function_score":{"query":{"term":{"foo":"bar"}},\ + "script_score":{"script":"a + 1","params":{"newField":{"a":0}}}}}""", STANDARD_ERROR))); } public void testAlterateQueriesWithArbitraryContent() throws IOException { Map arbitraryContentHolders = new HashMap<>(); arbitraryContentHolders.put("params", null); // no exception expected arbitraryContentHolders.put("doc", "my own error"); - Set queries = Sets.newHashSet( - "{\"query\":{\"script\":\"test\",\"params\":{\"foo\":\"bar\"}}}", - "{\"query\":{\"more_like_this\":{\"fields\":[\"a\",\"b\"],\"like\":{\"doc\":{\"c\":\"d\"}}}}}" - ); + Set queries = Sets.newHashSet(""" + {"query":{"script":"test","params":{"foo":"bar"}}}""", """ + {"query":{"more_like_this":{"fields":["a","b"],"like":{"doc":{"c":"d"}}}}}"""); List> alterations = alterateQueries(queries, arbitraryContentHolders); - assertAlterations( - alterations, - allOf( - hasEntry("{\"newField\":{\"query\":{\"script\":\"test\",\"params\":{\"foo\":\"bar\"}}}}", STANDARD_ERROR), - hasEntry("{\"query\":{\"newField\":{\"script\":\"test\",\"params\":{\"foo\":\"bar\"}}}}", STANDARD_ERROR), - hasEntry("{\"query\":{\"script\":\"test\",\"params\":{\"newField\":{\"foo\":\"bar\"}}}}", null) - ) - ); - assertAlterations( - alterations, - allOf( - hasEntry( - "{\"newField\":{\"query\":{\"more_like_this\":{\"fields\":[\"a\",\"b\"],\"like\":{\"doc\":{\"c\":\"d\"}}}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"query\":{\"newField\":{\"more_like_this\":{\"fields\":[\"a\",\"b\"],\"like\":{\"doc\":{\"c\":\"d\"}}}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"query\":{\"more_like_this\":{\"newField\":{\"fields\":[\"a\",\"b\"],\"like\":{\"doc\":{\"c\":\"d\"}}}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"query\":{\"more_like_this\":{\"fields\":[\"a\",\"b\"],\"like\":{\"newField\":{\"doc\":{\"c\":\"d\"}}}}}}", - STANDARD_ERROR - ), - hasEntry( - "{\"query\":{\"more_like_this\":{\"fields\":[\"a\",\"b\"],\"like\":{\"doc\":{\"newField\":{\"c\":\"d\"}}}}}}", - "my own error" - ) - ) - ); + assertAlterations(alterations, allOf(hasEntry(""" + {"newField":{"query":{"script":"test","params":{"foo":"bar"}}}}""", STANDARD_ERROR), hasEntry(""" + {"query":{"newField":{"script":"test","params":{"foo":"bar"}}}}""", STANDARD_ERROR), hasEntry(""" + {"query":{"script":"test","params":{"newField":{"foo":"bar"}}}}""", null))); + assertAlterations(alterations, allOf(hasEntry(""" + {"newField":{"query":{"more_like_this":{"fields":["a","b"],"like":{"doc":{"c":"d"}}}}}}""", STANDARD_ERROR), hasEntry(""" + {"query":{"newField":{"more_like_this":{"fields":["a","b"],"like":{"doc":{"c":"d"}}}}}}""", STANDARD_ERROR), hasEntry(""" + {"query":{"more_like_this":{"newField":{"fields":["a","b"],"like":{"doc":{"c":"d"}}}}}}""", STANDARD_ERROR), hasEntry(""" + {"query":{"more_like_this":{"fields":["a","b"],"like":{"newField":{"doc":{"c":"d"}}}}}}""", STANDARD_ERROR), hasEntry(""" + {"query":{"more_like_this":{"fields":["a","b"],"like":{"doc":{"newField":{"c":"d"}}}}}}""", "my own error"))); } private static void assertAlterations(List> alterations, Matcher> matcher) { diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java index 4ab92db7d26c6..36babefd2fdd5 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserFailingTests.java @@ -21,49 +21,63 @@ public class ClientYamlSuiteRestApiParserFailingTests extends ESTestCase { public void testDuplicateMethods() throws Exception { - parseAndExpectParsingException( - "{\n" - + " \"ping\": {" - + " \"documentation\": \"http://www.elasticsearch.org/guide/\"," - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"url\": {" - + " \"paths\": [{\"path\":\"/\", \"parts\": {}, \"methods\": [\"PUT\", \"PUT\"]}]," - + " \"params\": {" - + " \"type\" : \"boolean\",\n" - + " \"description\" : \"Whether specified concrete indices should be ignored when unavailable (missing or closed)\"" - + " }" - + " }," - + " \"body\": null" - + " }" - + "}", - "ping.json", - "ping API: found duplicate method [PUT]" - ); + parseAndExpectParsingException(""" + { + "ping": { + "documentation": "http://www.elasticsearch.org/guide/", + "stability": "stable", + "visibility": "public", + "url": { + "paths": [ + { + "path": "/", + "parts": {}, + "methods": [ + "PUT", + "PUT" + ] + } + ], + "params": { + "type": "boolean", + "description": "Whether specified concrete indices should be ignored when unavailable (missing or closed)" + } + }, + "body": null + } + }""", "ping.json", "ping API: found duplicate method [PUT]"); } public void testDuplicatePaths() throws Exception { - parseAndExpectIllegalArgumentException( - "{\n" - + " \"ping\": {" - + " \"documentation\": \"http://www.elasticsearch.org/guide/\"," - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"url\": {" - + " \"paths\": [" - + " {\"path\":\"/pingtwo\", \"methods\": [\"PUT\"]}, " - + "{\"path\":\"/pingtwo\", \"methods\": [\"PUT\"]}]," - + " \"params\": {" - + " \"type\" : \"boolean\",\n" - + " \"description\" : \"Whether specified concrete indices should be ignored when unavailable (missing or closed)\"" - + " }" - + " }," - + " \"body\": null" - + " }" - + "}", - "ping.json", - "ping API: found duplicate path [/pingtwo]" - ); + parseAndExpectIllegalArgumentException(""" + { + "ping": { + "documentation": "http://www.elasticsearch.org/guide/", + "stability": "stable", + "visibility": "public", + "url": { + "paths": [ + { + "path": "/pingtwo", + "methods": [ + "PUT" + ] + }, + { + "path": "/pingtwo", + "methods": [ + "PUT" + ] + } + ], + "params": { + "type": "boolean", + "description": "Whether specified concrete indices should be ignored when unavailable (missing or closed)" + } + }, + "body": null + } + }""", "ping.json", "ping API: found duplicate path [/pingtwo]"); } public void testBrokenSpecShouldThrowUsefulExceptionWhenParsingFailsOnParams() throws Exception { @@ -107,37 +121,47 @@ private void parseAndExpectIllegalArgumentException(String brokenJson, String lo } // see params section is broken, an inside param is missing - private static final String BROKEN_SPEC_PARAMS = "{\n" - + " \"ping\": {" - + " \"documentation\": \"http://www.elasticsearch.org/guide/\"," - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"url\": {" - + " \"paths\": [{\"path\": \"path\", \"methods\": [\"HEAD\"]}]" - + " }," - + " \"params\": {" - + " \"type\" : \"boolean\",\n" - + " \"description\" : \"Whether specified concrete indices should be ignored when unavailable (missing or closed)\"\n" - + " }," - + " \"body\": null" - + " }" - + "}"; + private static final String BROKEN_SPEC_PARAMS = """ + { + "ping": { + "documentation": "http://www.elasticsearch.org/guide/", + "stability": "stable", + "visibility": "public", + "url": { + "paths": [ + { + "path": "path", + "methods": [ + "HEAD" + ] + } + ] + }, + "params": { + "type": "boolean", + "description": "Whether specified concrete indices should be ignored when unavailable (missing or closed)" + }, + "body": null + } + }"""; // see parts section is broken, an inside param is missing - private static final String BROKEN_SPEC_PARTS = "{\n" - + " \"ping\": {" - + " \"documentation\": \"http://www.elasticsearch.org/guide/\"," - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"url\": {" - + " \"paths\": [{ \"path\":\"/\", \"parts\": { \"type\":\"boolean\",}}]," - + " \"params\": {\n" - + " \"ignore_unavailable\": {\n" - + " \"type\" : \"boolean\",\n" - + " \"description\" : \"Whether specified concrete indices should be ignored when unavailable (missing or closed)\"\n" - + " } \n" - + " }," - + " \"body\": null" - + " }" - + "}"; + private static final String BROKEN_SPEC_PARTS = """ + { + "ping": { + "documentation": "http://www.elasticsearch.org/guide/", + "stability": "stable", + "visibility": "public", + "url": { + "paths": [{ "path":"/", "parts": { "type":"boolean",}}], + "params": { + "ignore_unavailable": { + "type" : "boolean", + "description" : "Whether specified concrete indices should be ignored when unavailable (missing or closed)" + } + }, + "body": null + } + } + """; } diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java index ef437b39c8170..27367035a7df1 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiParserTests.java @@ -122,28 +122,29 @@ public void testParseRestSpecCountApi() throws Exception { } public void testRequiredBodyWithoutUrlParts() throws Exception { - String spec = "{\n" - + " \"count\": {\n" - + " \"documentation\": \"whatever\",\n" - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"url\": {\n" - + " \"paths\": [ \n" - + " {\n" - + " \"path\":\"/whatever\",\n" - + " \"methods\":[\n" - + " \"POST\",\n" - + " \"GET\"\n" - + " ]\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"body\": {\n" - + " \"description\" : \"whatever\",\n" - + " \"required\" : true\n" - + " }\n" - + " }\n" - + "}"; + String spec = """ + { + "count": { + "documentation": "whatever", + "stability": "stable", + "visibility": "public", + "url": { + "paths": [\s + { + "path":"/whatever", + "methods":[ + "POST", + "GET" + ] + } + ] + }, + "body": { + "description" : "whatever", + "required" : true + } + } + }"""; parser = createParser(YamlXContent.yamlXContent, spec); ClientYamlSuiteRestApi restApi = new ClientYamlSuiteRestApiParser().parse("count.json", parser); @@ -155,195 +156,201 @@ public void testRequiredBodyWithoutUrlParts() throws Exception { assertThat(restApi.isBodyRequired(), equalTo(true)); } - private static final String REST_SPEC_COUNT_API = "{\n" - + " \"count\":{\n" - + " \"documentation\":{\n" - + " \"url\":\"https://www.elastic.co/guide/en/elasticsearch/reference/master/search-count.html\",\n" - + " \"description\":\"Returns number of documents matching a query.\"\n" - + " },\n" - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"headers\": { \"accept\": [\"application/json\"] },\n" - + " \"url\":{\n" - + " \"paths\":[\n" - + " {\n" - + " \"path\":\"/_count\",\n" - + " \"methods\":[\n" - + " \"POST\",\n" - + " \"GET\"\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/_count\",\n" - + " \"methods\":[\n" - + " \"POST\",\n" - + " \"GET\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"index\":{\n" - + " \"type\":\"list\",\n" - + " \"description\":\"A comma-separated list of indices to restrict the results\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/{type}/_count\",\n" - + " \"methods\":[\n" - + " \"POST\",\n" - + " \"GET\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"index\":{\n" - + " \"type\":\"list\",\n" - + " \"description\":\"A comma-separated list of indices to restrict the results\"\n" - + " },\n" - + " \"type\":{\n" - + " \"type\":\"list\",\n" - + " \"description\":\"A comma-separated list of types to restrict the results\",\n" - + " \"deprecated\":true\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"params\":{\n" - + " \"ignore_unavailable\":{\n" - + " \"type\":\"boolean\",\n" - + " \"description\":\"Whether specified concrete indices should be ignored when unavailable (missing or closed)\"\n" - + " }\n" - + " },\n" - + " \"body\":{\n" - + " \"description\":\"A query to restrict the results specified with the Query DSL (optional)\",\n" - + " \"content_type\": [\"application/json\"]\n" - + " }\n" - + " }\n" - + "}\n\n"; + private static final String REST_SPEC_COUNT_API = """ + { + "count":{ + "documentation":{ + "url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/search-count.html", + "description":"Returns number of documents matching a query." + }, + "stability": "stable", + "visibility": "public", + "headers": { "accept": ["application/json"] }, + "url":{ + "paths":[ + { + "path":"/_count", + "methods":[ + "POST", + "GET" + ] + }, + { + "path":"/{index}/_count", + "methods":[ + "POST", + "GET" + ], + "parts":{ + "index":{ + "type":"list", + "description":"A comma-separated list of indices to restrict the results" + } + } + }, + { + "path":"/{index}/{type}/_count", + "methods":[ + "POST", + "GET" + ], + "parts":{ + "index":{ + "type":"list", + "description":"A comma-separated list of indices to restrict the results" + }, + "type":{ + "type":"list", + "description":"A comma-separated list of types to restrict the results", + "deprecated":true + } + } + } + ] + }, + "params":{ + "ignore_unavailable":{ + "type":"boolean", + "description":"Whether specified concrete indices should be ignored when unavailable (missing or closed)" + } + }, + "body":{ + "description":"A query to restrict the results specified with the Query DSL (optional)", + "content_type": ["application/json"] + } + } + } - private static final String REST_SPEC_GET_TEMPLATE_API = "{\n" - + " \"indices.get_template\":{\n" - + " \"documentation\":{\n" - + " \"url\":\"https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html\",\n" - + " \"description\":\"Returns an index template.\"\n" - + " },\n" - + " \"headers\": { \"accept\": [\"application/json\"] },\n" - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"url\":{\n" - + " \"paths\":[\n" - + " {\n" - + " \"path\":\"/_template\",\n" - + " \"methods\":[\n" - + " \"GET\"\n" - + " ]\n" - + " },\n" - + " {\n" - + " \"path\":\"/_template/{name}\",\n" - + " \"methods\":[\n" - + " \"GET\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"name\":{\n" - + " \"type\":\"list\",\n" - + " \"description\":\"The comma separated names of the index templates\"\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + "}\n"; + """; - private static final String REST_SPEC_INDEX_API = "{\n" - + " \"index\":{\n" - + " \"documentation\":{\n" - + " \"url\":\"https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html\",\n" - + " \"description\":\"Creates or updates a document in an index.\"\n" - + " },\n" - + " \"stability\": \"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"headers\": { " - + " \"accept\": [\"application/json\"],\n " - + " \"content_type\": [\"application/json\", \"a/mime-type\"]\n " - + " },\n" - + " \"url\":{\n" - + " \"paths\":[\n" - + " {\n" - + " \"path\":\"/{index}/{type}\",\n" - + " \"methods\":[\n" - + " \"POST\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The name of the index\"\n" - + " },\n" - + " \"type\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The type of the document\",\n" - + " \"deprecated\":true\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/{type}/{id}\",\n" - + " \"methods\":[\n" - + " \"PUT\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"id\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"Document ID\"\n" - + " },\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The name of the index\"\n" - + " },\n" - + " \"type\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The type of the document\",\n" - + " \"deprecated\":true\n" - + " }\n" - + " },\n" - + " \"deprecated\":{\n" - + " \"version\":\"7.0.0\",\n" - + " \"description\":\"Specifying types in urls has been deprecated\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"params\":{\n" - + " \"wait_for_active_shards\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"Sets the number of shard copies that must be active before proceeding with the index operation. \"\n" - + " },\n" - + " \"op_type\":{\n" - + " \"type\":\"enum\",\n" - + " \"options\":[\n" - + " \"index\",\n" - + " \"create\"\n" - + " ],\n" - + " \"default\":\"index\",\n" - + " \"description\":\"Explicit operation type\"\n" - + " },\n" - + " \"refresh\":{\n" - + " \"type\":\"enum\",\n" - + " \"options\":[\n" - + " \"true\",\n" - + " \"false\",\n" - + " \"wait_for\"\n" - + " ],\n" - + " \"description\":\"If `true` then refresh the affected shards to make this operation visible to search\"\n" - + " },\n" - + " \"routing\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"Specific routing value\"\n" - + " }\n" - + " },\n" - + " \"body\":{\n" - + " \"description\":\"The document\",\n" - + " \"content_type\": [\"application/json\"],\n" - + " \"required\":true\n" - + " }\n" - + " }\n" - + "}\n"; + private static final String REST_SPEC_GET_TEMPLATE_API = """ + { + "indices.get_template":{ + "documentation":{ + "url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-templates.html", + "description":"Returns an index template." + }, + "headers": { "accept": ["application/json"] }, + "stability": "stable", + "visibility": "public", + "url":{ + "paths":[ + { + "path":"/_template", + "methods":[ + "GET" + ] + }, + { + "path":"/_template/{name}", + "methods":[ + "GET" + ], + "parts":{ + "name":{ + "type":"list", + "description":"The comma separated names of the index templates" + } + } + } + ] + } + } + } + """; + + private static final String REST_SPEC_INDEX_API = """ + { + "index":{ + "documentation":{ + "url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html", + "description":"Creates or updates a document in an index." + }, + "stability": "stable", + "visibility": "public", + "headers": { "accept": ["application/json"], + "content_type": ["application/json", "a/mime-type"] + }, + "url":{ + "paths":[ + { + "path":"/{index}/{type}", + "methods":[ + "POST" + ], + "parts":{ + "index":{ + "type":"string", + "description":"The name of the index" + }, + "type":{ + "type":"string", + "description":"The type of the document", + "deprecated":true + } + } + }, + { + "path":"/{index}/{type}/{id}", + "methods":[ + "PUT" + ], + "parts":{ + "id":{ + "type":"string", + "description":"Document ID" + }, + "index":{ + "type":"string", + "description":"The name of the index" + }, + "type":{ + "type":"string", + "description":"The type of the document", + "deprecated":true + } + }, + "deprecated":{ + "version":"7.0.0", + "description":"Specifying types in urls has been deprecated" + } + } + ] + }, + "params":{ + "wait_for_active_shards":{ + "type":"string", + "description":"Sets the number of shard copies that must be active before proceeding with the index operation. " + }, + "op_type":{ + "type":"enum", + "options":[ + "index", + "create" + ], + "default":"index", + "description":"Explicit operation type" + }, + "refresh":{ + "type":"enum", + "options":[ + "true", + "false", + "wait_for" + ], + "description":"If `true` then refresh the affected shards to make this operation visible to search" + }, + "routing":{ + "type":"string", + "description":"Specific routing value" + } + }, + "body":{ + "description":"The document", + "content_type": ["application/json"], + "required":true + } + } + } + """; } diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiTests.java index c06383fa7f8ef..ccff64e0fd797 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/restspec/ClientYamlSuiteRestApiTests.java @@ -67,241 +67,243 @@ public void testPathMatching() throws IOException { } } - private static final String COMMON_SPEC = "{\n" - + " \"documentation\" : {\n" - + " \"url\": \"Parameters that are accepted by all API endpoints.\",\n" - + " \"documentation\": \"https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html\"\n" - + " },\n" - + " \"params\": {\n" - + " \"pretty\": {\n" - + " \"type\": \"boolean\",\n" - + " \"description\": \"Pretty format the returned JSON response.\",\n" - + " \"default\": false\n" - + " },\n" - + " \"human\": {\n" - + " \"type\": \"boolean\",\n" - + " \"description\": \"Return human readable values for statistics.\",\n" - + " \"default\": true\n" - + " },\n" - + " \"error_trace\": {\n" - + " \"type\": \"boolean\",\n" - + " \"description\": \"Include the stack trace of returned errors.\",\n" - + " \"default\": false\n" - + " },\n" - + " \"source\": {\n" - + " \"type\": \"string\",\n" - + " \"description\": \"The URL-encoded request definition." - + " Useful for libraries that do not accept a request body for non-POST requests.\"\n" - + " },\n" - + " \"filter_path\": {\n" - + " \"type\": \"list\",\n" - + " \"description\": \"A comma-separated list of filters used to reduce the response.\"\n" - + " }\n" - + " }\n" - + "}\n"; - - private static final String REST_SPEC_API = "{\n" - + " \"index\":{\n" - + " \"documentation\":{\n" - + " \"url\":\"https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html\",\n" - + " \"description\":\"Creates or updates a document in an index.\"\n" - + " },\n" - + " \"stability\":\"stable\",\n" - + " \"visibility\": \"public\",\n" - + " \"headers\": { \"accept\": [\"application/json\"] },\n" - + " \"url\":{\n" - + " \"paths\":[\n" - + " {\n" - + " \"path\":\"/_doc\",\n" - + " \"methods\":[\n" - + " \"PUT\"\n" - + " ],\n" - + " \"parts\":{\n" - + " }\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/_mapping/{type}\",\n" - + " \"methods\":[\n" - + " \"PUT\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"required\":true,\n" - + " \"description\":\"The name of the index\"\n" - + " },\n" - + " \"type\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The type of the document\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/_mappings/{type}\",\n" - + " \"methods\":[\n" - + " \"PUT\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"required\":true,\n" - + " \"description\":\"The name of the index\"\n" - + " },\n" - + " \"type\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The type of the document\"\n" - + " }\n" - + " }\n" - + " },\n" - + + private static final String COMMON_SPEC = """ + { + "documentation" : { + "url": "Parameters that are accepted by all API endpoints.", + "documentation": "https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html" + }, + "params": { + "pretty": { + "type": "boolean", + "description": "Pretty format the returned JSON response.", + "default": false + }, + "human": { + "type": "boolean", + "description": "Return human readable values for statistics.", + "default": true + }, + "error_trace": { + "type": "boolean", + "description": "Include the stack trace of returned errors.", + "default": false + }, + "source": { + "type": "string", + "description": "The URL-encoded request definition. Useful for libraries that do not accept a request body\ + for non-POST requests." + }, + "filter_path": { + "type": "list", + "description": "A comma-separated list of filters used to reduce the response." + } + } + } + """; - " {\n" - + " \"path\":\"/{index}/_doc/{id}\",\n" - + " \"methods\":[\n" - + " \"PUT\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"id\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"Document ID\"\n" - + " },\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"required\":true,\n" - + " \"description\":\"The name of the index\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/_doc\",\n" - + " \"methods\":[\n" - + " \"POST\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"required\":true,\n" - + " \"description\":\"The name of the index\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/{type}\",\n" - + " \"methods\":[\n" - + " \"POST\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"required\":true,\n" - + " \"description\":\"The name of the index\"\n" - + " },\n" - + " \"type\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The type of the document\",\n" - + " \"deprecated\":true\n" - + " }\n" - + " },\n" - + " \"deprecated\":{\n" - + " \"version\":\"7.0.0\",\n" - + " \"description\":\"Specifying types in urls has been deprecated\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"path\":\"/{index}/{type}/{id}\",\n" - + " \"methods\":[\n" - + " \"PUT\"\n" - + " ],\n" - + " \"parts\":{\n" - + " \"id\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"Document ID\"\n" - + " },\n" - + " \"index\":{\n" - + " \"type\":\"string\",\n" - + " \"required\":true,\n" - + " \"description\":\"The name of the index\"\n" - + " },\n" - + " \"type\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The type of the document\",\n" - + " \"deprecated\":true\n" - + " }\n" - + " },\n" - + " \"deprecated\":{\n" - + " \"version\":\"7.0.0\",\n" - + " \"description\":\"Specifying types in urls has been deprecated\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"params\":{\n" - + " \"wait_for_active_shards\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"Sets the number of shard copies that must be active before proceeding with the index operation. " - + "Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value less " - + "than or equal to the total number of copies for the shard (number of replicas + 1)\"\n" - + " },\n" - + " \"op_type\":{\n" - + " \"type\":\"enum\",\n" - + " \"options\":[\n" - + " \"index\",\n" - + " \"create\"\n" - + " ],\n" - + " \"default\":\"index\",\n" - + " \"description\":\"Explicit operation type\"\n" - + " },\n" - + " \"refresh\":{\n" - + " \"type\":\"enum\",\n" - + " \"options\":[\n" - + " \"true\",\n" - + " \"false\",\n" - + " \"wait_for\"\n" - + " ],\n" - + " \"description\":\"If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` " - + "then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes.\"\n" - + " },\n" - + " \"routing\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"Specific routing value\"\n" - + " },\n" - + " \"timeout\":{\n" - + " \"type\":\"time\",\n" - + " \"description\":\"Explicit operation timeout\"\n" - + " },\n" - + " \"version\":{\n" - + " \"type\":\"number\",\n" - + " \"description\":\"Explicit version number for concurrency control\"\n" - + " },\n" - + " \"version_type\":{\n" - + " \"type\":\"enum\",\n" - + " \"options\":[\n" - + " \"internal\",\n" - + " \"external\",\n" - + " \"external_gte\",\n" - + " \"force\"\n" - + " ],\n" - + " \"description\":\"Specific version type\"\n" - + " },\n" - + " \"if_seq_no\":{\n" - + " \"type\":\"number\",\n" - + " \"description\":\"only perform the index operation if the last operation that has changed the document has the " - + "specified sequence number\"\n" - + " },\n" - + " \"if_primary_term\":{\n" - + " \"type\":\"number\",\n" - + " \"description\":\"only perform the index operation if the last operation that has changed the document has the " - + "specified primary term\"\n" - + " },\n" - + " \"pipeline\":{\n" - + " \"type\":\"string\",\n" - + " \"description\":\"The pipeline id to preprocess incoming documents with\"\n" - + " }\n" - + " },\n" - + " \"body\":{\n" - + " \"description\":\"The document\",\n" - + " \"required\":true\n" - + " }\n" - + " }\n" - + "}\n"; + private static final String REST_SPEC_API = """ + { + "index":{ + "documentation":{ + "url":"https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-index_.html", + "description":"Creates or updates a document in an index." + }, + "stability":"stable", + "visibility": "public", + "headers": { "accept": ["application/json"] }, + "url":{ + "paths":[ + { + "path":"/_doc", + "methods":[ + "PUT" + ], + "parts":{ + } + }, + { + "path":"/{index}/_mapping/{type}", + "methods":[ + "PUT" + ], + "parts":{ + "index":{ + "type":"string", + "required":true, + "description":"The name of the index" + }, + "type":{ + "type":"string", + "description":"The type of the document" + } + } + }, + { + "path":"/{index}/_mappings/{type}", + "methods":[ + "PUT" + ], + "parts":{ + "index":{ + "type":"string", + "required":true, + "description":"The name of the index" + }, + "type":{ + "type":"string", + "description":"The type of the document" + } + } + }, + { + "path":"/{index}/_doc/{id}", + "methods":[ + "PUT" + ], + "parts":{ + "id":{ + "type":"string", + "description":"Document ID" + }, + "index":{ + "type":"string", + "required":true, + "description":"The name of the index" + } + } + }, + { + "path":"/{index}/_doc", + "methods":[ + "POST" + ], + "parts":{ + "index":{ + "type":"string", + "required":true, + "description":"The name of the index" + } + } + }, + { + "path":"/{index}/{type}", + "methods":[ + "POST" + ], + "parts":{ + "index":{ + "type":"string", + "required":true, + "description":"The name of the index" + }, + "type":{ + "type":"string", + "description":"The type of the document", + "deprecated":true + } + }, + "deprecated":{ + "version":"7.0.0", + "description":"Specifying types in urls has been deprecated" + } + }, + { + "path":"/{index}/{type}/{id}", + "methods":[ + "PUT" + ], + "parts":{ + "id":{ + "type":"string", + "description":"Document ID" + }, + "index":{ + "type":"string", + "required":true, + "description":"The name of the index" + }, + "type":{ + "type":"string", + "description":"The type of the document", + "deprecated":true + } + }, + "deprecated":{ + "version":"7.0.0", + "description":"Specifying types in urls has been deprecated" + } + } + ] + }, + "params":{ + "wait_for_active_shards":{ + "type":"string", + "description":"Sets the number of shard copies that must be active before proceeding with the index operation.\ + Defaults to 1, meaning the primary shard only. Set to `all` for all shard copies, otherwise set to any non-negative value\ + less than or equal to the total number of copies for the shard (number of replicas + 1)" + }, + "op_type":{ + "type":"enum", + "options":[ + "index", + "create" + ], + "default":"index", + "description":"Explicit operation type" + }, + "refresh":{ + "type":"enum", + "options":[ + "true", + "false", + "wait_for" + ], + "description":"If `true` then refresh the affected shards to make this operation visible to search, if `wait_for`\ + then wait for a refresh to make this operation visible to search, if `false` (the default) then do nothing with refreshes." + }, + "routing":{ + "type":"string", + "description":"Specific routing value" + }, + "timeout":{ + "type":"time", + "description":"Explicit operation timeout" + }, + "version":{ + "type":"number", + "description":"Explicit version number for concurrency control" + }, + "version_type":{ + "type":"enum", + "options":[ + "internal", + "external", + "external_gte", + "force" + ], + "description":"Specific version type" + }, + "if_seq_no":{ + "type":"number", + "description":"only perform the index operation if the last operation that has changed the document has the specified\ + sequence number" + }, + "if_primary_term":{ + "type":"number", + "description":"only perform the index operation if the last operation that has changed the document has the specified\ + primary term" + }, + "pipeline":{ + "type":"string", + "description":"The pipeline id to preprocess incoming documents with" + } + }, + "body":{ + "description":"The document", + "required":true + } + } + } + """; } diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSectionTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSectionTests.java index 3ec6454d06d8c..6a692e29926de 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSectionTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSectionTests.java @@ -24,13 +24,11 @@ public class ClientYamlTestSectionTests extends AbstractClientYamlTestFragmentParserTestCase { public void testWrongIndentation() throws Exception { { - XContentParser parser = createParser( - YamlXContent.yamlXContent, - "\"First test section\": \n" - + " - skip:\n" - + " version: \"2.0.0 - 2.2.0\"\n" - + " reason: \"Update doesn't return metadata fields, waiting for #3259\"" - ); + XContentParser parser = createParser(YamlXContent.yamlXContent, """ + "First test section":\s + - skip: + version: "2.0.0 - 2.2.0" + reason: "Update doesn't return metadata fields, waiting for #3259\""""); ParsingException e = expectThrows(ParsingException.class, () -> ClientYamlTestSection.parse(parser)); assertEquals("Error parsing test named [First test section]", e.getMessage()); @@ -41,15 +39,13 @@ public void testWrongIndentation() throws Exception { ); } { - XContentParser parser = createParser( - YamlXContent.yamlXContent, - "\"First test section\": \n" - + " - do :\n" - + " catch: missing\n" - + " indices.get_warmer:\n" - + " index: test_index\n" - + " name: test_warmer" - ); + XContentParser parser = createParser(YamlXContent.yamlXContent, """ + "First test section":\s + - do : + catch: missing + indices.get_warmer: + index: test_index + name: test_warmer"""); ParsingException e = expectThrows(ParsingException.class, () -> ClientYamlTestSection.parse(parser)); assertEquals("Error parsing test named [First test section]", e.getMessage()); assertThat(e.getCause(), instanceOf(IOException.class)); @@ -62,15 +58,13 @@ public void testWrongIndentation() throws Exception { } public void testParseTestSectionWithDoSection() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "\"First test section\": \n" - + " - do :\n" - + " catch: missing\n" - + " indices.get_warmer:\n" - + " index: test_index\n" - + " name: test_warmer" - ); + parser = createParser(YamlXContent.yamlXContent, """ + "First test section":\s + - do : + catch: missing + indices.get_warmer: + index: test_index + name: test_warmer"""); ClientYamlTestSection testSection = ClientYamlTestSection.parse(parser); @@ -87,19 +81,17 @@ public void testParseTestSectionWithDoSection() throws Exception { } public void testParseTestSectionWithDoSetAndSkipSectionsNoSkip() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "\"First test section\": \n" - + " - skip:\n" - + " version: \"6.0.0 - 6.2.0\"\n" - + " reason: \"Update doesn't return metadata fields, waiting for #3259\"\n" - + " - do :\n" - + " catch: missing\n" - + " indices.get_warmer:\n" - + " index: test_index\n" - + " name: test_warmer\n" - + " - set: {_scroll_id: scroll_id}" - ); + parser = createParser(YamlXContent.yamlXContent, """ + "First test section":\s + - skip: + version: "6.0.0 - 6.2.0" + reason: "Update doesn't return metadata fields, waiting for #3259" + - do : + catch: missing + indices.get_warmer: + index: test_index + name: test_warmer + - set: {_scroll_id: scroll_id}"""); ClientYamlTestSection testSection = ClientYamlTestSection.parse(parser); @@ -122,22 +114,20 @@ public void testParseTestSectionWithDoSetAndSkipSectionsNoSkip() throws Exceptio } public void testParseTestSectionWithMultipleDoSections() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "\"Basic\":\n" - + "\n" - + " - do:\n" - + " index:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 中文\n" - + " body: { \"foo\": \"Hello: 中文\" }\n" - + " - do:\n" - + " get:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 中文" - ); + parser = createParser(YamlXContent.yamlXContent, """ + "Basic": + + - do: + index: + index: test_1 + type: test + id: 中文 + body: { "foo": "Hello: 中文" } + - do: + get: + index: test_1 + type: test + id: 中文"""); ClientYamlTestSection testSection = ClientYamlTestSection.parse(parser); @@ -160,37 +150,35 @@ public void testParseTestSectionWithMultipleDoSections() throws Exception { } public void testParseTestSectionWithDoSectionsAndAssertions() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "\"Basic\":\n" - + "\n" - + " - do:\n" - + " index:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 中文\n" - + " body: { \"foo\": \"Hello: 中文\" }\n" - + "\n" - + " - do:\n" - + " get:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 中文\n" - + "\n" - + " - match: { _index: test_1 }\n" - + " - is_true: _source\n" - + " - match: { _source: { foo: \"Hello: 中文\" } }\n" - + "\n" - + " - do:\n" - + " get:\n" - + " index: test_1\n" - + " id: 中文\n" - + "\n" - + " - length: { _index: 6 }\n" - + " - is_false: whatever\n" - + " - gt: { size: 5 }\n" - + " - lt: { size: 10 }" - ); + parser = createParser(YamlXContent.yamlXContent, """ + "Basic": + + - do: + index: + index: test_1 + type: test + id: 中文 + body: { "foo": "Hello: 中文" } + + - do: + get: + index: test_1 + type: test + id: 中文 + + - match: { _index: test_1 } + - is_true: _source + - match: { _source: { foo: "Hello: 中文" } } + + - do: + get: + index: test_1 + id: 中文 + + - length: { _index: 6 } + - is_false: whatever + - gt: { size: 5 } + - lt: { size: 10 }"""); ClientYamlTestSection testSection = ClientYamlTestSection.parse(parser); @@ -254,15 +242,14 @@ public void testParseTestSectionWithDoSectionsAndAssertions() throws Exception { } public void testSmallSection() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "\"node_info test\":\n" - + " - do:\n" - + " cluster.node_info: {}\n" - + " \n" - + " - is_true: nodes\n" - + " - is_true: cluster_name\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + "node_info test": + - do: + cluster.node_info: {} + \s + - is_true: nodes + - is_true: cluster_name + """); ClientYamlTestSection testSection = ClientYamlTestSection.parse(parser); assertThat(testSection, notNullValue()); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuiteTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuiteTests.java index d69705edbd13b..6f54d5f103f92 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuiteTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/ClientYamlTestSuiteTests.java @@ -36,48 +36,50 @@ public void testParseTestSetupTeardownAndSections() throws Exception { final boolean includeTeardown = randomBoolean(); StringBuilder testSpecBuilder = new StringBuilder(); if (includeSetup) { - testSpecBuilder.append("---\n") - .append("setup:\n") - .append(" - do:\n") - .append(" indices.create:\n") - .append(" index: test_index\n") - .append("\n"); + testSpecBuilder.append(""" + --- + setup: + - do: + indices.create: + index: test_index + + """); } if (includeTeardown) { - testSpecBuilder.append("---\n") - .append("teardown:\n") - .append(" - do:\n") - .append(" indices.delete:\n") - .append(" index: test_index\n") - .append("\n"); + testSpecBuilder.append(""" + --- + teardown: + - do: + indices.delete: + index: test_index + + """); } - parser = createParser( - YamlXContent.yamlXContent, - testSpecBuilder.toString() - + "---\n" - + "\"Get index mapping\":\n" - + " - do:\n" - + " indices.get_mapping:\n" - + " index: test_index\n" - + "\n" - + " - match: {test_index.test_type.properties.text.type: string}\n" - + " - match: {test_index.test_type.properties.text.analyzer: whitespace}\n" - + "\n" - + "---\n" - + "\"Get type mapping - pre 6.0\":\n" - + "\n" - + " - skip:\n" - + " version: \"6.0.0 - \"\n" - + " reason: \"for newer versions the index name is always returned\"\n" - + "\n" - + " - do:\n" - + " indices.get_mapping:\n" - + " index: test_index\n" - + " type: test_type\n" - + "\n" - + " - match: {test_type.properties.text.type: string}\n" - + " - match: {test_type.properties.text.analyzer: whitespace}\n" - ); + parser = createParser(YamlXContent.yamlXContent, testSpecBuilder + """ + --- + "Get index mapping": + - do: + indices.get_mapping: + index: test_index + + - match: {test_index.test_type.properties.text.type: string} + - match: {test_index.test_type.properties.text.analyzer: whitespace} + + --- + "Get type mapping - pre 6.0": + + - skip: + version: "6.0.0 - " + reason: "for newer versions the index name is always returned" + + - do: + indices.get_mapping: + index: test_index + type: test_type + + - match: {test_type.properties.text.type: string} + - match: {test_type.properties.text.analyzer: whitespace} + """); ClientYamlTestSuite restTestSuite = ClientYamlTestSuite.parse(getTestClass().getName(), getTestName(), parser); @@ -164,36 +166,34 @@ public void testParseTestSetupTeardownAndSections() throws Exception { } public void testParseTestSingleTestSection() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "---\n" - + "\"Index with ID\":\n" - + "\n" - + " - do:\n" - + " index:\n" - + " index: test-weird-index-中文\n" - + " type: weird.type\n" - + " id: 1\n" - + " body: { foo: bar }\n" - + "\n" - + " - is_true: ok\n" - + " - match: { _index: test-weird-index-中文 }\n" - + " - match: { _type: weird.type }\n" - + " - match: { _id: \"1\"}\n" - + " - match: { _version: 1}\n" - + "\n" - + " - do:\n" - + " get:\n" - + " index: test-weird-index-中文\n" - + " type: weird.type\n" - + " id: 1\n" - + "\n" - + " - match: { _index: test-weird-index-中文 }\n" - + " - match: { _type: weird.type }\n" - + " - match: { _id: \"1\"}\n" - + " - match: { _version: 1}\n" - + " - match: { _source: { foo: bar }}" - ); + parser = createParser(YamlXContent.yamlXContent, """ + --- + "Index with ID": + + - do: + index: + index: test-weird-index-中文 + type: weird.type + id: 1 + body: { foo: bar } + + - is_true: ok + - match: { _index: test-weird-index-中文 } + - match: { _type: weird.type } + - match: { _id: "1"} + - match: { _version: 1} + + - do: + get: + index: test-weird-index-中文 + type: weird.type + id: 1 + + - match: { _index: test-weird-index-中文 } + - match: { _type: weird.type } + - match: { _id: "1"} + - match: { _version: 1} + - match: { _source: { foo: bar }}"""); ClientYamlTestSuite restTestSuite = ClientYamlTestSuite.parse(getTestClass().getName(), getTestName(), parser); @@ -262,51 +262,50 @@ public void testParseTestSingleTestSection() throws Exception { } public void testParseTestMultipleTestSections() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "---\n" - + "\"Missing document (partial doc)\":\n" - + "\n" - + " - do:\n" - + " catch: missing\n" - + " update:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body: { doc: { foo: bar } }\n" - + "\n" - + " - do:\n" - + " update:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body: { doc: { foo: bar } }\n" - + " ignore: 404\n" - + "\n" - + "---\n" - + "\"Missing document (script)\":\n" - + "\n" - + "\n" - + " - do:\n" - + " catch: missing\n" - + " update:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body:\n" - + " script: \"ctx._source.foo = bar\"\n" - + " params: { bar: 'xxx' }\n" - + "\n" - + " - do:\n" - + " update:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " ignore: 404\n" - + " body:\n" - + " script: \"ctx._source.foo = bar\"\n" - + " params: { bar: 'xxx' }\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + --- + "Missing document (partial doc)": + + - do: + catch: missing + update: + index: test_1 + type: test + id: 1 + body: { doc: { foo: bar } } + + - do: + update: + index: test_1 + type: test + id: 1 + body: { doc: { foo: bar } } + ignore: 404 + + --- + "Missing document (script)": + + + - do: + catch: missing + update: + index: test_1 + type: test + id: 1 + body: + script: "ctx._source.foo = bar" + params: { bar: 'xxx' } + + - do: + update: + index: test_1 + type: test + id: 1 + ignore: 404 + body: + script: "ctx._source.foo = bar" + params: { bar: 'xxx' } + """); ClientYamlTestSuite restTestSuite = ClientYamlTestSuite.parse(getTestClass().getName(), getTestName(), parser); @@ -353,34 +352,33 @@ public void testParseTestMultipleTestSections() throws Exception { } public void testParseTestDuplicateTestSections() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "---\n" - + "\"Missing document (script)\":\n" - + "\n" - + " - do:\n" - + " catch: missing\n" - + " update:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body: { doc: { foo: bar } }\n" - + "\n" - + "---\n" - + "\"Missing document (script)\":\n" - + "\n" - + "\n" - + " - do:\n" - + " catch: missing\n" - + " update:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body:\n" - + " script: \"ctx._source.foo = bar\"\n" - + " params: { bar: 'xxx' }\n" - + "\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + --- + "Missing document (script)": + + - do: + catch: missing + update: + index: test_1 + type: test + id: 1 + body: { doc: { foo: bar } } + + --- + "Missing document (script)": + + + - do: + catch: missing + update: + index: test_1 + type: test + id: 1 + body: + script: "ctx._source.foo = bar" + params: { bar: 'xxx' } + + """); Exception e = expectThrows( ParsingException.class, @@ -390,23 +388,22 @@ public void testParseTestDuplicateTestSections() throws Exception { } public void testParseSkipOs() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "\"Broken on some os\":\n" - + "\n" - + " - skip:\n" - + " features: skip_os\n" - + " os: [\"windows95\", \"debian-5\"]\n" - + " reason: \"not supported\"\n" - + "\n" - + " - do:\n" - + " indices.get_mapping:\n" - + " index: test_index\n" - + " type: test_type\n" - + "\n" - + " - match: {test_type.properties.text.type: string}\n" - + " - match: {test_type.properties.text.analyzer: whitespace}\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + "Broken on some os": + + - skip: + features: skip_os + os: ["windows95", "debian-5"] + reason: "not supported" + + - do: + indices.get_mapping: + index: test_index + type: test_type + + - match: {test_type.properties.text.type: string} + - match: {test_type.properties.text.analyzer: whitespace} + """); ClientYamlTestSuite restTestSuite = ClientYamlTestSuite.parse(getTestClass().getName(), getTestName(), parser); @@ -451,16 +448,11 @@ public void testAddingDoWithWarningWithoutSkipWarnings() { doSection.setApiCallSection(new ApiCallSection("test")); ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertThat( - e.getMessage(), - containsString( - "api/name:\nattempted to add a [do] with a [warnings] section without a corresponding " - + "[\"skip\": \"features\": \"warnings\"] so runners that do not support the [warnings] section can skip the test " - + "at line [" - + lineNumber - + "]" - ) - ); + assertThat(e.getMessage(), containsString(""" + api/name: + attempted to add a [do] with a [warnings] section without a corresponding ["skip": "features": "warnings"] \ + so runners that do not support the [warnings] section can skip the test at line [%d]\ + """.formatted(lineNumber))); } public void testAddingDoWithWarningRegexWithoutSkipWarnings() { @@ -470,16 +462,11 @@ public void testAddingDoWithWarningRegexWithoutSkipWarnings() { doSection.setApiCallSection(new ApiCallSection("test")); ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertThat( - e.getMessage(), - containsString( - "api/name:\nattempted to add a [do] with a [warnings_regex] section without a corresponding " - + "[\"skip\": \"features\": \"warnings_regex\"] so runners that do not support the [warnings_regex] section can " - + "skip the test at line [" - + lineNumber - + "]" - ) - ); + assertThat(e.getMessage(), containsString(""" + api/name: + attempted to add a [do] with a [warnings_regex] section without a corresponding ["skip": "features": "warnings_regex"] \ + so runners that do not support the [warnings_regex] section can skip the test at line [%d]\ + """.formatted(lineNumber))); } public void testAddingDoWithAllowedWarningWithoutSkipAllowedWarnings() { @@ -489,16 +476,12 @@ public void testAddingDoWithAllowedWarningWithoutSkipAllowedWarnings() { doSection.setApiCallSection(new ApiCallSection("test")); ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertThat( - e.getMessage(), - containsString( - "api/name:\nattempted to add a [do] with a [allowed_warnings] " - + "section without a corresponding [\"skip\": \"features\": \"allowed_warnings\"] so runners that do not " - + "support the [allowed_warnings] section can skip the test at line [" - + lineNumber - + "]" - ) - ); + assertThat(e.getMessage(), containsString(""" + api/name: + attempted to add a [do] with a [allowed_warnings] section without a corresponding ["skip": "features": \ + "allowed_warnings"] so runners that do not support the [allowed_warnings] section can skip the test at \ + line [%d]\ + """.formatted(lineNumber))); } public void testAddingDoWithAllowedWarningRegexWithoutSkipAllowedWarnings() { @@ -508,16 +491,12 @@ public void testAddingDoWithAllowedWarningRegexWithoutSkipAllowedWarnings() { doSection.setApiCallSection(new ApiCallSection("test")); ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertThat( - e.getMessage(), - containsString( - "api/name:\nattempted to add a [do] with a [allowed_warnings_regex] " - + "section without a corresponding [\"skip\": \"features\": \"allowed_warnings_regex\"] so runners that do not " - + "support the [allowed_warnings_regex] section can skip the test at line [" - + lineNumber - + "]" - ) - ); + assertThat(e.getMessage(), containsString(""" + api/name: + attempted to add a [do] with a [allowed_warnings_regex] section without a corresponding ["skip": "features": \ + "allowed_warnings_regex"] so runners that do not support the [allowed_warnings_regex] section can skip the test \ + at line [%d]\ + """.formatted(lineNumber))); } public void testAddingDoWithHeaderWithoutSkipHeaders() { @@ -528,16 +507,11 @@ public void testAddingDoWithHeaderWithoutSkipHeaders() { doSection.setApiCallSection(apiCallSection); ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertThat( - e.getMessage(), - containsString( - "api/name:\nattempted to add a [do] with a [headers] section without a corresponding " - + "[\"skip\": \"features\": \"headers\"] so runners that do not support the [headers] section can skip the " - + "test at line [" - + lineNumber - + "]" - ) - ); + assertThat(e.getMessage(), containsString(""" + api/name: + attempted to add a [do] with a [headers] section without a corresponding ["skip": "features": "headers"] \ + so runners that do not support the [headers] section can skip the test at line [%d]\ + """.formatted(lineNumber))); } public void testAddingDoWithNodeSelectorWithoutSkipNodeSelector() { @@ -548,16 +522,11 @@ public void testAddingDoWithNodeSelectorWithoutSkipNodeSelector() { doSection.setApiCallSection(apiCall); ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertThat( - e.getMessage(), - containsString( - "api/name:\nattempted to add a [do] with a [node_selector] section without a " - + "corresponding [\"skip\": \"features\": \"node_selector\"] so runners that do not support the [node_selector] " - + "section can skip the test at line [" - + lineNumber - + "]" - ) - ); + assertThat(e.getMessage(), containsString(""" + api/name: + attempted to add a [do] with a [node_selector] section without a corresponding ["skip": "features": "node_selector"] \ + so runners that do not support the [node_selector] section can skip the test at line [%d]\ + """.formatted(lineNumber))); } public void testAddingContainsWithoutSkipContains() { @@ -569,16 +538,11 @@ public void testAddingContainsWithoutSkipContains() { ); ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, containsAssertion); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertThat( - e.getMessage(), - containsString( - "api/name:\nattempted to add a [contains] assertion without a corresponding " - + "[\"skip\": \"features\": \"contains\"] so runners that do not support the [contains] assertion " - + "can skip the test at line [" - + lineNumber - + "]" - ) - ); + assertThat(e.getMessage(), containsString(""" + api/name: + attempted to add a [contains] assertion without a corresponding ["skip": "features": "contains"] \ + so runners that do not support the [contains] assertion can skip the test at line [%d]\ + """.formatted(lineNumber))); } public void testMultipleValidationErrors() { @@ -619,23 +583,16 @@ public void testMultipleValidationErrors() { ClientYamlTestSuite testSuite = new ClientYamlTestSuite("api", "name", SetupSection.EMPTY, TeardownSection.EMPTY, sections); Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate); - assertEquals( - "api/name:\n" - + "attempted to add a [contains] assertion without a corresponding [\"skip\": \"features\": \"contains\"] so runners " - + "that do not support the [contains] assertion can skip the test at line [" - + firstLineNumber - + "],\n" - + "attempted to add a [do] with a [warnings] section without a corresponding [\"skip\": \"features\": \"warnings\"] so " - + "runners that do not support the [warnings] section can skip the test at line [" - + secondLineNumber - + "],\n" - + "attempted to add a [do] with a [node_selector] section without a corresponding " - + "[\"skip\": \"features\": \"node_selector\"] " - + "so runners that do not support the [node_selector] section can skip the test at line [" - + thirdLineNumber - + "]", - e.getMessage() - ); + assertEquals(""" + api/name: + attempted to add a [contains] assertion without a corresponding ["skip": "features": "contains"] so runners that \ + do not support the [contains] assertion can skip the test at line [%d], + attempted to add a [do] with a [warnings] section without a corresponding ["skip": "features": "warnings"] so runners \ + that do not support the [warnings] section can skip the test at line [%d], + attempted to add a [do] with a [node_selector] section without a corresponding ["skip": "features": "node_selector"] so \ + runners that do not support the [node_selector] section can skip the test \ + at line [%d]\ + """.formatted(firstLineNumber, secondLineNumber, thirdLineNumber), e.getMessage()); } public void testAddingDoWithWarningWithSkip() { diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/DoSectionTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/DoSectionTests.java index 4ee9a2bd7641e..fdd3451012d5c 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/DoSectionTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/DoSectionTests.java @@ -150,15 +150,10 @@ public void testWarningHeadersRegex() { // require header and it matches (realistic example) section = new DoSection(new XContentLocation(1, 1)); - section.setExpectedWarningHeadersRegex( - singletonList( - Pattern.compile( - "^index template \\[(.+)\\] has index patterns \\[(.+)\\] matching patterns from existing " - + "older templates \\[(.+)\\] with patterns \\((.+)\\); this template \\[(.+)\\] will " - + "take precedence during new index creation$" - ) - ) - ); + section.setExpectedWarningHeadersRegex(singletonList(Pattern.compile(""" + ^index template \\[(.+)\\] has index patterns \\[(.+)\\] matching patterns from existing older \ + templates \\[(.+)\\] with patterns \\((.+)\\); this template \\[(.+)\\] will take precedence \ + during new index creation$"""))); section.checkWarningHeaders(singletonList(realisticTestHeader)); // require header, but no headers returned @@ -210,10 +205,11 @@ public void testWarningHeadersRegex() { } public void testParseDoSectionNoBody() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "get:\n" + " index: test_index\n" + " type: test_type\n" + " id: 1" - ); + parser = createParser(YamlXContent.yamlXContent, """ + get: + index: test_index + type: test_type + id: 1"""); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -240,11 +236,14 @@ public void testParseDoSectionNoParamsNoBody() throws Exception { } public void testParseDoSectionWithJsonBody() throws Exception { - String body = "{ \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }"; - parser = createParser( - YamlXContent.yamlXContent, - "index:\n" + " index: test_1\n" + " type: test\n" + " id: 1\n" + " body: " + body - ); + String body = """ + { "include": { "field1": "v1", "field2": "v2" }, "count": 1 }"""; + parser = createParser(YamlXContent.yamlXContent, """ + index: + index: test_1 + type: test + id: 1 + body: %s""".formatted(body)); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -261,25 +260,15 @@ public void testParseDoSectionWithJsonBody() throws Exception { } public void testParseDoSectionWithJsonMultipleBodiesAsLongString() throws Exception { - String bodies[] = new String[] { - "{ \"index\": { \"_index\":\"test_index\", \"_type\":\"test_type\", \"_id\":\"test_id\" } }\n", - "{ \"f1\":\"v1\", \"f2\":42 }\n", - "{ \"index\": { \"_index\":\"test_index2\", \"_type\":\"test_type2\", \"_id\":\"test_id2\" } }\n", - "{ \"f1\":\"v2\", \"f2\":47 }\n" }; - parser = createParser( - YamlXContent.yamlXContent, - "bulk:\n" - + " refresh: true\n" - + " body: |\n" - + " " - + bodies[0] - + " " - + bodies[1] - + " " - + bodies[2] - + " " - + bodies[3] - ); + parser = createParser(YamlXContent.yamlXContent, """ + bulk: + refresh: true + body: | + { "index": { "_index":"test_index", "_type":"test_type", "_id":"test_id" } } + { "f1":"v1", "f2":42 } + { "index": { "_index":"test_index2", "_type":"test_type2", "_id":"test_id2" } } + { "f1":"v2", "f2":47 } + """); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -293,14 +282,13 @@ public void testParseDoSectionWithJsonMultipleBodiesAsLongString() throws Except } public void testParseDoSectionWithYamlBody() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "search:\n" - + " body:\n" - + " \"_source\": [ include.field1, include.field2 ]\n" - + " \"query\": { \"match_all\": {} }" - ); - String body = "{ \"_source\": [ \"include.field1\", \"include.field2\" ], \"query\": { \"match_all\": {} }}"; + parser = createParser(YamlXContent.yamlXContent, """ + search: + body: + "_source": [ include.field1, include.field2 ] + "query": { "match_all": {} }"""); + String body = """ + { "_source": [ "include.field1", "include.field2" ], "query": { "match_all": {} }}"""; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -314,29 +302,27 @@ public void testParseDoSectionWithYamlBody() throws Exception { } public void testParseDoSectionWithYamlMultipleBodies() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "bulk:\n" - + " refresh: true\n" - + " body:\n" - + " - index:\n" - + " _index: test_index\n" - + " _type: test_type\n" - + " _id: test_id\n" - + " - f1: v1\n" - + " f2: 42\n" - + " - index:\n" - + " _index: test_index2\n" - + " _type: test_type2\n" - + " _id: test_id2\n" - + " - f1: v2\n" - + " f2: 47" - ); - String[] bodies = new String[4]; - bodies[0] = "{\"index\": {\"_index\": \"test_index\", \"_type\": \"test_type\", \"_id\": \"test_id\"}}"; - bodies[1] = "{ \"f1\":\"v1\", \"f2\": 42 }"; - bodies[2] = "{\"index\": {\"_index\": \"test_index2\", \"_type\": \"test_type2\", \"_id\": \"test_id2\"}}"; - bodies[3] = "{ \"f1\":\"v2\", \"f2\": 47 }"; + parser = createParser(YamlXContent.yamlXContent, """ + bulk: + refresh: true + body: + - index: + _index: test_index + _type: test_type + _id: test_id + - f1: v1 + f2: 42 + - index: + _index: test_index2 + _type: test_type2 + _id: test_id2 + - f1: v2 + f2: 47"""); + String[] bodies = new String[] { """ + {"index": {"_index": "test_index", "_type": "test_type", "_id": "test_id"}}""", """ + { "f1":"v1", "f2": 42 }""", """ + {"index": {"_index": "test_index2", "_type": "test_type2", "_id": "test_id2"}}""", """ + { "f1":"v2", "f2": 47 }""" }; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -354,18 +340,27 @@ public void testParseDoSectionWithYamlMultipleBodies() throws Exception { } public void testParseDoSectionWithYamlBodyMultiGet() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "mget:\n" - + " body:\n" - + " docs:\n" - + " - { _index: test_2, _type: test, _id: 1}\n" - + " - { _index: test_1, _type: none, _id: 1}" - ); - String body = "{ \"docs\": [ " - + "{\"_index\": \"test_2\", \"_type\":\"test\", \"_id\":1}, " - + "{\"_index\": \"test_1\", \"_type\":\"none\", \"_id\":1} " - + "]}"; + parser = createParser(YamlXContent.yamlXContent, """ + mget: + body: + docs: + - { _index: test_2, _type: test, _id: 1} + - { _index: test_1, _type: none, _id: 1}"""); + String body = """ + { + "docs": [ + { + "_index": "test_2", + "_type": "test", + "_id": 1 + }, + { + "_index": "test_1", + "_type": "none", + "_id": 1 + } + ] + }"""; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -379,14 +374,13 @@ public void testParseDoSectionWithYamlBodyMultiGet() throws Exception { } public void testParseDoSectionWithBodyStringified() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "index:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body: \"{ \\\"_source\\\": true, \\\"query\\\": { \\\"match_all\\\": {} } }\"" - ); + parser = createParser(YamlXContent.yamlXContent, """ + index: + index: test_1 + type: test + id: 1 + body: "{ \\"_source\\": true, \\"query\\": { \\"match_all\\": {} } }" + """); DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -398,21 +392,20 @@ public void testParseDoSectionWithBodyStringified() throws Exception { assertThat(apiCallSection.getParams().get("type"), equalTo("test")); assertThat(apiCallSection.getParams().get("id"), equalTo("1")); assertThat(apiCallSection.hasBody(), equalTo(true)); - assertThat(apiCallSection.getBodies().size(), equalTo(1)); - // stringified body is taken as is - assertJsonEquals(apiCallSection.getBodies().get(0), "{ \"_source\": true, \"query\": { \"match_all\": {} } }"); + assertThat(apiCallSection.getBodies().size(), equalTo(1));// stringified body is taken as is + assertJsonEquals(apiCallSection.getBodies().get(0), """ + { "_source": true, "query": { "match_all": {} } }"""); } public void testParseDoSectionWithBodiesStringifiedAndNot() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "index:\n" - + " body:\n" - + " - \"{ \\\"_source\\\": true, \\\"query\\\": { \\\"match_all\\\": {} } }\"\n" - + " - { size: 100, query: { match_all: {} } }" - ); + parser = createParser(YamlXContent.yamlXContent, """ + index: + body: + - "{ \\"_source\\": true, \\"query\\": { \\"match_all\\": {} } }" + - { size: 100, query: { match_all: {} } }"""); - String body = "{ \"size\": 100, \"query\": { \"match_all\": {} } }"; + String body = """ + { "size": 100, "query": { "match_all": {} } }"""; DoSection doSection = DoSection.parse(parser); ApiCallSection apiCallSection = doSection.getApiCallSection(); @@ -422,15 +415,17 @@ public void testParseDoSectionWithBodiesStringifiedAndNot() throws Exception { assertThat(apiCallSection.hasBody(), equalTo(true)); assertThat(apiCallSection.getBodies().size(), equalTo(2)); // stringified body is taken as is - assertJsonEquals(apiCallSection.getBodies().get(0), "{ \"_source\": true, \"query\": { \"match_all\": {} } }"); + assertJsonEquals(apiCallSection.getBodies().get(0), """ + { "_source": true, "query": { "match_all": {} } }"""); assertJsonEquals(apiCallSection.getBodies().get(1), body); } public void testParseDoSectionWithCatch() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "catch: missing\n" + "indices.get_warmer:\n" + " index: test_index\n" + " name: test_warmer" - ); + parser = createParser(YamlXContent.yamlXContent, """ + catch: missing + indices.get_warmer: + index: test_index + name: test_warmer"""); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), equalTo("missing")); @@ -450,15 +445,13 @@ public void testUnsupportedTopLevelField() throws Exception { } public void testParseDoSectionWithHeaders() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "headers:\n" - + " Authorization: \"thing one\"\n" - + " Content-Type: \"application/json\"\n" - + "indices.get_warmer:\n" - + " index: test_index\n" - + " name: test_warmer" - ); + parser = createParser(YamlXContent.yamlXContent, """ + headers: + Authorization: "thing one" + Content-Type: "application/json" + indices.get_warmer: + index: test_index + name: test_warmer"""); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getApiCallSection(), notNullValue()); @@ -479,10 +472,11 @@ public void testParseDoSectionWithoutClientCallSection() throws Exception { } public void testParseDoSectionMultivaluedField() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "indices.get_field_mapping:\n" + " index: test_index\n" + " type: test_type\n" + " field: [ text , text1 ]" - ); + parser = createParser(YamlXContent.yamlXContent, """ + indices.get_field_mapping: + index: test_index + type: test_type + field: [ text , text1 ]"""); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); @@ -497,15 +491,13 @@ public void testParseDoSectionMultivaluedField() throws Exception { } public void testParseDoSectionExpectedWarnings() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "indices.get_field_mapping:\n" - + " index: test_index\n" - + " type: test_type\n" - + "warnings:\n" - + " - some test warning they are typically pretty long\n" - + " - some other test warning sometimes they have [in] them" - ); + parser = createParser(YamlXContent.yamlXContent, """ + indices.get_field_mapping: + index: test_index + type: test_type + warnings: + - some test warning they are typically pretty long + - some other test warning sometimes they have [in] them"""); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); @@ -523,10 +515,11 @@ public void testParseDoSectionExpectedWarnings() throws Exception { ) ); - parser = createParser( - YamlXContent.yamlXContent, - "indices.get_field_mapping:\n" + " index: test_index\n" + "warnings:\n" + " - just one entry this time" - ); + parser = createParser(YamlXContent.yamlXContent, """ + indices.get_field_mapping: + index: test_index + warnings: + - just one entry this time"""); doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); @@ -535,15 +528,13 @@ public void testParseDoSectionExpectedWarnings() throws Exception { } public void testParseDoSectionAllowedWarnings() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "indices.get_field_mapping:\n" - + " index: test_index\n" - + " type: test_type\n" - + "allowed_warnings:\n" - + " - some test warning they are typically pretty long\n" - + " - some other test warning sometimes they have [in] them" - ); + parser = createParser(YamlXContent.yamlXContent, """ + indices.get_field_mapping: + index: test_index + type: test_type + allowed_warnings: + - some test warning they are typically pretty long + - some other test warning sometimes they have [in] them"""); DoSection doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); @@ -561,34 +552,34 @@ public void testParseDoSectionAllowedWarnings() throws Exception { ) ); - parser = createParser( - YamlXContent.yamlXContent, - "indices.get_field_mapping:\n" + " index: test_index\n" + "allowed_warnings:\n" + " - just one entry this time" - ); + parser = createParser(YamlXContent.yamlXContent, """ + indices.get_field_mapping: + index: test_index + allowed_warnings: + - just one entry this time"""); doSection = DoSection.parse(parser); assertThat(doSection.getCatch(), nullValue()); assertThat(doSection.getApiCallSection(), notNullValue()); assertThat(doSection.getAllowedWarningHeaders(), equalTo(singletonList("just one entry this time"))); - parser = createParser( - YamlXContent.yamlXContent, - "indices.get_field_mapping:\n" - + " index: test_index\n" - + "warnings:\n" - + " - foo\n" - + "allowed_warnings:\n" - + " - foo" - ); + parser = createParser(YamlXContent.yamlXContent, """ + indices.get_field_mapping: + index: test_index + warnings: + - foo + allowed_warnings: + - foo"""); Exception e = expectThrows(IllegalArgumentException.class, () -> DoSection.parse(parser)); assertThat(e.getMessage(), equalTo("the warning [foo] was both allowed and expected")); } public void testNodeSelectorByVersion() throws IOException { - parser = createParser( - YamlXContent.yamlXContent, - "node_selector:\n" + " version: 5.2.0-6.0.0\n" + "indices.get_field_mapping:\n" + " index: test_index" - ); + parser = createParser(YamlXContent.yamlXContent, """ + node_selector: + version: 5.2.0-6.0.0 + indices.get_field_mapping: + index: test_index"""); DoSection doSection = DoSection.parse(parser); assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector()); @@ -632,10 +623,11 @@ public void testNodeSelectorByVersion() throws IOException { } public void testNodeSelectorCurrentVersion() throws IOException { - parser = createParser( - YamlXContent.yamlXContent, - "node_selector:\n" + " version: current\n" + "indices.get_field_mapping:\n" + " index: test_index" - ); + parser = createParser(YamlXContent.yamlXContent, """ + node_selector: + version: current + indices.get_field_mapping: + index: test_index"""); DoSection doSection = DoSection.parse(parser); assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector()); @@ -657,10 +649,12 @@ private static Node nodeWithVersion(String version) { } public void testNodeSelectorByAttribute() throws IOException { - parser = createParser( - YamlXContent.yamlXContent, - "node_selector:\n" + " attribute:\n" + " attr: val\n" + "indices.get_field_mapping:\n" + " index: test_index" - ); + parser = createParser(YamlXContent.yamlXContent, """ + node_selector: + attribute: + attr: val + indices.get_field_mapping: + index: test_index"""); DoSection doSection = DoSection.parse(parser); assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector()); @@ -682,15 +676,13 @@ public void testNodeSelectorByAttribute() throws IOException { assertEquals("expected [attributes] metadata to be set but got [host=http://dummy]", e.getMessage()); } - parser = createParser( - YamlXContent.yamlXContent, - "node_selector:\n" - + " attribute:\n" - + " attr: val\n" - + " attr2: val2\n" - + "indices.get_field_mapping:\n" - + " index: test_index" - ); + parser = createParser(YamlXContent.yamlXContent, """ + node_selector: + attribute: + attr: val + attr2: val2 + indices.get_field_mapping: + index: test_index"""); DoSection doSectionWithTwoAttributes = DoSection.parse(parser); assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector()); @@ -716,15 +708,13 @@ private static Node nodeWithAttributes(Map> attributes) { } public void testNodeSelectorByTwoThings() throws IOException { - parser = createParser( - YamlXContent.yamlXContent, - "node_selector:\n" - + " version: 5.2.0-6.0.0\n" - + " attribute:\n" - + " attr: val\n" - + "indices.get_field_mapping:\n" - + " index: test_index" - ); + parser = createParser(YamlXContent.yamlXContent, """ + node_selector: + version: 5.2.0-6.0.0 + attribute: + attr: val + indices.get_field_mapping: + index: test_index"""); DoSection doSection = DoSection.parse(parser); assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector()); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SetupSectionTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SetupSectionTests.java index 50084e93a78c4..fa91726ca73e6 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SetupSectionTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SetupSectionTests.java @@ -20,21 +20,20 @@ public class SetupSectionTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseSetupSection() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - " - do:\n" - + " index1:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body: { \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }\n" - + " - do:\n" - + " index2:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 2\n" - + " body: { \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + - do: + index1: + index: test_1 + type: test + id: 1 + body: { "include": { "field1": "v1", "field2": "v2" }, "count": 1 } + - do: + index2: + index: test_1 + type: test + id: 2 + body: { "include": { "field1": "v1", "field2": "v2" }, "count": 1 } + """); SetupSection setupSection = SetupSection.parse(parser); @@ -48,17 +47,16 @@ public void testParseSetupSection() throws Exception { } public void testParseSetSectionInSetupSection() throws IOException { - parser = createParser( - YamlXContent.yamlXContent, - "- do:\n" - + " cluster.state: {}\n" - + "- set: { master_node: master }\n" - + "- do:\n" - + " nodes.info:\n" - + " metric: [ http, transport ]\n" - + "- set: {nodes.$master.http.publish_address: host}\n" - + "- set: {nodes.$master.transport.publish_address: transport_host}\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + - do: + cluster.state: {} + - set: { master_node: master } + - do: + nodes.info: + metric: [ http, transport ] + - set: {nodes.$master.http.publish_address: host} + - set: {nodes.$master.transport.publish_address: transport_host} + """); final SetupSection setupSection = SetupSection.parse(parser); @@ -87,24 +85,23 @@ public void testParseSetSectionInSetupSection() throws IOException { } public void testParseSetupAndSkipSectionNoSkip() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - " - skip:\n" - + " version: \"6.0.0 - 6.3.0\"\n" - + " reason: \"Update doesn't return metadata fields, waiting for #3259\"\n" - + " - do:\n" - + " index1:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 1\n" - + " body: { \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }\n" - + " - do:\n" - + " index2:\n" - + " index: test_1\n" - + " type: test\n" - + " id: 2\n" - + " body: { \"include\": { \"field1\": \"v1\", \"field2\": \"v2\" }, \"count\": 1 }\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + - skip: + version: "6.0.0 - 6.3.0" + reason: "Update doesn't return metadata fields, waiting for #3259" + - do: + index1: + index: test_1 + type: test + id: 1 + body: { "include": { "field1": "v1", "field2": "v2" }, "count": 1 } + - do: + index2: + index: test_1 + type: test + id: 2 + body: { "include": { "field1": "v1", "field2": "v2" }, "count": 1 } + """); SetupSection setupSection = SetupSection.parse(parser); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SkipSectionTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SkipSectionTests.java index 7c30d9d452971..ee93ca50dc291 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SkipSectionTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/SkipSectionTests.java @@ -70,10 +70,9 @@ public void testMessage() { public void testParseSkipSectionVersionNoFeature() throws Exception { Version version = VersionUtils.randomVersion(random()); - parser = createParser( - YamlXContent.yamlXContent, - "version: \" - " + version + "\"\n" + "reason: Delete ignores the parent param" - ); + parser = createParser(YamlXContent.yamlXContent, """ + version: " - %s" + reason: Delete ignores the parent param""".formatted(version)); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); @@ -84,7 +83,9 @@ public void testParseSkipSectionVersionNoFeature() throws Exception { } public void testParseSkipSectionAllVersions() throws Exception { - parser = createParser(YamlXContent.yamlXContent, "version: \" all \"\n" + "reason: Delete ignores the parent param"); + parser = createParser(YamlXContent.yamlXContent, """ + version: " all " + reason: Delete ignores the parent param"""); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); @@ -119,10 +120,10 @@ public void testParseSkipSectionFeaturesNoVersion() throws Exception { } public void testParseSkipSectionBothFeatureAndVersion() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "version: \" - 0.90.2\"\n" + "features: regex\n" + "reason: Delete ignores the parent param" - ); + parser = createParser(YamlXContent.yamlXContent, """ + version: " - 0.90.2" + features: regex + reason: Delete ignores the parent param"""); SkipSection skipSection = SkipSection.parse(parser); assertEquals(VersionUtils.getFirstVersion(), skipSection.getLowerVersion()); @@ -146,12 +147,11 @@ public void testParseSkipSectionNoVersionNorFeature() throws Exception { } public void testParseSkipSectionOsNoVersion() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "features: [\"skip_os\", \"some_feature\"]\n" - + "os: debian-9\n" - + "reason: memory accounting broken, see gh#xyz\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + features: ["skip_os", "some_feature"] + os: debian-9 + reason: memory accounting broken, see gh#xyz + """); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); @@ -163,10 +163,11 @@ public void testParseSkipSectionOsNoVersion() throws Exception { } public void testParseSkipSectionOsListNoVersion() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - "features: skip_os\n" + "os: [debian-9,windows-95,ms-dos]\n" + "reason: see gh#xyz\n" - ); + parser = createParser(YamlXContent.yamlXContent, """ + features: skip_os + os: [debian-9,windows-95,ms-dos] + reason: see gh#xyz + """); SkipSection skipSection = SkipSection.parse(parser); assertThat(skipSection, notNullValue()); @@ -179,7 +180,10 @@ public void testParseSkipSectionOsListNoVersion() throws Exception { } public void testParseSkipSectionOsNoFeatureNoVersion() throws Exception { - parser = createParser(YamlXContent.yamlXContent, "os: debian-9\n" + "reason: memory accounting broken, see gh#xyz\n"); + parser = createParser(YamlXContent.yamlXContent, """ + os: debian-9 + reason: memory accounting broken, see gh#xyz + """); Exception e = expectThrows(ParsingException.class, () -> SkipSection.parse(parser)); assertThat(e.getMessage(), is("if os is specified, feature skip_os must be set")); diff --git a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/TeardownSectionTests.java b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/TeardownSectionTests.java index db84d018fe144..52fbb3838ac9e 100644 --- a/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/TeardownSectionTests.java +++ b/test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/TeardownSectionTests.java @@ -19,21 +19,20 @@ */ public class TeardownSectionTests extends AbstractClientYamlTestFragmentParserTestCase { public void testParseTeardownSection() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - " - do:\n" - + " delete:\n" - + " index: foo\n" - + " type: doc\n" - + " id: 1\n" - + " ignore: 404\n" - + " - do:\n" - + " delete2:\n" - + " index: foo\n" - + " type: doc\n" - + " id: 1\n" - + " ignore: 404" - ); + parser = createParser(YamlXContent.yamlXContent, """ + - do: + delete: + index: foo + type: doc + id: 1 + ignore: 404 + - do: + delete2: + index: foo + type: doc + id: 1 + ignore: 404 + """); TeardownSection section = TeardownSection.parse(parser); assertThat(section, notNullValue()); @@ -44,24 +43,23 @@ public void testParseTeardownSection() throws Exception { } public void testParseWithSkip() throws Exception { - parser = createParser( - YamlXContent.yamlXContent, - " - skip:\n" - + " version: \"6.0.0 - 6.3.0\"\n" - + " reason: \"there is a reason\"\n" - + " - do:\n" - + " delete:\n" - + " index: foo\n" - + " type: doc\n" - + " id: 1\n" - + " ignore: 404\n" - + " - do:\n" - + " delete2:\n" - + " index: foo\n" - + " type: doc\n" - + " id: 1\n" - + " ignore: 404" - ); + parser = createParser(YamlXContent.yamlXContent, """ + - skip: + version: "6.0.0 - 6.3.0" + reason: "there is a reason" + - do: + delete: + index: foo + type: doc + id: 1 + ignore: 404 + - do: + delete2: + index: foo + type: doc + id: 1 + ignore: 404 + """); TeardownSection section = TeardownSection.parse(parser); assertThat(section, notNullValue()); diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/InternalTopMetricsTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/InternalTopMetricsTests.java index 66f5ef4ec60a4..e8a53e971dd51 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/InternalTopMetricsTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/InternalTopMetricsTests.java @@ -84,25 +84,21 @@ public void testToXContentDoubleSortValue() throws IOException { new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), singletonList(metricOneDouble)) ); InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 1, top, null); - assertThat( - Strings.toString(tm, true, true), - equalTo( - "{\n" - + " \"test\" : {\n" - + " \"top\" : [\n" - + " {\n" - + " \"sort\" : [\n" - + " 1.0\n" - + " ],\n" - + " \"metrics\" : {\n" - + " \"test\" : 1.0\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}" - ) - ); + assertThat(Strings.toString(tm, true, true), equalTo(""" + { + "test" : { + "top" : [ + { + "sort" : [ + 1.0 + ], + "metrics" : { + "test" : 1.0 + } + } + ] + } + }""")); } public void testToXContentDateSortValue() throws IOException { @@ -111,25 +107,21 @@ public void testToXContentDateSortValue() throws IOException { new InternalTopMetrics.TopMetric(strictDateTime(), sortValue, singletonList(metricOneDouble)) ); InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 1, top, null); - assertThat( - Strings.toString(tm, true, true), - equalTo( - "{\n" - + " \"test\" : {\n" - + " \"top\" : [\n" - + " {\n" - + " \"sort\" : [\n" - + " \"2007-12-03T10:15:30.000Z\"\n" - + " ],\n" - + " \"metrics\" : {\n" - + " \"test\" : 1.0\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}" - ) - ); + assertThat(Strings.toString(tm, true, true), equalTo(""" + { + "test" : { + "top" : [ + { + "sort" : [ + "2007-12-03T10:15:30.000Z" + ], + "metrics" : { + "test" : 1.0 + } + } + ] + } + }""")); } public void testToXContentLongMetricValue() throws IOException { @@ -137,25 +129,21 @@ public void testToXContentLongMetricValue() throws IOException { new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), singletonList(metricOneLong)) ); InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 1, top, null); - assertThat( - Strings.toString(tm, true, true), - equalTo( - "{\n" - + " \"test\" : {\n" - + " \"top\" : [\n" - + " {\n" - + " \"sort\" : [\n" - + " 1.0\n" - + " ],\n" - + " \"metrics\" : {\n" - + " \"test\" : 1\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}" - ) - ); + assertThat(Strings.toString(tm, true, true), equalTo(""" + { + "test" : { + "top" : [ + { + "sort" : [ + 1.0 + ], + "metrics" : { + "test" : 1 + } + } + ] + } + }""")); } public void testToXContentDateMetricValue() throws IOException { @@ -167,25 +155,21 @@ public void testToXContentDateMetricValue() throws IOException { new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(1.0), singletonList(metricValue)) ); InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 1, top, null); - assertThat( - Strings.toString(tm, true, true), - equalTo( - "{\n" - + " \"test\" : {\n" - + " \"top\" : [\n" - + " {\n" - + " \"sort\" : [\n" - + " 1.0\n" - + " ],\n" - + " \"metrics\" : {\n" - + " \"test\" : \"2007-12-03T10:15:30.000Z\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}" - ) - ); + assertThat(Strings.toString(tm, true, true), equalTo(""" + { + "test" : { + "top" : [ + { + "sort" : [ + 1.0 + ], + "metrics" : { + "test" : "2007-12-03T10:15:30.000Z" + } + } + ] + } + }""")); } public void testToXContentManyMetrics() throws IOException { @@ -197,27 +181,23 @@ public void testToXContentManyMetrics() throws IOException { ) ); InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, List.of("foo", "bar", "baz"), 1, top, null); - assertThat( - Strings.toString(tm, true, true), - equalTo( - "{\n" - + " \"test\" : {\n" - + " \"top\" : [\n" - + " {\n" - + " \"sort\" : [\n" - + " 1.0\n" - + " ],\n" - + " \"metrics\" : {\n" - + " \"foo\" : 1.0,\n" - + " \"bar\" : 1,\n" - + " \"baz\" : 1.0\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}" - ) - ); + assertThat(Strings.toString(tm, true, true), equalTo(""" + { + "test" : { + "top" : [ + { + "sort" : [ + 1.0 + ], + "metrics" : { + "foo" : 1.0, + "bar" : 1, + "baz" : 1.0 + } + } + ] + } + }""")); } public void testToXContentManyTopMetrics() throws IOException { @@ -226,33 +206,29 @@ public void testToXContentManyTopMetrics() throws IOException { new InternalTopMetrics.TopMetric(DocValueFormat.RAW, SortValue.from(2.0), singletonList(metricOneLong)) ); InternalTopMetrics tm = new InternalTopMetrics("test", sortOrder, singletonList("test"), 2, top, null); - assertThat( - Strings.toString(tm, true, true), - equalTo( - "{\n" - + " \"test\" : {\n" - + " \"top\" : [\n" - + " {\n" - + " \"sort\" : [\n" - + " 1.0\n" - + " ],\n" - + " \"metrics\" : {\n" - + " \"test\" : 1.0\n" - + " }\n" - + " },\n" - + " {\n" - + " \"sort\" : [\n" - + " 2.0\n" - + " ],\n" - + " \"metrics\" : {\n" - + " \"test\" : 1\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + "}" - ) - ); + assertThat(Strings.toString(tm, true, true), equalTo(""" + { + "test" : { + "top" : [ + { + "sort" : [ + 1.0 + ], + "metrics" : { + "test" : 1.0 + } + }, + { + "sort" : [ + 2.0 + ], + "metrics" : { + "test" : 1 + } + } + ] + } + }""")); } public void testGetProperty() { diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java index 1a90f42970c8f..54b0c11f8ff59 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchResponseTests.java @@ -164,20 +164,14 @@ public void testToXContent() throws IOException { try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { builder.prettyPrint(); asyncSearchResponse.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals( - "{\n" - + " \"id\" : \"id\",\n" - + " \"is_partial\" : true,\n" - + " \"is_running\" : true,\n" - + " \"start_time_in_millis\" : " - + date.getTime() - + ",\n" - + " \"expiration_time_in_millis\" : " - + date.getTime() - + "\n" - + "}", - Strings.toString(builder) - ); + assertEquals(""" + { + "id" : "id", + "is_partial" : true, + "is_running" : true, + "start_time_in_millis" : %s, + "expiration_time_in_millis" : %s + }""".formatted(date.getTime(), date.getTime()), Strings.toString(builder)); } try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { @@ -185,23 +179,21 @@ public void testToXContent() throws IOException { builder.humanReadable(true); asyncSearchResponse.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("human", "true"))); assertEquals( - "{\n" - + " \"id\" : \"id\",\n" - + " \"is_partial\" : true,\n" - + " \"is_running\" : true,\n" - + " \"start_time\" : \"" - + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()) - + "\",\n" - + " \"start_time_in_millis\" : " - + date.getTime() - + ",\n" - + " \"expiration_time\" : \"" - + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()) - + "\",\n" - + " \"expiration_time_in_millis\" : " - + date.getTime() - + "\n" - + "}", + """ + { + "id" : "id", + "is_partial" : true, + "is_running" : true, + "start_time" : "%s", + "start_time_in_millis" : %s, + "expiration_time" : "%s", + "expiration_time_in_millis" : %s + }""".formatted( + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()), + date.getTime(), + XContentElasticsearchExtension.DEFAULT_FORMATTER.format(date.toInstant()), + date.getTime() + ), Strings.toString(builder) ); } diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java index 945dc87f4967f..4a6146dfed3ac 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncSearchTaskTests.java @@ -105,11 +105,9 @@ public void testTaskDescription() { threadPool, (t) -> () -> null ); - assertEquals( - "async_search{indices[index1,index2], search_type[QUERY_THEN_FETCH], " - + "source[{\"query\":{\"term\":{\"field\":{\"value\":\"value\",\"boost\":1.0}}}}]}", - asyncSearchTask.getDescription() - ); + assertEquals(""" + async_search{indices[index1,index2], search_type[QUERY_THEN_FETCH], source\ + [{"query":{"term":{"field":{"value":"value","boost":1.0}}}}]}""", asyncSearchTask.getDescription()); } public void testWaitForInit() throws InterruptedException { diff --git a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java index 887e15e099ba1..53a35b8ba0e81 100644 --- a/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java +++ b/x-pack/plugin/async-search/src/test/java/org/elasticsearch/xpack/search/AsyncStatusResponseTests.java @@ -9,6 +9,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -77,48 +78,36 @@ protected AsyncStatusResponse mutateInstance(AsyncStatusResponse instance) { public void testToXContent() throws IOException { AsyncStatusResponse response = createTestInstance(); try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { - String expectedJson = "{\n" - + " \"id\" : \"" - + response.getId() - + "\",\n" - + " \"is_running\" : " - + response.isRunning() - + ",\n" - + " \"is_partial\" : " - + response.isPartial() - + ",\n" - + " \"start_time_in_millis\" : " - + response.getStartTime() - + ",\n" - + " \"expiration_time_in_millis\" : " - + response.getExpirationTime() - + ",\n" - + " \"_shards\" : {\n" - + " \"total\" : " - + response.getTotalShards() - + ",\n" - + " \"successful\" : " - + response.getSuccessfulShards() - + ",\n" - + " \"skipped\" : " - + response.getSkippedShards() - + ",\n" - + " \"failed\" : " - + response.getFailedShards() - + "\n"; - if (response.getCompletionStatus() == null) { - expectedJson = expectedJson + " }\n" + "}"; - } else { - expectedJson = expectedJson - + " },\n" - + " \"completion_status\" : " - + response.getCompletionStatus().getStatus() - + "\n" - + "}"; - } - builder.prettyPrint(); + String expectedJson = """ + { + "id" : "%s", + "is_running" : %s, + "is_partial" : %s, + "start_time_in_millis" : %s, + "expiration_time_in_millis" : %s, + "_shards" : { + "total" : %s, + "successful" : %s, + "skipped" : %s, + "failed" : %s + } + %s + } + """.formatted( + response.getId(), + response.isRunning(), + response.isPartial(), + response.getStartTime(), + response.getExpirationTime(), + response.getTotalShards(), + response.getSuccessfulShards(), + response.getSkippedShards(), + response.getFailedShards(), + response.getCompletionStatus() == null ? "" : """ + ,"completion_status" : %s""".formatted(response.getCompletionStatus().getStatus()) + ); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals(expectedJson, Strings.toString(builder)); + assertEquals(XContentHelper.stripWhitespace(expectedJson), Strings.toString(builder)); } } } diff --git a/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java b/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java index 98f10a0a08c82..457a0d4ad3f81 100644 --- a/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java +++ b/x-pack/plugin/ccr/qa/downgrade-to-basic-license/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java @@ -41,7 +41,8 @@ public void testDowngradeRemoteClusterToBasic() throws Exception { { Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); - request.setJsonEntity("{\"leader_index_patterns\": [\"logs-*\"], \"remote_cluster\": \"leader_cluster\"}"); + request.setJsonEntity(""" + {"leader_index_patterns": ["logs-*"], "remote_cluster": "leader_cluster"}"""); assertOK(client().performRequest(request)); } @@ -124,7 +125,8 @@ protected Boolean featureValueOf(JsonLogLine actual) { private void createNewIndexAndIndexDocs(RestClient client, String index) throws IOException { Request request = new Request("PUT", "/" + index); - request.setJsonEntity("{\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}"); + request.setJsonEntity(""" + {"mappings": {"properties": {"field": {"type": "keyword"}}}}"""); assertOK(client.performRequest(request)); for (int i = 0; i < 5; i++) { diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java index 1941d487e58e4..93462eb432780 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/AutoFollowIT.java @@ -58,14 +58,17 @@ public void testMultipleAutoFollowPatternsDifferentClusters() throws Exception { try { int initialNumberOfSuccessfulFollowedIndices = getNumberOfSuccessfulFollowedIndices(); Request putPatternRequest = new Request("PUT", "/_ccr/auto_follow/leader_cluster_pattern"); - putPatternRequest.setJsonEntity("{\"leader_index_patterns\": [\"index-*\"], \"remote_cluster\": \"leader_cluster\"}"); + putPatternRequest.setJsonEntity(""" + {"leader_index_patterns": ["index-*"], "remote_cluster": "leader_cluster"}"""); assertOK(client().performRequest(putPatternRequest)); putPatternRequest = new Request("PUT", "/_ccr/auto_follow/middle_cluster_pattern"); - putPatternRequest.setJsonEntity("{\"leader_index_patterns\": [\"index-*\"], \"remote_cluster\": \"middle_cluster\"}"); + putPatternRequest.setJsonEntity(""" + {"leader_index_patterns": ["index-*"], "remote_cluster": "middle_cluster"}"""); assertOK(client().performRequest(putPatternRequest)); try (RestClient leaderClient = buildLeaderClient()) { Request request = new Request("PUT", "/index-20190101"); - request.setJsonEntity("{\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}"); + request.setJsonEntity(""" + {"mappings": {"properties": {"field": {"type": "keyword"}}}}"""); assertOK(leaderClient.performRequest(request)); for (int i = 0; i < 5; i++) { String id = Integer.toString(i); @@ -74,7 +77,8 @@ public void testMultipleAutoFollowPatternsDifferentClusters() throws Exception { } try (RestClient middleClient = buildMiddleClient()) { Request request = new Request("PUT", "/index-20200101"); - request.setJsonEntity("{\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}"); + request.setJsonEntity(""" + {"mappings": {"properties": {"field": {"type": "keyword"}}}}"""); assertOK(middleClient.performRequest(request)); for (int i = 0; i < 5; i++) { String id = Integer.toString(i); @@ -142,13 +146,15 @@ public void testAutoFollowPatterns() throws Exception { try (RestClient leaderClient = buildLeaderClient()) { request = new Request("PUT", "/" + excludedIndex); - request.setJsonEntity("{\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}"); + request.setJsonEntity(""" + {"mappings": {"properties": {"field": {"type": "keyword"}}}}"""); assertOK(leaderClient.performRequest(request)); } try (RestClient leaderClient = buildLeaderClient()) { request = new Request("PUT", "/metrics-20210101"); - request.setJsonEntity("{\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}"); + request.setJsonEntity(""" + {"mappings": {"properties": {"field": {"type": "keyword"}}}}"""); assertOK(leaderClient.performRequest(request)); for (int i = 0; i < 5; i++) { @@ -664,13 +670,10 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { Request createDataStreamRequest = new Request("PUT", "/_data_stream/" + leaderDataStreamName); assertOK(leaderClient.performRequest(createDataStreamRequest)); Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[" - + "{\"add\":{\"index\":\"" - + leaderDataStreamName - + "\",\"alias\":\"logs-http\",\"is_write_index\":true}}" - + "]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ { "add": { "index": "%s", "alias": "logs-http", "is_write_index": true } } ] + }""".formatted(leaderDataStreamName)); assertOK(leaderClient.performRequest(updateAliasesRequest)); for (int i = 0; i < numDocs; i++) { @@ -692,13 +695,10 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { Request createDataStreamRequest = new Request("PUT", "/_data_stream/" + followerDataStreamName); assertOK(client().performRequest(createDataStreamRequest)); Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[" - + "{\"add\":{\"index\":\"" - + followerDataStreamName - + "\",\"alias\":\"logs-http\",\"is_write_index\":true}}" - + "]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ { "add": { "index": "%s", "alias": "logs-http", "is_write_index": true } } ] + }""".formatted(followerDataStreamName)); assertOK(client().performRequest(updateAliasesRequest)); for (int i = 0; i < numDocs; i++) { @@ -714,9 +714,10 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { // (only set the write flag to logs-http-na) // Create alias in follower cluster that point to leader and follower data streams: updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[" + "{\"add\":{\"index\":\"" + leaderDataStreamName + "\",\"alias\":\"logs-http\"}}" + "]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ { "add": { "index": "%s", "alias": "logs-http" } } ] + }""".formatted(leaderDataStreamName)); assertOK(client().performRequest(updateAliasesRequest)); try (var leaderClient = buildLeaderClient()) { @@ -730,9 +731,10 @@ public void testDataStreamsBiDirectionalReplication() throws Exception { verifyDocuments(leaderClient, followerDataStreamName, numDocs); }); updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[" + "{\"add\":{\"index\":\"" + followerDataStreamName + "\",\"alias\":\"logs-http\"}}" + "]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ { "add": { "index": "%s", "alias": "logs-http" } } ] + }""".formatted(followerDataStreamName)); assertOK(leaderClient.performRequest(updateAliasesRequest)); } diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java index c8722ae4417b5..8ffbaaaacb567 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/ChainIT.java @@ -22,7 +22,8 @@ public void testFollowIndex() throws Exception { logger.info("Running against leader cluster"); String mapping = ""; if (randomBoolean()) { // randomly do source filtering on indexing - mapping = "\"_source\": {" + " \"includes\": [\"field\"]," + " \"excludes\": [\"filtered_field\"]" + "}"; + mapping = """ + "_source": { "includes": ["field"], "excludes": ["filtered_field"]}"""; } createIndex(leaderIndexName, Settings.EMPTY, mapping); for (int i = 0; i < numDocs; i++) { diff --git a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java index e8b1ee702fc3d..b6952dc70dbed 100644 --- a/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java +++ b/x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java @@ -45,7 +45,8 @@ public void testFollowIndex() throws Exception { logger.info("Running against leader cluster"); String mapping = ""; if (randomBoolean()) { // randomly do source filtering on indexing - mapping = "\"_source\": {" + " \"includes\": [\"field\"]," + " \"excludes\": [\"filtered_field\"]" + "}"; + mapping = """ + "_source": { "includes": ["field"], "excludes": ["filtered_field"]}"""; } createIndex(leaderIndexName, Settings.EMPTY, mapping); for (int i = 0; i < numDocs; i++) { @@ -255,7 +256,8 @@ public void testFollowTsdbIndex() throws Exception { .put(IndexSettings.TIME_SERIES_START_TIME.getKey(), "2021-04-28T00:00:00Z") .put(IndexSettings.TIME_SERIES_END_TIME.getKey(), "2021-04-29T00:00:00Z") .build(), - "\"properties\": {\"@timestamp\": {\"type\": \"date\"}, \"dim\": {\"type\": \"keyword\", \"time_series_dimension\": true}}" + """ + "properties": {"@timestamp": {"type": "date"}, "dim": {"type": "keyword", "time_series_dimension": true}}""" ); for (int i = 0; i < numDocs; i++) { logger.info("Indexing doc [{}]", i); diff --git a/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java b/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java index 00773f6c0bf08..2f5fabd21cf98 100644 --- a/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java +++ b/x-pack/plugin/ccr/qa/non-compliant-license/src/test/java/org/elasticsearch/xpack/ccr/CcrMultiClusterLicenseIT.java @@ -23,7 +23,9 @@ public class CcrMultiClusterLicenseIT extends ESCCRRestTestCase { public void testFollow() { if ("follow".equals(targetCluster)) { final Request request = new Request("PUT", "/follower/_ccr/follow"); - request.setJsonEntity("{\"remote_cluster\": \"leader_cluster\", \"leader_index\": \"leader\"}"); + request.setJsonEntity(""" + {"remote_cluster": "leader_cluster", "leader_index": "leader"} + """); assertNonCompliantLicense(request, "remote index [leader_cluster:leader] metadata"); } } @@ -31,7 +33,9 @@ public void testFollow() { public void testAutoFollow() { if ("follow".equals(targetCluster)) { final Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); - request.setJsonEntity("{\"leader_index_patterns\":[\"*\"], \"remote_cluster\": \"leader_cluster\"}"); + request.setJsonEntity(""" + {"leader_index_patterns":["*"], "remote_cluster": "leader_cluster"} + """); assertNonCompliantLicense(request, "remote cluster state"); } } diff --git a/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java b/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java index eda5e46bcf334..764d50c94ea16 100644 --- a/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java +++ b/x-pack/plugin/ccr/qa/restart/src/test/java/org/elasticsearch/xpack/ccr/RestartIT.java @@ -34,12 +34,14 @@ public void testRestart() throws Exception { // now create an auto-follow pattern for "leader-*" final Request putPatternRequest = new Request("PUT", "/_ccr/auto_follow/leader_cluster_pattern"); - putPatternRequest.setJsonEntity( - "{" - + "\"leader_index_patterns\": [\"leader-*\"]," - + "\"remote_cluster\": \"leader_cluster\"," - + "\"follow_index_pattern\":\"follow-{{leader_index}}\"}" - ); + putPatternRequest.setJsonEntity(""" + { + "leader_index_patterns": [ + "leader-*" + ], + "remote_cluster": "leader_cluster", + "follow_index_pattern": "follow-{{leader_index}}" + }"""); assertOK(client().performRequest(putPatternRequest)); try (RestClient leaderClient = buildLeaderClient()) { // create "leader-1" on the leader, which should be replicated to "follow-leader-1" on the follower diff --git a/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java b/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java index 04477c62c182a..256701219639b 100644 --- a/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java +++ b/x-pack/plugin/ccr/qa/security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java @@ -147,18 +147,21 @@ public void testAutoFollowPatterns() throws Exception { { Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); - request.setJsonEntity("{\"leader_index_patterns\": [\"logs-*\"], \"remote_cluster\": \"leader_cluster\"}"); + request.setJsonEntity(""" + {"leader_index_patterns": ["logs-*"], "remote_cluster": "leader_cluster"}"""); Exception e = expectThrows(ResponseException.class, () -> assertOK(client().performRequest(request))); assertThat(e.getMessage(), containsString("insufficient privileges to follow index [logs-*]")); } Request request = new Request("PUT", "/_ccr/auto_follow/test_pattern"); - request.setJsonEntity("{\"leader_index_patterns\": [\"logs-eu*\"], \"remote_cluster\": \"leader_cluster\"}"); + request.setJsonEntity(""" + {"leader_index_patterns": ["logs-eu*"], "remote_cluster": "leader_cluster"}"""); assertOK(client().performRequest(request)); try (RestClient leaderClient = buildLeaderClient()) { for (String index : new String[] { allowedIndex, disallowedIndex }) { - String requestBody = "{\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}}"; + String requestBody = """ + {"mappings": {"properties": {"field": {"type": "keyword"}}}}"""; request = new Request("PUT", "/" + index); request.setJsonEntity(requestBody); assertOK(leaderClient.performRequest(request)); @@ -204,16 +207,13 @@ public void testForgetFollower() throws IOException { try (RestClient leaderClient = buildLeaderClient(restAdminSettings())) { final Request request = new Request("POST", "/" + forgetLeader + "/_ccr/forget_follower"); - final String requestBody = "{" - + "\"follower_cluster\":\"follow-cluster\"," - + "\"follower_index\":\"" - + forgetFollower - + "\"," - + "\"follower_index_uuid\":\"" - + followerIndexUUID - + "\"," - + "\"leader_remote_cluster\":\"leader_cluster\"" - + "}"; + final String requestBody = """ + { + "follower_cluster": "follow-cluster", + "follower_index": "%s", + "follower_index_uuid": "%s", + "leader_remote_cluster": "leader_cluster" + }""".formatted(forgetFollower, followerIndexUUID); request.setJsonEntity(requestBody); final Response forgetFollowerResponse = leaderClient.performRequest(request); assertOK(forgetFollowerResponse); @@ -286,7 +286,9 @@ public void testUnPromoteAndFollowDataStream() throws Exception { for (var i = 0; i < numDocs; i++) { var indexRequest = new Request("POST", "/" + dataStreamName + "/_doc"); indexRequest.addParameter("refresh", "true"); - indexRequest.setJsonEntity("{\"@timestamp\": \"" + dateFormat.format(new Date()) + "\",\"message\":\"abc\"}"); + indexRequest.setJsonEntity(""" + {"@timestamp": "%s","message":"abc"} + """.formatted(dateFormat.format(new Date()))); assertOK(leaderClient.performRequest(indexRequest)); } verifyDataStream(leaderClient, dataStreamName, backingIndexName(dataStreamName, 1)); diff --git a/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java b/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java index 2d8217dbe9e80..a6a3bf2fc47da 100644 --- a/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java +++ b/x-pack/plugin/ccr/qa/src/main/java/org/elasticsearch/xpack/ccr/ESCCRRestTestCase.java @@ -120,9 +120,9 @@ protected static void pauseFollow(RestClient client, String followIndex) throws protected static void putAutoFollowPattern(String patternName, String remoteCluster, String indexPattern) throws IOException { Request putPatternRequest = new Request("PUT", "/_ccr/auto_follow/" + patternName); - putPatternRequest.setJsonEntity( - "{\"leader_index_patterns\": [\"" + indexPattern + "\"], \"remote_cluster\": \"" + remoteCluster + "\"}" - ); + putPatternRequest.setJsonEntity(""" + {"leader_index_patterns": ["%s"], "remote_cluster": "%s"} + """.formatted(indexPattern, remoteCluster)); assertOK(client().performRequest(putPatternRequest)); } @@ -174,7 +174,9 @@ protected static void verifyDocuments(final RestClient client, final String inde protected static void verifyCcrMonitoring(final String expectedLeaderIndex, final String expectedFollowerIndex) throws IOException { Request request = new Request("GET", "/.monitoring-*/_search"); - request.setJsonEntity("{\"query\": {\"term\": {\"ccr_stats.leader_index\": \"" + expectedLeaderIndex + "\"}}}"); + request.setJsonEntity(""" + {"query": {"term": {"ccr_stats.leader_index": "%s"}}} + """.formatted(expectedLeaderIndex)); Map response; try { response = toMap(adminClient().performRequest(request)); @@ -216,7 +218,8 @@ protected static void verifyCcrMonitoring(final String expectedLeaderIndex, fina protected static void verifyAutoFollowMonitoring() throws IOException { Request request = new Request("GET", "/.monitoring-*/_search"); - request.setJsonEntity("{\"query\": {\"term\": {\"type\": \"ccr_auto_follow_stats\"}}}"); + request.setJsonEntity(""" + {"query": {"term": {"type": "ccr_auto_follow_stats"}}}"""); Map response; try { response = toMap(adminClient().performRequest(request)); diff --git a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java index f8093b705c8f9..326bfa0cc9d14 100644 --- a/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java +++ b/x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/CcrRepositoryIT.java @@ -434,11 +434,8 @@ public void testFollowerMappingIsUpdated() throws IOException { AtomicBoolean updateSent = new AtomicBoolean(false); Runnable updateMappings = () -> { if (updateSent.compareAndSet(false, true)) { - leaderClient().admin() - .indices() - .preparePutMapping(leaderIndex) - .setSource("{\"properties\":{\"k\":{\"type\":\"long\"}}}", XContentType.JSON) - .execute(ActionListener.wrap(latch::countDown)); + leaderClient().admin().indices().preparePutMapping(leaderIndex).setSource(""" + {"properties":{"k":{"type":"long"}}}""", XContentType.JSON).execute(ActionListener.wrap(latch::countDown)); } try { latch.await(); diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java index d82f4e345ecfc..986eb10130d8f 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java @@ -429,13 +429,20 @@ private ClusterHealthStatus ensureColor( ClusterHealthResponse actionGet = testCluster.client().admin().cluster().health(healthRequest).actionGet(); if (actionGet.isTimedOut()) { logger.info( - "{} timed out: " - + "\nleader cluster state:\n{}" - + "\nleader cluster hot threads:\n{}" - + "\nleader cluster tasks:\n{}" - + "\nfollower cluster state:\n{}" - + "\nfollower cluster hot threads:\n{}" - + "\nfollower cluster tasks:\n{}", + """ + {} timed out: + leader cluster state: + {} + leader cluster hot threads: + {} + leader cluster tasks: + {} + follower cluster state: + {} + follower cluster hot threads: + {} + follower cluster tasks: + {}""", method, leaderClient().admin().cluster().prepareState().get().getState(), getHotThreads(leaderClient()), diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java index 711179541ef3d..19ea2c80b722b 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/action/TransportResumeFollowActionTests.java @@ -148,18 +148,13 @@ public void testValidation() throws IOException { } { // should fail, because leader has a field with the same name mapped as keyword and follower as text - IndexMetadata leaderIMD = createIMD( - "index1", - State.OPEN, - "{\"properties\": {\"field\": {\"type\": \"keyword\"}}}", - 5, - Settings.EMPTY, - null - ); + IndexMetadata leaderIMD = createIMD("index1", State.OPEN, """ + {"properties": {"field": {"type": "keyword"}}}""", 5, Settings.EMPTY, null); IndexMetadata followIMD = createIMD( "index2", State.OPEN, - "{\"properties\": {\"field\": {\"type\": \"text\"}}}", + """ + {"properties": {"field": {"type": "text"}}}""", 5, Settings.builder().put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true).build(), customMetadata @@ -171,7 +166,8 @@ public void testValidation() throws IOException { } { // should fail because of non whitelisted settings not the same between leader and follow index - String mapping = "{\"properties\": {\"field\": {\"type\": \"text\", \"analyzer\": \"my_analyzer\"}}}"; + String mapping = """ + {"properties": {"field": {"type": "text", "analyzer": "my_analyzer"}}}"""; IndexMetadata leaderIMD = createIMD( "index1", State.OPEN, @@ -196,15 +192,11 @@ public void testValidation() throws IOException { customMetadata ); Exception e = expectThrows(IllegalArgumentException.class, () -> validate(request, leaderIMD, followIMD, UUIDs, null)); - assertThat( - e.getMessage(), - equalTo( - "the leader index settings [{\"index.analysis.analyzer.my_analyzer.tokenizer\"" - + ":\"whitespace\",\"index.analysis.analyzer.my_analyzer.type\":\"custom\",\"index.number_of_shards\":\"5\"}] " - + "and follower index settings [{\"index.analysis.analyzer.my_analyzer.tokenizer\":\"standard\"," - + "\"index.analysis.analyzer.my_analyzer.type\":\"custom\",\"index.number_of_shards\":\"5\"}] must be identical" - ) - ); + assertThat(e.getMessage(), equalTo(""" + the leader index settings [{"index.analysis.analyzer.my_analyzer.tokenizer":"whitespace",\ + "index.analysis.analyzer.my_analyzer.type":"custom","index.number_of_shards":"5"}] and follower index settings \ + [{"index.analysis.analyzer.my_analyzer.tokenizer":"standard","index.analysis.analyzer.my_analyzer.type":"custom",\ + "index.number_of_shards":"5"}] must be identical""")); } { // should fail because the following index does not have the following_index settings @@ -245,7 +237,8 @@ public void testValidation() throws IOException { } { // should succeed, index settings are identical - String mapping = "{\"properties\": {\"field\": {\"type\": \"text\", \"analyzer\": \"my_analyzer\"}}}"; + String mapping = """ + {"properties": {"field": {"type": "text", "analyzer": "my_analyzer"}}}"""; IndexMetadata leaderIMD = createIMD( "index1", State.OPEN, @@ -280,7 +273,8 @@ public void testValidation() throws IOException { } { // should succeed despite whitelisted settings being different - String mapping = "{\"properties\": {\"field\": {\"type\": \"text\", \"analyzer\": \"my_analyzer\"}}}"; + String mapping = """ + {"properties": {"field": {"type": "text", "analyzer": "my_analyzer"}}}"""; IndexMetadata leaderIMD = createIMD( "index1", State.OPEN, 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 c5e10371381c1..2e789d7cecd37 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 @@ -107,61 +107,56 @@ 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.stripWhitespace( + """ + { + "cluster_uuid": "_cluster", + "timestamp": "%s", + "interval_ms": %s, + "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": %s, + "number_of_failed_remote_cluster_state_requests": %s, + "number_of_successful_follow_indices": %s, + "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": %s, + "last_seen_metadata_version": %s + } + ] + } + }""".formatted( + 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() + ) + ) ) ); } 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 72502c044bf59..f002d86b3f1dd 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 @@ -153,118 +153,96 @@ public void testToXContent() throws IOException { assertThat( xContent.utf8ToString(), equalTo( - "{" - + "\"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\"}" - + "}" - + "}" + XContentHelper.stripWhitespace( + """ + { + "cluster_uuid": "_cluster", + "timestamp": "%s", + "interval_ms": %s, + "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": %s, + "leader_global_checkpoint": %s, + "leader_max_seq_no": %s, + "follower_global_checkpoint": %s, + "follower_max_seq_no": %s, + "last_requested_seq_no": %s, + "outstanding_read_requests": %s, + "outstanding_write_requests": %s, + "write_buffer_operation_count": %s, + "write_buffer_size_in_bytes": %s, + "follower_mapping_version": %s, + "follower_settings_version": %s, + "follower_aliases_version": %s, + "total_read_time_millis": %s, + "total_read_remote_exec_time_millis": %s, + "successful_read_requests": %s, + "failed_read_requests": %s, + "operations_read": %s, + "bytes_read": %s, + "total_write_time_millis": %s, + "successful_write_requests": %s, + "failed_write_requests": %s, + "operations_written": %s, + "read_exceptions": [ + { + "from_seq_no": %s, + "retries": %s, + "exception": { + "type": "exception", + "reason": "shard is sad" + } + } + ], + "time_since_last_read_millis": %s, + "fatal_exception": { + "type": "exception", + "reason": "fatal error" + } + } + }""".formatted( + 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 + ) + ) ) ); } diff --git a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java index aee2df5817305..4e9d18662074d 100644 --- a/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java +++ b/x-pack/plugin/core/src/internalClusterTest/java/org/elasticsearch/snapshots/sourceonly/SourceOnlySnapshotIT.java @@ -20,6 +20,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.CollectionUtils; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.env.Environment; import org.elasticsearch.index.IndexSettings; @@ -217,31 +218,60 @@ public void testSnapshotWithDanglingLocalSegment() throws Exception { assertSuccessful(startFullSnapshot(repo, "snapshot-3")); } - private static void assertMappings(String sourceIdx, boolean requireRouting, boolean useNested) { + private static void assertMappings(String sourceIdx, boolean requireRouting, boolean useNested) throws IOException { GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings(sourceIdx).get(); MappingMetadata mapping = getMappingsResponse.getMappings().get(sourceIdx); - String nested = useNested - ? ",\"incorrect\":{\"type\":\"object\"},\"nested\":{\"type\":\"nested\",\"properties\":{\"value\":{\"type\":\"long\"}}}" - : ""; + String nested = useNested ? """ + ,"incorrect":{"type":"object"},"nested":{"type":"nested","properties":{"value":{"type":"long"}}}""" : ""; if (requireRouting) { - assertEquals( - "{\"_doc\":{\"enabled\":false," - + "\"_meta\":{\"_doc\":{\"_routing\":{\"required\":true}," - + "\"properties\":{\"field1\":{\"type\":\"text\"," - + "\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}" - + nested - + "}}}}}", - mapping.source().string() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "enabled": false, + "_meta": { + "_doc": { + "_routing": { + "required": true + }, + "properties": { + "field1": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + %s + } + } + } + } + }""".formatted(nested)), mapping.source().string()); } else { - assertEquals( - "{\"_doc\":{\"enabled\":false," - + "\"_meta\":{\"_doc\":{\"properties\":{\"field1\":{\"type\":\"text\"," - + "\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":256}}}" - + nested - + "}}}}}", - mapping.source().string() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_doc": { + "enabled": false, + "_meta": { + "_doc": { + "properties": { + "field1": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 + } + } + } + %s + } + } + } + } + }""".formatted(nested)), mapping.source().string()); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java index 3cd169df87676..f0c583268332c 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java @@ -175,15 +175,11 @@ private void logExpirationWarning(long expirationMillis, boolean expired) { static CharSequence buildExpirationMessage(long expirationMillis, boolean expired) { String expiredMsg = expired ? "expired" : "will expire"; - String general = LoggerMessageFormat.format( - null, - "License [{}] on [{}].\n" - + "# If you have a new license, please update it. Otherwise, please reach out to\n" - + "# your support contact.\n" - + "# ", - expiredMsg, - DATE_FORMATTER.formatMillis(expirationMillis) - ); + String general = LoggerMessageFormat.format(null, """ + License [{}] on [{}]. + # If you have a new license, please update it. Otherwise, please reach out to + # your support contact. + #\s""", expiredMsg, DATE_FORMATTER.formatMillis(expirationMillis)); if (expired) { general = general.toUpperCase(Locale.ROOT); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java index 8963f420cf033..4be82c5e58d1d 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartupSelfGeneratedLicenseTask.java @@ -107,12 +107,14 @@ private ClusterState extendBasic(ClusterState currentState, LicensesMetadata cur Metadata.Builder mdBuilder = Metadata.builder(currentState.metadata()); LicensesMetadata newLicenseMetadata = createBasicLicenseFromExistingLicense(currentLicenseMetadata); mdBuilder.putCustom(LicensesMetadata.TYPE, newLicenseMetadata); - logger.info( - "Existing basic license has an expiration. Basic licenses no longer expire." - + "Regenerating license.\n\nOld license:\n {}\n\n New license:\n{}", - license, - newLicenseMetadata.getLicense() - ); + logger.info(""" + Existing basic license has an expiration. Basic licenses no longer expire.Regenerating license. + + Old license: + {} + + New license: + {}""", license, newLicenseMetadata.getLicense()); return ClusterState.builder(currentState).metadata(mdBuilder).build(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java index 0e050298a7ff9..4e4a48d6f97cc 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java @@ -180,10 +180,11 @@ private static String[] monitoringAcknowledgementMessages(OperationMode currentM case ENTERPRISE: return new String[] { LoggerMessageFormat.format( - "Multi-cluster support is disabled for clusters with [{}] license. If you are\n" - + "running multiple clusters, users won't be able to access the clusters with\n" - + "[{}] licenses from within a single X-Pack Kibana instance. You will have to deploy a\n" - + "separate and dedicated X-pack Kibana instance for each [{}] cluster you wish to monitor.", + """ + Multi-cluster support is disabled for clusters with [{}] license. If you are + running multiple clusters, users won't be able to access the clusters with + [{}] licenses from within a single X-Pack Kibana instance. You will have to deploy a + separate and dedicated X-pack Kibana instance for each [{}] cluster you wish to monitor.""", newMode, newMode, newMode diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java index f7a24ccaca448..8985c65e679ca 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlStatsIndex.java @@ -27,7 +27,10 @@ public class MlStatsIndex { private MlStatsIndex() {} public static String wrappedMapping() { - return "{\n\"_doc\" : " + mapping() + "\n}"; + return """ + { + "_doc" : %s + }""".formatted(mapping()); } public static String mapping() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java index 7a9cbbc557d52..fb1c9329513ca 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/persistence/AnomalyDetectorsIndex.java @@ -125,7 +125,10 @@ public static void createStateIndexAndAliasIfNecessaryAndWaitForYellow( } public static String wrappedResultsMapping() { - return "{\n\"_doc\" : " + resultsMapping() + "\n}"; + return """ + { + "_doc" : %s + }""".formatted(resultsMapping()); } public static String resultsMapping() { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheResponse.java index c320a6b7b2b73..f29b3c2ac8464 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/realm/ClearRealmCacheResponse.java @@ -63,7 +63,8 @@ public String toString() { builder.endObject(); return Strings.toString(builder); } catch (IOException e) { - return "{ \"error\" : \"" + e.getMessage() + "\"}"; + return """ + { "error" : "%s" }""".formatted(e.getMessage()); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java index 6b5ae85f641f0..79c74e364ac9a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/role/ClearRolesCacheResponse.java @@ -66,7 +66,8 @@ public String toString() { builder.endObject(); return Strings.toString(builder); } catch (IOException e) { - return "{ \"error\" : \"" + e.getMessage() + "\"}"; + return """ + { "error" : "%s" }""".formatted(e.getMessage()); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java index 3c2be30b6e93b..e1f9abaf17f82 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/RoleDescriptor.java @@ -604,9 +604,9 @@ private static RoleDescriptor.IndicesPrivileges parseIndex(String roleName, XCon grantedFields = readStringArray(roleName, parser, true); } else { throw new ElasticsearchParseException( - "[\"fields\": [...]] format has changed for field" - + " permissions in role [{}], use [\"{}\": {\"{}\":[...]," - + "\"{}\":[...]}] instead", + """ + ["fields": [...]] format has changed for field permissions in role [{}], \ + use ["{}": {"{}":[...],"{}":[...]}] instead""", roleName, Fields.FIELD_PERMISSIONS, Fields.GRANT_FIELDS, diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java index 6ba5d562a35eb..5356116c18fc9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseTests.java @@ -34,21 +34,25 @@ public class LicenseTests extends ESTestCase { public void testFromXContentForGoldLicenseWithVersion2Signature() throws Exception { - String licenseString = "{\"license\":" - + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," - + "\"type\":\"gold\"," - + "\"issue_date_in_millis\":1546589020459," - + "\"expiry_date_in_millis\":1546596340459," - + "\"max_nodes\":5," - + "\"issued_to\":\"customer\"," - + "\"issuer\":\"elasticsearch\"," - + "\"signature\":\"AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVpOW" - + "F5Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMlBWeU81" - + "V1hLUm1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBneWNJcFQ0bX" - + "l0cmhNNnRwbE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOHN1cHNtSTFIUGN1" - + "bWNGNEcxekhrblhNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9PQAAAQAth77fQLF7CC" - + "EL7wA6Z0/UuRm/weECcsjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs3LOOor6An/lhptxBWdwYmf" - + "bcp0m8mnXZh1vN9rmbTsZXnhBIoPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6f\"}"; + String licenseString = """ + { + "license": { + "uid": "4056779d-b823-4c12-a9cb-efa4a8d8c422", + "type": "gold", + "issue_date_in_millis": 1546589020459, + "expiry_date_in_millis": 1546596340459, + "max_nodes": 5, + "issued_to": "customer", + "issuer": "elasticsearch", + "signature": "AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVp\ + OWF5Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMl\ + BWeU81V1hLUm1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBn\ + eWNJcFQ0bXl0cmhNNnRwbE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOH\ + N1cHNtSTFIUGN1bWNGNEcxekhrblhNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9\ + PQAAAQAth77fQLF7CCEL7wA6Z0/UuRm/weECcsjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs\ + 3LOOor6An/lhptxBWdwYmfbcp0m8mnXZh1vN9rmbTsZXnhBIoPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6f" + } + }"""; License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); assertThat(license.type(), equalTo("gold")); assertThat(license.uid(), equalTo("4056779d-b823-4c12-a9cb-efa4a8d8c422")); @@ -62,19 +66,23 @@ public void testFromXContentForGoldLicenseWithVersion2Signature() throws Excepti } public void testFromXContentForGoldLicenseWithVersion4Signature() throws Exception { - String licenseString = "{\"license\":{" - + "\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," - + "\"type\":\"gold\"," - + "\"issue_date_in_millis\":1546589020459," - + "\"expiry_date_in_millis\":1546596340459," - + "\"max_nodes\":5," - + "\"issued_to\":\"customer\"," - + "\"issuer\":\"elasticsearch\"," - + "\"signature\":\"AAAABAAAAA22vXffI41oM4jLCwZ6AAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAH3oL4weubwYGjLGNZsz90" - + "EerX6yOX3Dh6wswG9EfqCiyv6lcjuC7aeKKuOkqhMRTHZ9vHnfMuakHWVlpuGC14WyGqaMwSmgTZ9jVAzt/W3sIotRxM/3rtlCXUc1rOUXNFcii1i3Kkrc" - + "kTzhENTKjdkOmUN3qZlTEmHkp93eYpx8++iIukHYU9K9Vm2VKgydFfxvYaN/Qr+iPfJSbHJB8+DmS2ywdrmdqW+ScE+1ZNouPNhnP3RKTleNvixXPG9l5B" - + "qZ2So1IlCrxVDByA1E6JH5AvjbOucpcGiWCm7IzvfpkzphKHMyxhUaIByoHl9UAf4AdPLhowWAQk0eHMRDDlo=\"," - + "\"start_date_in_millis\":-1}}\n"; + String licenseString = """ + { + "license": { + "uid": "4056779d-b823-4c12-a9cb-efa4a8d8c422", + "type": "gold", + "issue_date_in_millis": 1546589020459, + "expiry_date_in_millis": 1546596340459, + "max_nodes": 5, + "issued_to": "customer", + "issuer": "elasticsearch", + "signature": "AAAABAAAAA22vXffI41oM4jLCwZ6AAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAH3oL4weubwYGjLGNZsz90EerX6y\ + OX3Dh6wswG9EfqCiyv6lcjuC7aeKKuOkqhMRTHZ9vHnfMuakHWVlpuGC14WyGqaMwSmgTZ9jVAzt/W3sIotRxM/3rtlCXUc1rOUXNFcii1i3KkrckTzhENTKjdkOmU\ + N3qZlTEmHkp93eYpx8++iIukHYU9K9Vm2VKgydFfxvYaN/Qr+iPfJSbHJB8+DmS2ywdrmdqW+ScE+1ZNouPNhnP3RKTleNvixXPG9l5BqZ2So1IlCrxVDByA1E6JH5\ + AvjbOucpcGiWCm7IzvfpkzphKHMyxhUaIByoHl9UAf4AdPLhowWAQk0eHMRDDlo=", + "start_date_in_millis": -1 + } + }"""; License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); assertThat(license.type(), equalTo("gold")); assertThat(license.uid(), equalTo("4056779d-b823-4c12-a9cb-efa4a8d8c422")); @@ -88,20 +96,24 @@ public void testFromXContentForGoldLicenseWithVersion4Signature() throws Excepti } public void testFromXContentForEnterpriseLicenseWithV5Signature() throws Exception { - String licenseString = "{\"license\":{" - + "\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," - + "\"type\":\"enterprise\"," - + "\"issue_date_in_millis\":1546589020459," - + "\"expiry_date_in_millis\":1546596340459," - + "\"max_nodes\":null," - + "\"max_resource_units\":15," - + "\"issued_to\":\"customer\"," - + "\"issuer\":\"elasticsearch\"," - + "\"signature\":\"AAAABQAAAA2MUoEqXb9K9Ie5d6JJAAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAAAwVZKGAmDELUlS5PScBkhQsZa" - + "DaQTtJ4ZP5EnZ/nLpmCt9Dj7d/FRsgMtHmSJLrr2CdrIo4Vx5VuhmbwzZvXMttLz2lrJzG7770PX3TnC9e7F9GdnE9ec0FP2U0ZlLOBOtPuirX0q+j6GfB+DLyE" - + "5D+Lo1NQ3eLJGvbd3DBYPWJxkb+EBVHczCH2OrIEVWnN/TafmkdZCPX5PcultkNOs3j7d3s7b51EXHKoye8UTcB/RGmzZwMah+E6I/VJkqu7UHL8bB01wJeqo6W" - + "xI4LC/9+f5kpmHrUu3CHe5pHbmMGDk7O6/cwt1pw/hnJXKIFCi36IGaKcHLgORxQdN0uzE=\"," - + "\"start_date_in_millis\":-1}}"; + String licenseString = """ + { + "license": { + "uid": "4056779d-b823-4c12-a9cb-efa4a8d8c422", + "type": "enterprise", + "issue_date_in_millis": 1546589020459, + "expiry_date_in_millis": 1546596340459, + "max_nodes": null, + "max_resource_units": 15, + "issued_to": "customer", + "issuer": "elasticsearch", + "signature": "AAAABQAAAA2MUoEqXb9K9Ie5d6JJAAAAIAo5/x6hrsGh1GqqrJmy4qgmEC7gK0U4zQ6q5ZEMhm4jAAABAAAwVZKGAmDELUlS5PScBkhQsZaD\ + aQTtJ4ZP5EnZ/nLpmCt9Dj7d/FRsgMtHmSJLrr2CdrIo4Vx5VuhmbwzZvXMttLz2lrJzG7770PX3TnC9e7F9GdnE9ec0FP2U0ZlLOBOtPuirX0q+j6GfB+DLyE5D+L\ + o1NQ3eLJGvbd3DBYPWJxkb+EBVHczCH2OrIEVWnN/TafmkdZCPX5PcultkNOs3j7d3s7b51EXHKoye8UTcB/RGmzZwMah+E6I/VJkqu7UHL8bB01wJeqo6WxI4LC/9\ + +f5kpmHrUu3CHe5pHbmMGDk7O6/cwt1pw/hnJXKIFCi36IGaKcHLgORxQdN0uzE=", + "start_date_in_millis": -1 + } + }"""; License license = License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); assertThat(license.type(), equalTo("enterprise")); assertThat(license.uid(), equalTo("4056779d-b823-4c12-a9cb-efa4a8d8c422")); @@ -218,16 +230,19 @@ public void testSerializationOfLicenseForEveryLicenseType() throws Exception { public void testNotEnoughBytesFromXContent() throws Exception { - String licenseString = "{\"license\": " - + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," - + "\"type\":\"gold\"," - + "\"issue_date_in_millis\":1546589020459," - + "\"expiry_date_in_millis\":1546596340459," - + "\"max_nodes\":5," - + "\"issued_to\":\"customer\"," - + "\"issuer\":\"elasticsearch\"," - + "\"signature\":\"AA\"}" - + "}"; + String licenseString = """ + { + "license": { + "uid": "4056779d-b823-4c12-a9cb-efa4a8d8c422", + "type": "gold", + "issue_date_in_millis": 1546589020459, + "expiry_date_in_millis": 1546596340459, + "max_nodes": 5, + "issued_to": "customer", + "issuer": "elasticsearch", + "signature": "AA" + } + }"""; ElasticsearchException exception = expectThrows( ElasticsearchException.class, () -> { License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); } @@ -238,18 +253,19 @@ public void testNotEnoughBytesFromXContent() throws Exception { public void testMalformedSignatureFromXContent() throws Exception { - String licenseString = "{\"license\": " - + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," - + "\"type\":\"gold\"," - + "\"issue_date_in_millis\":1546589020459," - + "\"expiry_date_in_millis\":1546596340459," - + "\"max_nodes\":5," - + "\"issued_to\":\"customer\"," - + "\"issuer\":\"elasticsearch\"," - + "\"signature\":\"" - + randomAlphaOfLength(10) - + "\"}" - + "}"; + String licenseString = """ + { + "license": { + "uid": "4056779d-b823-4c12-a9cb-efa4a8d8c422", + "type": "gold", + "issue_date_in_millis": 1546589020459, + "expiry_date_in_millis": 1546596340459, + "max_nodes": 5, + "issued_to": "customer", + "issuer": "elasticsearch", + "signature": "%s" + } + }""".formatted(randomAlphaOfLength(10)); ElasticsearchException exception = expectThrows( ElasticsearchException.class, () -> { License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); } @@ -261,22 +277,25 @@ public void testMalformedSignatureFromXContent() throws Exception { public void testUnableToBase64DecodeFromXContent() throws Exception { - String licenseString = "{\"license\":" - + "{\"uid\":\"4056779d-b823-4c12-a9cb-efa4a8d8c422\"," - + "\"type\":\"gold\"," - + "\"issue_date_in_millis\":1546589020459," - + "\"expiry_date_in_millis\":1546596340459," - + "\"max_nodes\":5," - + "\"issued_to\":\"customer\"," - + "\"issuer\":\"elasticsearch\"," - + "\"signature\":\"AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVpOW" - + "F5Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMlBWeU81" - + "V1hLUm1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBneWNJcFQ0bX" - + "l0cmhNNnRwbE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOHN1cHNtSTFIUGN1" - + "bWNGNEcxekhrblhNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9PQAAAQAth77fQLF7CC" - + "EL7wA6Z0/UuRm/weECcsjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs3LOOor6An/lhptxBWdwYmf" - + "+xHAQ8tyvRqP5G+PRU7tiluEwR/eyHGZV2exdJNzmoGzdPSWwueBM5HK2GexORICH+UFI4cuGz444/hL2MMM1RdpVWQkT0SJ6D9x/VuSmHuYPdtX59Pp41LXvl" - + "bcp0m8mnXZh1vN9rmbTsZXnhBIoPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6fd\"}"; + String licenseString = """ + { + "license": { + "uid": "4056779d-b823-4c12-a9cb-efa4a8d8c422", + "type": "gold", + "issue_date_in_millis": 1546589020459, + "expiry_date_in_millis": 1546596340459, + "max_nodes": 5, + "issued_to": "customer", + "issuer": "elasticsearch", + "signature": "AAAAAgAAAA34V2kfTJVtvdL2LttwAAABmFJ6NGRnbEM3WVQrZVQwNkdKQmR1VytlMTMyM1J0dTZ1WGwyY2ZCVFhqMGtJU2gzZ3pnNTVpOWF5\ + Y1NaUkwyN2VsTEtCYnlZR2c5WWtjQ0phaDlhRjlDUXViUmUwMWhjSkE2TFcwSGdneTJHbUV4N2RHUWJxV20ybjRsZHRzV2xkN0ZmdDlYblJmNVcxMlBWeU81V1hLUm\ + 1EK0V1dmF3cFdlSGZzTU5SZE1qUmFra3JkS1hCanBWVmVTaFFwV3BVZERzeG9Sci9rYnlJK2toODZXY09tNmFHUVNUL3IyUHExV3VSTlBneWNJcFQ0bXl0cmhNNnRw\ + bE1CWE4zWjJ5eGFuWFo0NGhsb3B5WFd1eTdYbFFWQkxFVFFPSlBERlB0eVVJYXVSZ0lsR2JpRS9rN1h4MSsvNUpOcGN6cU1NOHN1cHNtSTFIUGN1bWNGNEcxekhrbl\ + hNOXZ2VEQvYmRzQUFwbytUZEpRR3l6QU5oS2ZFSFdSbGxxNDZyZ0xvUHIwRjdBL2JqcnJnNGFlK09Cek9pYlJ5Umc9PQAAAQAth77fQLF7CCEL7wA6Z0/UuRm/weEC\ + csjW/50kBnPLO8yEs+9/bPa5LSU0bF6byEXOVeO0ebUQfztpjulbXh8TrBDSG+6VdxGtohPo2IYPBaXzGs3LOOor6An/lhptxBWdwYmf+xHAQ8tyvRqP5G+PRU7til\ + uEwR/eyHGZV2exdJNzmoGzdPSWwueBM5HK2GexORICH+UFI4cuGz444/hL2MMM1RdpVWQkT0SJ6D9x/VuSmHuYPdtX59Pp41LXvlbcp0m8mnXZh1vN9rmbTsZXnhBI\ + oPTaRDwUBi3vJ3Ms3iLaEm4S8Slrfmtht2jUjgGZ2vAeZ9OHU2YsGtrSpz6fd" + }"""; ElasticsearchException exception = expectThrows( ElasticsearchException.class, () -> { License.fromSource(new BytesArray(licenseString.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java index 0616ca80ee63c..282ddfb641219 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/action/ReloadAnalyzersResponseTests.java @@ -8,6 +8,7 @@ import org.elasticsearch.action.support.DefaultShardOperationFailedException; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.AbstractBroadcastResponseTestCase; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.xcontent.XContentParser; @@ -48,19 +49,28 @@ protected ReloadAnalyzersResponse doParseInstance(XContentParser parser) throws } @Override - public void testToXContent() { + public void testToXContent() throws IOException { Map reloadedIndicesNodes = Collections.singletonMap( "index", new ReloadDetails("index", Collections.singleton("nodeId"), Collections.singleton("my_analyzer")) ); ReloadAnalyzersResponse response = new ReloadAnalyzersResponse(10, 5, 5, null, reloadedIndicesNodes); String output = Strings.toString(response); - assertEquals( - "{\"_shards\":{\"total\":10,\"successful\":5,\"failed\":5}," - + "\"reload_details\":[{\"index\":\"index\",\"reloaded_analyzers\":[\"my_analyzer\"],\"reloaded_node_ids\":[\"nodeId\"]}]" - + "}", - output - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "_shards": { + "total": 10, + "successful": 5, + "failed": 5 + }, + "reload_details": [ + { + "index": "index", + "reloaded_analyzers": [ "my_analyzer" ], + "reloaded_node_ids": [ "nodeId" ] + } + ] + }"""), output); } public void testSerialization() throws IOException { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java index f7c4c52d28263..94d96b383ebb6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java @@ -176,16 +176,9 @@ public void onFailure(Exception e) { ElasticsearchException stepException = failedStep.get(); assertThat(stepException, notNullValue()); - assertThat( - stepException.getMessage(), - is( - "index [" - + index.getName() - + "] in policy [ilmPolicy] encountered failures [{\"shard\":0,\"index\":\"" - + index.getName() - + "\",\"status\":\"BAD_REQUEST\",\"reason\":{\"type\":\"illegal_argument_exception\"," - + "\"reason\":\"couldn't merge\"}}] on step [forcemerge]" - ) - ); + assertThat(stepException.getMessage(), is(""" + index [%s] in policy [ilmPolicy] encountered failures [{"shard":0,"index":"%s","status":"BAD_REQUEST",\ + "reason":{"type":"illegal_argument_exception","reason":"couldn't merge"}}] on step [forcemerge]\ + """.formatted(index.getName(), index.getName()))); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java index 9a7b5aa84791c..dfb05876981b6 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/PhaseCacheManagementTests.java @@ -14,11 +14,13 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.NamedXContentRegistry; import org.elasticsearch.xcontent.ParseField; +import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -52,29 +54,28 @@ public class PhaseCacheManagementTests extends ESTestCase { ); } - public void testRefreshPhaseJson() { + public void testRefreshPhaseJson() throws IOException { LifecycleExecutionState.Builder exState = LifecycleExecutionState.builder() .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ); + .setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }"""); IndexMetadata meta = buildIndexMetadata("my-policy", exState); String indexName = meta.getIndex().getName(); @@ -103,13 +104,23 @@ public void testRefreshPhaseJson() { assertThat(beforeState, equalTo(afterState)); // Check that the phase definition has been refreshed - assertThat( - afterExState.getPhaseDefinition(), - equalTo( - "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":1}," - + "\"set_priority\":{\"priority\":100}}},\"version\":2,\"modified_date_in_millis\":2}" - ) - ); + assertThat(afterExState.getPhaseDefinition(), equalTo(XContentHelper.stripWhitespace(""" + { + "policy": "my-policy", + "phase_definition": { + "min_age": "0ms", + "actions": { + "rollover": { + "max_docs": 1 + }, + "set_priority": { + "priority": 100 + } + } + }, + "version": 2, + "modified_date_in_millis": 2 + }"""))); } public void testEligibleForRefresh() { @@ -202,25 +213,20 @@ public void testReadStepKeys() { assertNull(readStepKeys(REGISTRY, client, "", "phase", null)); assertThat( - readStepKeys( - REGISTRY, - client, - "{\n" - + " \"policy\": \"my_lifecycle3\",\n" - + " \"phase_definition\": { \n" - + " \"min_age\": \"0ms\",\n" - + " \"actions\": {\n" - + " \"rollover\": {\n" - + " \"max_age\": \"30s\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\": 3, \n" - + " \"modified_date_in_millis\": 1539609701576 \n" - + " }", - "phase", - null - ), + readStepKeys(REGISTRY, client, """ + { + "policy": "my_lifecycle3", + "phase_definition": { + "min_age": "0ms", + "actions": { + "rollover": { + "max_age": "30s" + } + } + }, + "version": 3, + "modified_date_in_millis": 1539609701576 + }""", "phase", null), contains( new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME), new Step.StepKey("phase", "rollover", RolloverStep.NAME), @@ -231,28 +237,23 @@ public void testReadStepKeys() { ); assertThat( - readStepKeys( - REGISTRY, - client, - "{\n" - + " \"policy\" : \"my_lifecycle3\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }", - "phase", - null - ), + readStepKeys(REGISTRY, client, """ + { + "policy" : "my_lifecycle3", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }""", "phase", null), containsInAnyOrder( new Step.StepKey("phase", "rollover", WaitForRolloverReadyStep.NAME), new Step.StepKey("phase", "rollover", RolloverStep.NAME), @@ -293,24 +294,23 @@ public void testIndexCanBeSafelyUpdated() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ) + .setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }""") .build(); IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); @@ -331,24 +331,23 @@ public void testIndexCanBeSafelyUpdated() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ) + .setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }""") .build(); IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); @@ -368,24 +367,23 @@ public void testIndexCanBeSafelyUpdated() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ) + .setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }""") .build(); IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); @@ -401,26 +399,23 @@ public void testIndexCanBeSafelyUpdated() { // Failure case, index doesn't have enough info to check { - LifecycleExecutionState exState = LifecycleExecutionState.builder() - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ) - .build(); + LifecycleExecutionState exState = LifecycleExecutionState.builder().setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }""").build(); IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); @@ -456,15 +451,28 @@ public void testIndexCanBeSafelyUpdated() { } } - public void testUpdateIndicesForPolicy() { + public void testUpdateIndicesForPolicy() throws IOException { LifecycleExecutionState exState = LifecycleExecutionState.builder() .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":" - + "{\"max_docs\":1},\"set_priority\":{\"priority\":100}}},\"version\":1,\"modified_date_in_millis\":1578521007076}" - ) + .setPhaseDefinition(""" + { + "policy": "my-policy", + "phase_definition": { + "min_age": "0ms", + "actions": { + "rollover": { + "max_docs": 1 + }, + "set_priority": { + "priority": 100 + } + } + }, + "version": 1, + "modified_date_in_millis": 1578521007076 + }""") .build(); IndexMetadata meta = mkMeta().putCustom(ILM_CUSTOM_METADATA_KEY, exState.asMap()).build(); @@ -540,13 +548,23 @@ public void testUpdateIndicesForPolicy() { assertThat(beforeState, equalTo(afterState)); // Check that the phase definition has been refreshed - assertThat( - afterExState.getPhaseDefinition(), - equalTo( - "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":2}," - + "\"set_priority\":{\"priority\":150}}},\"version\":2,\"modified_date_in_millis\":2}" - ) - ); + assertThat(afterExState.getPhaseDefinition(), equalTo(XContentHelper.stripWhitespace(""" + { + "policy": "my-policy", + "phase_definition": { + "min_age": "0ms", + "actions": { + "rollover": { + "max_docs": 2 + }, + "set_priority": { + "priority": 150 + } + } + }, + "version": 2, + "modified_date_in_millis": 2 + }"""))); } private IndexMetadata buildIndexMetadata(String policy, LifecycleExecutionState.Builder lifecycleState) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java index 43fe7a217b408..75db1d482004f 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedConfigTests.java @@ -132,109 +132,112 @@ protected DatafeedConfig doParseInstance(XContentParser parser) { return DatafeedConfig.STRICT_PARSER.apply(parser, null).build(); } - private static final String FUTURE_DATAFEED = "{\n" - + " \"datafeed_id\": \"farequote-datafeed\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + " \"tomorrows_technology_today\": \"amazing\",\n" - + " \"scroll_size\": 1234\n" - + "}"; - - private static final String ANACHRONISTIC_QUERY_DATAFEED = "{\n" - + " \"datafeed_id\": \"farequote-datafeed\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + - // query:match:type stopped being supported in 6.x - " \"query\": {\"match\" : {\"query\":\"fieldName\", \"type\": \"phrase\"}},\n" - + " \"scroll_size\": 1234\n" - + "}"; - - private static final String ANACHRONISTIC_AGG_DATAFEED = "{\n" - + " \"datafeed_id\": \"farequote-datafeed\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + " \"aggregations\": {\n" - + " \"buckets\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"fixed_interval\": \"360s\",\n" - + " \"time_zone\": \"UTC\"\n" - + " },\n" - + " \"aggregations\": {\n" - + " \"time\": {\n" - + " \"max\": {\"field\": \"time\"}\n" - + " },\n" - + " \"airline\": {\n" - + " \"terms\": {\n" - + " \"field\": \"airline\",\n" - + " \"size\": 0\n" - + // size: 0 stopped being supported in 6.x - " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; - - private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = "{\n" - + " \"datafeed_id\": \"farequote-datafeed\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + " \"aggregations\": {\n" - + " \"buckets\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"interval\": \"360s\",\n" - + " \"time_zone\": \"UTC\"\n" - + " },\n" - + " \"aggregations\": {\n" - + " \"time\": {\n" - + " \"max\": {\"field\": \"time\"}\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; - - private static final String MULTIPLE_AGG_DEF_DATAFEED = "{\n" - + " \"datafeed_id\": \"farequote-datafeed\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + " \"aggregations\": {\n" - + " \"buckets\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"fixed_interval\": \"360s\",\n" - + " \"time_zone\": \"UTC\"\n" - + " },\n" - + " \"aggregations\": {\n" - + " \"time\": {\n" - + " \"max\": {\"field\": \"time\"}\n" - + " }\n" - + " }\n" - + " }\n" - + " }," - + " \"aggs\": {\n" - + " \"buckets2\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"fixed_interval\": \"360s\",\n" - + " \"time_zone\": \"UTC\"\n" - + " },\n" - + " \"aggregations\": {\n" - + " \"time\": {\n" - + " \"max\": {\"field\": \"time\"}\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + private static final String FUTURE_DATAFEED = """ + { + "datafeed_id": "farequote-datafeed", + "job_id": "farequote", + "frequency": "1h", + "indices": ["farequote1", "farequote2"], + "tomorrows_technology_today": "amazing", + "scroll_size": 1234 + }"""; + + // query:match:type stopped being supported in 6.x + private static final String ANACHRONISTIC_QUERY_DATAFEED = """ + { + "datafeed_id": "farequote-datafeed", + "job_id": "farequote", + "frequency": "1h", + "indices": ["farequote1", "farequote2"], + "query": {"match" : {"query":"fieldName", "type": "phrase"}}, + "scroll_size": 1234 + }"""; + + // size: 0 stopped being supported in 6.x + private static final String ANACHRONISTIC_AGG_DATAFEED = """ + { + "datafeed_id": "farequote-datafeed", + "job_id": "farequote", + "frequency": "1h", + "indices": ["farequote1", "farequote2"], + "aggregations": { + "buckets": { + "date_histogram": { + "field": "time", + "fixed_interval": "360s", + "time_zone": "UTC" + }, + "aggregations": { + "time": { + "max": {"field": "time"} + }, + "airline": { + "terms": { + "field": "airline", + "size": 0 + } + } + } + } + } + }"""; + + private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = """ + { + "datafeed_id": "farequote-datafeed", + "job_id": "farequote", + "frequency": "1h", + "indices": ["farequote1", "farequote2"], + "aggregations": { + "buckets": { + "date_histogram": { + "field": "time", + "interval": "360s", + "time_zone": "UTC" + }, + "aggregations": { + "time": { + "max": {"field": "time"} + } + } + } + } + }"""; + + private static final String MULTIPLE_AGG_DEF_DATAFEED = """ + { + "datafeed_id": "farequote-datafeed", + "job_id": "farequote", + "frequency": "1h", + "indices": ["farequote1", "farequote2"], + "aggregations": { + "buckets": { + "date_histogram": { + "field": "time", + "fixed_interval": "360s", + "time_zone": "UTC" + }, + "aggregations": { + "time": { + "max": {"field": "time"} + } + } + } + }, "aggs": { + "buckets2": { + "date_histogram": { + "field": "time", + "fixed_interval": "360s", + "time_zone": "UTC" + }, + "aggregations": { + "time": { + "max": {"field": "time"} + } + } + } + } + }"""; public void testFutureConfigParse() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java index 68b021bcce63c..8c820737fda9c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/datafeed/DatafeedUpdateTests.java @@ -161,40 +161,40 @@ protected NamedXContentRegistry xContentRegistry() { return new NamedXContentRegistry(searchModule.getNamedXContents()); } - private static final String MULTIPLE_AGG_DEF_DATAFEED = "{\n" - + " \"datafeed_id\": \"farequote-datafeed\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + " \"aggregations\": {\n" - + " \"buckets\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"fixed_interval\": \"360s\",\n" - + " \"time_zone\": \"UTC\"\n" - + " },\n" - + " \"aggregations\": {\n" - + " \"time\": {\n" - + " \"max\": {\"field\": \"time\"}\n" - + " }\n" - + " }\n" - + " }\n" - + " }," - + " \"aggs\": {\n" - + " \"buckets2\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"fixed_interval\": \"360s\",\n" - + " \"time_zone\": \"UTC\"\n" - + " },\n" - + " \"aggregations\": {\n" - + " \"time\": {\n" - + " \"max\": {\"field\": \"time\"}\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + private static final String MULTIPLE_AGG_DEF_DATAFEED = """ + { + "datafeed_id": "farequote-datafeed", + "job_id": "farequote", + "frequency": "1h", + "indices": ["farequote1", "farequote2"], + "aggregations": { + "buckets": { + "date_histogram": { + "field": "time", + "fixed_interval": "360s", + "time_zone": "UTC" + }, + "aggregations": { + "time": { + "max": {"field": "time"} + } + } + } + }, "aggs": { + "buckets2": { + "date_histogram": { + "field": "time", + "fixed_interval": "360s", + "time_zone": "UTC" + }, + "aggregations": { + "time": { + "max": {"field": "time"} + } + } + } + } + }"""; public void testMultipleDefinedAggParse() throws IOException { try ( 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 0ba8bc58653e7..5f17e5c4739ca 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 @@ -183,19 +183,23 @@ 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 ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS = """ + { + "id": "old-data-frame", + "source": {"index":"my-index", "query": {"match" : {"query":"fieldName", "type": "phrase"}}}, + "dest": {"index":"dest-index"}, + "analysis": {"outlier_detection": {"n_neighbors": 10}} + }"""; - 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" - + "}"; + private static final String MODERN_QUERY_DATA_FRAME_ANALYTICS = """ + { + "id": "data-frame", + "source": {"index":"my-index", "query": {"match_all" : {}}}, + "dest": {"index":"dest-index"}, + "analysis": {"outlier_detection": {"n_neighbors": 10}} + }"""; private boolean lenient; @@ -381,11 +385,8 @@ public void testBuildForExplain_MissingDest() { } public void testPreventCreateTimeInjection() throws IOException { - String json = "{" - + " \"create_time\" : 123456789 }," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + "}"; + String json = """ + { "create_time" : 123456789 }, "source" : {"index":"src"}, "dest" : {"index": "dest"},}"""; try ( XContentParser parser = XContentFactory.xContent(XContentType.JSON) @@ -397,7 +398,8 @@ 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) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java index 825061b9b3216..4a54a5b644c80 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/ClassificationTests.java @@ -141,42 +141,42 @@ protected Writeable.Reader instanceReader() { } public void testDeserialization() throws IOException { - String toDeserialize = "{\n" - + " \"dependent_variable\": \"FlightDelayMin\",\n" - + " \"feature_processors\": [\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"OriginWeather\",\n" - + " \"hot_map\": {\n" - + " \"sunny_col\": \"Sunny\",\n" - + " \"clear_col\": \"Clear\",\n" - + " \"rainy_col\": \"Rain\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"DestWeather\",\n" - + " \"hot_map\": {\n" - + " \"dest_sunny_col\": \"Sunny\",\n" - + " \"dest_clear_col\": \"Clear\",\n" - + " \"dest_rainy_col\": \"Rain\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"frequency_encoding\": {\n" - + " \"field\": \"OriginWeather\",\n" - + " \"feature_name\": \"mean\",\n" - + " \"frequency_map\": {\n" - + " \"Sunny\": 0.8,\n" - + " \"Rain\": 0.2\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " }" - + ""; + String toDeserialize = """ + { + "dependent_variable": "FlightDelayMin", + "feature_processors": [ + { + "one_hot_encoding": { + "field": "OriginWeather", + "hot_map": { + "sunny_col": "Sunny", + "clear_col": "Clear", + "rainy_col": "Rain" + } + } + }, + { + "one_hot_encoding": { + "field": "DestWeather", + "hot_map": { + "dest_sunny_col": "Sunny", + "dest_clear_col": "Clear", + "dest_rainy_col": "Rain" + } + } + }, + { + "frequency_encoding": { + "field": "OriginWeather", + "feature_name": "mean", + "frequency_map": { + "Sunny": 0.8, + "Rain": 0.2 + } + } + } + ] + }"""; try ( XContentParser parser = XContentHelper.createParser( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java index 9433fbce57a12..3eb4f053ff661 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/analyses/RegressionTests.java @@ -136,42 +136,42 @@ protected Writeable.Reader instanceReader() { } public void testDeserialization() throws IOException { - String toDeserialize = "{\n" - + " \"dependent_variable\": \"FlightDelayMin\",\n" - + " \"feature_processors\": [\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"OriginWeather\",\n" - + " \"hot_map\": {\n" - + " \"sunny_col\": \"Sunny\",\n" - + " \"clear_col\": \"Clear\",\n" - + " \"rainy_col\": \"Rain\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"DestWeather\",\n" - + " \"hot_map\": {\n" - + " \"dest_sunny_col\": \"Sunny\",\n" - + " \"dest_clear_col\": \"Clear\",\n" - + " \"dest_rainy_col\": \"Rain\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"frequency_encoding\": {\n" - + " \"field\": \"OriginWeather\",\n" - + " \"feature_name\": \"mean\",\n" - + " \"frequency_map\": {\n" - + " \"Sunny\": 0.8,\n" - + " \"Rain\": 0.2\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " }" - + ""; + String toDeserialize = """ + { + "dependent_variable": "FlightDelayMin", + "feature_processors": [ + { + "one_hot_encoding": { + "field": "OriginWeather", + "hot_map": { + "sunny_col": "Sunny", + "clear_col": "Clear", + "rainy_col": "Rain" + } + } + }, + { + "one_hot_encoding": { + "field": "DestWeather", + "hot_map": { + "dest_sunny_col": "Sunny", + "dest_clear_col": "Clear", + "dest_rainy_col": "Rain" + } + } + }, + { + "frequency_encoding": { + "field": "OriginWeather", + "feature_name": "mean", + "frequency_map": { + "Sunny": 0.8, + "Rain": 0.2 + } + } + } + ] + }"""; try ( XContentParser parser = XContentHelper.createParser( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java index b5c321768d27d..91cab206b18e5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/ConfusionMatrixTests.java @@ -64,7 +64,8 @@ public void testEvaluate() { ConfusionMatrix confusionMatrix = new ConfusionMatrix(Arrays.asList(0.25, 0.5)); EvaluationMetricResult result = confusionMatrix.evaluate(aggs); - String expected = "{\"0.25\":{\"tp\":1,\"fp\":2,\"tn\":3,\"fn\":4},\"0.5\":{\"tp\":5,\"fp\":6,\"tn\":7,\"fn\":8}}"; + String expected = """ + {"0.25":{"tp":1,"fp":2,"tn":3,"fn":4},"0.5":{"tp":5,"fp":6,"tn":7,"fn":8}}"""; assertThat(Strings.toString(result), equalTo(expected)); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java index 1dfff1f7baf04..62d35a841d7f0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/PrecisionTests.java @@ -62,7 +62,8 @@ public void testEvaluate() { Precision precision = new Precision(Arrays.asList(0.25, 0.5, 0.75)); EvaluationMetricResult result = precision.evaluate(aggs); - String expected = "{\"0.25\":0.2,\"0.5\":0.75,\"0.75\":1.0}"; + String expected = """ + {"0.25":0.2,"0.5":0.75,"0.75":1.0}"""; assertThat(Strings.toString(result), equalTo(expected)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java index 5c2c782a2c268..a21ed18d40398 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/evaluation/outlierdetection/RecallTests.java @@ -62,7 +62,8 @@ public void testEvaluate() { Recall recall = new Recall(Arrays.asList(0.25, 0.5, 0.75)); EvaluationMetricResult result = recall.evaluate(aggs); - String expected = "{\"0.25\":0.2,\"0.5\":0.75,\"0.75\":1.0}"; + String expected = """ + {"0.25":0.2,"0.5":0.75,"0.75":1.0}"""; assertThat(Strings.toString(result), equalTo(expected)); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsageTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsageTests.java index 64eebbfffaeb9..a66cbb1eed184 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsageTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/stats/common/MemoryUsageTests.java @@ -67,6 +67,7 @@ protected MemoryUsage createTestInstance() { public void testZeroUsage() { MemoryUsage memoryUsage = new MemoryUsage("zero_usage_job"); String asJson = Strings.toString(memoryUsage); - assertThat(asJson, equalTo("{\"peak_usage_bytes\":0,\"status\":\"ok\"}")); + assertThat(asJson, equalTo(""" + {"peak_usage_bytes":0,"status":"ok"}""")); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java index 12324cb9c08ed..2a2edb14805b5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/TrainedModelDefinitionTests.java @@ -77,193 +77,193 @@ public static TrainedModelDefinition.Builder createRandomBuilder() { return createRandomBuilder(randomFrom(TargetType.values())); } - public static final String ENSEMBLE_MODEL = "" - + "{\n" - + " \"preprocessors\": [\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"col1\",\n" - + " \"hot_map\": {\n" - + " \"male\": \"col1_male\",\n" - + " \"female\": \"col1_female\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"target_mean_encoding\": {\n" - + " \"field\": \"col2\",\n" - + " \"feature_name\": \"col2_encoded\",\n" - + " \"target_map\": {\n" - + " \"S\": 5.0,\n" - + " \"M\": 10.0,\n" - + " \"L\": 20\n" - + " },\n" - + " \"default_value\": 5.0\n" - + " }\n" - + " },\n" - + " {\n" - + " \"frequency_encoding\": {\n" - + " \"field\": \"col3\",\n" - + " \"feature_name\": \"col3_encoded\",\n" - + " \"frequency_map\": {\n" - + " \"none\": 0.75,\n" - + " \"true\": 0.10,\n" - + " \"false\": 0.15\n" - + " }\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"trained_model\": {\n" - + " \"ensemble\": {\n" - + " \"feature_names\": [\n" - + " \"col1_male\",\n" - + " \"col1_female\",\n" - + " \"col2_encoded\",\n" - + " \"col3_encoded\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"aggregate_output\": {\n" - + " \"weighted_sum\": {\n" - + " \"weights\": [\n" - + " 0.5,\n" - + " 0.5\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"target_type\": \"regression\",\n" - + " \"trained_models\": [\n" - + " {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"col1_male\",\n" - + " \"col1_female\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"split_gain\": 12.0,\n" - + " \"threshold\": 10.0,\n" - + " \"decision_type\": \"lte\",\n" - + " \"default_left\": true,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"leaf_value\": 1\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"leaf_value\": 2\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"col2_encoded\",\n" - + " \"col3_encoded\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"split_gain\": 12.0,\n" - + " \"threshold\": 10.0,\n" - + " \"decision_type\": \"lte\",\n" - + " \"default_left\": true,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"leaf_value\": 1\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"leaf_value\": 2\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; + public static final String ENSEMBLE_MODEL = """ + { + "preprocessors": [ + { + "one_hot_encoding": { + "field": "col1", + "hot_map": { + "male": "col1_male", + "female": "col1_female" + } + } + }, + { + "target_mean_encoding": { + "field": "col2", + "feature_name": "col2_encoded", + "target_map": { + "S": 5.0, + "M": 10.0, + "L": 20 + }, + "default_value": 5.0 + } + }, + { + "frequency_encoding": { + "field": "col3", + "feature_name": "col3_encoded", + "frequency_map": { + "none": 0.75, + "true": 0.10, + "false": 0.15 + } + } + } + ], + "trained_model": { + "ensemble": { + "feature_names": [ + "col1_male", + "col1_female", + "col2_encoded", + "col3_encoded", + "col4" + ], + "aggregate_output": { + "weighted_sum": { + "weights": [ + 0.5, + 0.5 + ] + } + }, + "target_type": "regression", + "trained_models": [ + { + "tree": { + "feature_names": [ + "col1_male", + "col1_female", + "col4" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "split_gain": 12.0, + "threshold": 10.0, + "decision_type": "lte", + "default_left": true, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "leaf_value": 1 + }, + { + "node_index": 2, + "leaf_value": 2 + } + ], + "target_type": "regression" + } + }, + { + "tree": { + "feature_names": [ + "col2_encoded", + "col3_encoded", + "col4" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "split_gain": 12.0, + "threshold": 10.0, + "decision_type": "lte", + "default_left": true, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "leaf_value": 1 + }, + { + "node_index": 2, + "leaf_value": 2 + } + ], + "target_type": "regression" + } + } + ] + } + } + }"""; - public static final String TREE_MODEL = "" - + "{\n" - + " \"preprocessors\": [\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"col1\",\n" - + " \"hot_map\": {\n" - + " \"male\": \"col1_male\",\n" - + " \"female\": \"col1_female\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"target_mean_encoding\": {\n" - + " \"field\": \"col2\",\n" - + " \"feature_name\": \"col2_encoded\",\n" - + " \"target_map\": {\n" - + " \"S\": 5.0,\n" - + " \"M\": 10.0,\n" - + " \"L\": 20\n" - + " },\n" - + " \"default_value\": 5.0\n" - + " }\n" - + " },\n" - + " {\n" - + " \"frequency_encoding\": {\n" - + " \"field\": \"col3\",\n" - + " \"feature_name\": \"col3_encoded\",\n" - + " \"frequency_map\": {\n" - + " \"none\": 0.75,\n" - + " \"true\": 0.10,\n" - + " \"false\": 0.15\n" - + " }\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"trained_model\": {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"col1_male\",\n" - + " \"col1_female\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"split_gain\": 12.0,\n" - + " \"threshold\": 10.0,\n" - + " \"decision_type\": \"lte\",\n" - + " \"default_left\": true,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"leaf_value\": 1\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"leaf_value\": 2\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " }\n" - + "}"; + public static final String TREE_MODEL = """ + { + "preprocessors": [ + { + "one_hot_encoding": { + "field": "col1", + "hot_map": { + "male": "col1_male", + "female": "col1_female" + } + } + }, + { + "target_mean_encoding": { + "field": "col2", + "feature_name": "col2_encoded", + "target_map": { + "S": 5.0, + "M": 10.0, + "L": 20 + }, + "default_value": 5.0 + } + }, + { + "frequency_encoding": { + "field": "col3", + "feature_name": "col3_encoded", + "frequency_map": { + "none": 0.75, + "true": 0.10, + "false": 0.15 + } + } + } + ], + "trained_model": { + "tree": { + "feature_names": [ + "col1_male", + "col1_female", + "col4" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "split_gain": 12.0, + "threshold": 10.0, + "decision_type": "lte", + "default_left": true, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "leaf_value": 1 + }, + { + "node_index": 2, + "leaf_value": 2 + } + ], + "target_type": "regression" + } + } + }"""; public void testEnsembleSchemaDeserialization() throws IOException { XContentParser parser = XContentFactory.xContent(XContentType.JSON) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java index 8b0b2ee995173..ac0d0c5fec1fc 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/ClassificationInferenceResultsTests.java @@ -8,11 +8,13 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ClassificationConfig; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.ClassificationConfigTests; import org.elasticsearch.xpack.core.ml.inference.trainedmodel.PredictionFieldType; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -135,7 +137,7 @@ protected Writeable.Reader instanceReader() { return ClassificationInferenceResults::new; } - public void testToXContent() { + public void testToXContent() throws IOException { ClassificationConfig toStringConfig = new ClassificationConfig(1, null, null, null, PredictionFieldType.STRING); ClassificationInferenceResults result = new ClassificationInferenceResults( 1.0, @@ -147,25 +149,29 @@ public void testToXContent() { 1.0 ); String stringRep = Strings.toString(result); - String expected = "{\"predicted_value\":\"1.0\",\"prediction_probability\":1.0,\"prediction_score\":1.0}"; + String expected = """ + {"predicted_value":"1.0","prediction_probability":1.0,"prediction_score":1.0}"""; assertEquals(expected, stringRep); ClassificationConfig toDoubleConfig = new ClassificationConfig(1, null, null, null, PredictionFieldType.NUMBER); result = new ClassificationInferenceResults(1.0, null, null, Collections.emptyList(), toDoubleConfig, 1.0, 1.0); stringRep = Strings.toString(result); - expected = "{\"predicted_value\":1.0,\"prediction_probability\":1.0,\"prediction_score\":1.0}"; + expected = """ + {"predicted_value":1.0,"prediction_probability":1.0,"prediction_score":1.0}"""; assertEquals(expected, stringRep); ClassificationConfig boolFieldConfig = new ClassificationConfig(1, null, null, null, PredictionFieldType.BOOLEAN); result = new ClassificationInferenceResults(1.0, null, null, Collections.emptyList(), boolFieldConfig, 1.0, 1.0); stringRep = Strings.toString(result); - expected = "{\"predicted_value\":true,\"prediction_probability\":1.0,\"prediction_score\":1.0}"; + expected = """ + {"predicted_value":true,"prediction_probability":1.0,"prediction_score":1.0}"""; assertEquals(expected, stringRep); ClassificationConfig config = new ClassificationConfig(1); result = new ClassificationInferenceResults(1.0, "label1", null, Collections.emptyList(), config, 1.0, 1.0); stringRep = Strings.toString(result); - expected = "{\"predicted_value\":\"label1\",\"prediction_probability\":1.0,\"prediction_score\":1.0}"; + expected = """ + {"predicted_value":"label1","prediction_probability":1.0,"prediction_score":1.0}"""; assertEquals(expected, stringRep); ClassificationFeatureImportance fi = new ClassificationFeatureImportance("foo", Collections.emptyList()); @@ -180,15 +186,20 @@ public void testToXContent() { 1.0 ); stringRep = Strings.toString(result); - expected = "{\"predicted_value\":\"label1\"," - + "\"top_classes\":[{\"class_name\":\"class\",\"class_probability\":1.0,\"class_score\":1.0}]," - + "\"prediction_probability\":1.0,\"prediction_score\":1.0}"; - assertEquals(expected, stringRep); + expected = """ + { + "predicted_value": "label1", + "top_classes": [ { "class_name": "class", "class_probability": 1.0, "class_score": 1.0 } ], + "prediction_probability": 1.0, + "prediction_score": 1.0 + }"""; + assertEquals(XContentHelper.stripWhitespace(expected), stringRep); config = new ClassificationConfig(0); result = new ClassificationInferenceResults(1.0, "label1", Collections.emptyList(), Collections.emptyList(), config, 1.0, 1.0); stringRep = Strings.toString(result); - expected = "{\"predicted_value\":\"label1\",\"prediction_probability\":1.0,\"prediction_score\":1.0}"; + expected = """ + {"predicted_value":"label1","prediction_probability":1.0,"prediction_score":1.0}"""; assertEquals(expected, stringRep); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResultsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResultsTests.java index bfff97a1523cb..f46e84b55a425 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResultsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/results/RegressionInferenceResultsTests.java @@ -83,7 +83,9 @@ public void testToXContent() { RegressionFeatureImportance fi = new RegressionFeatureImportance("foo", 1.0); result = new RegressionInferenceResults(1.0, resultsField, Collections.singletonList(fi)); stringRep = Strings.toString(result); - expected = "{\"" + resultsField + "\":1.0,\"feature_importance\":[{\"feature_name\":\"foo\",\"importance\":1.0}]}"; + expected = """ + {"%s":1.0,"feature_importance":[{"feature_name":"foo","importance":1.0}]}\ + """.formatted(resultsField); assertEquals(expected, stringRep); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java index aedd95b95bb8e..014572cf9651c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/inference/trainedmodel/inference/InferenceDefinitionTests.java @@ -199,134 +199,136 @@ public void testComplexInferenceDefinitionInferWithCustomPreProcessor() throws I } public static String getClassificationDefinition(boolean customPreprocessor) { - return "{" - + " \"preprocessors\": [\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"col1\",\n" - + " \"custom\": " - + customPreprocessor - + ",\n" - + " \"hot_map\": {\n" - + " \"male\": \"col1_male\",\n" - + " \"female\": \"col1_female\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"target_mean_encoding\": {\n" - + " \"field\": \"col2\",\n" - + " \"feature_name\": \"col2_encoded\",\n" - + " \"target_map\": {\n" - + " \"S\": 5.0,\n" - + " \"M\": 10.0,\n" - + " \"L\": 20\n" - + " },\n" - + " \"default_value\": 5.0\n" - + " }\n" - + " },\n" - + " {\n" - + " \"frequency_encoding\": {\n" - + " \"field\": \"col3\",\n" - + " \"feature_name\": \"col3_encoded\",\n" - + " \"frequency_map\": {\n" - + " \"none\": 0.75,\n" - + " \"true\": 0.10,\n" - + " \"false\": 0.15\n" - + " }\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"trained_model\": {\n" - + " \"ensemble\": {\n" - + " \"feature_names\": [\n" - + " \"col1_male\",\n" - + " \"col1_female\",\n" - + " \"col2_encoded\",\n" - + " \"col3_encoded\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"aggregate_output\": {\n" - + " \"weighted_mode\": {\n" - + " \"num_classes\": \"2\",\n" - + " \"weights\": [\n" - + " 0.5,\n" - + " 0.5\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"target_type\": \"classification\",\n" - + " \"classification_labels\": [\"first\", \"second\"],\n" - + " \"trained_models\": [\n" - + " {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"col1_male\",\n" - + " \"col1_female\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"number_samples\": 100,\n" - + " \"split_gain\": 12.0,\n" - + " \"threshold\": 10.0,\n" - + " \"decision_type\": \"lte\",\n" - + " \"default_left\": true,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"number_samples\": 80,\n" - + " \"leaf_value\": 1\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"number_samples\": 20,\n" - + " \"leaf_value\": 0\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"col2_encoded\",\n" - + " \"col3_encoded\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"split_gain\": 12.0,\n" - + " \"number_samples\": 180,\n" - + " \"threshold\": 10.0,\n" - + " \"decision_type\": \"lte\",\n" - + " \"default_left\": true,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"number_samples\": 10,\n" - + " \"leaf_value\": 1\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"number_samples\": 170,\n" - + " \"leaf_value\": 0\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; + return """ + { + "preprocessors": [ + { + "one_hot_encoding": { + "field": "col1", + "custom": %s, + "hot_map": { + "male": "col1_male", + "female": "col1_female" + } + } + }, + { + "target_mean_encoding": { + "field": "col2", + "feature_name": "col2_encoded", + "target_map": { + "S": 5.0, + "M": 10.0, + "L": 20 + }, + "default_value": 5.0 + } + }, + { + "frequency_encoding": { + "field": "col3", + "feature_name": "col3_encoded", + "frequency_map": { + "none": 0.75, + "true": 0.10, + "false": 0.15 + } + } + } + ], + "trained_model": { + "ensemble": { + "feature_names": [ + "col1_male", + "col1_female", + "col2_encoded", + "col3_encoded", + "col4" + ], + "aggregate_output": { + "weighted_mode": { + "num_classes": "2", + "weights": [ + 0.5, + 0.5 + ] + } + }, + "target_type": "classification", + "classification_labels": [ + "first", + "second" + ], + "trained_models": [ + { + "tree": { + "feature_names": [ + "col1_male", + "col1_female", + "col4" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "number_samples": 100, + "split_gain": 12.0, + "threshold": 10.0, + "decision_type": "lte", + "default_left": true, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "number_samples": 80, + "leaf_value": 1 + }, + { + "node_index": 2, + "number_samples": 20, + "leaf_value": 0 + } + ], + "target_type": "regression" + } + }, + { + "tree": { + "feature_names": [ + "col2_encoded", + "col3_encoded", + "col4" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "split_gain": 12.0, + "number_samples": 180, + "threshold": 10.0, + "decision_type": "lte", + "default_left": true, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "number_samples": 10, + "leaf_value": 1 + }, + { + "node_index": 2, + "number_samples": 170, + "leaf_value": 0 + } + ], + "target_type": "regression" + } + } + ] + } + } + }""".formatted(customPreprocessor); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java index 39e9e467b59ce..e05f3e6e2a229 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/JobTests.java @@ -56,20 +56,21 @@ public class JobTests extends AbstractSerializingTestCase { - private static final String FUTURE_JOB = "{\n" - + " \"job_id\": \"farequote\",\n" - + " \"create_time\": 1234567890000,\n" - + " \"tomorrows_technology_today\": \"wow\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"something_new\": \"gasp\",\n" - + " \"detectors\": [{\"function\": \"metric\", \"field_name\": \"responsetime\", \"by_field_name\": \"airline\"}]\n" - + " },\n" - + " \"data_description\": {\n" - + " \"time_field\": \"time\",\n" - + " \"the_future\": 123\n" - + " }\n" - + "}"; + private static final String FUTURE_JOB = """ + { + "job_id": "farequote", + "create_time": 1234567890000, + "tomorrows_technology_today": "wow", + "analysis_config": { + "bucket_span": "1h", + "something_new": "gasp", + "detectors": [{"function": "metric", "field_name": "responsetime", "by_field_name": "airline"}] + }, + "data_description": { + "time_field": "time", + "the_future": 123 + } + }"""; @Override protected Job createTestInstance() { @@ -651,18 +652,19 @@ public void testDeletingAndBlockReasonAreSynced() { } public void testParseJobWithDeletingButWithoutBlockReason() throws IOException { - String jobWithDeleting = "{\n" - + " \"job_id\": \"deleting_job\",\n" - + " \"create_time\": 1234567890000,\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"detectors\": [{\"function\": \"count\"}]\n" - + " },\n" - + " \"data_description\": {\n" - + " \"time_field\": \"time\"\n" - + " },\n" - + " \"deleting\": true\n" - + "}"; + String jobWithDeleting = """ + { + "job_id": "deleting_job", + "create_time": 1234567890000, + "analysis_config": { + "bucket_span": "1h", + "detectors": [{"function": "count"}] + }, + "data_description": { + "time_field": "time" + }, + "deleting": true + }"""; try ( XContentParser parser = JsonXContent.jsonXContent.createParser( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java index 207c6436e4e1b..be93346eefde7 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/persistence/ElasticsearchMappingsTests.java @@ -193,7 +193,8 @@ public void testAddDocMappingIfMissing() { ClusterState clusterState = getClusterStateWithMappingsWithMetadata(Collections.singletonMap("index-name", "0.0")); ElasticsearchMappings.addDocMappingIfMissing( "index-name", - () -> "{\"_doc\":{\"properties\":{\"some-field\":{\"type\":\"long\"}}}}", + () -> """ + {"_doc":{"properties":{"some-field":{"type":"long"}}}}""", client, clusterState, MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT, @@ -207,7 +208,8 @@ public void testAddDocMappingIfMissing() { PutMappingRequest request = requestCaptor.getValue(); assertThat(request.indices(), equalTo(new String[] { "index-name" })); - assertThat(request.source(), equalTo("{\"_doc\":{\"properties\":{\"some-field\":{\"type\":\"long\"}}}}")); + assertThat(request.source(), equalTo(""" + {"_doc":{"properties":{"some-field":{"type":"long"}}}}""")); } private ClusterState getClusterStateWithMappingsWithMetadata(Map namesAndVersions) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java index 9ce49efa68668..e4f41820a0da0 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/process/autodetect/state/QuantilesTests.java @@ -109,7 +109,8 @@ protected Quantiles doParseInstance(XContentParser parser) { } public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123456789, \"quantile_state\":\"...\", \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp": 123456789, "quantile_state":"...", "foo":"bar"}"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Quantiles.STRICT_PARSER.apply(parser, null)); @@ -118,7 +119,8 @@ public void testStrictParser() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123456789, \"quantile_state\":\"...\", \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp": 123456789, "quantile_state":"...", "foo":"bar"}"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { Quantiles.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java index 26206c29aa0e3..44d8898c2cd34 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/AnomalyRecordTests.java @@ -199,15 +199,22 @@ public void testId() { } public void testStrictParser_IsLenientOnTopLevelFields() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp": 123544456, "bucket_span": 3600, "foo":"bar"}"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { AnomalyRecord.STRICT_PARSER.apply(parser, null); } } public void testStrictParser_IsStrictOnNestedFields() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"," - + " \"causes\":[{\"cause_foo\":\"bar\"}]}"; + String json = """ + { + "job_id": "job_1", + "timestamp": 123544456, + "bucket_span": 3600, + "foo": "bar", + "causes": [ { "cause_foo": "bar" } ] + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { XContentParseException e = expectThrows(XContentParseException.class, () -> AnomalyRecord.STRICT_PARSER.apply(parser, null)); assertThat(e.getCause().getMessage(), containsString("[anomaly_cause] unknown field [cause_foo]")); @@ -215,8 +222,14 @@ public void testStrictParser_IsStrictOnNestedFields() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"," - + " \"causes\":[{\"cause_foo\":\"bar\"}]}"; + String json = """ + { + "job_id": "job_1", + "timestamp": 123544456, + "bucket_span": 3600, + "foo": "bar", + "causes": [ { "cause_foo": "bar" } ] + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { AnomalyRecord.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java index 738fbb76f469e..53c3a3bf72305 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/BucketInfluencerTests.java @@ -148,7 +148,8 @@ public void testId() { } public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp": 123544456, "bucket_span": 3600, "foo":"bar"}"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, @@ -160,7 +161,8 @@ public void testStrictParser() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp": 123544456, "bucket_span": 3600, "foo":"bar"}"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { BucketInfluencer.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java index 0326bb204bce0..1ab5e658d397c 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/results/InfluencerTests.java @@ -76,8 +76,15 @@ public void testId() { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600," - + "\"influencer_field_name\":\"foo_1\", \"influencer_field_value\": \"foo_2\", \"foo\":\"bar\"}"; + String json = """ + { + "job_id": "job_1", + "timestamp": 123544456, + "bucket_span": 3600, + "influencer_field_name": "foo_1", + "influencer_field_value": "foo_2", + "foo": "bar" + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { Influencer.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java index baa4f59d27d1a..33e692bd3c723 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/utils/NamedXContentObjectHelperTests.java @@ -66,8 +66,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws } public void testSerializeInOrder() throws IOException { - String expected = - "{\"my_objects\":[{\"my_named_object\":{\"my_field\":\"value1\"}},{\"my_named_object\":{\"my_field\":\"value2\"}}]}"; + String expected = """ + {"my_objects":[{"my_named_object":{"my_field":"value1"}},{"my_named_object":{"my_field":"value2"}}]}"""; try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.startObject(); List objects = Arrays.asList(new NamedTestObject("value1"), new NamedTestObject("value2")); @@ -78,7 +78,8 @@ public void testSerializeInOrder() throws IOException { } public void testSerialize() throws IOException { - String expected = "{\"my_objects\":{\"my_named_object\":{\"my_field\":\"value1\"},\"my_named_object\":{\"my_field\":\"value2\"}}}"; + String expected = """ + {"my_objects":{"my_named_object":{"my_field":"value1"},"my_named_object":{"my_field":"value2"}}}"""; try (XContentBuilder builder = XContentFactory.jsonBuilder()) { builder.startObject(); List objects = Arrays.asList(new NamedTestObject("value1"), new NamedTestObject("value2")); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java index 5aeaab4ff7875..cf60589953262 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequestBuilderTests.java @@ -27,13 +27,32 @@ public class CreateApiKeyRequestBuilderTests extends ESTestCase { public void testParserAndCreateApiRequestBuilder() throws IOException { boolean withExpiration = randomBoolean(); - final String json = "{ \"name\" : \"my-api-key\", " - + ((withExpiration) ? " \"expiration\": \"1d\", " : "") - + " \"role_descriptors\": { \"role-a\": {\"cluster\":[\"a-1\", \"a-2\"]," - + " \"index\": [{\"names\": [\"indx-a\"], \"privileges\": [\"read\"] }] }, " - + " \"role-b\": {\"cluster\":[\"b\"]," - + " \"index\": [{\"names\": [\"indx-b\"], \"privileges\": [\"read\"] }] } " - + "} }"; + final String json = """ + { + "name": "my-api-key", + %s + "role_descriptors": { + "role-a": { + "cluster": [ "a-1", "a-2" ], + "index": [ + { + "names": [ "indx-a" ], + "privileges": [ "read" ] + } + ] + }, + "role-b": { + "cluster": [ "b" ], + "index": [ + { + "names": [ "indx-b" ], + "privileges": [ "read" ] + } + ] + } + } + }""".formatted(withExpiration ? """ + "expiration": "1d",""" : ""); final BytesArray source = new BytesArray(json); final NodeClient mockClient = mock(NodeClient.class); final CreateApiKeyRequest request = new CreateApiKeyRequestBuilder(mockClient).source(source, XContentType.JSON).request(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java index 028b83342b1d3..270add8dcc193 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/GetApiKeyResponseTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -82,21 +83,42 @@ public void testToXContent() throws IOException { GetApiKeyResponse response = new GetApiKeyResponse(Arrays.asList(apiKeyInfo1, apiKeyInfo2, apiKeyInfo3)); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat( - Strings.toString(builder), - equalTo( - "{" - + "\"api_keys\":[" - + "{\"id\":\"id-1\",\"name\":\"name1\",\"creation\":100000,\"expiration\":10000000,\"invalidated\":false," - + "\"username\":\"user-a\",\"realm\":\"realm-x\",\"metadata\":{}}," - + "{\"id\":\"id-2\",\"name\":\"name2\",\"creation\":100000,\"expiration\":10000000,\"invalidated\":true," - + "\"username\":\"user-b\",\"realm\":\"realm-y\",\"metadata\":{}}," - + "{\"id\":\"id-3\",\"name\":null,\"creation\":100000,\"invalidated\":true," - + "\"username\":\"user-c\",\"realm\":\"realm-z\",\"metadata\":{\"foo\":\"bar\"}}" - + "]" - + "}" - ) - ); + assertThat(Strings.toString(builder), equalTo(XContentHelper.stripWhitespace(""" + { + "api_keys": [ + { + "id": "id-1", + "name": "name1", + "creation": 100000, + "expiration": 10000000, + "invalidated": false, + "username": "user-a", + "realm": "realm-x", + "metadata": {} + }, + { + "id": "id-2", + "name": "name2", + "creation": 100000, + "expiration": 10000000, + "invalidated": true, + "username": "user-b", + "realm": "realm-y", + "metadata": {} + }, + { + "id": "id-3", + "name": null, + "creation": 100000, + "invalidated": true, + "username": "user-c", + "realm": "realm-z", + "metadata": { + "foo": "bar" + } + } + ] + }"""))); } private ApiKey createApiKeyInfo( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java index 60c56f375c966..09d9ad92ed1c4 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/InvalidateApiKeyResponseTests.java @@ -11,6 +11,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -70,29 +71,29 @@ public void testToXContent() throws IOException { ); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat( - Strings.toString(builder), - equalTo( - "{" - + "\"invalidated_api_keys\":[\"api-key-id-1\"]," - + "\"previously_invalidated_api_keys\":[\"api-key-id-2\",\"api-key-id-3\"]," - + "\"error_count\":2," - + "\"error_details\":[" - + "{\"type\":\"exception\"," - + "\"reason\":\"error1\"," - + "\"caused_by\":{" - + "\"type\":\"illegal_argument_exception\"," - + "\"reason\":\"msg - 1\"}" - + "}," - + "{\"type\":\"exception\"," - + "\"reason\":\"error2\"," - + "\"caused_by\":" - + "{\"type\":\"illegal_argument_exception\"," - + "\"reason\":\"msg - 2\"}" - + "}" - + "]" - + "}" - ) - ); + assertThat(Strings.toString(builder), equalTo(XContentHelper.stripWhitespace(""" + { + "invalidated_api_keys": [ "api-key-id-1" ], + "previously_invalidated_api_keys": [ "api-key-id-2", "api-key-id-3" ], + "error_count": 2, + "error_details": [ + { + "type": "exception", + "reason": "error1", + "caused_by": { + "type": "illegal_argument_exception", + "reason": "msg - 1" + } + }, + { + "type": "exception", + "reason": "error2", + "caused_by": { + "type": "illegal_argument_exception", + "reason": "msg - 2" + } + } + ] + }"""))); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java index fcfac6cc297fc..ee10af5b1cb56 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/action/token/InvalidateTokenResponseTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -89,33 +90,30 @@ public void testToXContent() throws IOException { InvalidateTokenResponse response = new InvalidateTokenResponse(result); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat( - Strings.toString(builder), - equalTo( - "{" - + "\"invalidated_tokens\":" - + invalidatedTokens.size() - + "," - + "\"previously_invalidated_tokens\":" - + previouslyInvalidatedTokens.size() - + "," - + "\"error_count\":2," - + "\"error_details\":[" - + "{\"type\":\"exception\"," - + "\"reason\":\"foo\"," - + "\"caused_by\":{" - + "\"type\":\"illegal_argument_exception\"," - + "\"reason\":\"this is an error message\"}" - + "}," - + "{\"type\":\"exception\"," - + "\"reason\":\"bar\"," - + "\"caused_by\":" - + "{\"type\":\"illegal_argument_exception\"," - + "\"reason\":\"this is an error message2\"}" - + "}" - + "]" - + "}" - ) - ); + assertThat(Strings.toString(builder), equalTo(XContentHelper.stripWhitespace(""" + { + "invalidated_tokens": %s, + "previously_invalidated_tokens": %s, + "error_count": 2, + "error_details": [ + { + "type": "exception", + "reason": "foo", + "caused_by": { + "type": "illegal_argument_exception", + "reason": "this is an error message" + } + }, + { + "type": "exception", + "reason": "bar", + "caused_by": { + "type": "illegal_argument_exception", + "reason": "this is an error message2" + } + } + ] + } + """.formatted(invalidatedTokens.size(), previouslyInvalidatedTokens.size())))); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java index 2d89a3612f615..13ac1dd1bbf75 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/TemplateRoleNameTests.java @@ -57,28 +57,25 @@ public class TemplateRoleNameTests extends ESTestCase { public void testParseRoles() throws Exception { - final TemplateRoleName role1 = parse("{ \"template\": { \"source\": \"_user_{{username}}\" } }"); + final TemplateRoleName role1 = parse(""" + { "template": { "source": "_user_{{username}}" } }"""); assertThat(role1, Matchers.instanceOf(TemplateRoleName.class)); - assertThat(role1.getTemplate().utf8ToString(), equalTo("{\"source\":\"_user_{{username}}\"}")); + assertThat(role1.getTemplate().utf8ToString(), equalTo(""" + {"source":"_user_{{username}}"}""")); assertThat(role1.getFormat(), equalTo(Format.STRING)); - final TemplateRoleName role2 = parse( - "{ \"template\": \"{\\\"source\\\":\\\"{{#tojson}}groups{{/tojson}}\\\"}\", \"format\":\"json\" }" - ); + final TemplateRoleName role2 = parse(""" + { "template": "{\\"source\\":\\"{{#tojson}}groups{{/tojson}}\\"}", "format":"json" }"""); assertThat(role2, Matchers.instanceOf(TemplateRoleName.class)); - assertThat(role2.getTemplate().utf8ToString(), equalTo("{\"source\":\"{{#tojson}}groups{{/tojson}}\"}")); + assertThat(role2.getTemplate().utf8ToString(), equalTo(""" + {"source":"{{#tojson}}groups{{/tojson}}"}""")); assertThat(role2.getFormat(), equalTo(Format.JSON)); } public void testToXContent() throws Exception { - final String json = "{" - + "\"template\":\"{\\\"source\\\":\\\"" - + randomAlphaOfLengthBetween(8, 24) - + "\\\"}\"," - + "\"format\":\"" - + randomFrom(Format.values()).formatName() - + "\"" - + "}"; + final String json = """ + {"template":"{\\"source\\":\\"%s\\"}","format":"%s"}\ + """.formatted(randomAlphaOfLengthBetween(8, 24), randomFrom(Format.values()).formatName()); assertThat(Strings.toString(parse(json)), equalTo(json)); } @@ -101,16 +98,16 @@ public void testEvaluateRoles() throws Exception { model.defineField("username", "hulk"); model.defineField("groups", Arrays.asList("avengers", "defenders", "panthenon")); - final TemplateRoleName plainString = new TemplateRoleName(new BytesArray("{ \"source\":\"heroes\" }"), Format.STRING); + final TemplateRoleName plainString = new TemplateRoleName(new BytesArray(""" + { "source":"heroes" }"""), Format.STRING); assertThat(plainString.getRoleNames(scriptService, model), contains("heroes")); - final TemplateRoleName user = new TemplateRoleName(new BytesArray("{ \"source\":\"_user_{{username}}\" }"), Format.STRING); + final TemplateRoleName user = new TemplateRoleName(new BytesArray(""" + { "source":"_user_{{username}}" }"""), Format.STRING); assertThat(user.getRoleNames(scriptService, model), contains("_user_hulk")); - final TemplateRoleName groups = new TemplateRoleName( - new BytesArray("{ \"source\":\"{{#tojson}}groups{{/tojson}}\" }"), - Format.JSON - ); + final TemplateRoleName groups = new TemplateRoleName(new BytesArray(""" + { "source":"{{#tojson}}groups{{/tojson}}" }"""), Format.JSON); assertThat(groups.getRoleNames(scriptService, model), contains("avengers", "defenders", "panthenon")); } @@ -155,22 +152,23 @@ public void testValidate() { () -> 1L ); - final TemplateRoleName plainString = new TemplateRoleName(new BytesArray("{ \"source\":\"heroes\" }"), Format.STRING); + final TemplateRoleName plainString = new TemplateRoleName(new BytesArray(""" + { "source":"heroes" }"""), Format.STRING); plainString.validate(scriptService); - final TemplateRoleName user = new TemplateRoleName(new BytesArray("{ \"source\":\"_user_{{username}}\" }"), Format.STRING); + final TemplateRoleName user = new TemplateRoleName(new BytesArray(""" + { "source":"_user_{{username}}" }"""), Format.STRING); user.validate(scriptService); - final TemplateRoleName groups = new TemplateRoleName( - new BytesArray("{ \"source\":\"{{#tojson}}groups{{/tojson}}\" }"), - Format.JSON - ); + final TemplateRoleName groups = new TemplateRoleName(new BytesArray(""" + { "source":"{{#tojson}}groups{{/tojson}}" }"""), Format.JSON); groups.validate(scriptService); final TemplateRoleName notObject = new TemplateRoleName(new BytesArray("heroes"), Format.STRING); expectThrows(IllegalArgumentException.class, () -> notObject.validate(scriptService)); - final TemplateRoleName invalidField = new TemplateRoleName(new BytesArray("{ \"foo\":\"heroes\" }"), Format.STRING); + final TemplateRoleName invalidField = new TemplateRoleName(new BytesArray(""" + { "foo":"heroes" }"""), Format.STRING); expectThrows(IllegalArgumentException.class, () -> invalidField.validate(scriptService)); } @@ -182,25 +180,25 @@ public void testValidateWillPassWithEmptyContext() { () -> 1L ); - final BytesReference template = new BytesArray( - "{ \"source\":\"" - + "{{username}}/{{dn}}/{{realm}}/{{metadata}}" - + "{{#realm}}" - + " {{name}}/{{type}}" - + "{{/realm}}" - + "{{#toJson}}groups{{/toJson}}" - + "{{^groups}}{{.}}{{/groups}}" - + "{{#metadata}}" - + " {{#first}}" - + "

  • {{name}}
  • " - + " {{/first}}" - + " {{#link}}" - + "
  • {{name}}
  • " - + " {{/link}}" - + " {{#toJson}}subgroups{{/toJson}}" - + " {{something-else}}" - + "{{/metadata}}\" }" - ); + final BytesReference template = new BytesArray(""" + { "source":"\ + {{username}}/{{dn}}/{{realm}}/{{metadata}}\ + {{#realm}}\ + {{name}}/{{type}}\ + {{/realm}}\ + {{#toJson}}groups{{/toJson}}\ + {{^groups}}{{.}}{{/groups}}\ + {{#metadata}}\ + {{#first}}\ +
  • {{name}}
  • \ + {{/first}}\ + {{#link}}\ +
  • {{name}}
  • \ + {{/link}}\ + {{#toJson}}subgroups{{/toJson}}\ + {{something-else}}\ + {{/metadata}}" } + """); final TemplateRoleName templateRoleName = new TemplateRoleName(template, Format.STRING); templateRoleName.validate(scriptService); } @@ -213,7 +211,8 @@ public void testValidateWillFailForSyntaxError() { () -> 1L ); - final BytesReference template = new BytesArray("{ \"source\":\" {{#not-closed}} {{other-variable}} \" }"); + final BytesReference template = new BytesArray(""" + { "source":" {{#not-closed}} {{other-variable}} " }"""); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, @@ -256,15 +255,14 @@ protected StoredScriptSource getScriptFromClusterState(String id) { } }; // Validation succeeds if compilation is successful - new TemplateRoleName(new BytesArray("{ \"id\":\"valid\" }"), Format.STRING).validate(scriptService); + new TemplateRoleName(new BytesArray(""" + { "id":"valid" }"""), Format.STRING).validate(scriptService); verify(scriptEngine, times(1)).compile(eq("valid"), eq("params.metedata.group"), any(), eq(Map.of())); verify(compiledScript, never()).execute(); // Validation fails if compilation fails - final IllegalArgumentException e = expectThrows( - IllegalArgumentException.class, - () -> new TemplateRoleName(new BytesArray("{ \"id\":\"invalid\" }"), Format.STRING).validate(scriptService) - ); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new TemplateRoleName(new BytesArray(""" + { "id":"invalid" }"""), Format.STRING).validate(scriptService)); assertSame(scriptException, e.getCause()); } @@ -276,7 +274,8 @@ public void testValidationWillFailWhenInlineScriptIsNotEnabled() { ScriptModule.CORE_CONTEXTS, () -> 1L ); - final BytesReference inlineScript = new BytesArray("{ \"source\":\"\" }"); + final BytesReference inlineScript = new BytesArray(""" + { "source":"" }"""); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> new TemplateRoleName(inlineScript, Format.STRING).validate(scriptService) @@ -305,7 +304,8 @@ public void testValidateWillFailWhenStoredScriptIsNotEnabled() { when(storedScriptSource.getOptions()).thenReturn(Collections.emptyMap()); scriptService.applyClusterState(clusterChangedEvent); - final BytesReference storedScript = new BytesArray("{ \"id\":\"foo\" }"); + final BytesReference storedScript = new BytesArray(""" + { "id":"foo" }"""); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService) @@ -329,7 +329,8 @@ public void testValidateWillFailWhenStoredScriptIsNotFound() { when(metadata.custom(ScriptMetadata.TYPE)).thenReturn(scriptMetadata); scriptService.applyClusterState(clusterChangedEvent); - final BytesReference storedScript = new BytesArray("{ \"id\":\"foo\" }"); + final BytesReference storedScript = new BytesArray(""" + { "id":"foo" }"""); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> new TemplateRoleName(storedScript, Format.STRING).validate(scriptService) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java index dda3407337399..3e2f46a82bd5b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authc/support/mapper/expressiondsl/ExpressionParserTests.java @@ -47,16 +47,17 @@ public void testParseSimpleFieldExpression() throws Exception { } public void testParseComplexExpression() throws Exception { - String json = "{ \"any\": [" - + " { \"field\": { \"username\" : \"*@shield.gov\" } }, " - + " { \"all\": [" - + " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " - + " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " - + " { \"except\":" - + " { \"field\": { \"groups\" : \"disavowed\" } }" - + " }" - + " ] }" - + "] }"; + String json = """ + { "any": [ + { "field": { "username" : "*@shield.gov" } }, + { "all": [ + { "field": { "username" : "/.*\\\\@avengers\\\\.(net|org)/" } }, + { "field": { "groups" : [ "admin", "operators" ] } }, + { "except": + { "field": { "groups" : "disavowed" } } + } + ] } + ] }"""; final RoleMapperExpression expr = parse(json); assertThat(expr, instanceOf(AnyExpression.class)); @@ -119,16 +120,17 @@ public void testParseComplexExpression() throws Exception { } public void testWriteAndReadFromStream() throws Exception { - String json = "{ \"any\": [" - + " { \"field\": { \"username\" : \"*@shield.gov\" } }, " - + " { \"all\": [" - + " { \"field\": { \"username\" : \"/.*\\\\@avengers\\\\.(net|org)/\" } }, " - + " { \"field\": { \"groups\" : [ \"admin\", \"operators\" ] } }, " - + " { \"except\":" - + " { \"field\": { \"groups\" : \"disavowed\" } }" - + " }" - + " ] }" - + "] }"; + String json = """ + { "any": [ + { "field": { "username" : "*@shield.gov" } }, + { "all": [ + { "field": { "username" : "/.*\\\\@avengers\\\\.(net|org)/" } }, + { "field": { "groups" : [ "admin", "operators" ] } }, + { "except": + { "field": { "groups" : "disavowed" } } + } + ] } + ] }"""; final RoleMapperExpression exprSource = parse(json); final BytesStreamOutput out = new BytesStreamOutput(); ExpressionParser.writeExpression(exprSource, out); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java index dfeb31e1b9580..531f8d849816d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java @@ -1250,57 +1250,59 @@ public void testProducesStoredFieldsReader() throws Exception { IOUtils.close(ir, iw, dir); } - private static final String DOC_TEST_ITEM = "{\n" - + " \"field_text\" : \"text\",\n" - + " \"object\" : {\n" - + " \"inner1\" : \"text\",\n" - + " \"inner2\" : \"keyword\"\n" - + " },\n" - + " \"nested\" : [\n" - + " {\n" - + " \"inner1\" : 1,\n" - + " \"inner2\" : \"2017/12/12\"\n" - + " },\n" - + " {\n" - + " \"inner1\" : 2,\n" - + " \"inner2\" : \"2017/11/11\"\n" - + " }\n" - + " ]\n" - + "}"; - - private static final String MAPPING_TEST_ITEM = "{\n" - + " \"_doc\": {\n" - + " \"properties\" : {\n" - + " \"field_text\" : {\n" - + " \"type\":\"text\"\n" - + " },\n" - + " \"object\" : {\n" - + " \"properties\" : {\n" - + " \"inner1\" : {\n" - + " \"type\": \"text\",\n" - + " \"fields\" : {\n" - + " \"keyword\" : {\n" - + " \"type\" : \"keyword\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"inner2\" : {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"nested\" : {\n" - + " \"type\":\"nested\",\n" - + " \"properties\" : {\n" - + " \"inner1\" : {\n" - + " \"type\": \"integer\"\n" - + " },\n" - + " \"inner2\" : {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + private static final String DOC_TEST_ITEM = """ + { + "field_text" : "text", + "object" : { + "inner1" : "text", + "inner2" : "keyword" + }, + "nested" : [ + { + "inner1" : 1, + "inner2" : "2017/12/12" + }, + { + "inner1" : 2, + "inner2" : "2017/11/11" + } + ] + }"""; + + private static final String MAPPING_TEST_ITEM = """ + { + "_doc": { + "properties" : { + "field_text" : { + "type":"text" + }, + "object" : { + "properties" : { + "inner1" : { + "type": "text", + "fields" : { + "keyword" : { + "type" : "keyword" + } + } + }, + "inner2" : { + "type": "keyword" + } + } + }, + "nested" : { + "type":"nested", + "properties" : { + "inner1" : { + "type": "integer" + }, + "inner2" : { + "type": "date" + } + } + } + } + } + }"""; } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java index 3e204988033c0..3cc53d170139b 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/privilege/ApplicationPrivilegeDescriptorTests.java @@ -118,7 +118,8 @@ public void testParseXContentWithDefaultNames() throws IOException { } public void testParseXContentWithoutUsingDefaultNames() throws IOException { - final String json = "{" + " \"application\": \"your_app\"," + " \"name\": \"write\"," + " \"actions\": [ \"data:write\" ]" + "}"; + final String json = """ + { "application": "your_app", "name": "write", "actions": [ "data:write" ]}"""; final XContent xContent = XContentType.JSON.xContent(); try (XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, THROW_UNSUPPORTED_OPERATION, json)) { final ApplicationPrivilegeDescriptor privilege = ApplicationPrivilegeDescriptor.parse(parser, "my_app", "read", false); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java index 4824195936548..fed11c75715b5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/support/SecurityQueryTemplateEvaluatorTests.java @@ -108,10 +108,20 @@ public void testDocLevelSecurityTemplateWithOpenIdConnectStyleMetadata() throws return factory; }); - String template = "{ \"template\" : { \"source\" : {\"term\":{\"field\":\"{{_user.metadata.oidc(email)}}\"}} } }"; + String template = """ + { + "template": { + "source": { + "term": { + "field": "{{_user.metadata.oidc(email)}}" + } + } + } + }"""; String evaluated = SecurityQueryTemplateEvaluator.evaluateTemplate(template, scriptService, user); - assertThat(evaluated, equalTo("{\"term\":{\"field\":\"sample@example.com\"}}")); + assertThat(evaluated, equalTo(""" + {"term":{"field":"sample@example.com"}}""")); } public void testSkipTemplating() throws Exception { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java index 9d867381559e0..7805a65af4947 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/template/TemplateUtilsTests.java @@ -9,6 +9,7 @@ import org.apache.lucene.util.Constants; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.hamcrest.Matcher; @@ -27,39 +28,32 @@ public class TemplateUtilsTests extends ESTestCase { private static final String SIMPLE_TEST_TEMPLATE = "/monitoring-%s.json"; private static final String TEST_TEMPLATE_WITH_VARIABLES = "/template_with_variables-test.json"; - public void testLoadTemplate() { + public void testLoadTemplate() throws IOException { final int version = randomIntBetween(0, 10_000); String resource = String.format(Locale.ROOT, SIMPLE_TEST_TEMPLATE, "test"); String source = TemplateUtils.loadTemplate(resource, String.valueOf(version), "monitoring.template.version"); assertThat(source, notNullValue()); assertThat(source.length(), greaterThan(0)); - assertTemplate( - source, - equalTo( - "{\n" - + " \"index_patterns\": \".monitoring-data-" - + version - + "\",\n" - + " \"mappings\": {\n" - + " \"doc\": {\n" - + " \"_meta\": {\n" - + " \"template.version\": \"" - + version - + "\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n" - ) - ); + assertTemplate(XContentHelper.stripWhitespace(source), equalTo(XContentHelper.stripWhitespace(""" + { + "index_patterns": ".monitoring-data-%s", + "mappings": { + "doc": { + "_meta": { + "template.version": "%s" + } + } + } + }""".formatted(version, version)))); } - public void testLoadTemplate_GivenTemplateWithVariables() { + public void testLoadTemplate_GivenTemplateWithVariables() throws IOException { final int version = randomIntBetween(0, 10_000); Map variables = new HashMap<>(); variables.put("test.template.field_1", "test_field_1"); - variables.put("test.template.field_2", "\"test_field_2\": {\"type\": \"long\"}"); + variables.put("test.template.field_2", """ + "test_field_2": {"type": "long"}"""); String source = TemplateUtils.loadTemplate( TEST_TEMPLATE_WITH_VARIABLES, @@ -70,29 +64,25 @@ public void testLoadTemplate_GivenTemplateWithVariables() { assertThat(source, notNullValue()); assertThat(source.length(), greaterThan(0)); - assertTemplate( - source, - equalTo( - "{\n" - + " \"index_patterns\": \".test-" - + version - + "\",\n" - + " \"mappings\": {\n" - + " \"doc\": {\n" - + " \"_meta\": {\n" - + " \"template.version\": \"" - + version - + "\"\n" - + " },\n" - + " \"properties\": {\n" - + " \"test_field_1\": {\"type\": \"keyword\"},\n" - + " \"test_field_2\": {\"type\": \"long\"}\n" - + " }\n" - + " }\n" - + " }\n" - + "}\n" - ) - ); + assertTemplate(XContentHelper.stripWhitespace(source), equalTo(XContentHelper.stripWhitespace(""" + { + "index_patterns": ".test-%s", + "mappings": { + "doc": { + "_meta": { + "template.version": "%s" + }, + "properties": { + "test_field_1": { + "type": "keyword" + }, + "test_field_2": { + "type": "long" + } + } + } + } + }""".formatted(version, version)))); } public void testLoad() throws IOException { @@ -136,23 +126,17 @@ public void testReplaceVariable() { equalTo("{\"template\": \"2-test\"}") ); assertTemplate( - TemplateUtils.replaceVariable( - "{\"template\": \"test-${monitoring.template.version}-test\"}", - "monitoring.template.version", - "3" - ), + TemplateUtils.replaceVariable(""" + {"template": "test-${monitoring.template.version}-test"}""", "monitoring.template.version", "3"), equalTo("{\"template\": \"test-3-test\"}") ); final int version = randomIntBetween(0, 100); - assertTemplate( - TemplateUtils.replaceVariable( - "{\"foo-${monitoring.template.version}\": " + "\"bar-${monitoring.template.version}\"}", - "monitoring.template.version", - String.valueOf(version) - ), - equalTo("{\"foo-" + version + "\": \"bar-" + version + "\"}") - ); + assertTemplate(TemplateUtils.replaceVariable(""" + {"foo-${monitoring.template.version}": "bar-${monitoring.template.version}"} + """, "monitoring.template.version", String.valueOf(version)), equalTo(""" + {"foo-%s": "bar-%s"} + """.formatted(version, version))); } public static void assertTemplate(String actual, Matcher matcher) { diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java index b979db884834b..8fc0105ab7cb5 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/TermsEnumResponseTests.java @@ -82,9 +82,7 @@ public void testToXContent() { TermsEnumResponse response = new TermsEnumResponse(terms, 10, 10, 0, new ArrayList<>(), true); String output = Strings.toString(response); - assertEquals( - "{\"_shards\":{\"total\":10,\"successful\":10,\"failed\":0},\"terms\":[" + "\"" + s + "\"" + "],\"complete\":true}", - output - ); + assertEquals(""" + {"_shards":{"total":10,"successful":10,"failed":0},"terms":["%s"],"complete":true}""".formatted(s), output); } } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java index 68a798d2bd1fa..0120ff250c80d 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/termsenum/action/RestTermsEnumActionTests.java @@ -108,11 +108,21 @@ public static void terminateThreadPool() { public void testRestTermEnumAction() throws Exception { // GIVEN a valid query - final String content = "{" - + "\"field\":\"a\", " - + "\"string\":\"foo\", " - + "\"search_after\":\"football\", " - + "\"index_filter\":{\"bool\":{\"must\":{\"term\":{\"user\":\"kimchy\"}}}}}"; + final String content = """ + { + "field": "a", + "string": "foo", + "search_after": "football", + "index_filter": { + "bool": { + "must": { + "term": { + "user": "kimchy" + } + } + } + } + }"""; final RestRequest request = createRestRequest(content); final FakeRestChannel channel = new FakeRestChannel(request, true, 0); @@ -128,10 +138,19 @@ public void testRestTermEnumAction() throws Exception { public void testRestTermEnumActionMissingField() throws Exception { // GIVEN an invalid query - final String content = "{" - // + "\"field\":\"a\", " - + "\"string\":\"foo\", " - + "\"index_filter\":{\"bool\":{\"must\":{\"term\":{\"user\":\"kimchy\"}}}}}"; + final String content = """ + { + "string": "foo", + "index_filter": { + "bool": { + "must": { + "term": { + "user": "kimchy" + } + } + } + } + }"""; final RestRequest request = createRestRequest(content); final FakeRestChannel channel = new FakeRestChannel(request, true, 0); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java index 81504e0d1755b..515b55b530754 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/action/PreviewTransformActionRequestTests.java @@ -66,13 +66,8 @@ protected Request createTestInstance() { } public void testParsingOverwritesIdField() throws IOException { - testParsingOverwrites( - "", - "\"dest\": {" + "\"index\": \"bar\"," + "\"pipeline\": \"baz\"" + "},", - "transform-preview", - "bar", - "baz" - ); + testParsingOverwrites("", """ + "dest": {"index": "bar","pipeline": "baz"},""", "transform-preview", "bar", "baz"); } public void testParsingOverwritesDestField() throws IOException { @@ -80,13 +75,8 @@ public void testParsingOverwritesDestField() throws IOException { } public void testParsingOverwritesIdAndDestIndexFields() throws IOException { - testParsingOverwrites( - "", - "\"dest\": {" + "\"pipeline\": \"baz\"" + "},", - "transform-preview", - "unused-transform-preview-index", - "baz" - ); + testParsingOverwrites("", """ + "dest": {"pipeline": "baz"},""", "transform-preview", "unused-transform-preview-index", "baz"); } public void testParsingOverwritesIdAndDestFields() throws IOException { @@ -100,19 +90,33 @@ private void testParsingOverwrites( String expectedDestIndex, String expectedDestPipeline ) throws IOException { - BytesArray json = new BytesArray( - "{ " - + transformIdJson - + "\"source\": {" - + " \"index\": \"foo\", " - + " \"query\": {\"match_all\": {}}}," - + destConfigJson - + "\"pivot\": {" - + "\"group_by\": {\"destination-field2\": {\"terms\": {\"field\": \"term-field\"}}}," - + "\"aggs\": {\"avg_response\": {\"avg\": {\"field\": \"responsetime\"}}}" - + "}" - + "}" - ); + BytesArray json = new BytesArray(""" + { + %s + "source": { + "index": "foo", + "query": { + "match_all": {} + } + }, + %s + "pivot": { + "group_by": { + "destination-field2": { + "terms": { + "field": "term-field" + } + } + }, + "aggs": { + "avg_response": { + "avg": { + "field": "responsetime" + } + } + } + } + }""".formatted(transformIdJson, destConfigJson)); try ( XContentParser parser = JsonXContent.jsonXContent.createParser( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigTests.java index a226d76f2c900..1629f75525965 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/TransformConfigTests.java @@ -258,20 +258,31 @@ private static Map randomHeaders() { } public void testDefaultMatchAll() throws IOException { - String pivotTransform = "{" - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String pivotTransform = """ + { + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }"""; TransformConfig transformConfig = createTransformConfigFromString(pivotTransform, "test_match_all"); assertNotNull(transformConfig.getSource().getQueryConfig()); @@ -286,15 +297,15 @@ public void testDefaultMatchAll() throws IOException { } public void testConstructor_NoFunctionProvided() throws IOException { - // tag::NO_CODE_FORMAT - String json = "{" - + " \"source\": {" - + " \"index\": \"src\"" - + " }," - + " \"dest\": {" - + " \"index\": \"dest\"" - + "} }"; - // end::NO_CODE_FORMAT + String json = """ + { + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + } + }"""; // Should parse with lenient parser createTransformConfigFromString(json, "dummy", true); // Should throw with strict parser @@ -302,24 +313,35 @@ public void testConstructor_NoFunctionProvided() throws IOException { } public void testConstructor_TwoFunctionsProvided() throws IOException { - String json = "{" - + " \"source\" : {\"index\": \"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"latest\": {" - + " \"unique_key\": [ \"event1\", \"event2\", \"event3\" ]," - + " \"sort\": \"timestamp\"" - + " }," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String json = """ + { + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "latest": { + "unique_key": [ "event1", "event2", "event3" ], + "sort": "timestamp" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }"""; // Should parse with lenient parser createTransformConfigFromString(json, "dummy", true); @@ -328,63 +350,96 @@ public void testConstructor_TwoFunctionsProvided() throws IOException { } public void testPreventHeaderInjection() { - String pivotTransform = "{" - + " \"headers\" : {\"key\" : \"value\" }," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String pivotTransform = """ + { + "headers": { + "key": "value" + }, + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }"""; expectThrows(IllegalArgumentException.class, () -> createTransformConfigFromString(pivotTransform, "test_header_injection")); } public void testPreventCreateTimeInjection() { - String pivotTransform = "{" - + " \"create_time\" : " - + Instant.now().toEpochMilli() - + " }," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String pivotTransform = """ + { + "create_time": %s, + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }""".formatted(Instant.now().toEpochMilli()); expectThrows(IllegalArgumentException.class, () -> createTransformConfigFromString(pivotTransform, "test_createTime_injection")); } public void testPreventVersionInjection() { - String pivotTransform = "{" - + " \"version\" : \"7.3.0\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String pivotTransform = """ + { + "version": "7.3.0", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }"""; expectThrows(IllegalArgumentException.class, () -> createTransformConfigFromString(pivotTransform, "test_createTime_injection")); } @@ -449,21 +504,32 @@ public void testMaxLengthDescription() { } public void testSetIdInBody() throws IOException { - String pivotTransform = "{" - + " \"id\" : \"body_id\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String pivotTransform = """ + { + "id": "body_id", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }"""; TransformConfig transformConfig = createTransformConfigFromString(pivotTransform, "body_id"); assertEquals("body_id", transformConfig.getId()); @@ -480,27 +546,34 @@ public void testSetIdInBody() throws IOException { } public void testRewriteForUpdate() throws IOException { - String pivotTransform = "{" - + " \"id\" : \"body_id\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } }," - + " \"max_page_search_size\" : 111" - + "}," - + " \"version\" : \"" - + Version.V_7_6_0.toString() - + "\"" - + "}"; + String pivotTransform = """ + { + "id": "body_id", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + }, + "max_page_search_size": 111 + }, + "version": "%s" + }""".formatted(Version.V_7_6_0.toString()); TransformConfig transformConfig = createTransformConfigFromString(pivotTransform, "body_id", true); TransformConfig transformConfigRewritten = TransformConfig.rewriteForUpdate(transformConfig); @@ -516,26 +589,33 @@ public void testRewriteForUpdate() throws IOException { } public void testRewriteForUpdateAlignCheckpoints() throws IOException { - String pivotTransform = "{" - + " \"id\" : \"body_id\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } }" - + "}," - + " \"version\" : \"" - + Version.V_7_12_0.toString() - + "\"" - + "}"; + String pivotTransform = """ + { + "id": "body_id", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + }, + "version": "%s" + }""".formatted(Version.V_7_12_0.toString()); TransformConfig transformConfig = createTransformConfigFromString(pivotTransform, "body_id", true); TransformConfig transformConfigRewritten = TransformConfig.rewriteForUpdate(transformConfig); @@ -553,29 +633,37 @@ public void testRewriteForUpdateAlignCheckpoints() throws IOException { } public void testRewriteForUpdateMaxPageSizeSearchConflicting() throws IOException { - String pivotTransform = "{" - + " \"id\" : \"body_id\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } }," - + " \"max_page_search_size\": 111" - + "}," - + " \"settings\" : { \"max_page_search_size\": 555" - + "}," - + " \"version\" : \"" - + Version.V_7_5_0.toString() - + "\"" - + "}"; + String pivotTransform = """ + { + "id": "body_id", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + }, + "max_page_search_size": 111 + }, + "settings": { + "max_page_search_size": 555 + }, + "version": "%s" + }""".formatted(Version.V_7_5_0.toString()); TransformConfig transformConfig = createTransformConfigFromString(pivotTransform, "body_id", true); TransformConfig transformConfigRewritten = TransformConfig.rewriteForUpdate(transformConfig); @@ -588,26 +676,33 @@ public void testRewriteForUpdateMaxPageSizeSearchConflicting() throws IOExceptio } public void testRewriteForBWCOfDateNormalization() throws IOException { - String pivotTransform = "{" - + " \"id\" : \"body_id\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } }" - + "}," - + " \"version\" : \"" - + Version.V_7_6_0.toString() - + "\"" - + "}"; + String pivotTransform = """ + { + "id": "body_id", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + }, + "version": "%s" + }""".formatted(Version.V_7_6_0.toString()); TransformConfig transformConfig = createTransformConfigFromString(pivotTransform, "body_id", true); TransformConfig transformConfigRewritten = TransformConfig.rewriteForUpdate(transformConfig); @@ -635,45 +730,67 @@ public void testRewriteForBWCOfDateNormalization() throws IOException { } public void testGetAdditionalSourceDestValidations_WithNoRuntimeMappings() throws IOException { - String transformWithRuntimeMappings = "{" - + " \"id\" : \"body_id\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String transformWithRuntimeMappings = """ + { + "id": "body_id", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }"""; TransformConfig transformConfig = createTransformConfigFromString(transformWithRuntimeMappings, "body_id", true); assertThat(transformConfig.getAdditionalSourceDestValidations(), is(empty())); } public void testGetAdditionalSourceDestValidations_WithRuntimeMappings() throws IOException { - String json = "{" - + " \"id\" : \"body_id\"," - + " \"source\" : {" - + " \"index\":\"src\"," - + " \"runtime_mappings\":{ \"some-field\": \"some-value\" }" - + "}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String json = """ + { + "id": "body_id", + "source": { + "index": "src", + "runtime_mappings": { + "some-field": "some-value" + } + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }"""; TransformConfig transformConfig = createTransformConfigFromString(json, "body_id", true); List additiionalValidations = transformConfig.getAdditionalSourceDestValidations(); @@ -686,34 +803,43 @@ public void testGetAdditionalSourceDestValidations_WithRuntimeMappings() throws } public void testGroupByStayInOrder() throws IOException { - String json = "{" - + " \"id\" : \"" - + transformId - + "\"," - + " \"source\" : {" - + " \"index\":\"src\"" - + "}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"time\": {" - + " \"date_histogram\": {" - + " \"field\": \"timestamp\"," - + " \"fixed_interval\": \"1d\"" - + "} }," - + " \"alert\": {" - + " \"terms\": {" - + " \"field\": \"alert\"" - + "} }," - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String json = """ + { + "id": "%s", + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "time": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "1d" + } + }, + "alert": { + "terms": { + "field": "alert" + } + }, + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }""".formatted(transformId); TransformConfig transformConfig = createTransformConfigFromString(json, transformId, true); List originalGroups = new ArrayList<>(transformConfig.getPivotConfig().getGroupConfig().getGroups().keySet()); assertThat(originalGroups, contains("time", "alert", "id")); @@ -813,31 +939,40 @@ public void testCheckForDeprecations() { } public void testSerializingMetadataPreservesOrder() throws IOException { - String json = "{" - + " \"id\" : \"" - + transformId - + "\"," - + " \"_meta\": {" - + " \"d\": 4," - + " \"a\": 1," - + " \"c\": 3," - + " \"e\": 5," - + " \"b\": 2" - + "}," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + " \"pivot\" : {" - + " \"group_by\": {" - + " \"time\": {" - + " \"date_histogram\": {" - + " \"field\": \"timestamp\"," - + " \"fixed_interval\": \"1d\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } } }"; + String json = """ + { + "id": "%s", + "_meta": { + "d": 4, + "a": 1, + "c": 3, + "e": 5, + "b": 2 + }, + "source": { + "index": "src" + }, + "dest": { + "index": "dest" + }, + "pivot": { + "group_by": { + "time": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "1d" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + } + }""".formatted(transformId); // Read TransformConfig from JSON and verify that metadata keys are in the same order as in JSON TransformConfig transformConfig = createTransformConfigFromString(json, transformId, true); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java index c320120cf8ee6..d499846540f8e 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/latest/LatestConfigTests.java @@ -49,12 +49,8 @@ protected Reader instanceReader() { } public void testValidate_ValidConfig() throws IOException { - // tag::NO_CODE_FORMAT - String json = "{" - + " \"unique_key\": [ \"event1\", \"event2\", \"event3\" ]," - + " \"sort\": \"timestamp\"" - + "}"; - // end::NO_CODE_FORMAT + String json = """ + { "unique_key": [ "event1", "event2", "event3" ], "sort": "timestamp"}"""; LatestConfig config = createLatestConfigFromString(json); assertThat(config.validate(null), is(nullValue())); @@ -64,24 +60,16 @@ public void testValidate_ValidConfig() throws IOException { } public void testValidate_EmptyUniqueKey() throws IOException { - // tag::NO_CODE_FORMAT - String json = "{" - + " \"unique_key\": []," - + " \"sort\": \"timestamp\"" - + "}"; - // end::NO_CODE_FORMAT + String json = """ + { "unique_key": [], "sort": "timestamp"}"""; LatestConfig config = createLatestConfigFromString(json); assertThat(config.validate(null).validationErrors(), contains("latest.unique_key must be non-empty")); } public void testValidate_EmptyUniqueKeyElement() throws IOException { - // tag::NO_CODE_FORMAT - String json = "{" - + " \"unique_key\": [ \"event1\", \"\", \"event2\", \"\", \"event3\" ]," - + " \"sort\": \"timestamp\"" - + "}"; - // end::NO_CODE_FORMAT + String json = """ + { "unique_key": [ "event1", "", "event2", "", "event3" ], "sort": "timestamp"}"""; LatestConfig config = createLatestConfigFromString(json); assertThat( @@ -91,12 +79,8 @@ public void testValidate_EmptyUniqueKeyElement() throws IOException { } public void testValidate_DuplicateUniqueKeyElement() throws IOException { - // tag::NO_CODE_FORMAT - String json = "{" - + " \"unique_key\": [ \"event1\", \"event2\", \"event1\" ]," - + " \"sort\": \"timestamp\"" - + "}"; - // end::NO_CODE_FORMAT + String json = """ + { "unique_key": [ "event1", "event2", "event1" ], "sort": "timestamp"}"""; LatestConfig config = createLatestConfigFromString(json); assertThat( @@ -106,24 +90,16 @@ public void testValidate_DuplicateUniqueKeyElement() throws IOException { } public void testValidate_EmptySort() throws IOException { - // tag::NO_CODE_FORMAT - String json = "{" - + " \"unique_key\": [ \"event1\", \"event2\", \"event3\" ]," - + " \"sort\": \"\"" - + "}"; - // end::NO_CODE_FORMAT + String json = """ + { "unique_key": [ "event1", "event2", "event3" ], "sort": ""}"""; LatestConfig config = createLatestConfigFromString(json); assertThat(config.validate(null).validationErrors(), contains("latest.sort must be non-empty")); } public void testValidate_EmptyUniqueKeyAndSort() throws IOException { - // tag::NO_CODE_FORMAT - String json = "{" - + " \"unique_key\": []," - + " \"sort\": \"\"" - + "}"; - // end::NO_CODE_FORMAT + String json = """ + { "unique_key": [], "sort": ""}"""; LatestConfig config = createLatestConfigFromString(json); assertThat( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java index 73d42e2618cdf..f6b2858bbb524 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/AggregationConfigTests.java @@ -111,12 +111,13 @@ public void testEmptyAggregation() throws IOException { } public void testFailOnStrictPassOnLenient() throws IOException { - String source = "{\n" - + " \"avg_rating\": { \"some_removed_agg\": { \"field\": \"rating\" } }\n" - + " },\n" - + " {\n" - + " \"max_rating\": { \"max_rating\" : { \"field\" : \"rating\" } }\n" - + " }"; + String source = """ + { + "avg_rating": { "some_removed_agg": { "field": "rating" } } + }, + { + "max_rating": { "max_rating" : { "field" : "rating" } } + }"""; // lenient, passes but reports invalid try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { @@ -133,7 +134,9 @@ public void testFailOnStrictPassOnLenient() throws IOException { } public void testDeprecation() throws IOException { - String source = "{\"dep_agg\": {\"" + MockDeprecatedAggregationBuilder.NAME + "\" : {}}}"; + String source = """ + {"dep_agg": {"%s" : {}}} + """.formatted(MockDeprecatedAggregationBuilder.NAME); try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { AggregationConfig agg = AggregationConfig.fromXContent(parser, false); assertNull(agg.validate(null)); 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 282ec0e93ce49..c834c9a0366c3 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 @@ -77,17 +77,23 @@ protected Reader instanceReader() { } public void testAggsAbbreviations() throws IOException { - String pivotAggs = "{" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } }"; + String pivotAggs = """ + { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + }"""; PivotConfig p1 = createPivotConfigFromString(pivotAggs, false); String pivotAggregations = pivotAggs.replace("aggs", "aggregations"); @@ -97,20 +103,32 @@ 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)); } public void testEmptyAggs() throws IOException { - String pivot = "{" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + "\"aggs\": {}" - + " }"; + String pivot = """ + { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": {} + }"""; expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false)); @@ -122,13 +140,17 @@ public void testEmptyAggs() throws IOException { } public void testEmptyGroupBy() throws IOException { - String pivot = "{" - + " \"group_by\": {}," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } }"; + String pivot = """ + { + "group_by": {}, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + } + }"""; expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false)); @@ -140,235 +162,271 @@ 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)); } public void testDoubleAggs() { - String pivot = "{" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } }," - + " \"aggregations\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } }" - + "}"; + String pivot = """ + { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg": { + "avg": { + "field": "points" + } + } + }, + "aggregations": { + "avg": { + "avg": { + "field": "points" + } + } + } + }"""; expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false)); } public void testAggDuplicates() throws IOException { - String pivot = "{" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"points\": {" - + " \"max\": {" - + " \"field\": \"points\"" - + "} }," - + " \"points\": {" - + " \"min\": {" - + " \"field\": \"points\"" - + "} } }" - + "}"; + String pivot = """ + { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "points": { + "max": { + "field": "points" + } + }, + "points": { + "min": { + "field": "points" + } + } + } + }"""; // this throws early in the agg framework expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false)); } public void testValidAggNames() throws IOException { - String pivotAggs = "{" - + " \"group_by\": {" - + " \"user.id.field\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } }," - + " \"aggs\": {" - + " \"avg.field.value\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } }"; + String pivotAggs = """ + { + "group_by": { + "user.id.field": { + "terms": { + "field": "id" + } + } + }, + "aggs": { + "avg.field.value": { + "avg": { + "field": "points" + } + } + } + }"""; PivotConfig pivotConfig = createPivotConfigFromString(pivotAggs, true); assertNull(pivotConfig.validate(null)); } public void testValidAggNamesNested() throws IOException { - String pivotAggs = "{" - + "\"group_by\": {" - + " \"timestamp\": {" - + " \"date_histogram\": {" - + " \"field\": \"timestamp\"," - + " \"fixed_interval\": \"1d\"" - + " }" - + " }" - + "}," - + "\"aggregations\": {" - + " \"jp\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.src\": \"JP\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"os.dc\": {" - + " \"cardinality\": {" - + " \"field\": \"machine.os.keyword\"" - + " }" - + " }" - + " }" - + " }," - + " \"us\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.src\": \"US\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"os.dc\": {" - + " \"cardinality\": {" - + " \"field\": \"machine.os.keyword\"" - + "} } } } } }"; + String pivotAggs = """ + { + "group_by": { + "timestamp": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "1d" + } + } + }, + "aggregations": { + "jp": { + "filter": { + "term": { + "geo.src": "JP" + } + }, + "aggs": { + "os.dc": { + "cardinality": { + "field": "machine.os.keyword" + } + } + } + }, + "us": { + "filter": { + "term": { + "geo.src": "US" + } + }, + "aggs": { + "os.dc": { + "cardinality": { + "field": "machine.os.keyword" + } + } + } + } + } + }"""; PivotConfig pivotConfig = createPivotConfigFromString(pivotAggs, true); assertNull(pivotConfig.validate(null)); } public void testValidAggNamesNestedTwice() throws IOException { - String pivotAggs = "{" - + " \"group_by\": {" - + " \"timestamp\": {" - + " \"date_histogram\": {" - + " \"field\": \"timestamp\"," - + " \"fixed_interval\": \"1d\"" - + " }" - + " }" - + " }," - + " \"aggregations\": {" - + " \"jp\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.src\": \"JP\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"us\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.dest\": \"US\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"os.dc\": {" - + " \"cardinality\": {" - + " \"field\": \"machine.os.keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }," - + " \"us\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.src\": \"US\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"jp\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.dest\": \"JP\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"os.dc\": {" - + " \"cardinality\": {" - + " \"field\": \"machine.os.keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }"; + String pivotAggs = """ + { + "group_by": { + "timestamp": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "1d" + } + } + }, + "aggregations": { + "jp": { + "filter": { + "term": { + "geo.src": "JP" + } + }, + "aggs": { + "us": { + "filter": { + "term": { + "geo.dest": "US" + } + }, + "aggs": { + "os.dc": { + "cardinality": { + "field": "machine.os.keyword" + } + } + } + } + } + }, + "us": { + "filter": { + "term": { + "geo.src": "US" + } + }, + "aggs": { + "jp": { + "filter": { + "term": { + "geo.dest": "JP" + } + }, + "aggs": { + "os.dc": { + "cardinality": { + "field": "machine.os.keyword" + } + } + } + } + } + } + } + }"""; PivotConfig pivotConfig = createPivotConfigFromString(pivotAggs, true); assertNull(pivotConfig.validate(null)); } public void testInValidAggNamesNestedTwice() throws IOException { - String pivotAggs = "{" - + " \"group_by\": {" - + " \"jp.us.os.dc\": {" - + " \"date_histogram\": {" - + " \"field\": \"timestamp\"," - + " \"fixed_interval\": \"1d\"" - + " }" - + " }" - + " }," - + " \"aggregations\": {" - + " \"jp\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.src\": \"JP\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"us\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.dest\": \"US\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"os.dc\": {" - + " \"cardinality\": {" - + " \"field\": \"machine.os.keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }," - + " \"us\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.src\": \"US\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"jp\": {" - + " \"filter\": {" - + " \"term\": {" - + " \"geo.dest\": \"JP\"" - + " }" - + " }," - + " \"aggs\": {" - + " \"os.dc\": {" - + " \"cardinality\": {" - + " \"field\": \"machine.os.keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }"; + String pivotAggs = """ + { + "group_by": { + "jp.us.os.dc": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "1d" + } + } + }, + "aggregations": { + "jp": { + "filter": { + "term": { + "geo.src": "JP" + } + }, + "aggs": { + "us": { + "filter": { + "term": { + "geo.dest": "US" + } + }, + "aggs": { + "os.dc": { + "cardinality": { + "field": "machine.os.keyword" + } + } + } + } + } + }, + "us": { + "filter": { + "term": { + "geo.src": "US" + } + }, + "aggs": { + "jp": { + "filter": { + "term": { + "geo.dest": "JP" + } + }, + "aggs": { + "os.dc": { + "cardinality": { + "field": "machine.os.keyword" + } + } + } + } + } + } + } + }"""; PivotConfig pivotConfig = createPivotConfigFromString(pivotAggs, true); ValidationException validationException = pivotConfig.validate(null); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java index 2a7fb20c85467..3be52dffc52df 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/ScriptConfigTests.java @@ -90,7 +90,10 @@ protected ScriptConfig createTestInstance() { public void testFailOnStrictPassOnLenient() throws IOException { // use a wrong syntax to trigger a parsing exception for strict parsing - String source = "{\n" + " \"source-code\": \"a=b\"" + " }"; + String source = """ + { + "source-code": "a=b" + }"""; // lenient, passes but reports invalid try (XContentParser parser = createParser(JsonXContent.jsonXContent, source)) { diff --git a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java index 5e8ff688d98f5..c77b52385e67d 100644 --- a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java +++ b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamUpgradeRestIT.java @@ -38,36 +38,37 @@ public void testCompatibleMappingUpgrade() throws Exception { // Create a template Request putComposableIndexTemplateRequest = new Request("POST", "/_index_template/mysql-error"); - putComposableIndexTemplateRequest.setJsonEntity( - "{" - + "\"index_patterns\":[\"logs-mysql-*\"]," - + "\"priority\":200," - + "\"composed_of\":[\"logs-mappings\",\"logs-settings\"]," - + "\"data_stream\":{}," - + "\"template\":{" - + "\"mappings\":{" - + "\"properties\":{" - + "\"thread_id\":{\"type\":\"long\"}" - + "}" - + "}," - + "\"settings\":{" - + "\"index.default_pipeline\":\"mysql-error1\"" - + "}" - + "}" - + "}" - ); + putComposableIndexTemplateRequest.setJsonEntity(""" + { + "index_patterns": [ "logs-mysql-*" ], + "priority": 200, + "composed_of": [ "logs-mappings", "logs-settings" ], + "data_stream": {}, + "template": { + "mappings": { + "properties": { + "thread_id": { + "type": "long" + } + } + }, + "settings": { + "index.default_pipeline": "mysql-error1" + } + } + }"""); assertOK(client().performRequest(putComposableIndexTemplateRequest)); // Create a data stream and index first doc Request indexRequest = new Request("POST", "/logs-mysql-error/_doc"); - indexRequest.setJsonEntity("{\"@timestamp\": \"2020-12-12\",\"message\":\"abc\",\"thread_id\":23}"); + indexRequest.setJsonEntity(""" + {"@timestamp": "2020-12-12","message":"abc","thread_id":23}"""); assertOK(client().performRequest(indexRequest)); // Create new pipeline and update default pipeline: putPipelineRequest = new Request("PUT", "/_ingest/pipeline/mysql-error2"); - putPipelineRequest.setJsonEntity( - "{\"processors\":[{\"rename\":{\"field\":\"thread_id\",\"target_field\":\"thread.id\"," + "\"ignore_failure\":true}}]}" - ); + putPipelineRequest.setJsonEntity(""" + {"processors":[{"rename":{"field":"thread_id","target_field":"thread.id","ignore_failure":true}}]}"""); assertOK(client().performRequest(putPipelineRequest)); Request updateSettingsRequest = new Request("PUT", "/logs-mysql-error/_settings"); updateSettingsRequest.setJsonEntity("{ \"index\": { \"default_pipeline\" : \"mysql-error2\" }}"); @@ -75,34 +76,36 @@ public void testCompatibleMappingUpgrade() throws Exception { // Update template putComposableIndexTemplateRequest = new Request("POST", "/_index_template/mysql-error"); - putComposableIndexTemplateRequest.setJsonEntity( - "{" - + "\"index_patterns\":[\"logs-mysql-*\"]," - + "\"priority\":200," - + "\"composed_of\":[\"logs-mappings\",\"logs-settings\"]," - + "\"data_stream\":{}," - + "\"template\":{" - + "\"mappings\":{" - + "\"properties\":{" - + "\"thread\":{" - + "\"properties\":{" - + "\"id\":{\"type\":\"long\"}" - + "}" - + "}" - + "}" - + "}," - + "\"settings\":{" - + "\"index.default_pipeline\":\"mysql-error2\"" - + "}" - + "}" - + "}" - ); + putComposableIndexTemplateRequest.setJsonEntity(""" + { + "index_patterns": [ "logs-mysql-*" ], + "priority": 200, + "composed_of": [ "logs-mappings", "logs-settings" ], + "data_stream": {}, + "template": { + "mappings": { + "properties": { + "thread": { + "properties": { + "id": { + "type": "long" + } + } + } + } + }, + "settings": { + "index.default_pipeline": "mysql-error2" + } + } + }"""); assertOK(client().performRequest(putComposableIndexTemplateRequest)); // Update mapping Request putMappingRequest = new Request("PUT", "/logs-mysql-error/_mappings"); putMappingRequest.addParameters(Map.of("write_index_only", "true")); - putMappingRequest.setJsonEntity("{\"properties\":{\"thread\":{\"properties\":{\"id\":{\"type\":\"long\"}}}}}"); + putMappingRequest.setJsonEntity(""" + {"properties":{"thread":{"properties":{"id":{"type":"long"}}}}}"""); assertOK(client().performRequest(putMappingRequest)); // Delete old pipeline @@ -111,10 +114,12 @@ public void testCompatibleMappingUpgrade() throws Exception { // Index more docs indexRequest = new Request("POST", "/logs-mysql-error/_doc"); - indexRequest.setJsonEntity("{\"@timestamp\": \"2020-12-12\",\"message\":\"abc\",\"thread_id\":24}"); + indexRequest.setJsonEntity(""" + {"@timestamp": "2020-12-12","message":"abc","thread_id":24}"""); assertOK(client().performRequest(indexRequest)); indexRequest = new Request("POST", "/logs-mysql-error/_doc"); - indexRequest.setJsonEntity("{\"@timestamp\": \"2020-12-12\",\"message\":\"abc\",\"thread\":{\"id\":24}}"); + indexRequest.setJsonEntity(""" + {"@timestamp": "2020-12-12","message":"abc","thread":{"id":24}}"""); assertOK(client().performRequest(indexRequest)); Request refreshRequest = new Request("POST", "/logs-mysql-error/_refresh"); @@ -134,24 +139,25 @@ public void testConflictingMappingUpgrade() throws Exception { // Create a template Request putComposableIndexTemplateRequest = new Request("POST", "/_index_template/mysql-error"); - putComposableIndexTemplateRequest.setJsonEntity( - "{" - + "\"index_patterns\":[\"logs-mysql-*\"]," - + "\"priority\":200," - + "\"composed_of\":[\"logs-mappings\",\"logs-settings\"]," - + "\"data_stream\":{}," - + "\"template\":{" - + "\"mappings\":{" - + "\"properties\":{" - + "\"thread\":{\"type\":\"long\"}" - + "}" - + "}," - + "\"settings\":{" - + "\"index.default_pipeline\":\"mysql-error1\"" - + "}" - + "}" - + "}" - ); + putComposableIndexTemplateRequest.setJsonEntity(""" + { + "index_patterns": [ "logs-mysql-*" ], + "priority": 200, + "composed_of": [ "logs-mappings", "logs-settings" ], + "data_stream": {}, + "template": { + "mappings": { + "properties": { + "thread": { + "type": "long" + } + } + }, + "settings": { + "index.default_pipeline": "mysql-error1" + } + } + }"""); assertOK(client().performRequest(putComposableIndexTemplateRequest)); // Create a data stream and index first doc @@ -161,9 +167,8 @@ public void testConflictingMappingUpgrade() throws Exception { // Create new pipeline and update default pipeline: putPipelineRequest = new Request("PUT", "/_ingest/pipeline/mysql-error2"); - putPipelineRequest.setJsonEntity( - "{\"processors\":[{\"rename\":{\"field\":\"thread\",\"target_field\":\"thread.id\"," + "\"ignore_failure\":true}}]}" - ); + putPipelineRequest.setJsonEntity(""" + {"processors":[{"rename":{"field":"thread","target_field":"thread.id","ignore_failure":true}}]}"""); assertOK(client().performRequest(putPipelineRequest)); Request updateSettingsRequest = new Request("PUT", "/logs-mysql-error/_settings"); updateSettingsRequest.setJsonEntity("{ \"index\": { \"default_pipeline\" : \"mysql-error2\" }}"); @@ -171,28 +176,29 @@ public void testConflictingMappingUpgrade() throws Exception { // Update template putComposableIndexTemplateRequest = new Request("POST", "/_index_template/mysql-error"); - putComposableIndexTemplateRequest.setJsonEntity( - "{" - + "\"index_patterns\":[\"logs-mysql-*\"]," - + "\"priority\":200," - + "\"composed_of\":[\"logs-mappings\",\"logs-settings\"]," - + "\"data_stream\":{}," - + "\"template\":{" - + "\"mappings\":{" - + "\"properties\":{" - + "\"thread\":{" - + "\"properties\":{" - + "\"id\":{\"type\":\"long\"}" - + "}" - + "}" - + "}" - + "}," - + "\"settings\":{" - + "\"index.default_pipeline\":\"mysql-error2\"" - + "}" - + "}" - + "}" - ); + putComposableIndexTemplateRequest.setJsonEntity(""" + { + "index_patterns": [ "logs-mysql-*" ], + "priority": 200, + "composed_of": [ "logs-mappings", "logs-settings" ], + "data_stream": {}, + "template": { + "mappings": { + "properties": { + "thread": { + "properties": { + "id": { + "type": "long" + } + } + } + } + }, + "settings": { + "index.default_pipeline": "mysql-error2" + } + } + }"""); assertOK(client().performRequest(putComposableIndexTemplateRequest)); // Update mapping @@ -212,16 +218,19 @@ public void testConflictingMappingUpgrade() throws Exception { // Index more docs indexRequest = new Request("POST", "/logs-mysql-error/_doc"); - indexRequest.setJsonEntity("{\"@timestamp\": \"2020-12-12\",\"message\":\"abc\",\"thread\":24}"); + indexRequest.setJsonEntity(""" + {"@timestamp": "2020-12-12","message":"abc","thread":24}"""); assertOK(client().performRequest(indexRequest)); indexRequest = new Request("POST", "/logs-mysql-error/_doc"); - indexRequest.setJsonEntity("{\"@timestamp\": \"2020-12-12\",\"message\":\"abc\",\"thread\":{\"id\":24}}"); + indexRequest.setJsonEntity(""" + {"@timestamp": "2020-12-12","message":"abc","thread":{"id":24}}"""); assertOK(client().performRequest(indexRequest)); Request refreshRequest = new Request("POST", "/logs-mysql-error/_refresh"); assertOK(client().performRequest(refreshRequest)); - verifyTotalHitCount("logs-mysql-error", "{\"query\":{\"match\":{\"thread.id\": 24}}}", 2, "thread.id"); + verifyTotalHitCount("logs-mysql-error", """ + {"query":{"match":{"thread.id": 24}}}""", 2, "thread.id"); Request deleteDateStreamRequest = new Request("DELETE", "/_data_stream/logs-mysql-error"); assertOK(client().performRequest(deleteDateStreamRequest)); diff --git a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java index a6bff6f32b0e8..872f592e25ed3 100644 --- a/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java +++ b/x-pack/plugin/data-streams/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/datastreams/DataStreamsRestIT.java @@ -34,7 +34,13 @@ public void cleanUp() throws IOException { public void testHiddenDataStream() throws IOException { // Create a template Request putComposableIndexTemplateRequest = new Request("POST", "/_index_template/hidden"); - putComposableIndexTemplateRequest.setJsonEntity("{\"index_patterns\": [\"hidden\"], \"data_stream\": {\"hidden\": true}}"); + putComposableIndexTemplateRequest.setJsonEntity(""" + { + "index_patterns": [ "hidden" ], + "data_stream": { + "hidden": true + } + }"""); assertOK(client().performRequest(putComposableIndexTemplateRequest)); Request createDocRequest = new Request("POST", "/hidden/_doc?refresh=true"); @@ -102,10 +108,23 @@ public void testDataStreamAliases() throws Exception { // Add logs-myapp1 -> logs & logs-myapp2 -> logs Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"add\":{\"index\":\"logs-myapp1\",\"alias\":\"logs\"}}," - + "{\"add\":{\"index\":\"logs-myapp2\",\"alias\":\"logs\"}}]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ + { + "add": { + "index": "logs-myapp1", + "alias": "logs" + } + }, + { + "add": { + "index": "logs-myapp2", + "alias": "logs" + } + } + ] + }"""); assertOK(client().performRequest(updateAliasesRequest)); Request getAliasesRequest = new Request("GET", "/_aliases"); @@ -120,10 +139,23 @@ public void testDataStreamAliases() throws Exception { // Remove logs-myapp1 -> logs & logs-myapp2 -> logs updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"remove\":{\"index\":\"logs-myapp1\",\"alias\":\"logs\"}}," - + "{\"remove\":{\"index\":\"logs-myapp2\",\"alias\":\"logs\"}}]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ + { + "remove": { + "index": "logs-myapp1", + "alias": "logs" + } + }, + { + "remove": { + "index": "logs-myapp2", + "alias": "logs" + } + } + ] + }"""); assertOK(client().performRequest(updateAliasesRequest)); getAliasesRequest = new Request("GET", "/_aliases"); @@ -147,7 +179,10 @@ public void testDataStreamAliases() throws Exception { // Remove logs-* -> logs updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity("{\"actions\":[{\"remove\":{\"index\":\"logs-*\",\"alias\":\"logs\"}}]}"); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ { "remove": { "index": "logs-*", "alias": "logs" } } ] + }"""); assertOK(client().performRequest(updateAliasesRequest)); getAliasesRequest = new Request("GET", "/_aliases"); @@ -171,10 +206,23 @@ public void testDeleteDataStreamApiWithAliasFails() throws IOException { assertOK(client().performRequest(createDocRequest)); Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\"}}," - + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\"}}]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ + { + "add": { + "index": "logs-emea", + "alias": "logs" + } + }, + { + "add": { + "index": "logs-nasa", + "alias": "logs" + } + } + ] + }"""); assertOK(client().performRequest(updateAliasesRequest)); Request getAliasesRequest = new Request("GET", "/logs-*/_alias"); @@ -209,10 +257,23 @@ public void testGetAliasApiFilterByDataStreamAlias() throws Exception { assertOK(client().performRequest(createDocRequest)); Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"emea\"}}," - + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"nasa\"}}]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ + { + "add": { + "index": "logs-emea", + "alias": "emea" + } + }, + { + "add": { + "index": "logs-nasa", + "alias": "nasa" + } + } + ] + }"""); assertOK(client().performRequest(updateAliasesRequest)); Response response = client().performRequest(new Request("GET", "/_alias")); @@ -243,9 +304,16 @@ public void testGetAliasApiFilterByDataStreamAlias() throws Exception { public void testDataStreamWithAliasFromTemplate() throws IOException { // Create a template Request putComposableIndexTemplateRequest = new Request("POST", "/_index_template/1"); - putComposableIndexTemplateRequest.setJsonEntity( - "{\"index_patterns\": [\"logs-*\"], \"template\": { \"aliases\": { \"logs\": {} } }, \"data_stream\": {}}" - ); + putComposableIndexTemplateRequest.setJsonEntity(""" + { + "index_patterns": [ "logs-*" ], + "template": { + "aliases": { + "logs": {} + } + }, + "data_stream": {} + }"""); assertOK(client().performRequest(putComposableIndexTemplateRequest)); Request createDocRequest = new Request("POST", "/logs-emea/_doc?refresh=true"); @@ -282,10 +350,24 @@ public void testDataStreamWriteAlias() throws IOException { assertOK(client().performRequest(createDocRequest)); Request updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\",\"is_write_index\":true}}," - + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\"}}]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ + { + "add": { + "index": "logs-emea", + "alias": "logs", + "is_write_index": true + } + }, + { + "add": { + "index": "logs-nasa", + "alias": "logs" + } + } + ] + }"""); assertOK(client().performRequest(updateAliasesRequest)); Request getAliasesRequest = new Request("GET", "/_aliases"); @@ -307,12 +389,25 @@ public void testDataStreamWriteAlias() throws IOException { assertThat((String) entityAsMap(createDocResponse).get("_index"), startsWith(".ds-logs-emea")); updateAliasesRequest = new Request("POST", "/_aliases"); - updateAliasesRequest.setJsonEntity( - "{\"actions\":[" - + "{\"add\":{\"index\":\"logs-emea\",\"alias\":\"logs\",\"is_write_index\":false}}," - + "{\"add\":{\"index\":\"logs-nasa\",\"alias\":\"logs\",\"is_write_index\":true}}" - + "]}" - ); + updateAliasesRequest.setJsonEntity(""" + { + "actions": [ + { + "add": { + "index": "logs-emea", + "alias": "logs", + "is_write_index": false + } + }, + { + "add": { + "index": "logs-nasa", + "alias": "logs", + "is_write_index": true + } + } + ] + }"""); assertOK(client().performRequest(updateAliasesRequest)); createDocRequest = new Request("POST", "/logs/_doc?refresh=true"); diff --git a/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamIT.java b/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamIT.java index 50ed2c8594b6a..883e204673039 100644 --- a/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamIT.java +++ b/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamIT.java @@ -418,16 +418,17 @@ public void testOtherWriteOps() throws Exception { public void testComposableTemplateOnlyMatchingWithDataStreamName() throws Exception { String dataStreamName = "logs-foobar"; - String mapping = "{\n" - + " \"properties\": {\n" - + " \"baz_field\": {\n" - + " \"type\": \"keyword\"\n" - + " },\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "baz_field": { + "type": "keyword" + }, + "@timestamp": { + "type": "date" + } + } + }"""; PutComposableIndexTemplateAction.Request request = new PutComposableIndexTemplateAction.Request("id_1"); request.indexTemplate( new ComposableIndexTemplate( @@ -505,13 +506,14 @@ public void testComposableTemplateOnlyMatchingWithDataStreamName() throws Except public void testTimeStampValidationInvalidFieldMapping() throws Exception { // Adding a template with an invalid mapping for timestamp field and expect template creation to fail. - String mapping = "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "@timestamp": { + "type": "keyword" + } + } + }"""; PutComposableIndexTemplateAction.Request createTemplateRequest = new PutComposableIndexTemplateAction.Request("logs-foo"); createTemplateRequest.indexTemplate( new ComposableIndexTemplate( @@ -569,14 +571,8 @@ public void testResolvabilityOfDataStreamsInAPIs() throws Exception { verifyResolvability(dataStreamName, client().admin().indices().prepareRecoveries(dataStreamName), false); verifyResolvability(dataStreamName, client().admin().indices().prepareGetAliases("dummy").addIndices(dataStreamName), false); verifyResolvability(dataStreamName, client().admin().indices().prepareGetFieldMappings(dataStreamName), false); - verifyResolvability( - dataStreamName, - client().admin() - .indices() - .preparePutMapping(dataStreamName) - .setSource("{\"_doc\":{\"properties\": {\"my_field\":{\"type\":\"keyword\"}}}}", XContentType.JSON), - false - ); + verifyResolvability(dataStreamName, client().admin().indices().preparePutMapping(dataStreamName).setSource(""" + {"_doc":{"properties": {"my_field":{"type":"keyword"}}}}""", XContentType.JSON), false); verifyResolvability(dataStreamName, client().admin().indices().prepareGetMappings(dataStreamName), false); verifyResolvability( dataStreamName, @@ -624,14 +620,8 @@ public void testResolvabilityOfDataStreamsInAPIs() throws Exception { verifyResolvability(wildcardExpression, client().admin().indices().prepareRecoveries(wildcardExpression), false); verifyResolvability(wildcardExpression, client().admin().indices().prepareGetAliases(wildcardExpression), false); verifyResolvability(wildcardExpression, client().admin().indices().prepareGetFieldMappings(wildcardExpression), false); - verifyResolvability( - wildcardExpression, - client().admin() - .indices() - .preparePutMapping(wildcardExpression) - .setSource("{\"_doc\":{\"properties\": {\"my_field\":{\"type\":\"keyword\"}}}}", XContentType.JSON), - false - ); + verifyResolvability(wildcardExpression, client().admin().indices().preparePutMapping(wildcardExpression).setSource(""" + {"_doc":{"properties": {"my_field":{"type":"keyword"}}}}""", XContentType.JSON), false); verifyResolvability(wildcardExpression, client().admin().indices().prepareGetMappings(wildcardExpression), false); verifyResolvability(wildcardExpression, client().admin().indices().prepareGetSettings(wildcardExpression), false); verifyResolvability( @@ -993,17 +983,18 @@ public void testDataStreamAliasesUnsupportedParametersValidation() throws Except } public void testTimestampFieldCustomAttributes() throws Exception { - String mapping = "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\",\n" - + " \"format\": \"yyyy-MM\",\n" - + " \"meta\": {\n" - + " \"x\": \"y\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "@timestamp": { + "type": "date", + "format": "yyyy-MM", + "meta": { + "x": "y" + } + } + } + }"""; putComposableIndexTemplate("id1", mapping, List.of("logs-foo*"), null, null); CreateDataStreamAction.Request createDataStreamRequest = new CreateDataStreamAction.Request("logs-foobar"); @@ -1811,7 +1802,12 @@ public void testPartitionedTemplate() throws IOException { List.of("logs"), new Template( Settings.builder().put("index.number_of_shards", "3").put("index.routing_partition_size", "2").build(), - new CompressedXContent("{\n" + " \"_routing\": {\n" + " \"required\": true\n" + " }\n" + " }"), + new CompressedXContent(""" + { + "_routing": { + "required": true + } + }"""), null ), null, @@ -1832,7 +1828,12 @@ public void testPartitionedTemplate() throws IOException { List.of("logs"), new Template( Settings.builder().put("index.number_of_shards", "3").put("index.routing_partition_size", "2").build(), - new CompressedXContent("{\n" + " \"_routing\": {\n" + " \"required\": true\n" + " }\n" + " }"), + new CompressedXContent(""" + { + "_routing": { + "required": true + } + }"""), null ), null, @@ -1869,7 +1870,12 @@ public void testSearchWithRouting() throws IOException, ExecutionException, Inte .put("index.number_of_routing_shards", "10") .put("index.routing_partition_size", "4") .build(), - new CompressedXContent("{\n" + " \"_routing\": {\n" + " \"required\": true\n" + " }\n" + " }"), + new CompressedXContent(""" + { + "_routing": { + "required": true + } + }"""), null ), null, diff --git a/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/DataStreamsStatsTests.java b/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/DataStreamsStatsTests.java index d6d47ee1df17c..b101d2f692708 100644 --- a/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/DataStreamsStatsTests.java +++ b/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/DataStreamsStatsTests.java @@ -230,11 +230,9 @@ private String createDataStream() throws Exception { private String createDataStream(boolean hidden) throws Exception { String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.getDefault()); - Template idxTemplate = new Template( - null, - new CompressedXContent("{\"properties\":{\"" + timestampFieldName + "\":{\"type\":\"date\"},\"data\":{\"type\":\"keyword\"}}}"), - null - ); + Template idxTemplate = new Template(null, new CompressedXContent(""" + {"properties":{"%s":{"type":"date"},"data":{"type":"keyword"}}} + """.formatted(timestampFieldName)), null); ComposableIndexTemplate template = new ComposableIndexTemplate( List.of(dataStreamName + "*"), idxTemplate, diff --git a/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java b/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java index 33aee4900a660..7fcd574135d9d 100644 --- a/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java +++ b/x-pack/plugin/data-streams/src/test/java/org/elasticsearch/xpack/datastreams/mapper/MetadataCreateDataStreamServiceTests.java @@ -36,18 +36,17 @@ public void testValidateTimestampFieldMapping() throws Exception { public void testValidateTimestampFieldMappingNoFieldMapping() { Exception e = expectThrows(IllegalStateException.class, () -> validateTimestampFieldMapping(createMappingLookup("{}"))); assertThat(e.getMessage(), equalTo("[" + DataStreamTimestampFieldMapper.NAME + "] meta field has been disabled")); - String mapping1 = "{\n" - + " \"" - + DataStreamTimestampFieldMapper.NAME - + "\": {\n" - + " \"enabled\": false\n" - + " }," - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + " }"; + String mapping1 = """ + { + "%s": { + "enabled": false + }, + "properties": { + "@timestamp": { + "type": "date" + } + } + }""".formatted(DataStreamTimestampFieldMapper.NAME); e = expectThrows(IllegalStateException.class, () -> validateTimestampFieldMapping(createMappingLookup(mapping1))); assertThat(e.getMessage(), equalTo("[" + DataStreamTimestampFieldMapper.NAME + "] meta field has been disabled")); diff --git a/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java b/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java index e64d9e6652132..8cecfc1eaeac0 100644 --- a/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java +++ b/x-pack/plugin/enrich/qa/common/src/main/java/org/elasticsearch/test/enrich/CommonEnrichRestTestCase.java @@ -70,35 +70,34 @@ private void setupGenericLifecycleTest(boolean deletePipeilne, String field, Str // Add entry to source index and then refresh: Request indexRequest = new Request("PUT", "/my-source-index/_doc/elastic.co"); - indexRequest.setJsonEntity( - "{" - + "\"host\": \"elastic.co\"," - + "\"globalRank\": 25," - + "\"tldRank\": 7," - + "\"tld\": \"co\", " - + "\"date\": {" - + "\"gte\" : \"2021-09-05\"," - + "\"lt\" : \"2021-09-07\"" - + "}, " - + "\"integer\": {" - + "\"gte\" : 40," - + "\"lt\" : 42" - + "}, " - + "\"long\": {" - + "\"gte\" : 8000000," - + "\"lt\" : 9000000" - + "}, " - + "\"double\": {" - + "\"gte\" : 10.10," - + "\"lt\" : 20.20" - + "}, " - + "\"float\": {" - + "\"gte\" : 10000.5," - + "\"lt\" : 10000.7" - + "}, " - + "\"ip\": \"100.0.0.0/4\"" - + "}" - ); + indexRequest.setJsonEntity(""" + { + "host": "elastic.co", + "globalRank": 25, + "tldRank": 7, + "tld": "co", + "date": { + "gte": "2021-09-05", + "lt": "2021-09-07" + }, + "integer": { + "gte": 40, + "lt": 42 + }, + "long": { + "gte": 8000000, + "lt": 9000000 + }, + "double": { + "gte": 10.1, + "lt": 20.2 + }, + "float": { + "gte": 10000.5, + "lt": 10000.7 + }, + "ip": "100.0.0.0/4" + }"""); assertOK(client().performRequest(indexRequest)); Request refreshRequest = new Request("POST", "/my-source-index/_refresh"); assertOK(client().performRequest(refreshRequest)); @@ -109,13 +108,10 @@ private void setupGenericLifecycleTest(boolean deletePipeilne, String field, Str // Create pipeline Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/my_pipeline"); - putPipelineRequest.setJsonEntity( - "{\"processors\":[" - + "{\"enrich\":{\"policy_name\":\"my_policy\",\"field\":\"" - + field - + "\",\"target_field\":\"entry\"}}" - + "]}" - ); + putPipelineRequest.setJsonEntity(""" + { + "processors": [ { "enrich": { "policy_name": "my_policy", "field": "%s", "target_field": "entry" } } ] + }""".formatted(field)); assertOK(client().performRequest(putPipelineRequest)); // Index document using pipeline with enrich processor: @@ -205,9 +201,10 @@ public void testDeleteExistingPipeline() throws Exception { setupGenericLifecycleTest(false, "host", "match", "elastic.co"); Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/another_pipeline"); - putPipelineRequest.setJsonEntity( - "{\"processors\":[" + "{\"enrich\":{\"policy_name\":\"my_policy\",\"field\":\"host\",\"target_field\":\"entry\"}}" + "]}" - ); + putPipelineRequest.setJsonEntity(""" + { + "processors": [ { "enrich": { "policy_name": "my_policy", "field": "host", "target_field": "entry" } } ] + }"""); assertOK(client().performRequest(putPipelineRequest)); ResponseException exc = expectThrows( @@ -251,20 +248,41 @@ public static void createSourceIndex(String index) throws IOException { } public static String createSourceIndexMapping() { - return "\"properties\":" - + "{\"host\": {\"type\":\"keyword\"}," - + "\"globalRank\":{\"type\":\"keyword\"}," - + "\"tldRank\":{\"type\":\"keyword\"}," - + "\"tld\":{\"type\":\"keyword\"}," - + "\"date\":{\"type\":\"date_range\"" - + (randomBoolean() ? "" : ", \"format\": \"yyyy-MM-dd\"") - + "}," - + "\"integer\":{\"type\":\"integer_range\"}," - + "\"long\":{\"type\":\"long_range\"}," - + "\"double\":{\"type\":\"double_range\"}," - + "\"float\":{\"type\":\"float_range\"}," - + "\"ip\":{\"type\":\"ip_range\"}" - + "}"; + return """ + "properties": { + "host": { + "type": "keyword" + }, + "globalRank": { + "type": "keyword" + }, + "tldRank": { + "type": "keyword" + }, + "tld": { + "type": "keyword" + }, + "date": { + "type": "date_range" + %s + }, + "integer": { + "type": "integer_range" + }, + "long": { + "type": "long_range" + }, + "double": { + "type": "double_range" + }, + "float": { + "type": "float_range" + }, + "ip": { + "type": "ip_range" + } + } + """.formatted(randomBoolean() ? "" : ", \"format\": \"yyyy-MM-dd\""); } protected static Map toMap(Response response) throws IOException { @@ -277,7 +295,8 @@ protected static Map toMap(String response) { private static void verifyEnrichMonitoring() throws IOException { Request request = new Request("GET", "/.monitoring-*/_search"); - request.setJsonEntity("{\"query\": {\"term\": {\"type\": \"enrich_coordinator_stats\"}}}"); + request.setJsonEntity(""" + {"query": {"term": {"type": "enrich_coordinator_stats"}}}"""); Map response; try { response = toMap(adminClient().performRequest(request)); diff --git a/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java b/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java index 8b466f6c439a7..7621e975eab22 100644 --- a/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java +++ b/x-pack/plugin/enrich/qa/rest-with-advanced-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichAdvancedSecurityIT.java @@ -50,9 +50,18 @@ public void testEnrichEnforcesDLS() throws IOException { // Create pipeline Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/my_pipeline"); - putPipelineRequest.setJsonEntity( - "{\"processors\":[" + "{\"enrich\":{\"policy_name\":\"my_policy\",\"field\":\"host\",\"target_field\":\"entry\"}}" + "]}" - ); + putPipelineRequest.setJsonEntity(""" + { + "processors": [ + { + "enrich": { + "policy_name": "my_policy", + "field": "host", + "target_field": "entry" + } + } + ] + }"""); assertOK(client().performRequest(putPipelineRequest)); // Verify that the pipeline works as expected for the source doc included in the DLS filter @@ -103,9 +112,18 @@ public void testEnrichEnforcesFLS() throws IOException { // Create pipeline Request putPipelineRequest = new Request("PUT", "/_ingest/pipeline/my_pipeline"); - putPipelineRequest.setJsonEntity( - "{\"processors\":[" + "{\"enrich\":{\"policy_name\":\"my_policy\",\"field\":\"host\",\"target_field\":\"entry\"}}" + "]}" - ); + putPipelineRequest.setJsonEntity(""" + { + "processors": [ + { + "enrich": { + "policy_name": "my_policy", + "field": "host", + "target_field": "entry" + } + } + ] + }"""); assertOK(client().performRequest(putPipelineRequest)); // Index document using pipeline with enrich processor: @@ -138,7 +156,13 @@ private void setupSourceIndexAndPolicy(String sourceIndexName) throws IOExceptio // Add entry to source index and then refresh: Request indexRequest = new Request("PUT", "/" + sourceIndexName + "/_doc/elastic.co"); - indexRequest.setJsonEntity("{\"host\": \"elastic.co\",\"globalRank\": 25,\"tldRank\": 7,\"tld\": \"co\"}"); + indexRequest.setJsonEntity(""" + { + "host": "elastic.co", + "globalRank": 25, + "tldRank": 7, + "tld": "co" + }"""); assertOK(adminClient().performRequest(indexRequest)); Request refreshRequest = new Request("POST", "/" + sourceIndexName + "/_refresh"); assertOK(adminClient().performRequest(refreshRequest)); diff --git a/x-pack/plugin/enrich/qa/rest-with-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichSecurityIT.java b/x-pack/plugin/enrich/qa/rest-with-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichSecurityIT.java index 3a1aee7401e44..9f79e49881756 100644 --- a/x-pack/plugin/enrich/qa/rest-with-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichSecurityIT.java +++ b/x-pack/plugin/enrich/qa/rest-with-security/src/javaRestTest/java/org/elasticsearch/xpack/enrich/EnrichSecurityIT.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.test.enrich.CommonEnrichRestTestCase; -import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; import static org.hamcrest.CoreMatchers.containsString; public class EnrichSecurityIT extends CommonEnrichRestTestCase { @@ -34,7 +33,10 @@ public void testInsufficientPermissionsOnNonExistentIndex() throws Exception { // This test is here because it requires a valid user that has permission to execute policy PUTs but should fail if the user // does not have access to read the backing indices used to enrich the data. Request request = new Request("PUT", "/some-other-index"); - request.setJsonEntity("{\n \"mappings\" : {" + createSourceIndexMapping() + "} }"); + request.setJsonEntity(""" + { + "mappings" : {%s} + }""".formatted(createSourceIndexMapping())); adminClient().performRequest(request); Request putPolicyRequest = new Request("PUT", "/_enrich/policy/my_policy"); putPolicyRequest.setJsonEntity(generatePolicySource("some-other-index")); diff --git a/x-pack/plugin/enrich/src/internalClusterTest/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java b/x-pack/plugin/enrich/src/internalClusterTest/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java index e3722846fec3a..b559bad18e9fc 100644 --- a/x-pack/plugin/enrich/src/internalClusterTest/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java +++ b/x-pack/plugin/enrich/src/internalClusterTest/java/org/elasticsearch/xpack/enrich/EnrichMultiNodeIT.java @@ -313,11 +313,10 @@ private static void createAndExecutePolicy() { } private static void createPipeline() { - String pipelineBody = "{\"processors\": [{\"enrich\": {\"policy_name\":\"" - + POLICY_NAME - + "\", \"field\": \"" - + MATCH_FIELD - + "\", \"target_field\": \"user\"}}]}"; + String pipelineBody = """ + { + "processors": [ { "enrich": { "policy_name": "%s", "field": "%s", "target_field": "user" } } ] + }""".formatted(POLICY_NAME, MATCH_FIELD); PutPipelineRequest request = new PutPipelineRequest(PIPELINE_NAME, new BytesArray(pipelineBody), XContentType.JSON); client().admin().cluster().putPipeline(request).actionGet(); } diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/BasicEnrichTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/BasicEnrichTests.java index 3133c2c701ea4..a6ca3ef20ce64 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/BasicEnrichTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/BasicEnrichTests.java @@ -97,13 +97,19 @@ public void testIngestDataWithMatchProcessor() { client().execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(policyName)).actionGet(); String pipelineName = "my-pipeline"; - String pipelineBody = "{\"processors\": [{\"enrich\": {\"policy_name\":\"" - + policyName - + "\", \"field\": \"" - + MATCH_FIELD - + "\", \"target_field\": \"users\", \"max_matches\": " - + maxMatches - + "}}]}"; + String pipelineBody = """ + { + "processors": [ + { + "enrich": { + "policy_name": "%s", + "field": "%s", + "target_field": "users", + "max_matches": %s + } + } + ] + }""".formatted(policyName, MATCH_FIELD, maxMatches); PutPipelineRequest putPipelineRequest = new PutPipelineRequest(pipelineName, new BytesArray(pipelineBody), XContentType.JSON); client().admin().cluster().putPipeline(putPipelineRequest).actionGet(); @@ -184,11 +190,19 @@ public void testIngestDataWithGeoMatchProcessor() { client().execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(policyName)).actionGet(); String pipelineName = "my-pipeline"; - String pipelineBody = "{\"processors\": [{\"enrich\": {\"policy_name\":\"" - + policyName - + "\", \"field\": \"" - + matchField - + "\", \"target_field\": \"enriched\", \"max_matches\": 1 }}]}"; + String pipelineBody = """ + { + "processors": [ + { + "enrich": { + "policy_name": "%s", + "field": "%s", + "target_field": "enriched", + "max_matches": 1 + } + } + ] + }""".formatted(policyName, matchField); PutPipelineRequest putPipelineRequest = new PutPipelineRequest(pipelineName, new BytesArray(pipelineBody), XContentType.JSON); client().admin().cluster().putPipeline(putPipelineRequest).actionGet(); @@ -236,9 +250,10 @@ public void testMultiplePolicies() { client().execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(policyName)).actionGet(); String pipelineName = "pipeline" + i; - String pipelineBody = "{\"processors\": [{\"enrich\": {\"policy_name\":\"" - + policyName - + "\", \"field\": \"key\", \"target_field\": \"target\"}}]}"; + String pipelineBody = """ + { + "processors": [ { "enrich": { "policy_name": "%s", "field": "key", "target_field": "target" } } ] + }""".formatted(policyName); PutPipelineRequest putPipelineRequest = new PutPipelineRequest(pipelineName, new BytesArray(pipelineBody), XContentType.JSON); client().admin().cluster().putPipeline(putPipelineRequest).actionGet(); } @@ -293,9 +308,10 @@ public void testAsyncTaskExecute() throws Exception { }); String pipelineName = "test-pipeline"; - String pipelineBody = "{\"processors\": [{\"enrich\": {\"policy_name\":\"" - + policyName - + "\", \"field\": \"key\", \"target_field\": \"target\"}}]}"; + String pipelineBody = """ + { + "processors": [ { "enrich": { "policy_name": "%s", "field": "key", "target_field": "target" } } ] + }""".formatted(policyName); PutPipelineRequest putPipelineRequest = new PutPipelineRequest(pipelineName, new BytesArray(pipelineBody), XContentType.JSON); client().admin().cluster().putPipeline(putPipelineRequest).actionGet(); @@ -334,10 +350,9 @@ public void testTemplating() throws Exception { client().execute(ExecuteEnrichPolicyAction.INSTANCE, new ExecuteEnrichPolicyAction.Request(policyName)).actionGet(); String pipelineName = "my-pipeline"; - String pipelineBody = "{\"processors\": [{\"enrich\": {\"policy_name\":\"" - + policyName - + "\", \"field\": \"{{indirection1}}\", \"target_field\": \"{{indirection2}}\"" - + "}}]}"; + String pipelineBody = """ + {"processors": [{"enrich": {"policy_name":"%s", "field": "{{indirection1}}", "target_field": "{{indirection2}}"}}]}""" + .formatted(policyName); PutPipelineRequest putPipelineRequest = new PutPipelineRequest(pipelineName, new BytesArray(pipelineBody), XContentType.JSON); client().admin().cluster().putPipeline(putPipelineRequest).actionGet(); diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyRunnerTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyRunnerTests.java index fcac8e1587674..add2bfc88853e 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyRunnerTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyRunnerTests.java @@ -105,21 +105,14 @@ public static void afterClass() { public void testRunner() throws Exception { final String sourceIndex = "source-index"; - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source( - "{" - + "\"field1\":\"value1\"," - + "\"field2\":2," - + "\"field3\":\"ignored\"," - + "\"field4\":\"ignored\"," - + "\"field5\":\"value5\"" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + { + "field1": "value1", + "field2": 2, + "field3": "ignored", + "field4": "ignored", + "field5": "value5" + }""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -189,12 +182,9 @@ public void testRunner() throws Exception { public void testRunnerGeoMatchType() throws Exception { final String sourceIndex = "source-index"; - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source("{" + "\"location\":" + "\"POINT(10.0 10.0)\"," + "\"zipcode\":90210" + "}", XContentType.JSON) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + {"location":"POINT(10.0 10.0)","zipcode":90210}""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) + .actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -277,12 +267,9 @@ public void testRunnerDoubleRangeMatchType() throws Exception { private void testNumberRangeMatchType(String rangeType) throws Exception { final String sourceIndex = "source-index"; createIndex(sourceIndex, Settings.EMPTY, "_doc", "range", "type=" + rangeType + "_range"); - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source("{" + "\"range\":" + "{" + "\"gt\":1," + "\"lt\":10" + "}," + "\"zipcode\":90210" + "}", XContentType.JSON) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + {"range":{"gt":1,"lt":10},"zipcode":90210}""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) + .actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -359,12 +346,9 @@ private GetIndexResponse getGetIndexResponseAndCheck(String createdEnrichIndex) public void testRunnerRangeTypeWithIpRange() throws Exception { final String sourceIndexName = "source-index"; createIndex(sourceIndexName, Settings.EMPTY, "_doc", "subnet", "type=ip_range"); - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndexName) - .id("id") - .source("{" + "\"subnet\":" + "\"10.0.0.0/8\"," + "\"department\":\"research\"" + "}", XContentType.JSON) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndexName).id("id").source(""" + {"subnet":"10.0.0.0/8","department":"research"}""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)) + .actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); GetIndexResponse sourceIndex = client().admin().indices().getIndex(new GetIndexRequest().indices(sourceIndexName)).actionGet(); @@ -443,28 +427,16 @@ public void testRunnerMultiSource() throws Exception { int numberOfSourceIndices = 3; for (int idx = 0; idx < numberOfSourceIndices; idx++) { final String sourceIndex = baseSourceName + idx; - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id(randomAlphaOfLength(10)) - .source( - "{" - + "\"idx\":" - + idx - + "," - + "\"key\":" - + "\"key" - + idx - + "\"," - + "\"field1\":\"value1\"," - + "\"field2\":2," - + "\"field3\":\"ignored\"," - + "\"field4\":\"ignored\"," - + "\"field5\":\"value5\"" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id(randomAlphaOfLength(10)).source(""" + { + "idx": %s, + "key": "key%s", + "field1": "value1", + "field2": 2, + "field3": "ignored", + "field4": "ignored", + "field5": "value5" + }""".formatted(idx, idx), XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -543,27 +515,16 @@ public void testRunnerMultiSourceDocIdCollisions() throws Exception { for (int idx = 0; idx < numberOfSourceIndices; idx++) { final String sourceIndex = baseSourceName + idx; IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id(collidingDocId) - .routing(collidingDocId + idx) - .source( - "{" - + "\"idx\":" - + idx - + "," - + "\"key\":" - + "\"key" - + idx - + "\"," - + "\"field1\":\"value1\"," - + "\"field2\":2," - + "\"field3\":\"ignored\"," - + "\"field4\":\"ignored\"," - + "\"field5\":\"value5\"" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) + new IndexRequest().index(sourceIndex).id(collidingDocId).routing(collidingDocId + idx).source(""" + { + "idx": %s, + "key": "key%s", + "field1": "value1", + "field2": 2, + "field3": "ignored", + "field4": "ignored", + "field5": "value5" + }""".formatted(idx, idx), XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) ).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); @@ -657,26 +618,16 @@ public void testRunnerMultiSourceEnrichKeyCollisions() throws Exception { int numberOfSourceIndices = 3; for (int idx = 0; idx < numberOfSourceIndices; idx++) { final String sourceIndex = baseSourceName + idx; - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id(randomAlphaOfLength(10)) - .source( - "{" - + "\"idx\":" - + idx - + "," - + "\"key\":" - + "\"key\"," - + "\"field1\":\"value1\"," - + "\"field2\":2," - + "\"field3\":\"ignored\"," - + "\"field4\":\"ignored\"," - + "\"field5\":\"value5\"" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id(randomAlphaOfLength(10)).source(""" + { + "idx": %s, + "key": "key", + "field1": "value1", + "field2": 2, + "field3": "ignored", + "field4": "ignored", + "field5": "value5" + }""".formatted(idx), XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -968,10 +919,8 @@ public void testRunnerObjectSourceMapping() throws Exception { IndexResponse indexRequest = client().index( new IndexRequest().index(sourceIndex) .id("id") - .source( - "{" + "\"data\":{" + "\"field1\":\"value1\"," + "\"field2\":2," + "\"field3\":\"ignored\"" + "}" + "}", - XContentType.JSON - ) + .source(""" + {"data":{"field1":"value1","field2":2,"field3":"ignored"}}""", XContentType.JSON) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) ).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); @@ -1081,10 +1030,8 @@ public void testRunnerExplicitObjectSourceMapping() throws Exception { IndexResponse indexRequest = client().index( new IndexRequest().index(sourceIndex) .id("id") - .source( - "{" + "\"data\":{" + "\"field1\":\"value1\"," + "\"field2\":2," + "\"field3\":\"ignored\"" + "}" + "}", - XContentType.JSON - ) + .source(""" + {"data":{"field1":"value1","field2":2,"field3":"ignored"}}""", XContentType.JSON) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) ).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); @@ -1191,21 +1138,14 @@ public void testRunnerExplicitObjectSourceMappingRangePolicy() throws Exception .actionGet(); assertTrue(createResponse.isAcknowledged()); - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source( - "{" - + "\"data\":{" - + "\"subnet\":\"10.0.0.0/8\"," - + "\"department\":\"research\"," - + "\"field3\":\"ignored\"" - + "}" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + { + "data": { + "subnet": "10.0.0.0/8", + "department": "research", + "field3": "ignored" + } + }""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -1315,23 +1255,16 @@ public void testRunnerTwoObjectLevelsSourceMapping() throws Exception { .actionGet(); assertTrue(createResponse.isAcknowledged()); - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source( - "{" - + "\"data\":{" - + "\"fields\":{" - + "\"field1\":\"value1\"," - + "\"field2\":2," - + "\"field3\":\"ignored\"" - + "}" - + "}" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + { + "data": { + "fields": { + "field1": "value1", + "field2": 2, + "field3": "ignored" + } + } + }""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -1449,23 +1382,16 @@ public void testRunnerTwoObjectLevelsSourceMappingRangePolicy() throws Exception .actionGet(); assertTrue(createResponse.isAcknowledged()); - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source( - "{" - + "\"data\":{" - + "\"fields\":{" - + "\"subnet\":\"10.0.0.0/8\"," - + "\"department\":\"research\"," - + "\"field3\":\"ignored\"" - + "}" - + "}" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + { + "data": { + "fields": { + "subnet": "10.0.0.0/8", + "department": "research", + "field3": "ignored" + } + } + }""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -1584,26 +1510,19 @@ public void testRunnerTwoObjectLevelsSourceMappingDateRangeWithFormat() throws E .actionGet(); assertTrue(createResponse.isAcknowledged()); - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source( - "{" - + "\"data\":{" - + "\"fields\":{" - + "\"period\": {" - + " \"gte\" : \"2021/08/20 at 12:00\"," - + " \"lte\" : \"2021/08/28 at 23:00\"" - + "}," - + "\"status\":\"enrolled\"," - + "\"field3\":\"ignored\"" - + "}" - + "}" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + { + "data": { + "fields": { + "period": { + "gte": "2021/08/20 at 12:00", + "lte": "2021/08/28 at 23:00" + }, + "status": "enrolled", + "field3": "ignored" + } + } + }""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -1739,7 +1658,8 @@ public void testRunnerDottedKeyNameSourceMapping() throws Exception { IndexResponse indexRequest = client().index( new IndexRequest().index(sourceIndex) .id("id") - .source("{" + "\"data.field1\":\"value1\"," + "\"data.field2\":2," + "\"data.field3\":\"ignored\"" + "}", XContentType.JSON) + .source(""" + {"data.field1":"value1","data.field2":2,"data.field3":"ignored"}""", XContentType.JSON) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) ).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); @@ -1814,21 +1734,14 @@ public void testRunnerDottedKeyNameSourceMapping() throws Exception { public void testRunnerWithForceMergeRetry() throws Exception { final String sourceIndex = "source-index"; - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source( - "{" - + "\"field1\":\"value1\"," - + "\"field2\":2," - + "\"field3\":\"ignored\"," - + "\"field4\":\"ignored\"," - + "\"field5\":\"value5\"" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + { + "field1": "value1", + "field2": 2, + "field3": "ignored", + "field4": "ignored", + "field5": "value5" + }""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); SearchResponse sourceSearchResponse = client().search( @@ -1981,21 +1894,14 @@ protected void ensureSingleSegment(String destinationIndexName, int attempt) { public void testRunnerCancel() throws Exception { final String sourceIndex = "source-index"; - IndexResponse indexRequest = client().index( - new IndexRequest().index(sourceIndex) - .id("id") - .source( - "{" - + "\"field1\":\"value1\"," - + "\"field2\":2," - + "\"field3\":\"ignored\"," - + "\"field4\":\"ignored\"," - + "\"field5\":\"value5\"" - + "}", - XContentType.JSON - ) - .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) - ).actionGet(); + IndexResponse indexRequest = client().index(new IndexRequest().index(sourceIndex).id("id").source(""" + { + "field1": "value1", + "field2": 2, + "field3": "ignored", + "field4": "ignored", + "field5": "value5" + }""", XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)).actionGet(); assertEquals(RestStatus.CREATED, indexRequest.status()); List enrichFields = List.of("field2", "field5"); diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java index 9b1081bb15d4b..5a31f70c98381 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichPolicyUpdateTests.java @@ -63,8 +63,8 @@ public void testUpdatePolicyOnly() { equalTo(true) ); - String pipelineConfig = - "{\"processors\":[{\"enrich\": {\"policy_name\": \"my_policy\", \"field\": \"key\", \"target_field\": \"target\"}}]}"; + String pipelineConfig = """ + {"processors":[{"enrich": {"policy_name": "my_policy", "field": "key", "target_field": "target"}}]}"""; PutPipelineRequest putPipelineRequest = new PutPipelineRequest("1", new BytesArray(pipelineConfig), XContentType.JSON); assertAcked(client().admin().cluster().putPipeline(putPipelineRequest).actionGet()); Pipeline pipelineInstance1 = ingestService.getPipeline("1"); diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java index 6518107a4e3da..1b0b55eb6c943 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactoryTests.java @@ -335,13 +335,9 @@ static Metadata createMetadata(String name, EnrichPolicy policy) throws IOExcept .build(); IndexMetadata.Builder builder = IndexMetadata.builder(EnrichPolicy.getBaseName(name) + "-1"); builder.settings(settings); - builder.putMapping( - "{\"_meta\": {\"enrich_match_field\": \"" - + policy.getMatchField() - + "\", \"enrich_policy_type\": \"" - + policy.getType() - + "\"}}" - ); + builder.putMapping(""" + {"_meta": {"enrich_match_field": "%s", "enrich_policy_type": "%s"}} + """.formatted(policy.getMatchField(), policy.getType())); builder.putAlias(AliasMetadata.builder(EnrichPolicy.getBaseName(name)).build()); return Metadata.builder().put(builder).build(); } diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDocTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDocTests.java index 56d6bdc3314ac..32606fe927b19 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDocTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/EnrichCoordinatorDocTests.java @@ -82,42 +82,39 @@ public void testToXContent() throws IOException { assertThat( xContent.utf8ToString(), equalTo( - "{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"" - + DATE_TIME_FORMATTER.formatMillis(timestamp) - + "\"," - + "\"interval_ms\":" - + intervalMillis - + "," - + "\"type\":\"enrich_coordinator_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"" - + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) - + "\"" - + "}," - + "\"enrich_coordinator_stats\":{" - + "\"node_id\":\"" - + stats.getNodeId() - + "\"," - + "\"queue_size\":" - + stats.getQueueSize() - + "," - + "\"remote_requests_current\":" - + stats.getRemoteRequestsCurrent() - + "," - + "\"remote_requests_total\":" - + stats.getRemoteRequestsTotal() - + "," - + "\"executed_searches_total\":" - + stats.getExecutedSearchesTotal() - + "}" - + "}" + XContentHelper.stripWhitespace( + """ + { + "cluster_uuid": "_cluster", + "timestamp": "%s", + "interval_ms": %s, + "type": "enrich_coordinator_stats", + "source_node": { + "uuid": "_uuid", + "host": "_host", + "transport_address": "_addr", + "ip": "_ip", + "name": "_name", + "timestamp": "%s" + }, + "enrich_coordinator_stats": { + "node_id": "%s", + "queue_size": %s, + "remote_requests_current": %s, + "remote_requests_total": %s, + "executed_searches_total": %s + } + }""".formatted( + DATE_TIME_FORMATTER.formatMillis(timestamp), + intervalMillis, + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp), + stats.getNodeId(), + stats.getQueueSize(), + stats.getRemoteRequestsCurrent(), + stats.getRemoteRequestsTotal(), + stats.getExecutedSearchesTotal() + ) + ) ) ); } diff --git a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDocTests.java b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDocTests.java index 0c2e506659f3f..dcf615d242fcc 100644 --- a/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDocTests.java +++ b/x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/monitoring/collector/enrich/ExecutingPolicyDocTests.java @@ -77,60 +77,56 @@ public void testToXContent() throws IOException { assertThat( xContent.utf8ToString(), equalTo( - "{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"" - + DATE_TIME_FORMATTER.formatMillis(timestamp) - + "\"," - + "\"interval_ms\":" - + intervalMillis - + "," - + "\"type\":\"enrich_executing_policy_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"" - + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) - + "\"" - + "}," - + "\"enrich_executing_policy_stats\":{" - + "\"name\":\"" - + executingPolicy.getName() - + "\"," - + "\"task\":{" - + "\"node\":\"" - + executingPolicy.getTaskInfo().getTaskId().getNodeId() - + "\"," - + "\"id\":" - + executingPolicy.getTaskInfo().getTaskId().getId() - + "," - + "\"type\":\"" - + executingPolicy.getTaskInfo().getType() - + "\"," - + "\"action\":\"" - + executingPolicy.getTaskInfo().getAction() - + "\"," - + "\"description\":\"" - + executingPolicy.getTaskInfo().getDescription() - + "\"," - + "\"start_time_in_millis\":" - + executingPolicy.getTaskInfo().getStartTime() - + "," - + "\"running_time_in_nanos\":" - + executingPolicy.getTaskInfo().getRunningTimeNanos() - + "," - + "\"cancellable\":" - + executingPolicy.getTaskInfo().isCancellable() - + (executingPolicy.getTaskInfo().isCancellable() ? ",\"cancelled\":" + executingPolicy.getTaskInfo().isCancelled() : "") - + "," - + header.map(entry -> String.format(Locale.ROOT, "\"headers\":{\"%s\":\"%s\"}", entry.getKey(), entry.getValue())) - .orElse("\"headers\":{}") - + "}" - + "}" - + "}" + XContentHelper.stripWhitespace( + """ + { + "cluster_uuid": "_cluster", + "timestamp": "%s", + "interval_ms": %s, + "type": "enrich_executing_policy_stats", + "source_node": { + "uuid": "_uuid", + "host": "_host", + "transport_address": "_addr", + "ip": "_ip", + "name": "_name", + "timestamp": "%s" + }, + "enrich_executing_policy_stats": { + "name": "%s", + "task": { + "node": "%s", + "id": %s, + "type": "%s", + "action": "%s", + "description": "%s", + "start_time_in_millis": %s, + "running_time_in_nanos": %s, + "cancellable": %s, + %s + "headers": %s + } + } + }""".formatted( + DATE_TIME_FORMATTER.formatMillis(timestamp), + intervalMillis, + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp), + executingPolicy.getName(), + executingPolicy.getTaskInfo().getTaskId().getNodeId(), + executingPolicy.getTaskInfo().getTaskId().getId(), + executingPolicy.getTaskInfo().getType(), + executingPolicy.getTaskInfo().getAction(), + executingPolicy.getTaskInfo().getDescription(), + executingPolicy.getTaskInfo().getStartTime(), + executingPolicy.getTaskInfo().getRunningTimeNanos(), + executingPolicy.getTaskInfo().isCancellable(), + executingPolicy.getTaskInfo().isCancellable() + ? "\"cancelled\": %s,".formatted(executingPolicy.getTaskInfo().isCancelled()) + : "", + header.map(entry -> String.format(Locale.ROOT, """ + {"%s":"%s"}""", entry.getKey(), entry.getValue())).orElse("{}") + ) + ) ) ); } diff --git a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java index d0cfb45dad3aa..ef484fa593e83 100644 --- a/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java +++ b/x-pack/plugin/eql/qa/common/src/main/java/org/elasticsearch/test/eql/EqlRestTestCase.java @@ -38,12 +38,23 @@ public void checkSearchContent() throws Exception { private static final String[][] testBadRequests = { { null, "request body or source parameter is required" }, { "{}", "query is null or empty" }, - { "{\"query\": \"\"}", "query is null or empty" }, - { "{\"query\": \"" + validQuery + "\", \"timestamp_field\": \"\"}", "timestamp field is null or empty" }, - { "{\"query\": \"" + validQuery + "\", \"event_category_field\": \"\"}", "event category field is null or empty" }, - { "{\"query\": \"" + validQuery + "\", \"size\": -1}", "size must be greater than or equal to 0" }, - { "{\"query\": \"" + validQuery + "\", \"filter\": null}", "filter doesn't support values of type: VALUE_NULL" }, - { "{\"query\": \"" + validQuery + "\", \"filter\": {}}", "query malformed, empty clause found" } }; + { """ + {"query": ""}""", "query is null or empty" }, + { """ + {"query": "%s", "timestamp_field": ""} + """.formatted(validQuery), "timestamp field is null or empty" }, + { """ + {"query": "%s", "event_category_field": ""} + """.formatted(validQuery), "event category field is null or empty" }, + { """ + {"query": "%s", "size": -1} + """.formatted(validQuery), "size must be greater than or equal to 0" }, + { """ + {"query": "%s", "filter": null} + """.formatted(validQuery), "filter doesn't support values of type: VALUE_NULL" }, + { """ + {"query": "%s", "filter": {}} + """.formatted(validQuery), "query malformed, empty clause found" } }; public void testBadRequests() throws Exception { createIndex(defaultValidationIndexName, (String) null); @@ -67,15 +78,17 @@ public void testBadRequests() throws Exception { @SuppressWarnings("unchecked") public void testIndexWildcardPatterns() throws Exception { - createIndex("test1", "\"my_alias\" : {}, \"test_alias\" : {}"); - createIndex("test2", "\"my_alias\" : {}"); - - StringBuilder bulk = new StringBuilder(); - bulk.append("{\"index\": {\"_index\": \"test1\", \"_id\": 1}}\n"); - bulk.append("{\"event\":{\"category\":\"process\"},\"@timestamp\":\"2020-09-04T12:34:56Z\"}\n"); - bulk.append("{\"index\": {\"_index\": \"test2\", \"_id\": 2}}\n"); - bulk.append("{\"event\":{\"category\":\"process\"},\"@timestamp\":\"2020-09-05T12:34:56Z\"}\n"); - bulkIndex(bulk.toString()); + createIndex("test1", """ + "my_alias" : {}, "test_alias" : {}"""); + createIndex("test2", """ + "my_alias" : {}"""); + + bulkIndex(""" + {"index": {"_index": "test1", "_id": 1}} + {"event":{"category":"process"},"@timestamp":"2020-09-04T12:34:56Z"} + {"index": {"_index": "test2", "_id": 2}} + {"event":{"category":"process"},"@timestamp":"2020-09-05T12:34:56Z"} + """); String[] wildcardRequests = { "test1,test2", @@ -92,7 +105,8 @@ public void testIndexWildcardPatterns() throws Exception { for (String indexPattern : wildcardRequests) { String endpoint = "/" + indexPattern(indexPattern) + "/_eql/search"; Request request = new Request("GET", endpoint); - request.setJsonEntity("{\"query\":\"process where true\"}"); + request.setJsonEntity(""" + {"query":"process where true"}"""); Response response = client().performRequest(request); Map responseMap; @@ -114,16 +128,18 @@ public void testIndexWildcardPatterns() throws Exception { public void testUnicodeChars() throws Exception { createIndex("test", (String) null); - StringBuilder bulk = new StringBuilder(); - bulk.append("{\"index\": {\"_index\": \"test\", \"_id\": 1}}\n"); - bulk.append("{\"event\":{\"category\":\"process\"},\"@timestamp\":\"2020-09-04T12:34:56Z\",\"log\" : \"prefix_ë_suffix\"}\n"); - bulk.append("{\"index\": {\"_index\": \"test\", \"_id\": 2}}\n"); - bulk.append("{\"event\":{\"category\":\"process\"},\"@timestamp\":\"2020-09-05T12:34:57Z\",\"log\" : \"prefix_𖠋_suffix\"}\n"); - bulkIndex(bulk.toString()); + String bulk = """ + {"index": {"_index": "test", "_id": 1}} + {"event":{"category":"process"},"@timestamp":"2020-09-04T12:34:56Z","log" : "prefix_ë_suffix"} + {"index": {"_index": "test", "_id": 2}} + {"event":{"category":"process"},"@timestamp":"2020-09-05T12:34:57Z","log" : "prefix_𖠋_suffix"} + """; + bulkIndex(bulk); String endpoint = "/" + indexPattern("test") + "/_eql/search"; Request request = new Request("GET", endpoint); - request.setJsonEntity("{\"query\":\"process where log==\\\"prefix_\\\\u{0eb}_suffix\\\"\"}"); + request.setJsonEntity(""" + {"query":"process where log==\\"prefix_\\\\u{0eb}_suffix\\""}"""); Response response = client().performRequest(request); Map responseMap; @@ -135,7 +151,8 @@ public void testUnicodeChars() throws Exception { assertEquals(1, events.size()); assertEquals("1", events.get(0).get("_id")); - request.setJsonEntity("{\"query\":\"process where log==\\\"prefix_\\\\u{01680b}_suffix\\\"\"}"); + request.setJsonEntity(""" + {"query":"process where log==\\"prefix_\\\\u{01680b}_suffix\\""}"""); response = client().performRequest(request); try (InputStream content = response.getEntity().getContent()) { diff --git a/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java b/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java index f0dafda705619..ebcf498019475 100644 --- a/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java +++ b/x-pack/plugin/eql/qa/rest/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java @@ -19,31 +19,18 @@ protected String getInexistentIndexErrorMessage() { } protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException { - assertErrorMessage( - "inexistent1*", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" - ); - assertErrorMessage( - "inexistent1*,inexistent2*", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" - ); - assertErrorMessage( - "test_eql,inexistent*", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent*]\"" - ); - assertErrorMessage( - "inexistent", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent]\"" - ); - // TODO: revisit after https://github.com/elastic/elasticsearch/issues/64197 is closed - assertErrorMessage( - "inexistent1,inexistent2", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [null]\"" - ); + assertErrorMessage("inexistent1*", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\""""); + assertErrorMessage("inexistent1*,inexistent2*", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\""""); + assertErrorMessage("test_eql,inexistent*", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent*]\""""); + assertErrorMessage("inexistent", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent]\""""); + // TODO: revisit after + // https://github.com/elastic/elasticsearch/issues/64197 + // is closed + assertErrorMessage("inexistent1,inexistent2", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [null]\""""); } } diff --git a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java index 066c76fe3a9dd..c9e05fca72321 100644 --- a/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java +++ b/x-pack/plugin/eql/qa/security/src/javaRestTest/java/org/elasticsearch/xpack/eql/EqlRestValidationIT.java @@ -16,38 +16,24 @@ protected Settings restClientSettings() { @Override protected String getInexistentIndexErrorMessage() { - return "\"root_cause\":[{\"type\":\"verification_exception\",\"reason\":\"Found 1 problem\\nline -1:-1: Unknown index [*,-*]\"}]," - + "\"type\":\"index_not_found_exception\",\"reason\":\"no such index "; + return """ + "root_cause":[{"type":"verification_exception","reason":"Found 1 problem\\nline -1:-1: Unknown index [*,-*]"}],\ + "type":"index_not_found_exception","reason":"no such index\s"""; } @Override protected void assertErrorMessageWhenAllowNoIndicesIsFalse(String reqParameter) throws IOException { - assertErrorMessage( - "inexistent1*", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" - ); - assertErrorMessage( - "inexistent1*,inexistent2*", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent1*]\"" - ); - assertErrorMessage( - "test_eql,inexistent*", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [inexistent*]\"" - ); + assertErrorMessage("inexistent1*", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\""""); + assertErrorMessage("inexistent1*,inexistent2*", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent1*]\""""); + assertErrorMessage("test_eql,inexistent*", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [inexistent*]\""""); // TODO: revisit the next two tests when https://github.com/elastic/elasticsearch/issues/64190 is closed - assertErrorMessage( - "inexistent", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [[inexistent]]\"" - ); - assertErrorMessage( - "inexistent1,inexistent2", - reqParameter, - "\"root_cause\":[{\"type\":\"index_not_found_exception\"," + "\"reason\":\"no such index [[inexistent1, inexistent2]]\"" - ); + assertErrorMessage("inexistent", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [[inexistent]]\""""); + assertErrorMessage("inexistent1,inexistent2", reqParameter, """ + "root_cause":[{"type":"index_not_found_exception","reason":"no such index [[inexistent1, inexistent2]]\""""); } } diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java index 3530283b7d722..8219e18d2014a 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlRequestParserTests.java @@ -55,21 +55,21 @@ public void testSearchRequestParser() throws IOException { "query doesn't support values of type: VALUE_NUMBER", EqlSearchRequest::fromXContent ); - assertParsingErrorMessage( - "{\"query\" : \"whatever\", \"size\":\"abc\"}", - "failed to parse field [size]", - EqlSearchRequest::fromXContent - ); + assertParsingErrorMessage(""" + {"query" : "whatever", "size":"abc"}""", "failed to parse field [size]", EqlSearchRequest::fromXContent); - EqlSearchRequest request = generateRequest( - "endgame-*", - "{\"filter\" : {\"match\" : {\"foo\":\"bar\"}}, " - + "\"timestamp_field\" : \"tsf\", " - + "\"event_category_field\" : \"etf\"," - + "\"size\" : \"101\"," - + "\"query\" : \"file where user != 'SYSTEM' by file_path\"}", - EqlSearchRequest::fromXContent - ); + EqlSearchRequest request = generateRequest("endgame-*", """ + { + "filter": { + "match": { + "foo": "bar" + } + }, + "timestamp_field": "tsf", + "event_category_field": "etf", + "size": "101", + "query": "file where user != 'SYSTEM' by file_path" + }""", EqlSearchRequest::fromXContent); assertArrayEquals(new String[] { "endgame-*" }, request.indices()); assertNotNull(request.query()); assertTrue(request.filter() instanceof MatchQueryBuilder); diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java index 0afddb518c59d..069f19da7f48a 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/action/EqlSearchRequestTests.java @@ -31,7 +31,11 @@ public class EqlSearchRequestTests extends AbstractBWCSerializationTestCase { // TODO: possibly add mutations - static String defaultTestFilter = "{\n" + " \"match\" : {\n" + " \"foo\": \"bar\"\n" + " }" + "}"; + static String defaultTestFilter = """ + { + "match" : { + "foo": "bar" + }}"""; static String defaultTestIndex = "endgame-*"; boolean ccsMinimizeRoundtrips; diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java index 017489003ce50..3835f14f35d1b 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/planner/QueryTranslatorFailTests.java @@ -91,31 +91,26 @@ public void testCIDRMatchNonIPField() { () -> plan("process where cidrMatch(hostname, \"10.0.0.0/8\")") ); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\n" - + "line 1:15: first argument of [cidrMatch(hostname, \"10.0.0.0/8\")] must be [ip], found value [hostname] type [text]", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: first argument of [cidrMatch(hostname, "10.0.0.0/8")] must be [ip], found value [hostname] type [text]""", msg); } public void testCIDRMatchNonString() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where cidrMatch(source_address, 12345)")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\n" - + "line 1:15: second argument of [cidrMatch(source_address, 12345)] must be [string], found value [12345] type [integer]", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: second argument of [cidrMatch(source_address, 12345)] must be [string], found value [12345] type [integer]""", msg); } public void testConcatWithInexact() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where concat(plain_text)")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: [concat(plain_text)] cannot operate on field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: [concat(plain_text)] cannot operate on field of data type [text]: No keyword/multi-field defined exact matches \ + for [plain_text]; define one or use MATCH/QUERY instead""", msg); } public void testEndsWithFunctionWithInexact() { @@ -124,75 +119,69 @@ public void testEndsWithFunctionWithInexact() { () -> plan("process where endsWith(plain_text, \"foo\") == true") ); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: [endsWith(plain_text, \"foo\")] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: [endsWith(plain_text, "foo")] cannot operate on first argument field of data type [text]: \ + No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead""", msg); } public void testIndexOfFunctionWithInexact() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where indexOf(plain_text, \"foo\") == 1")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: [indexOf(plain_text, \"foo\")] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: [indexOf(plain_text, "foo")] cannot operate on first argument field of data type [text]: \ + No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead""", msg); e = expectThrows(VerificationException.class, () -> plan("process where indexOf(\"bla\", plain_text) == 1")); msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: [indexOf(\"bla\", plain_text)] cannot operate on second argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: [indexOf("bla", plain_text)] cannot operate on second argument field of data type [text]: \ + No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead""", msg); } public void testLengthFunctionWithInexact() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where length(plain_text) > 0")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: [length(plain_text)] cannot operate on field of data type [text]: No keyword/multi-field " - + "defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: [length(plain_text)] cannot operate on field of data type [text]: \ + No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead""", msg); } public void testMatchIsNotValidFunction() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where match(plain_text, \"foo.*\")")); String msg = e.getMessage(); - assertEquals("Found 1 problem\n" + "line 1:15: Unknown function [match], did you mean [cidrmatch]?", msg); + assertEquals(""" + Found 1 problem + line 1:15: Unknown function [match], did you mean [cidrmatch]?""", msg); } public void testNumberFunctionAlreadyNumber() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where number(pid) == 1")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: first argument of [number(pid)] must be [string], " + "found value [pid] type [long]", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: first argument of [number(pid)] must be [string], found value [pid] type [long]""", msg); } public void testNumberFunctionFloatBase() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where number(process_name, 1.0) == 1")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: second argument of [number(process_name, 1.0)] must be [integer], " - + "found value [1.0] type [double]", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: second argument of [number(process_name, 1.0)] must be [integer], found value [1.0] type [double]""", msg); } public void testNumberFunctionNonString() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where number(plain_text) == 1")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: [number(plain_text)] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: [number(plain_text)] cannot operate on first argument field of data type [text]: \ + No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead""", msg); } @@ -211,11 +200,10 @@ public void testPropertyEquationInClauseFilterUnsupported() { () -> plan("process where opcode in (1,3) and process_name in (parent_process_name, \"SYSTEM\")") ); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:35: Comparisons against fields are not (currently) supported; " - + "offender [parent_process_name] in [process_name in (parent_process_name, \"SYSTEM\")]", - msg - ); + assertEquals(""" + Found 1 problem + line 1:35: Comparisons against fields are not (currently) supported; offender [parent_process_name] \ + in [process_name in (parent_process_name, "SYSTEM")]""", msg); } public void testSequenceWithBeforeBy() { @@ -234,11 +222,10 @@ public void testStartsWithFunctionWithInexact() { () -> plan("process where startsWith(plain_text, \"foo\") == true") ); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\nline 1:15: [startsWith(plain_text, \"foo\")] cannot operate on first argument field of data type " - + "[text]: No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: [startsWith(plain_text, "foo")] cannot operate on first argument field of data type [text]: \ + No keyword/multi-field defined exact matches for [plain_text]; define one or use MATCH/QUERY instead""", msg); } public void testStringContainsWrongParams() { @@ -261,10 +248,9 @@ public void testStringContainsWrongParams() { public void testLikeWithNumericField() { VerificationException e = expectThrows(VerificationException.class, () -> plan("process where pid like \"*.exe\"")); String msg = e.getMessage(); - assertEquals( - "Found 1 problem\n" + "line 1:15: argument of [pid like \"*.exe\"] must be [string], found value [pid] type [long]", - msg - ); + assertEquals(""" + Found 1 problem + line 1:15: argument of [pid like "*.exe"] must be [string], found value [pid] type [long]""", msg); } public void testSequenceWithTooLittleQueries() throws Exception { diff --git a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java index d56b750d67f7a..9628370c0543e 100644 --- a/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java +++ b/x-pack/plugin/eql/src/test/java/org/elasticsearch/xpack/eql/stats/VerifierMetricsTests.java @@ -52,19 +52,21 @@ public void testEventQuery() { } public void testSequenceQuery() { - Counters c = eql("sequence\r\n" + " [process where serial_event_id == 1]\r\n" + " [process where serial_event_id == 2]"); + Counters c = eql(""" + sequence\r + [process where serial_event_id == 1]\r + [process where serial_event_id == 2]"""); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO)); } @AwaitsFix(bugUrl = "waiting for the join implementation") public void testJoinQuery() { - Counters c = eql( - "join\r\n" - + " [file where file_name=\"*.exe\"] by ppid\r\n" - + " [file where file_name=\"*.com\"] by pid\r\n" - + "until [process where opcode=1] by ppid\r\n" - + "| head 1" - ); + Counters c = eql(""" + join\r + [file where file_name="*.exe"] by ppid\r + [file where file_name="*.com"] by pid\r + until [process where opcode=1] by ppid\r + | head 1"""); assertCounters(c, Set.of(JOIN, PIPE_HEAD, JOIN_UNTIL, JOIN_QUERIES_TWO, JOIN_KEYS_ONE)); } @@ -79,94 +81,89 @@ public void testTailQuery() { } public void testSequenceMaxSpanQuery() { - Counters c = eql( - "sequence with maxspan=1d\r\n" - + " [process where serial_event_id < 4] by exit_code\r\n" - + " [process where opcode == 1] by opcode\r\n" - + " [process where opcode == 2] by opcode\r\n" - + " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" - + "until [process where opcode==1] by ppid\r\n" - + "| head 4\r\n" - + "| tail 2" - ); + Counters c = eql(""" + sequence with maxspan=1d\r + [process where serial_event_id < 4] by exit_code\r + [process where opcode == 1] by opcode\r + [process where opcode == 2] by opcode\r + [file where parent_process_name == "file_delete_event"] by exit_code\r + until [process where opcode==1] by ppid\r + | head 4\r + | tail 2"""); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, PIPE_TAIL, SEQUENCE_MAXSPAN, SEQUENCE_UNTIL, SEQUENCE_QUERIES_FOUR, JOIN_KEYS_ONE)); } public void testSequenceWithTwoQueries() { - Counters c = eql( - "sequence with maxspan=1d\r\n" - + " [process where serial_event_id < 4] by exit_code\r\n" - + " [process where opcode == 1] by opcode\r\n" - + "until [process where opcode==1] by ppid\r\n" - + "| head 4\r\n" - + "| tail 2" - ); + Counters c = eql(""" + sequence with maxspan=1d\r + [process where serial_event_id < 4] by exit_code\r + [process where opcode == 1] by opcode\r + until [process where opcode==1] by ppid\r + | head 4\r + | tail 2"""); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, PIPE_TAIL, SEQUENCE_MAXSPAN, SEQUENCE_UNTIL, SEQUENCE_QUERIES_TWO, JOIN_KEYS_ONE)); } public void testSequenceWithThreeQueries() { - Counters c = eql( - "sequence with maxspan=1d\r\n" - + " [process where serial_event_id < 4] by exit_code\r\n" - + " [process where opcode == 1] by opcode\r\n" - + " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" - + "| head 4" - ); + Counters c = eql(""" + sequence with maxspan=1d\r + [process where serial_event_id < 4] by exit_code\r + [process where opcode == 1] by opcode\r + [file where parent_process_name == "file_delete_event"] by exit_code\r + | head 4"""); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_MAXSPAN, SEQUENCE_QUERIES_THREE, JOIN_KEYS_ONE)); } public void testSequenceWithFiveQueries() { - Counters c = eql( - "sequence with maxspan=1d\r\n" - + " [process where serial_event_id < 4] by exit_code\r\n" - + " [process where opcode == 1] by opcode\r\n" - + " [file where parent_process_name == \"file_delete_event\"] by exit_code\r\n" - + " [process where serial_event_id < 4] by exit_code\r\n" - + " [process where opcode == 1] by opcode\r\n" - + "| head 4" - ); + Counters c = eql(""" + sequence with maxspan=1d\r + [process where serial_event_id < 4] by exit_code\r + [process where opcode == 1] by opcode\r + [file where parent_process_name == "file_delete_event"] by exit_code\r + [process where serial_event_id < 4] by exit_code\r + [process where opcode == 1] by opcode\r + | head 4"""); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_MAXSPAN, SEQUENCE_QUERIES_FIVE_OR_MORE, JOIN_KEYS_ONE)); } public void testSequenceWithSevenQueries() { - Counters c = eql( - "sequence by exit_code, opcode\r\n" - + " [process where serial_event_id < 4]\r\n" - + " [process where opcode == 1]\r\n" - + " [file where parent_process_name == \"file_delete_event\"]\r\n" - + " [process where serial_event_id < 4]\r\n" - + " [process where opcode == 1]\r\n" - + " [process where true]\r\n" - + " [process where true]\r\n" - + "| tail 1" - ); + Counters c = eql(""" + sequence by exit_code, opcode\r + [process where serial_event_id < 4]\r + [process where opcode == 1]\r + [file where parent_process_name == "file_delete_event"]\r + [process where serial_event_id < 4]\r + [process where opcode == 1]\r + [process where true]\r + [process where true]\r + | tail 1"""); assertCounters(c, Set.of(SEQUENCE, PIPE_TAIL, SEQUENCE_QUERIES_FIVE_OR_MORE, JOIN_KEYS_TWO)); } public void testSequenceWithThreeKeys() { - Counters c = eql( - "sequence by exit_code, opcode, serial_event_id\r\n" - + " [process where serial_event_id < 4]\r\n" - + " [process where opcode == 1]\r\n" - ); + Counters c = eql(""" + sequence by exit_code, opcode, serial_event_id\r + [process where serial_event_id < 4]\r + [process where opcode == 1]\r + """); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO, JOIN_KEYS_THREE)); } public void testSequenceWithFourKeys() { - Counters c = eql( - "sequence by exit_code, user, serial_event_id, pid\r\n" - + " [process where serial_event_id < 4]\r\n" - + " [process where opcode == 1]\r\n" - ); + Counters c = eql(""" + sequence by exit_code, user, serial_event_id, pid\r + [process where serial_event_id < 4]\r + [process where opcode == 1]\r + """); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO, JOIN_KEYS_FOUR)); } public void testSequenceWithFiveKeys() { - Counters c = eql( - "sequence by exit_code, user, serial_event_id, pid, ppid\r\n" - + " [process where serial_event_id < 4]\r\n" - + " [process where opcode == 1]\r\n" - ); + Counters c = eql(""" + sequence by exit_code, user, serial_event_id, pid, ppid\r + [process where serial_event_id < 4]\r + [process where opcode == 1]\r + """); assertCounters(c, Set.of(SEQUENCE, PIPE_HEAD, SEQUENCE_QUERIES_TWO, JOIN_KEYS_FIVE_OR_MORE)); } diff --git a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java index 521cff20114ae..7638de5079265 100644 --- a/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java +++ b/x-pack/plugin/identity-provider/qa/idp-rest-tests/src/javaRestTest/java/org/elasticsearch/xpack/idp/IdentityProviderAuthenticationIT.java @@ -128,18 +128,13 @@ private SamlPrepareAuthenticationResponse generateSamlAuthnRequest(String realmN private String generateSamlResponse(String entityId, String acs, @Nullable Map authnState) throws Exception { final Request request = new Request("POST", "/_idp/saml/init"); if (authnState != null && authnState.isEmpty() == false) { - request.setJsonEntity( - "{\"entity_id\":\"" - + entityId - + "\", \"acs\":\"" - + acs - + "\"," - + "\"authn_state\":" - + Strings.toString(JsonXContent.contentBuilder().map(authnState)) - + "}" - ); + request.setJsonEntity(""" + {"entity_id":"%s", "acs":"%s","authn_state":%s} + """.formatted(entityId, acs, Strings.toString(JsonXContent.contentBuilder().map(authnState)))); } else { - request.setJsonEntity("{\"entity_id\":\"" + entityId + "\", \"acs\":\"" + acs + "\"}"); + request.setJsonEntity(""" + {"entity_id":"%s", "acs":"%s"} + """.formatted(entityId, acs)); } request.setOptions( RequestOptions.DEFAULT.toBuilder() @@ -158,9 +153,13 @@ private void authenticateWithSamlResponse(String samlResponse, @Nullable String final String encodedResponse = Base64.getEncoder().encodeToString(samlResponse.getBytes(StandardCharsets.UTF_8)); final Request request = new Request("POST", "/_security/saml/authenticate"); if (Strings.hasText(id)) { - request.setJsonEntity("{\"content\":\"" + encodedResponse + "\", \"realm\":\"" + REALM_NAME + "\", \"ids\":[\"" + id + "\"]}"); + request.setJsonEntity(""" + {"content":"%s", "realm":"%s", "ids":["%s"]} + """.formatted(encodedResponse, REALM_NAME, id)); } else { - request.setJsonEntity("{\"content\":\"" + encodedResponse + "\", \"realm\":\"" + REALM_NAME + "\"}"); + request.setJsonEntity(""" + {"content":"%s", "realm":"%s"} + """.formatted(encodedResponse, REALM_NAME)); } final String accessToken; try (RestClient kibanaClient = restClientAsKibanaSystem()) { diff --git a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java index 401f658be67d7..6a1425d386190 100644 --- a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java +++ b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/action/SamlIdentityProviderTests.java @@ -200,13 +200,9 @@ public void testSpInitiatedSso() throws Exception { ); XContentBuilder authnStateBuilder = jsonBuilder(); authnStateBuilder.map(authnState); - initRequest.setJsonEntity( - "{" - + ("\"entity_id\":\"" + entityId + "\",") - + ("\"acs\":\"" + serviceProvider.get("acs") + "\",") - + ("\"authn_state\":" + Strings.toString(authnStateBuilder)) - + "}" - ); + initRequest.setJsonEntity(""" + {"entity_id":"%s","acs":"%s","authn_state":%s} + """.formatted(entityId, serviceProvider.get("acs"), Strings.toString(authnStateBuilder))); Response initResponse = getRestClient().performRequest(initRequest); ObjectPath initResponseObject = ObjectPath.createFromResponse(initResponse); assertThat(initResponseObject.evaluate("post_url").toString(), equalTo(acsUrl)); @@ -270,16 +266,9 @@ public void testSpInitiatedSsoFailsForUserWithNoAccess() throws Exception { ); XContentBuilder authnStateBuilder = jsonBuilder(); authnStateBuilder.map(authnState); - initRequest.setJsonEntity( - "{ \"entity_id\":\"" - + entityId - + "\", \"acs\":\"" - + acsUrl - + "\"," - + "\"authn_state\":" - + Strings.toString(authnStateBuilder) - + "}" - ); + initRequest.setJsonEntity(""" + {"entity_id":"%s", "acs":"%s","authn_state":%s} + """.formatted(entityId, acsUrl, Strings.toString(authnStateBuilder))); Response initResponse = getRestClient().performRequest(initRequest); ObjectPath initResponseObject = ObjectPath.createFromResponse(initResponse); assertThat(initResponseObject.evaluate("post_url").toString(), equalTo(acsUrl)); @@ -500,20 +489,10 @@ private byte[] sign(byte[] content, String algo, X509Credential credential) thro } private void assertContainsAttributeWithValue(String message, String attribute, String value) { - assertThat( - message, - containsString( - "" - + value - + "" - ) - ); + assertThat(message, containsString(""" + %s\ + """.formatted(attribute, attribute, value))); } } diff --git a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java index c339f0baac416..35b6fdd812050 100644 --- a/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java +++ b/x-pack/plugin/identity-provider/src/internalClusterTest/java/org/elasticsearch/xpack/idp/saml/test/IdentityProviderIntegTestCase.java @@ -203,35 +203,29 @@ protected Path nodeConfigPath(int nodeOrdinal) { private String configRoles() { // test role allows for everything - return SAMPLE_USER_ROLE - + ":\n" - + " cluster: [ ALL ]\n" - + " indices:\n" - + " - names: '*'\n" - + " allow_restricted_indices: true\n" - + " privileges: [ ALL ]\n" - + "\n" - + - // IDP end user doesn't need any privileges on the security cluster - SAMPLE_IDPUSER_ROLE - + ":\n" - + - // Could switch to grant apikey for user and call this as console_user - " cluster: ['cluster:admin/xpack/security/api_key/create']\n" - + " indices: []\n" - + " applications:\n " - + " - application: elastic-cloud\n" - + " resources: [ '" - + SP_ENTITY_ID - + "' ]\n" - + " privileges: [ 'sso:superuser' ]\n" - + "\n" - + - // Console user should be able to call all IDP related endpoints and register application privileges - CONSOLE_USER_ROLE - + ":\n" - + " cluster: ['cluster:admin/idp/*', 'cluster:admin/xpack/security/privilege/*' ]\n" - + " indices: []\n"; + // IDP end user doesn't need any privileges on the security cluster + // Could switch to grant apikey for user and call this as console_user + // Console user should be able to call all IDP related endpoints and register application privileges + return """ + %s: + cluster: [ ALL ] + indices: + - names: '*' + allow_restricted_indices: true + privileges: [ ALL ] + + %s: + cluster: ['cluster:admin/xpack/security/api_key/create'] + indices: [] + applications: + - application: elastic-cloud + resources: [ '%s' ] + privileges: [ 'sso:superuser' ] + + %s: + cluster: ['cluster:admin/idp/*', 'cluster:admin/xpack/security/privilege/*' ] + indices: [] + """.formatted(SAMPLE_USER_ROLE, SAMPLE_IDPUSER_ROLE, SP_ENTITY_ID, CONSOLE_USER_ROLE); } private String configUsers() { diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java index 1cfd4be509fb2..dc6ba1ff3d152 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/action/TransportSamlInitiateSingleSignOnActionTests.java @@ -220,20 +220,10 @@ private TransportSamlInitiateSingleSignOnAction setupTransportAction(boolean wit } private void assertContainsAttributeWithValue(String message, String attribute, String value) { - assertThat( - message, - containsString( - "" - + value - + "" - ) - ); + assertThat(message, containsString(""" + %s\ + """.formatted(attribute, attribute, value))); } } diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java index bd39a8aa697ea..903174d4d9c13 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/idp/SamlIdpMetadataBuilderTests.java @@ -45,19 +45,16 @@ public void testSimpleMetadataGeneration() throws Exception { ).build(); final Element element = new EntityDescriptorMarshaller().marshall(entityDescriptor); final String xml = samlFactory.toString(element, false); - assertThat( - xml, - equalTo( - "" - + "" - + "" - + "" - + "" - + "" - ) - ); + assertThat(xml, equalTo(normaliseXml(""" + + + + + + """.replaceAll("\\R", "")))); } public void testMetadataGenerationWithAllParameters() throws Exception { @@ -124,51 +121,52 @@ public void testMetadataGenerationWithAllParameters() throws Exception { "MUeWfI+F8kK4NH5GkGggGqQDtes3Y+bWQ28lV7ny44TkMBARz6zH" ); - final String expectedXml = "" - + "" - + " " - + " " - + " " - + " " - + " %(signingCertificateOne)" - + " " - + " " - + " " - + " " - + " " - + " " - + " %(signingCertificateTwo)" - + " " - + " " - + " " - + " " - + " " - + " urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" - + " urn:oasis:names:tc:SAML:2.0:nameid-format:transient" - + " " - + " " - + " " - + " " - + " Avengers" - + " The Avengers" - + " https://linktotheidp.org" - + " " - + " " - + " Tony" - + " Stark" - + " tony@starkindustries.com" - + " " - + ""; + final String expectedXml = """ + + + + + + + %(signingCertificateOne) + + + + + + + %(signingCertificateTwo) + + + + + + urn:oasis:names:tc:SAML:2.0:nameid-format:persistent + urn:oasis:names:tc:SAML:2.0:nameid-format:transient + + + + + Avengers + The Avengers + https://linktotheidp.org + + + Tony + Stark + tony@starkindustries.com + + """.replaceAll("\\R", ""); final Map replacements = new HashMap<>(); replacements.put("signingCertificateOne", signingCertificateOne); diff --git a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java index e251a2af22ceb..848e14927d6c7 100644 --- a/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java +++ b/x-pack/plugin/identity-provider/src/test/java/org/elasticsearch/xpack/idp/saml/sp/WildcardServiceProviderResolverTests.java @@ -29,64 +29,65 @@ public class WildcardServiceProviderResolverTests extends IdpSamlTestCase { - private static final String SERVICES_JSON = "{" - + "\"services\": {" - + " \"service1a\": {" - + " \"entity_id\": \"https://(?\\\\w+)\\\\.example\\\\.com/\"," - + " \"acs\": \"https://(?\\\\w+)\\\\.service\\\\.example\\\\.com/saml2/acs\"," - + " \"tokens\": [ \"service\" ]," - + " \"template\": { " - + " \"name\": \"{{service}} at example.com (A)\"," - + " \"privileges\": {" - + " \"resource\": \"service1:example:{{service}}\"," - + " \"roles\": [ \"sso:(.*)\" ]" - + " }," - + " \"attributes\": {" - + " \"principal\": \"http://cloud.elastic.co/saml/principal\"," - + " \"name\": \"http://cloud.elastic.co/saml/name\"," - + " \"email\": \"http://cloud.elastic.co/saml/email\"," - + " \"roles\": \"http://cloud.elastic.co/saml/roles\"" - + " }" - + " }" - + " }," - + " \"service1b\": {" - + " \"entity_id\": \"https://(?\\\\w+)\\\\.example\\\\.com/\"," - + " \"acs\": \"https://services\\\\.example\\\\.com/(?\\\\w+)/saml2/acs\"," - + " \"tokens\": [ \"service\" ]," - + " \"template\": { " - + " \"name\": \"{{service}} at example.com (B)\"," - + " \"privileges\": {" - + " \"resource\": \"service1:example:{{service}}\"," - + " \"roles\": [ \"sso:(.*)\" ]" - + " }," - + " \"attributes\": {" - + " \"principal\": \"http://cloud.elastic.co/saml/principal\"," - + " \"name\": \"http://cloud.elastic.co/saml/name\"," - + " \"email\": \"http://cloud.elastic.co/saml/email\"," - + " \"roles\": \"http://cloud.elastic.co/saml/roles\"" - + " }" - + " }" - + " }," - + " \"service2\": {" - + " \"entity_id\": \"https://service-(?\\\\d+)\\\\.example\\\\.net/\"," - + " \"acs\": \"https://saml\\\\.example\\\\.net/(?\\\\d+)/acs\"," - + " \"tokens\": [ \"id\" ]," - + " \"template\": { " - + " \"name\": \"{{id}} at example.net\"," - + " \"privileges\": {" - + " \"resource\": \"service2:example:{{id}}\"," - + " \"roles\": [ \"sso:(.*)\" ]" - + " }," - + " \"attributes\": {" - + " \"principal\": \"http://cloud.elastic.co/saml/principal\"," - + " \"name\": \"http://cloud.elastic.co/saml/name\"," - + " \"email\": \"http://cloud.elastic.co/saml/email\"," - + " \"roles\": \"http://cloud.elastic.co/saml/roles\"" - + " }" // attributes - + " }" // template - + " }" // service2 - + " }" // services - + "}"; // root object + private static final String SERVICES_JSON = """ + { + "services": { + "service1a": { + "entity_id": "https://(?\\\\w+)\\\\.example\\\\.com/", + "acs": "https://(?\\\\w+)\\\\.service\\\\.example\\\\.com/saml2/acs", + "tokens": [ "service" ], + "template": { + "name": "{{service}} at example.com (A)", + "privileges": { + "resource": "service1:example:{{service}}", + "roles": [ "sso:(.*)" ] + }, + "attributes": { + "principal": "http://cloud.elastic.co/saml/principal", + "name": "http://cloud.elastic.co/saml/name", + "email": "http://cloud.elastic.co/saml/email", + "roles": "http://cloud.elastic.co/saml/roles" + } + } + }, + "service1b": { + "entity_id": "https://(?\\\\w+)\\\\.example\\\\.com/", + "acs": "https://services\\\\.example\\\\.com/(?\\\\w+)/saml2/acs", + "tokens": [ "service" ], + "template": { + "name": "{{service}} at example.com (B)", + "privileges": { + "resource": "service1:example:{{service}}", + "roles": [ "sso:(.*)" ] + }, + "attributes": { + "principal": "http://cloud.elastic.co/saml/principal", + "name": "http://cloud.elastic.co/saml/name", + "email": "http://cloud.elastic.co/saml/email", + "roles": "http://cloud.elastic.co/saml/roles" + } + } + }, + "service2": { + "entity_id": "https://service-(?\\\\d+)\\\\.example\\\\.net/", + "acs": "https://saml\\\\.example\\\\.net/(?\\\\d+)/acs", + "tokens": [ "id" ], + "template": { + "name": "{{id}} at example.net", + "privileges": { + "resource": "service2:example:{{id}}", + "roles": [ "sso:(.*)" ] + }, + "attributes": { + "principal": "http://cloud.elastic.co/saml/principal", + "name": "http://cloud.elastic.co/saml/name", + "email": "http://cloud.elastic.co/saml/email", + "roles": "http://cloud.elastic.co/saml/roles" + } + } + } + } + }"""; // root object private WildcardServiceProviderResolver resolver; @Before diff --git a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java index 4ae4d4302dd73..d2dca266d5870 100644 --- a/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java +++ b/x-pack/plugin/ilm/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ilm/CCRIndexLifecycleIT.java @@ -201,22 +201,32 @@ public void testCcrAndIlmWithRollover() throws Exception { // Set up an auto-follow pattern Request createAutoFollowRequest = new Request("PUT", "/_ccr/auto_follow/my_auto_follow_pattern"); - createAutoFollowRequest.setJsonEntity( - "{\"leader_index_patterns\": [\"mymetrics-*\"], " - + "\"remote_cluster\": \"leader_cluster\", \"read_poll_timeout\": \"1000ms\"}" - ); + createAutoFollowRequest.setJsonEntity(""" + { + "leader_index_patterns": [ "mymetrics-*" ], + "remote_cluster": "leader_cluster", + "read_poll_timeout": "1000ms" + }"""); assertOK(client().performRequest(createAutoFollowRequest)); try (RestClient leaderClient = buildLeaderClient()) { // Create an index on the leader using the template set up above Request createIndexRequest = new Request("PUT", "/" + indexName); - createIndexRequest.setJsonEntity( - "{" - + "\"mappings\": {\"properties\": {\"field\": {\"type\": \"keyword\"}}}, " - + "\"aliases\": {\"" - + alias - + "\": {\"is_write_index\": true}} }" - ); + createIndexRequest.setJsonEntity(""" + { + "mappings": { + "properties": { + "field": { + "type": "keyword" + } + } + }, + "aliases": { + "%s": { + "is_write_index": true + } + } + }""".formatted(alias)); assertOK(leaderClient.performRequest(createIndexRequest)); // Check that the new index is created Request checkIndexRequest = new Request("GET", "/_cluster/health/" + indexName); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java index 4e7f7ba5033a1..f8ed92be22e7d 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/MigrateToDataTiersIT.java @@ -180,7 +180,9 @@ public void testMigrateToDataTiersAction() throws Exception { updateIndexSettings(indexWithDataWarmRouting, Settings.builder().putNull(DataTier.TIER_PREFERENCE)); Request migrateRequest = new Request("POST", "_ilm/migrate_to_data_tiers"); - migrateRequest.setJsonEntity("{\"legacy_template_to_delete\": \"" + templateName + "\", \"node_attribute\": \"data\"}"); + migrateRequest.setJsonEntity(""" + {"legacy_template_to_delete": "%s", "node_attribute": "data"} + """.formatted(templateName)); Response migrateDeploymentResponse = client().performRequest(migrateRequest); assertOK(migrateDeploymentResponse); @@ -360,22 +362,18 @@ private String getCachedPhaseDefAsMap(Response clusterMetadataResponse, String i private void createLegacyTemplate(String templateName) throws IOException { String indexPrefix = randomAlphaOfLengthBetween(5, 15).toLowerCase(Locale.ROOT); - final StringEntity template = new StringEntity( - "{\n" - + " \"index_patterns\": \"" - + indexPrefix - + "*\",\n" - + " \"settings\": {\n" - + " \"index\": {\n" - + " \"lifecycle\": {\n" - + " \"name\": \"does_not_exist\",\n" - + " \"rollover_alias\": \"test_alias\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}", - ContentType.APPLICATION_JSON - ); + final StringEntity template = new StringEntity(""" + { + "index_patterns": "%s*", + "settings": { + "index": { + "lifecycle": { + "name": "does_not_exist", + "rollover_alias": "test_alias" + } + } + } + }""".formatted(indexPrefix), ContentType.APPLICATION_JSON); Request templateRequest = new Request("PUT", "_template/" + templateName); templateRequest.setEntity(template); templateRequest.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java index 6a48f4313955b..2486b4e4cd50d 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/TimeSeriesRestDriver.java @@ -149,15 +149,12 @@ public static void createComposableTemplate(RestClient client, String templateNa throws IOException { XContentBuilder builder = jsonBuilder(); template.toXContent(builder, ToXContent.EMPTY_PARAMS); - StringEntity templateJSON = new StringEntity( - String.format( - Locale.ROOT, - "{\n" + " \"index_patterns\": \"%s\",\n" + " \"data_stream\": {},\n" + " \"template\": %s\n" + "}", - indexPattern, - Strings.toString(builder) - ), - ContentType.APPLICATION_JSON - ); + StringEntity templateJSON = new StringEntity(String.format(Locale.ROOT, """ + { + "index_patterns": "%s", + "data_stream": {}, + "template": %s + }""", indexPattern, Strings.toString(builder)), ContentType.APPLICATION_JSON); Request createIndexTemplateRequest = new Request("PUT", "_index_template/" + templateName); createIndexTemplateRequest.setEntity(templateJSON); client.performRequest(createIndexTemplateRequest); @@ -165,7 +162,12 @@ public static void createComposableTemplate(RestClient client, String templateNa public static void rolloverMaxOneDocCondition(RestClient client, String indexAbstractionName) throws IOException { Request rolloverRequest = new Request("POST", "/" + indexAbstractionName + "/_rollover"); - rolloverRequest.setJsonEntity("{\n" + " \"conditions\": {\n" + " \"max_docs\": \"1\"\n" + " }\n" + "}"); + rolloverRequest.setJsonEntity(""" + { + "conditions": { + "max_docs": "1" + } + }"""); client.performRequest(rolloverRequest); } @@ -303,15 +305,11 @@ public static void createIndexWithSettings( if (useWriteIndex) { writeIndexSnippet = "\"is_write_index\": true"; } - request.setJsonEntity( - "{\n \"settings\": " - + Strings.toString(settings.build()) - + ", \"aliases\" : { \"" - + alias - + "\": { " - + writeIndexSnippet - + " } } }" - ); + request.setJsonEntity(""" + { + "settings": %s, + "aliases" : { "%s": { %s } } + }""".formatted(Strings.toString(settings.build()), alias, writeIndexSnippet)); client.performRequest(request); // wait for the shards to initialize ensureGreen(index); @@ -319,7 +317,10 @@ public static void createIndexWithSettings( public static void createIndexWithSettings(RestClient client, String index, Settings.Builder settings) throws IOException { Request request = new Request("PUT", "/" + index); - request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings.build()) + "}"); + request.setJsonEntity(""" + { + "settings": %s + }""".formatted(Strings.toString(settings.build()))); client.performRequest(request); // wait for the shards to initialize ensureGreen(index); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java index a90a90b348490..f3df25cbee3d8 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/ChangePolicyforIndexIT.java @@ -118,9 +118,15 @@ public void testChangePolicyForIndex() throws Exception { .put(LifecycleSettings.LIFECYCLE_NAME, "policy_1") .build(); Request createIndexRequest = new Request("PUT", "/" + indexName); - createIndexRequest.setJsonEntity( - "{\n \"settings\": " + Strings.toString(settings) + ", \"aliases\" : { \"alias\": { \"is_write_index\": true } } }" - ); + createIndexRequest.setJsonEntity(""" + { + "settings": %s, + "aliases": { + "alias": { + "is_write_index": true + } + } + }""".formatted(Strings.toString(settings))); client().performRequest(createIndexRequest); // wait for the shards to initialize ensureGreen(indexName); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java index cf393addd2376..0b5f38072d937 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java @@ -519,22 +519,18 @@ public void testSetNullPriority() throws Exception { @SuppressWarnings("unchecked") public void testNonexistentPolicy() throws Exception { String indexPrefix = randomAlphaOfLengthBetween(5, 15).toLowerCase(Locale.ROOT); - final StringEntity template = new StringEntity( - "{\n" - + " \"index_patterns\": \"" - + indexPrefix - + "*\",\n" - + " \"settings\": {\n" - + " \"index\": {\n" - + " \"lifecycle\": {\n" - + " \"name\": \"does_not_exist\",\n" - + " \"rollover_alias\": \"test_alias\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}", - ContentType.APPLICATION_JSON - ); + final StringEntity template = new StringEntity(""" + { + "index_patterns": "%s*", + "settings": { + "index": { + "lifecycle": { + "name": "does_not_exist", + "rollover_alias": "test_alias" + } + } + } + }""".formatted(indexPrefix), ContentType.APPLICATION_JSON); Request templateRequest = new Request("PUT", "_template/test"); templateRequest.setEntity(template); templateRequest.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); @@ -544,10 +540,14 @@ public void testNonexistentPolicy() throws Exception { createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 1L)); index = indexPrefix + "-000001"; - final StringEntity putIndex = new StringEntity( - "{\n" + " \"aliases\": {\n" + " \"test_alias\": {\n" + " \"is_write_index\": true\n" + " }\n" + " }\n" + "}", - ContentType.APPLICATION_JSON - ); + final StringEntity putIndex = new StringEntity(""" + { + "aliases": { + "test_alias": { + "is_write_index": true + } + } + }""", ContentType.APPLICATION_JSON); Request putIndexRequest = new Request("PUT", index); putIndexRequest.setEntity(putIndex); client().performRequest(putIndexRequest); @@ -686,18 +686,13 @@ public void testRemoveAndReaddPolicy() throws Exception { // Add the policy again Request addPolicyRequest = new Request("PUT", "/" + originalIndex + "/_settings"); - addPolicyRequest.setJsonEntity( - "{\n" - + " \"settings\": {\n" - + " \"index.lifecycle.name\": \"" - + policy - + "\",\n" - + " \"index.lifecycle.rollover_alias\": \"" - + alias - + "\"\n" - + " }\n" - + "}" - ); + addPolicyRequest.setJsonEntity(""" + { + "settings": { + "index.lifecycle.name": "%s", + "index.lifecycle.rollover_alias": "%s" + } + }""".formatted(policy, alias)); client().performRequest(addPolicyRequest); assertBusy(() -> assertTrue((boolean) explainIndex(client(), originalIndex).getOrDefault("managed", false))); @@ -752,18 +747,14 @@ public void testWaitForActiveShardsStep() throws Exception { // update policy on index updatePolicy(client(), originalIndex, policy); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity( - "{" - + "\"index_patterns\": [\"" - + index - + "-*\"], \n" - + " \"settings\": {\n" - + " \"number_of_shards\": 1,\n" - + " \"number_of_replicas\": 142,\n" - + " \"index.write.wait_for_active_shards\": \"all\"\n" - + " }\n" - + "}" - ); + createIndexTemplate.setJsonEntity(""" + {"index_patterns": ["%s-*"], + "settings": { + "number_of_shards": 1, + "number_of_replicas": 142, + "index.write.wait_for_active_shards": "all" + } + }""".formatted(index)); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); @@ -782,23 +773,16 @@ public void testWaitForActiveShardsStep() throws Exception { public void testHistoryIsWrittenWithSuccess() throws Exception { createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 1L)); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity( - "{" - + "\"index_patterns\": [\"" - + index - + "-*\"], \n" - + " \"settings\": {\n" - + " \"number_of_shards\": 1,\n" - + " \"number_of_replicas\": 0,\n" - + " \"index.lifecycle.name\": \"" - + policy - + "\",\n" - + " \"index.lifecycle.rollover_alias\": \"" - + alias - + "\"\n" - + " }\n" - + "}" - ); + createIndexTemplate.setJsonEntity(""" + { + "index_patterns": [ "%s-*" ], + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0, + "index.lifecycle.name": "%s", + "index.lifecycle.rollover_alias": "%s" + } + }""".formatted(index, policy, alias)); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); @@ -898,23 +882,16 @@ public void testRefreshablePhaseJson() throws Exception { createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 100L)); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity( - "{" - + "\"index_patterns\": [\"" - + index - + "-*\"], \n" - + " \"settings\": {\n" - + " \"number_of_shards\": 1,\n" - + " \"number_of_replicas\": 0,\n" - + " \"index.lifecycle.name\": \"" - + policy - + "\",\n" - + " \"index.lifecycle.rollover_alias\": \"" - + alias - + "\"\n" - + " }\n" - + "}" - ); + createIndexTemplate.setJsonEntity(""" + { + "index_patterns": ["%s-*"], + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0, + "index.lifecycle.name": "%s", + "index.lifecycle.rollover_alias": "%s" + } + }""".formatted(index, policy, alias)); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); @@ -1125,44 +1102,44 @@ private void assertHistoryIsPresent( ); final Request historySearchRequest = new Request("GET", "ilm-history*/_search?expand_wildcards=all"); historySearchRequest.setJsonEntity( - "{\n" - + " \"query\": {\n" - + " \"bool\": {\n" - + " \"must\": [\n" - + " {\n" - + " \"term\": {\n" - + " \"policy\": \"" - + policyName - + "\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"term\": {\n" - + " \"success\": " - + success - + "\n" - + " }\n" - + " },\n" - + " {\n" - + " \"term\": {\n" - + " \"index\": \"" - + indexName - + "\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"term\": {\n" - + " \"state.step\": \"" - + stepName - + "\"\n" - + " }\n" - + " }\n" - + (phase == null ? "" : ",{\"term\": {\"state.phase\": \"" + phase + "\"}}") - + (action == null ? "" : ",{\"term\": {\"state.action\": \"" + action + "\"}}") - + " ]\n" - + " }\n" - + " }\n" - + "}" + """ + { + "query": { + "bool": { + "must": [ + { + "term": { + "policy": "%s" + } + }, + { + "term": { + "success": %s + } + }, + { + "term": { + "index": "%s" + } + }, + { + "term": { + "state.step": "%s" + } + } + %s + %s + ] + } + } + }""".formatted( + policyName, + success, + indexName, + stepName, + phase == null ? "" : ",{\"term\": {\"state.phase\": \"%s\"}}".formatted(phase), + action == null ? "" : ",{\"term\": {\"state.action\": \"" + action + "\"}}" + ) ); Response historyResponse; try { @@ -1177,30 +1154,25 @@ private void assertHistoryIsPresent( // For a failure, print out whatever history we *do* have for the index if (hits == 0) { final Request allResults = new Request("GET", "ilm-history*/_search"); - allResults.setJsonEntity( - "{\n" - + " \"query\": {\n" - + " \"bool\": {\n" - + " \"must\": [\n" - + " {\n" - + " \"term\": {\n" - + " \"policy\": \"" - + policyName - + "\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"term\": {\n" - + " \"index\": \"" - + indexName - + "\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + "}" - ); + allResults.setJsonEntity(""" + { + "query": { + "bool": { + "must": [ + { + "term": { + "policy": "%s" + } + }, + { + "term": { + "index": "%s" + } + } + ] + } + } + }""".formatted(policyName, indexName)); final Response allResultsResp = client().performRequest(historySearchRequest); Map allResultsMap; try (InputStream is = allResultsResp.getEntity().getContent()) { diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java index f7842965cb7da..a1382af072bf0 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeseriesMoveToStepIT.java @@ -72,20 +72,19 @@ public void testMoveToAllocateStep() throws Exception { // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + originalIndex); assertBusy(() -> assertTrue(getStepKeyForIndex(client(), originalIndex).equals(new StepKey("new", "complete", "complete")))); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"cold\",\n" - + " \"action\": \"allocate\",\n" - + " \"name\": \"allocate\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "cold", + "action": "allocate", + "name": "allocate" + } + }"""); client().performRequest(moveToStepRequest); assertBusy(() -> assertFalse(indexExists(originalIndex))); } @@ -113,20 +112,19 @@ public void testMoveToRolloverStep() throws Exception { // index document to trigger rollover index(client(), originalIndex, "_id", "foo", "bar"); logger.info(getStepKeyForIndex(client(), originalIndex)); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"attempt-rollover\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "hot", + "action": "rollover", + "name": "attempt-rollover" + } + }"""); client().performRequest(moveToStepRequest); /* @@ -161,20 +159,19 @@ public void testMoveToInjectedStep() throws Exception { // Move to a step from the injected unfollow action Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": { \n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": { \n" - + " \"phase\": \"warm\",\n" - + " \"action\": \"unfollow\",\n" - + " \"name\": \"wait-for-indexing-complete\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "warm", + "action": "unfollow", + "name": "wait-for-indexing-complete" + } + }"""); // If we get an OK on this request we have successfully moved to the injected step assertOK(client().performRequest(moveToStepRequest)); @@ -218,20 +215,19 @@ public void testMoveToStepRereadsPolicy() throws Exception { // Move to the same step, which should re-read the policy Request moveToStepRequest = new Request("POST", "_ilm/move/test-1"); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": { \n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"check-rollover-ready\"\n" - + " },\n" - + " \"next_step\": { \n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"check-rollover-ready\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "hot", + "action": "rollover", + "name": "check-rollover-ready" + }, + "next_step": { + "phase": "hot", + "action": "rollover", + "name": "check-rollover-ready" + } + }"""); // busy asserting here as ILM moves the index from the `check-rollover-ready` step into the `error` step and back into the // `check-rollover-ready` when retrying. the `_ilm/move` api might fail when the as the `current_step` of the index might be // the `error` step at execution time. @@ -257,20 +253,19 @@ public void testMoveToStepWithInvalidNextStep() throws Exception { // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"attempt-rollover\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "hot", + "action": "rollover", + "name": "attempt-rollover" + } + }"""); assertBusy(() -> { ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); @@ -300,19 +295,18 @@ public void testMoveToStepWithoutStepName() throws Exception { // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"warm\",\n" - + " \"action\": \"forcemerge\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "warm", + "action": "forcemerge" + } + }"""); assertOK(client().performRequest(moveToStepRequest)); @@ -338,18 +332,17 @@ public void testMoveToStepWithoutAction() throws Exception { // move to a step Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"warm\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "warm" + } + }"""); assertOK(client().performRequest(moveToStepRequest)); @@ -375,19 +368,18 @@ public void testInvalidToMoveToStepWithoutActionButWithName() throws Exception { // move to a step with an invalid request Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"warm\",\n" - + " \"name\": \"forcemerge\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "warm", + "name": "forcemerge" + } + }"""); assertBusy(() -> { ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); @@ -412,25 +404,25 @@ public void testResolveToNonexistentStep() throws Exception { // move to a step with an invalid request Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"new\",\n" - + " \"action\": \"complete\",\n" - + " \"name\": \"complete\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"warm\",\n" - + " \"action\": \"shrink\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "new", + "action": "complete", + "name": "complete" + }, + "next_step": { + "phase": "warm", + "action": "shrink" + } + }"""); assertBusy(() -> { ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(moveToStepRequest)); String responseEntityAsString = EntityUtils.toString(exception.getResponse().getEntity()); - String expectedErrorMessage = "unable to determine concrete step key from target next step key: " - + "{\\\"phase\\\":\\\"warm\\\",\\\"action\\\":\\\"shrink\\\"}"; + String expectedErrorMessage = """ + unable to determine concrete step key from target next step key: \ + {\\"phase\\":\\"warm\\",\\"action\\":\\"shrink\\"}"""; assertThat(responseEntityAsString, containsStringIgnoringCase(expectedErrorMessage)); }); } diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java index a4f0a3d4aff39..6edacb78accac 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/RolloverActionIT.java @@ -95,28 +95,26 @@ public void testRolloverActionWithIndexingComplete() throws Exception { ); Request updateSettingsRequest = new Request("PUT", "/" + originalIndex + "/_settings"); - updateSettingsRequest.setJsonEntity( - "{\n" + " \"settings\": {\n" + " \"" + LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE + "\": true\n" + " }\n" + "}" - ); + updateSettingsRequest.setJsonEntity(""" + { + "settings": { + "%s": true + } + }""".formatted(LifecycleSettings.LIFECYCLE_INDEXING_COMPLETE)); client().performRequest(updateSettingsRequest); Request updateAliasRequest = new Request("POST", "/_aliases"); - updateAliasRequest.setJsonEntity( - "{\n" - + " \"actions\": [\n" - + " {\n" - + " \"add\": {\n" - + " \"index\": \"" - + originalIndex - + "\",\n" - + " \"alias\": \"" - + alias - + "\",\n" - + " \"is_write_index\": false\n" - + " }\n" - + " }\n" - + " ]\n" - + "}" - ); + updateAliasRequest.setJsonEntity(""" + { + "actions": [ + { + "add": { + "index": "%s", + "alias": "%s", + "is_write_index": false + } + } + ] + }""".formatted(originalIndex, alias)); client().performRequest(updateAliasRequest); // create policy @@ -188,9 +186,11 @@ public void testILMRolloverRetriesOnReadOnlyBlock() throws Exception { // remove the read only block Request allowWritesOnIndexSettingUpdate = new Request("PUT", firstIndex + "/_settings"); - allowWritesOnIndexSettingUpdate.setJsonEntity( - "{" + " \"index\": {\n" + " \"blocks.read_only\" : \"false\" \n" + " }\n" + "}" - ); + allowWritesOnIndexSettingUpdate.setJsonEntity(""" + { "index": { + "blocks.read_only" : "false"\s + } + }"""); client().performRequest(allowWritesOnIndexSettingUpdate); // index is not readonly so the ILM should complete successfully @@ -205,23 +205,16 @@ public void testILMRolloverOnManuallyRolledIndex() throws Exception { // Set up a policy with rollover createNewSingletonPolicy(client(), policy, "hot", new RolloverAction(null, null, null, 2L)); Request createIndexTemplate = new Request("PUT", "_template/rolling_indexes"); - createIndexTemplate.setJsonEntity( - "{" - + "\"index_patterns\": [\"" - + index - + "-*\"], \n" - + " \"settings\": {\n" - + " \"number_of_shards\": 1,\n" - + " \"number_of_replicas\": 0,\n" - + " \"index.lifecycle.name\": \"" - + policy - + "\", \n" - + " \"index.lifecycle.rollover_alias\": \"" - + alias - + "\"\n" - + " }\n" - + "}" - ); + createIndexTemplate.setJsonEntity(""" + { + "index_patterns": ["%s-*"], + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0, + "index.lifecycle.name": "%s", + "index.lifecycle.rollover_alias": "%s" + } + }""".formatted(index, policy, alias)); createIndexTemplate.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createIndexTemplate); @@ -301,20 +294,19 @@ public void testRolloverStepRetriesUntilRolledOverIndexIsDeleted() throws Except ); Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"check-rollover-ready\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"attempt-rollover\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "hot", + "action": "rollover", + "name": "check-rollover-ready" + }, + "next_step": { + "phase": "hot", + "action": "rollover", + "name": "attempt-rollover" + } + }"""); // Using {@link #waitUntil} here as ILM moves back and forth between the {@link WaitForRolloverReadyStep} step and // {@link org.elasticsearch.xpack.core.ilm.ErrorStep} in order to retry the failing step. As {@link #assertBusy} @@ -370,20 +362,19 @@ public void testUpdateRolloverLifecycleDateStepRetriesWhenRolloverInfoIsMissing( // moving ILM to the "update-rollover-lifecycle-date" without having gone through the actual rollover step // the "update-rollover-lifecycle-date" step will fail as the index has no rollover information Request moveToStepRequest = new Request("POST", "_ilm/move/" + index); - moveToStepRequest.setJsonEntity( - "{\n" - + " \"current_step\": {\n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"check-rollover-ready\"\n" - + " },\n" - + " \"next_step\": {\n" - + " \"phase\": \"hot\",\n" - + " \"action\": \"rollover\",\n" - + " \"name\": \"update-rollover-lifecycle-date\"\n" - + " }\n" - + "}" - ); + moveToStepRequest.setJsonEntity(""" + { + "current_step": { + "phase": "hot", + "action": "rollover", + "name": "check-rollover-ready" + }, + "next_step": { + "phase": "hot", + "action": "rollover", + "name": "update-rollover-lifecycle-date" + } + }"""); client().performRequest(moveToStepRequest); assertBusy( diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java index cc7781d598f35..c58fecc26dd39 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/ShrinkActionIT.java @@ -196,25 +196,16 @@ public void testShrinkActionInTheHotPhase() throws Exception { // and a template Request createTemplateRequest = new Request("PUT", "_template/" + index); - createTemplateRequest.setJsonEntity( - "{" - + "\"index_patterns\": [\"" - + index - + "-*\"], \n" - + " \"settings\": {\n" - + " \"number_of_shards\": " - + numShards - + ",\n" - + " \"number_of_replicas\": 0,\n" - + " \"index.lifecycle.name\": \"" - + policy - + "\", \n" - + " \"index.lifecycle.rollover_alias\": \"" - + alias - + "\"\n" - + " }\n" - + "}" - ); + createTemplateRequest.setJsonEntity(""" + { + "index_patterns": ["%s-*"], + "settings": { + "number_of_shards": %s, + "number_of_replicas": 0, + "index.lifecycle.name": "%s", + "index.lifecycle.rollover_alias": "%s" + } + }""".formatted(index, numShards, policy, alias)); createTemplateRequest.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); client().performRequest(createTemplateRequest); @@ -248,9 +239,12 @@ public void testSetSingleNodeAllocationRetriesUntilItSucceeds() throws Exception // unallocate all index shards Request setAllocationToMissingAttribute = new Request("PUT", "/" + index + "/_settings"); - setAllocationToMissingAttribute.setJsonEntity( - "{\n" + " \"settings\": {\n" + " \"index.routing.allocation.include.rack\": \"bogus_rack\"" + " }\n" + "}" - ); + setAllocationToMissingAttribute.setJsonEntity(""" + { + "settings": { + "index.routing.allocation.include.rack": "bogus_rack" + } + }"""); client().performRequest(setAllocationToMissingAttribute); ensureHealth(index, (request) -> { @@ -292,9 +286,11 @@ public void testSetSingleNodeAllocationRetriesUntilItSucceeds() throws Exception }, 30, TimeUnit.SECONDS)); Request resetAllocationForIndex = new Request("PUT", "/" + index + "/_settings"); - resetAllocationForIndex.setJsonEntity( - "{\n" + " \"settings\": {\n" + " \"index.routing.allocation.include.rack\": null" + " }\n" + "}" - ); + resetAllocationForIndex.setJsonEntity(""" + { + "settings": { + "index.routing.allocation.include.rack": null } + }"""); client().performRequest(resetAllocationForIndex); String shrunkenIndex = waitAndGetShrinkIndexName(client(), index); diff --git a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java index b0c05737c5d72..05c0c77464a98 100644 --- a/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java +++ b/x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java @@ -498,13 +498,14 @@ public void testDataStreams() throws Exception { String repoId = "ds-repo"; String policyName = "ds-policy"; - String mapping = "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "@timestamp": { + "type": "date" + } + } + }"""; Template template = new Template(null, new CompressedXContent(mapping), null); createComposableTemplate(client(), "ds-template", dataStreamName, template); @@ -668,44 +669,35 @@ private Map getSLMStats() { @SuppressWarnings("unchecked") private void assertHistoryIsPresent(String policyName, boolean success, String repository, String operation) throws IOException { final Request historySearchRequest = new Request("GET", ".slm-history*/_search"); - historySearchRequest.setJsonEntity( - "{\n" - + " \"query\": {\n" - + " \"bool\": {\n" - + " \"must\": [\n" - + " {\n" - + " \"term\": {\n" - + " \"policy\": \"" - + policyName - + "\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"term\": {\n" - + " \"success\": " - + success - + "\n" - + " }\n" - + " },\n" - + " {\n" - + " \"term\": {\n" - + " \"repository\": \"" - + repository - + "\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"term\": {\n" - + " \"operation\": \"" - + operation - + "\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + "}" - ); + historySearchRequest.setJsonEntity(""" + { + "query": { + "bool": { + "must": [ + { + "term": { + "policy": "%s" + } + }, + { + "term": { + "success": %s + } + }, + { + "term": { + "repository": "%s" + } + }, + { + "term": { + "operation": "%s" + } + } + ] + } + } + }""".formatted(policyName, success, repository, operation)); Response historyResponse; try { historyResponse = client().performRequest(historySearchRequest); diff --git a/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java b/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java index c714ab1a7e59d..d8ba9b720d32a 100644 --- a/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java +++ b/x-pack/plugin/ilm/qa/with-security/src/javaRestTest/java/org/elasticsearch/xpack/security/PermissionsIT.java @@ -70,7 +70,8 @@ @SuppressWarnings("removal") public class PermissionsIT extends ESRestTestCase { - private static final String jsonDoc = "{ \"name\" : \"elasticsearch\", \"body\": \"foo bar\" }"; + private static final String jsonDoc = """ + {"name" : "elasticsearch", "body": "foo bar" }"""; private String deletePolicy = "deletePolicy"; private Settings indexSettingsWithPolicy; @@ -170,13 +171,20 @@ public void testSLMWithPermissions() throws Exception { // Set up two roles and users, one for reading SLM, another for managing SLM Request roleRequest = new Request("PUT", "/_security/role/slm-read"); - roleRequest.setJsonEntity("{ \"cluster\": [\"read_slm\"] }"); + roleRequest.setJsonEntity(""" + { "cluster": ["read_slm"] }"""); assertOK(adminClient().performRequest(roleRequest)); roleRequest = new Request("PUT", "/_security/role/slm-manage"); - roleRequest.setJsonEntity( - "{ \"cluster\": [\"manage_slm\", \"cluster:admin/repository/*\", \"cluster:admin/snapshot/*\"]," - + "\"indices\": [{ \"names\": [\".slm-history*\"],\"privileges\": [\"all\"] }] }" - ); + roleRequest.setJsonEntity(""" + { + "cluster": [ "manage_slm", "cluster:admin/repository/*", "cluster:admin/snapshot/*" ], + "indices": [ + { + "names": [ ".slm-history*" ], + "privileges": [ "all" ] + } + ] + }"""); assertOK(adminClient().performRequest(roleRequest)); createUser("slm_admin", "slm-admin-password", "slm-manage"); @@ -353,7 +361,10 @@ private void createNewSingletonPolicy(RestClient client, String policy, String p private void createIndexAsAdmin(String name, Settings settings, String mapping) throws IOException { Request request = new Request("PUT", "/" + name); - request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings) + ", \"mappings\" : {" + mapping + "} }"); + request.setJsonEntity(""" + { + "settings": %s, "mappings" : {%s} + }""".formatted(Strings.toString(settings), mapping)); assertOK(adminClient().performRequest(request)); } @@ -365,23 +376,18 @@ private void createIndexAsAdmin(String name, String alias, boolean isWriteIndex) private void createIndexTemplate(String name, String pattern, String alias, String policy) throws IOException { Request request = new Request("PUT", "/_template/" + name); - request.setJsonEntity( - "{\n" - + " \"index_patterns\": [\"" - + pattern - + "\"],\n" - + " \"settings\": {\n" - + " \"number_of_shards\": 1,\n" - + " \"number_of_replicas\": 0,\n" - + " \"index.lifecycle.name\": \"" - + policy - + "\",\n" - + " \"index.lifecycle.rollover_alias\": \"" - + alias - + "\"\n" - + " }\n" - + " }" - ); + request.setJsonEntity(""" + { + "index_patterns": [ + "%s" + ], + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0, + "index.lifecycle.name": "%s", + "index.lifecycle.rollover_alias": "%s" + } + }""".formatted(pattern, policy, alias)); request.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); assertOK(adminClient().performRequest(request)); } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java index d4a4a4729c69f..71af4f83ace51 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/cluster/metadata/MetadataMigrateToDataTiersRoutingServiceTests.java @@ -920,51 +920,49 @@ private LifecyclePolicyMetadata getWarmColdPolicyMeta( } private String getWarmPhaseDef() { - return "{\n" - + " \"policy\" : \"" - + lifecycleName - + "\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"0m\",\n" - + " \"actions\" : {\n" - + " \"allocate\" : {\n" - + " \"number_of_replicas\" : \"0\",\n" - + " \"require\" : {\n" - + " \"data\": \"cold\"\n" - + " }\n" - + " },\n" - + " \"set_priority\": {\n" - + " \"priority\": 100 \n" - + " },\n" - + " \"shrink\": {\n" - + " \"number_of_shards\": 2 \n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }"; + return """ + { + "policy": "%s", + "phase_definition": { + "min_age": "0m", + "actions": { + "allocate": { + "number_of_replicas": "0", + "require": { + "data": "cold" + } + }, + "set_priority": { + "priority": 100 + }, + "shrink": { + "number_of_shards": 2 + } + } + }, + "version": 1, + "modified_date_in_millis": 1578521007076 + }""".formatted(lifecycleName); } private String getColdPhaseDefinition() { - return "{\n" - + " \"policy\" : \"" - + lifecycleName - + "\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"0m\",\n" - + " \"actions\" : {\n" - + " \"allocate\" : {\n" - + " \"number_of_replicas\" : \"0\",\n" - + " \"require\" : {\n" - + " \"data\": \"cold\"\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }"; + return """ + { + "policy": "%s", + "phase_definition": { + "min_age": "0m", + "actions": { + "allocate": { + "number_of_replicas": "0", + "require": { + "data": "cold" + } + } + } + }, + "version": 1, + "modified_date_in_millis": 1578521007076 + }""".formatted(lifecycleName); } @SuppressWarnings("unchecked") diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java index 85d6e5e99afe1..cc4fb27781ec7 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/ExecuteStepsUpdateTaskTests.java @@ -302,7 +302,8 @@ public void testClusterActionStepThrowsException() throws Exception { assertNull(task.getNextStepKey()); assertThat(lifecycleState.getPhaseTime(), nullValue()); assertThat(lifecycleState.getActionTime(), nullValue()); - assertThat(lifecycleState.getStepInfo(), containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\"")); + assertThat(lifecycleState.getStepInfo(), containsString(""" + {"type":"runtime_exception","reason":"error","stack_trace":\"""")); } public void testClusterWaitStepThrowsException() throws Exception { @@ -320,7 +321,8 @@ public void testClusterWaitStepThrowsException() throws Exception { assertThat(secondStep.getExecuteCount(), equalTo(1L)); assertThat(lifecycleState.getPhaseTime(), nullValue()); assertThat(lifecycleState.getActionTime(), nullValue()); - assertThat(lifecycleState.getStepInfo(), containsString("{\"type\":\"runtime_exception\",\"reason\":\"error\",\"stack_trace\":\"")); + assertThat(lifecycleState.getStepInfo(), containsString(""" + {"type":"runtime_exception","reason":"error","stack_trace":\"""")); } private void setStateToKey(StepKey stepKey) throws IOException { diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java index 224a721c19e44..bb7147c60d333 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java @@ -361,17 +361,19 @@ public void testRunStateChangePolicyWithNextStep() throws Exception { .stream() .findFirst() .orElseThrow(() -> new AssertionError("failed to register ILM history")); - assertThat( - historyItem.toString(), - containsString( - "{\"index\":\"test\",\"policy\":\"foo\",\"@timestamp\":" - + stepTime - + ",\"success\":true,\"state\":{\"phase\":\"phase\",\"action\":\"action\"," - + "\"step\":\"next_cluster_state_action_step\",\"step_time\":\"" - + stepTime - + "\"}}" - ) - ); + assertThat(historyItem.toString(), containsString(""" + { + "index": "test", + "policy": "foo", + "@timestamp": %s, + "success": true, + "state": { + "phase": "phase", + "action": "action", + "step": "next_cluster_state_action_step", + "step_time": "%s" + } + }""".formatted(stepTime, stepTime).replaceAll("\\s", ""))); } public void testRunPeriodicPolicyWithFailureToReadPolicy() throws Exception { @@ -556,13 +558,19 @@ public void testRunStateChangePolicyWithAsyncActionNextStep() throws Exception { .stream() .findFirst() .orElseThrow(() -> new AssertionError("failed to register ILM history")); - assertThat( - historyItem.toString(), - containsString( - "{\"index\":\"test\",\"policy\":\"foo\",\"@timestamp\":0,\"success\":true," - + "\"state\":{\"phase\":\"phase\",\"action\":\"action\",\"step\":\"async_action_step\",\"step_time\":\"0\"}}" - ) - ); + assertThat(historyItem.toString(), containsString(""" + { + "index": "test", + "policy": "foo", + "@timestamp": 0, + "success": true, + "state": { + "phase": "phase", + "action": "action", + "step": "async_action_step", + "step_time": "0" + } + }""".replaceAll("\\s", ""))); } public void testRunPeriodicStep() throws Exception { @@ -642,10 +650,8 @@ public void testRunPolicyClusterStateActionStep() { final ExecuteStepsUpdateTaskMatcher taskMatcher = new ExecuteStepsUpdateTaskMatcher(indexMetadata.getIndex(), policyName, step); Mockito.verify(clusterService, Mockito.times(1)) .submitStateUpdateTask( - Mockito.eq( - "ilm-execute-cluster-state-steps [{\"phase\":\"phase\",\"action\":\"action\"," - + "\"name\":\"cluster_state_action_step\"} => null]" - ), + Mockito.eq(""" + ilm-execute-cluster-state-steps [{"phase":"phase","action":"action","name":"cluster_state_action_step"} => null]"""), Mockito.argThat(taskMatcher), eq(IndexLifecycleRunner.ILM_TASK_CONFIG), any(), @@ -672,10 +678,8 @@ public void testRunPolicyClusterStateWaitStep() { final ExecuteStepsUpdateTaskMatcher taskMatcher = new ExecuteStepsUpdateTaskMatcher(indexMetadata.getIndex(), policyName, step); Mockito.verify(clusterService, Mockito.times(1)) .submitStateUpdateTask( - Mockito.eq( - "ilm-execute-cluster-state-steps [{\"phase\":\"phase\",\"action\":\"action\"," - + "\"name\":\"cluster_state_action_step\"} => null]" - ), + Mockito.eq(""" + ilm-execute-cluster-state-steps [{"phase":"phase","action":"action","name":"cluster_state_action_step"} => null]"""), Mockito.argThat(taskMatcher), eq(IndexLifecycleRunner.ILM_TASK_CONFIG), any(), diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java index e2f3c7f9b1768..9db1d92e5001b 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleTransitionTests.java @@ -16,6 +16,7 @@ import org.elasticsearch.cluster.metadata.Metadata; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.Index; import org.elasticsearch.test.ESTestCase; @@ -307,13 +308,9 @@ public void testValidatedMoveClusterStateToNextStepInvalidNextStep() { IllegalArgumentException.class, () -> IndexLifecycleTransition.moveClusterStateToStep(index, clusterState, nextStepKey, () -> now, stepRegistry, true) ); - assertThat( - exception.getMessage(), - equalTo( - "step [{\"phase\":\"next_phase\",\"action\":\"next_action\",\"name\":\"next_step\"}] " - + "for index [my_index] with policy [my_policy] does not exist" - ) - ); + assertThat(exception.getMessage(), equalTo(""" + step [{"phase":"next_phase","action":"next_action","name":"next_step"}] \ + for index [my_index] with policy [my_policy] does not exist""")); } public void testMoveClusterStateToErrorStep() throws IOException { @@ -337,14 +334,8 @@ public void testMoveClusterStateToErrorStep() throws IOException { () -> now, (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey) ); - assertClusterStateOnErrorStep( - clusterState, - index, - currentStep, - newClusterState, - now, - "{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\"" - ); + assertClusterStateOnErrorStep(clusterState, index, currentStep, newClusterState, now, """ + {"type":"exception","reason":"THIS IS AN EXPECTED CAUSE\""""); cause = new IllegalArgumentException("non elasticsearch-exception"); newClusterState = IndexLifecycleTransition.moveClusterStateToErrorStep( @@ -354,14 +345,8 @@ public void testMoveClusterStateToErrorStep() throws IOException { () -> now, (idxMeta, stepKey) -> new MockStep(stepKey, nextStepKey) ); - assertClusterStateOnErrorStep( - clusterState, - index, - currentStep, - newClusterState, - now, - "{\"type\":\"illegal_argument_exception\",\"reason\":\"non elasticsearch-exception\",\"stack_trace\":\"" - ); + assertClusterStateOnErrorStep(clusterState, index, currentStep, newClusterState, now, """ + {"type":"illegal_argument_exception","reason":"non elasticsearch-exception","stack_trace":\""""); } public void testAddStepInfoToClusterState() throws IOException { @@ -585,24 +570,23 @@ public void testValidateTransitionToCachedStepMissingFromPolicy() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ); + .setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }"""); IndexMetadata meta = buildIndexMetadata("my-policy", executionState); @@ -828,29 +812,28 @@ public void testMoveClusterStateToPreviouslyFailedStepAsAutomaticRetry() { assertThat(executionState.getFailedStepRetryCount(), is(1)); } - public void testRefreshPhaseJson() { + public void testRefreshPhaseJson() throws IOException { LifecycleExecutionState.Builder exState = LifecycleExecutionState.builder() .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ); + .setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }"""); IndexMetadata meta = buildIndexMetadata("my-policy", exState); String index = meta.getIndex().getName(); @@ -879,13 +862,23 @@ public void testRefreshPhaseJson() { assertThat(beforeState, equalTo(afterState)); // Check that the phase definition has been refreshed - assertThat( - afterExState.getPhaseDefinition(), - equalTo( - "{\"policy\":\"my-policy\",\"phase_definition\":{\"min_age\":\"0ms\",\"actions\":{\"rollover\":{\"max_docs\":1}," - + "\"set_priority\":{\"priority\":100}}},\"version\":2,\"modified_date_in_millis\":2}" - ) - ); + assertThat(afterExState.getPhaseDefinition(), equalTo(XContentHelper.stripWhitespace(""" + { + "policy": "my-policy", + "phase_definition": { + "min_age": "0ms", + "actions": { + "rollover": { + "max_docs": 1 + }, + "set_priority": { + "priority": 100 + } + } + }, + "version": 2, + "modified_date_in_millis": 2 + }"""))); } public void testEligibleForRefresh() { @@ -976,24 +969,23 @@ public void testMoveStateToNextActionAndUpdateCachedPhase() { .setPhase("hot") .setAction("rollover") .setStep("check-rollover-ready") - .setPhaseDefinition( - "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " },\n" - + " \"set_priority\" : {\n" - + " \"priority\" : 150\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }" - ); + .setPhaseDefinition(""" + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + }, + "set_priority" : { + "priority" : 150 + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }"""); IndexMetadata meta = buildIndexMetadata("my-policy", currentExecutionState); diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java index e2679fe02afb0..6b1accd915cc9 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/MoveToErrorStepUpdateTaskTests.java @@ -102,10 +102,8 @@ public void testExecuteSuccessfullyMoved() throws IOException { ElasticsearchException.generateThrowableXContent(causeXContentBuilder, ToXContent.EMPTY_PARAMS, cause); causeXContentBuilder.endObject(); String expectedCauseValue = BytesReference.bytes(causeXContentBuilder).utf8ToString(); - assertThat( - lifecycleState.getStepInfo(), - containsString("{\"type\":\"exception\",\"reason\":\"THIS IS AN EXPECTED CAUSE\",\"stack_trace\":\"") - ); + assertThat(lifecycleState.getStepInfo(), containsString(""" + {"type":"exception","reason":"THIS IS AN EXPECTED CAUSE","stack_trace":\"""")); } public void testExecuteNoopDifferentStep() throws IOException { diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java index a252ea344be4a..1d255b55f32d6 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/action/TransportExplainLifecycleActionTests.java @@ -35,19 +35,20 @@ public class TransportExplainLifecycleActionTests extends ESTestCase { - public static final String PHASE_DEFINITION = "{\n" - + " \"policy\" : \"my-policy\",\n" - + " \"phase_definition\" : {\n" - + " \"min_age\" : \"20m\",\n" - + " \"actions\" : {\n" - + " \"rollover\" : {\n" - + " \"max_age\" : \"5s\"\n" - + " }\n" - + " }\n" - + " },\n" - + " \"version\" : 1,\n" - + " \"modified_date_in_millis\" : 1578521007076\n" - + " }"; + public static final String PHASE_DEFINITION = """ + { + "policy" : "my-policy", + "phase_definition" : { + "min_age" : "20m", + "actions" : { + "rollover" : { + "max_age" : "5s" + } + } + }, + "version" : 1, + "modified_date_in_millis" : 1578521007076 + }"""; private static final NamedXContentRegistry REGISTRY; diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java index d888154aa064b..2e8c578f98b0d 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/history/ILMHistoryItemTests.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.ilm.history; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; @@ -63,50 +64,51 @@ public void testToXContent() throws IOException { try (XContentBuilder builder = jsonBuilder()) { success.toXContent(builder, ToXContent.EMPTY_PARAMS); String json = Strings.toString(builder); - assertThat( - json, - equalTo( - "{\"index\":\"index\"," - + "\"policy\":\"policy\"," - + "\"@timestamp\":1234," - + "\"index_age\":100," - + "\"success\":true," - + "\"state\":{\"phase\":\"phase\"," - + "\"phase_definition\":\"{}\"," - + "\"action_time\":\"20\"," - + "\"phase_time\":\"10\"," - + "\"step_info\":\"{\\\"step_info\\\": \\\"foo\\\"\",\"action\":\"action\",\"step\":\"step\",\"step_time\":\"30\"}}" - ) - ); + assertThat(json, equalTo(XContentHelper.stripWhitespace(""" + { + "index": "index", + "policy": "policy", + "@timestamp": 1234, + "index_age": 100, + "success": true, + "state": { + "phase": "phase", + "phase_definition": "{}", + "action_time": "20", + "phase_time": "10", + "step_info": "{\\"step_info\\": \\"foo\\"", + "action": "action", + "step": "step", + "step_time": "30" + } + }"""))); } try (XContentBuilder builder = jsonBuilder()) { failure.toXContent(builder, ToXContent.EMPTY_PARAMS); String json = Strings.toString(builder); - assertThat( - json, - startsWith( - "{\"index\":\"index\"," - + "\"policy\":\"policy\"," - + "\"@timestamp\":1234," - + "\"index_age\":100," - + "\"success\":false," - + "\"state\":{\"phase\":\"phase\"," - + "\"failed_step\":\"step\"," - + "\"phase_definition\":\"{\\\"phase_json\\\": \\\"eggplant\\\"}\"," - + "\"action_time\":\"20\"," - + "\"is_auto_retryable_error\":\"true\"," - + "\"failed_step_retry_count\":\"7\"," - + "\"phase_time\":\"10\"," - + "\"step_info\":\"{\\\"step_info\\\": \\\"foo\\\"\"," - + "\"action\":\"action\"," - + "\"step\":\"ERROR\"," - + "\"step_time\":\"30\"}," - + "\"error_details\":\"{\\\"type\\\":\\\"illegal_argument_exception\\\"," - + "\\\"reason\\\":\\\"failure\\\"," - + "\\\"stack_trace\\\":\\\"java.lang.IllegalArgumentException: failure" - ) - ); + assertThat(json.replaceAll("\\s", ""), startsWith(""" + { + "index": "index", + "policy": "policy", + "@timestamp": 1234, + "index_age": 100, + "success": false, + "state": { + "phase": "phase", + "failed_step": "step", + "phase_definition": "{\\"phase_json\\": \\"eggplant\\"}", + "action_time": "20", + "is_auto_retryable_error": "true", + "failed_step_retry_count": "7", + "phase_time": "10", + "step_info": "{\\"step_info\\": \\"foo\\"", + "action": "action", + "step": "ERROR", + "step_time": "30" + }, + "error_details": "{\\"type\\":\\"illegal_argument_exception\\",\\"reason\\":\\"failure\\",\ + \\"stack_trace\\":\\"java.lang.IllegalArgumentException: failure""".replaceAll("\\s", ""))); } } } diff --git a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java index 83a94020f95bd..be825d7008163 100644 --- a/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java +++ b/x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleTaskTests.java @@ -136,32 +136,29 @@ public void testCreateSnapshotOnTrigger() { .build(); final ThreadPool threadPool = new TestThreadPool("test"); - final String createSnapResponse = "{" - + " \"snapshot\" : {" - + " \"snapshot\" : \"snapshot_1\"," - + " \"uuid\" : \"bcP3ClgCSYO_TP7_FCBbBw\"," - + " \"version_id\" : " - + Version.CURRENT.id - + "," - + " \"version\" : \"" - + Version.CURRENT - + "\"," - + " \"indices\" : [ ]," - + " \"include_global_state\" : true," - + " \"state\" : \"SUCCESS\"," - + " \"start_time\" : \"2019-03-19T22:19:53.542Z\"," - + " \"start_time_in_millis\" : 1553033993542," - + " \"end_time\" : \"2019-03-19T22:19:53.567Z\"," - + " \"end_time_in_millis\" : 1553033993567," - + " \"duration_in_millis\" : 25," - + " \"failures\" : [ ]," - + " \"shards\" : {" - + " \"total\" : 0," - + " \"failed\" : 0," - + " \"successful\" : 0" - + " }" - + " }" - + "}"; + final String createSnapResponse = """ + { + "snapshot": { + "snapshot": "snapshot_1", + "uuid": "bcP3ClgCSYO_TP7_FCBbBw", + "version_id": %s, + "version": "%s", + "indices": [], + "include_global_state": true, + "state": "SUCCESS", + "start_time": "2019-03-19T22:19:53.542Z", + "start_time_in_millis": 1553033993542, + "end_time": "2019-03-19T22:19:53.567Z", + "end_time_in_millis": 1553033993567, + "duration_in_millis": 25, + "failures": [], + "shards": { + "total": 0, + "failed": 0, + "successful": 0 + } + } + }""".formatted(Version.CURRENT.id, Version.CURRENT); final AtomicBoolean clientCalled = new AtomicBoolean(false); final SetOnce snapshotName = new SetOnce<>(); diff --git a/x-pack/plugin/logstash/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/LogstashSystemIndexIT.java b/x-pack/plugin/logstash/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/LogstashSystemIndexIT.java index ff583f6dd90ba..bf7f88d0fc86b 100644 --- a/x-pack/plugin/logstash/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/LogstashSystemIndexIT.java +++ b/x-pack/plugin/logstash/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/LogstashSystemIndexIT.java @@ -158,7 +158,11 @@ private String getPipelineJson(String date) throws IOException { } builder.endObject(); builder.field("username", "john.doe"); - builder.field("pipeline", "\"input\": {},\n \"filter\": {},\n \"output\": {}\n"); + builder.field("pipeline", """ + "input": {}, + "filter": {}, + "output": {} + """); builder.startObject("pipeline_settings"); { builder.field("pipeline.batch.delay", 50); diff --git a/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java b/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java index f4998e63b297b..3a823415ddd03 100644 --- a/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java +++ b/x-pack/plugin/ml/qa/basic-multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlBasicMultiNodeIT.java @@ -73,8 +73,9 @@ public void testMiniFarequote() throws Exception { Request addData = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_data"); addData.setEntity( new NStringEntity( - "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" - + "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}", + """ + {"airline":"AAL","responsetime":"132.2046","sourcetype":"farequote","time":"1403481600"} + {"airline":"JZA","responsetime":"990.4628","sourcetype":"farequote","time":"1403481700"}""", randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")) ) ); @@ -166,11 +167,12 @@ public void testMiniFarequoteReopen() throws Exception { Request addDataRequest = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_data"); addDataRequest.setEntity( new NStringEntity( - "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" - + "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}\n" - + "{\"airline\":\"JBU\",\"responsetime\":\"877.5927\",\"sourcetype\":\"farequote\",\"time\":\"1403481800\"}\n" - + "{\"airline\":\"KLM\",\"responsetime\":\"1355.4812\",\"sourcetype\":\"farequote\",\"time\":\"1403481900\"}\n" - + "{\"airline\":\"NKS\",\"responsetime\":\"9991.3981\",\"sourcetype\":\"farequote\",\"time\":\"1403482000\"}", + """ + {"airline":"AAL","responsetime":"132.2046","sourcetype":"farequote","time":"1403481600"} + {"airline":"JZA","responsetime":"990.4628","sourcetype":"farequote","time":"1403481700"} + {"airline":"JBU","responsetime":"877.5927","sourcetype":"farequote","time":"1403481800"} + {"airline":"KLM","responsetime":"1355.4812","sourcetype":"farequote","time":"1403481900"} + {"airline":"NKS","responsetime":"9991.3981","sourcetype":"farequote","time":"1403482000"}""", randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")) ) ); @@ -209,11 +211,12 @@ public void testMiniFarequoteReopen() throws Exception { Request addDataRequest2 = new Request("POST", BASE_PATH + "anomaly_detectors/" + jobId + "/_data"); addDataRequest2.setEntity( new NStringEntity( - "{\"airline\":\"AAL\",\"responsetime\":\"136.2361\",\"sourcetype\":\"farequote\",\"time\":\"1407081600\"}\n" - + "{\"airline\":\"VRD\",\"responsetime\":\"282.9847\",\"sourcetype\":\"farequote\",\"time\":\"1407081700\"}\n" - + "{\"airline\":\"JAL\",\"responsetime\":\"493.0338\",\"sourcetype\":\"farequote\",\"time\":\"1407081800\"}\n" - + "{\"airline\":\"UAL\",\"responsetime\":\"8.4275\",\"sourcetype\":\"farequote\",\"time\":\"1407081900\"}\n" - + "{\"airline\":\"FFT\",\"responsetime\":\"221.8693\",\"sourcetype\":\"farequote\",\"time\":\"1407082000\"}", + """ + {"airline":"AAL","responsetime":"136.2361","sourcetype":"farequote","time":"1407081600"} + {"airline":"VRD","responsetime":"282.9847","sourcetype":"farequote","time":"1407081700"} + {"airline":"JAL","responsetime":"493.0338","sourcetype":"farequote","time":"1407081800"} + {"airline":"UAL","responsetime":"8.4275","sourcetype":"farequote","time":"1407081900"} + {"airline":"FFT","responsetime":"221.8693","sourcetype":"farequote","time":"1407082000"}""", randomFrom(ContentType.APPLICATION_JSON, ContentType.create("application/x-ndjson")) ) ); @@ -541,21 +544,23 @@ private void createAndIndexFarequote() throws Exception { String dateFormat = datesHaveNanoSecondResolution ? "strict_date_optional_time_nanos" : "strict_date_optional_time"; String randomNanos = datesHaveNanoSecondResolution ? "," + randomIntBetween(100000000, 999999999) : ""; Request createAirlineDataRequest = new Request("PUT", "/airline-data"); - createAirlineDataRequest.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time\": { \"type\":\"" - + dateMappingType - + "\", \"format\":\"" - + dateFormat - + "\"}," - + " \"airline\": { \"type\":\"keyword\"}," - + " \"responsetime\": { \"type\":\"float\"}" - + " }" - + " }" - + "}" - ); + createAirlineDataRequest.setJsonEntity(""" + { + "mappings": { + "properties": { + "time": { + "type": "%s", + "format": "%s" + }, + "airline": { + "type": "keyword" + }, + "responsetime": { + "type": "float" + } + } + } + }""".formatted(dateMappingType, dateFormat)); client().performRequest(createAirlineDataRequest); Request airlineData1 = new Request("PUT", "/airline-data/_doc/1"); airlineData1.setJsonEntity("{\"time\":\"2016-06-01T00:00:00" + randomNanos + "Z\",\"airline\":\"AAA\",\"responsetime\":135.22}"); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java index 0d9ab24b351a0..9f33fc1d862e9 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/CategorizationIT.java @@ -410,14 +410,9 @@ public void testStopOnWarn() throws IOException { for (int docNum = 0; docNum < 200; ++docNum) { // Two thirds of our messages are "Node 1 started", the rest "Failed to shutdown" int partitionNum = (docNum % 3) / 2; - json.append( - String.format( - Locale.ROOT, - "{\"time\":1000000,\"part\":\"%s\",\"msg\":\"%s\"}\n", - partitions[partitionNum], - messages[partitionNum] - ) - ); + json.append(String.format(Locale.ROOT, """ + {"time":1000000,"part":"%s","msg":"%s"} + """, partitions[partitionNum], messages[partitionNum])); } postData(jobId, json.toString()); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java index a255b72aefd8e..5fa54e07f1eaa 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationEvaluationWithSecurityIT.java @@ -35,9 +35,9 @@ protected Settings restClientSettings() { private static void setupDataAccessRole(String index) throws IOException { Request request = new Request("PUT", "/_security/role/test_data_access"); - request.setJsonEntity( - "{" + " \"indices\" : [" + " { \"names\": [\"" + index + "\"], \"privileges\": [\"read\"] }" + " ]" + "}" - ); + request.setJsonEntity(""" + { "indices" : [ { "names": ["%s"], "privileges": ["read"] } ]} + """.formatted(index)); client().performRequest(request); } @@ -45,23 +45,20 @@ private void setupUser(String user, List roles) throws IOException { String password = new String(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING.getChars()); Request request = new Request("PUT", "/_security/user/" + user); - request.setJsonEntity( - "{" - + " \"password\" : \"" - + password - + "\"," - + " \"roles\" : [ " - + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) - + " ]" - + "}" - ); + request.setJsonEntity(""" + { "password" : "%s", "roles" : [ %s ]} + """.formatted(password, roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")))); client().performRequest(request); } public void testEvaluate_withSecurity() throws Exception { String index = "test_data"; Request createDoc = new Request("POST", index + "/_doc"); - createDoc.setJsonEntity("{\n" + " \"is_outlier\": 0.0,\n" + " \"ml.outlier_score\": 1.0\n" + "}"); + createDoc.setJsonEntity(""" + { + "is_outlier": 0.0, + "ml.outlier_score": 1.0 + }"""); client().performRequest(createDoc); Request refreshRequest = new Request("POST", index + "/_refresh"); client().performRequest(refreshRequest); @@ -82,19 +79,17 @@ public void testEvaluate_withSecurity() throws Exception { private static Request buildRegressionEval(String index, String primaryHeader, String secondaryHeader) { Request evaluateRequest = new Request("POST", "_ml/data_frame/_evaluate"); - evaluateRequest.setJsonEntity( - "{\n" - + " \"index\": \"" - + index - + "\",\n" - + " \"evaluation\": {\n" - + " \"regression\": {\n" - + " \"actual_field\": \"is_outlier\",\n" - + " \"predicted_field\": \"ml.outlier_score\"\n" - + " }\n" - + " }\n" - + "}\n" - ); + evaluateRequest.setJsonEntity(""" + { + "index": "%s", + "evaluation": { + "regression": { + "actual_field": "is_outlier", + "predicted_field": "ml.outlier_score" + } + } + } + """.formatted(index)); RequestOptions.Builder options = evaluateRequest.getOptions().toBuilder(); options.addHeader("Authorization", primaryHeader); options.addHeader("es-secondary-authorization", secondaryHeader); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java index 9dac95da3e45f..7d927a2f362d9 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ClassificationIT.java @@ -1096,64 +1096,56 @@ private void initialize(String jobId, boolean isDatastream) { } static void createIndex(String index, boolean isDatastream) { - String mapping = "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }," - + " \"" - + BOOLEAN_FIELD - + "\": {\n" - + " \"type\": \"boolean\"\n" - + " }," - + " \"" - + NUMERICAL_FIELD - + "\": {\n" - + " \"type\": \"double\"\n" - + " }," - + " \"" - + DISCRETE_NUMERICAL_FIELD - + "\": {\n" - + " \"type\": \"integer\"\n" - + " }," - + " \"" - + TEXT_FIELD - + "\": {\n" - + " \"type\": \"text\",\n" - + " \"fields\": {" - + " \"keyword\": {" - + " \"type\": \"keyword\"\n" - + " }" - + " }" - + " }," - + " \"" - + KEYWORD_FIELD - + "\": {\n" - + " \"type\": \"keyword\"\n" - + " }," - + " \"" - + NESTED_FIELD - + "\": {\n" - + " \"type\": \"keyword\"\n" - + " }," - + " \"" - + ALIAS_TO_KEYWORD_FIELD - + "\": {\n" - + " \"type\": \"alias\",\n" - + " \"path\": \"" - + KEYWORD_FIELD - + "\"\n" - + " }," - + " \"" - + ALIAS_TO_NESTED_FIELD - + "\": {\n" - + " \"type\": \"alias\",\n" - + " \"path\": \"" - + NESTED_FIELD - + "\"\n" - + " }" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "@timestamp": { + "type": "date" + }, + "%s": { + "type": "boolean" + }, + "%s": { + "type": "double" + }, + "%s": { + "type": "integer" + }, + "%s": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword" + } + } + }, + "%s": { + "type": "keyword" + }, + "%s": { + "type": "keyword" + }, + "%s": { + "type": "alias", + "path": "%s" + }, + "%s": { + "type": "alias", + "path": "%s" + } + } + }""".formatted( + BOOLEAN_FIELD, + NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD, + TEXT_FIELD, + KEYWORD_FIELD, + NESTED_FIELD, + ALIAS_TO_KEYWORD_FIELD, + KEYWORD_FIELD, + ALIAS_TO_NESTED_FIELD, + NESTED_FIELD + ); if (isDatastream) { try { createDataStreamAndTemplate(index, mapping); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java index 0ef2d7f20a64b..4c6b5a37de9c8 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DataFrameAnalysisCustomFeatureIT.java @@ -199,59 +199,51 @@ private void initialize(String jobId, boolean isDatastream) { } private static void createIndex(String index, boolean isDatastream) { - String mapping = "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }," - + " \"" - + BOOLEAN_FIELD - + "\": {\n" - + " \"type\": \"boolean\"\n" - + " }," - + " \"" - + NUMERICAL_FIELD - + "\": {\n" - + " \"type\": \"double\"\n" - + " }," - + " \"" - + DISCRETE_NUMERICAL_FIELD - + "\": {\n" - + " \"type\": \"unsigned_long\"\n" - + " }," - + " \"" - + TEXT_FIELD - + "\": {\n" - + " \"type\": \"text\"\n" - + " }," - + " \"" - + KEYWORD_FIELD - + "\": {\n" - + " \"type\": \"keyword\"\n" - + " }," - + " \"" - + NESTED_FIELD - + "\": {\n" - + " \"type\": \"keyword\"\n" - + " }," - + " \"" - + ALIAS_TO_KEYWORD_FIELD - + "\": {\n" - + " \"type\": \"alias\",\n" - + " \"path\": \"" - + KEYWORD_FIELD - + "\"\n" - + " }," - + " \"" - + ALIAS_TO_NESTED_FIELD - + "\": {\n" - + " \"type\": \"alias\",\n" - + " \"path\": \"" - + NESTED_FIELD - + "\"\n" - + " }" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "@timestamp": { + "type": "date" + }, + "%s": { + "type": "boolean" + }, + "%s": { + "type": "double" + }, + "%s": { + "type": "unsigned_long" + }, + "%s": { + "type": "text" + }, + "%s": { + "type": "keyword" + }, + "%s": { + "type": "keyword" + }, + "%s": { + "type": "alias", + "path": "%s" + }, + "%s": { + "type": "alias", + "path": "%s" + } + } + }""".formatted( + BOOLEAN_FIELD, + NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD, + TEXT_FIELD, + KEYWORD_FIELD, + NESTED_FIELD, + ALIAS_TO_KEYWORD_FIELD, + KEYWORD_FIELD, + ALIAS_TO_NESTED_FIELD, + NESTED_FIELD + ); if (isDatastream) { try { createDataStreamAndTemplate(index, mapping); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java index 1ea3d629d9975..cbe9ec9229437 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsIT.java @@ -119,16 +119,15 @@ public void testLookbackOnly() throws Exception { } public void testLookbackOnlyDataStream() throws Exception { - String mapping = "{\n" - + " \"properties\": {\n" - + " \"time\": {\n" - + " \"type\": \"date\"\n" - + " }," - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "time": { + "type": "date" + }, "@timestamp": { + "type": "date" + } } + }"""; createDataStreamAndTemplate("datafeed_data_stream", mapping); long numDocs = randomIntBetween(32, 2048); long now = System.currentTimeMillis(); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java index 2b5f3cd6be6fb..06e54eec4f3bc 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/DatafeedJobsRestIT.java @@ -66,17 +66,17 @@ protected boolean preserveTemplatesUponCompletion() { private static void setupDataAccessRole(String index) throws IOException { Request request = new Request("PUT", "/_security/role/test_data_access"); - request.setJsonEntity( - "{" + " \"indices\" : [" + " { \"names\": [\"" + index + "\"], \"privileges\": [\"read\"] }" + " ]" + "}" - ); + request.setJsonEntity(""" + { "indices" : [ { "names": ["%s"], "privileges": ["read"] } ]} + """.formatted(index)); client().performRequest(request); } private void setupFullAccessRole(String index) throws IOException { Request request = new Request("PUT", "/_security/role/test_data_access"); - request.setJsonEntity( - "{" + " \"indices\" : [" + " { \"names\": [\"" + index + "\"], \"privileges\": [\"all\"] }" + " ]" + "}" - ); + request.setJsonEntity(""" + { "indices" : [ { "names": ["%s"], "privileges": ["all"] } ]} + """.formatted(index)); client().performRequest(request); } @@ -84,16 +84,9 @@ private void setupUser(String user, List roles) throws IOException { String password = new String(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING.getChars()); Request request = new Request("PUT", "/_security/user/" + user); - request.setJsonEntity( - "{" - + " \"password\" : \"" - + password - + "\"," - + " \"roles\" : [ " - + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) - + " ]" - + "}" - ); + request.setJsonEntity(""" + { "password" : "%s", "roles" : [ %s ]} + """.formatted(password, roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")))); client().performRequest(request); } @@ -113,134 +106,190 @@ private void addAirlineData() throws IOException { StringBuilder bulk = new StringBuilder(); Request createEmptyAirlineDataRequest = new Request("PUT", "/airline-data-empty"); - createEmptyAirlineDataRequest.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time stamp\": { \"type\":\"date\"}," // space in 'time stamp' is intentional - + " \"airline\": { \"type\":\"keyword\"}," - + " \"responsetime\": { \"type\":\"float\"}" - + " }" - + " }" - + "}" - ); + // space in 'time stamp' is intentional + createEmptyAirlineDataRequest.setJsonEntity(""" + { + "mappings": { + "properties": { + "time stamp": { + "type": "date" + }, + "airline": { + "type": "keyword" + }, + "responsetime": { + "type": "float" + } + } + } + }"""); client().performRequest(createEmptyAirlineDataRequest); // Create index with source = enabled, doc_values = enabled, stored = false + multi-field Request createAirlineDataRequest = new Request("PUT", "/airline-data"); - createAirlineDataRequest.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"runtime\": {" - + " \"airline_lowercase_rt\": { " - + " \"type\":\"keyword\"," - + " \"script\" : { \"source\": \"emit(params._source.airline.toLowerCase())\" }" - + " }" - + " }," - + " \"properties\": {" - + " \"time stamp\": { \"type\":\"date\"}," // space in 'time stamp' is intentional - + " \"airline\": {" - + " \"type\":\"text\"," - + " \"fields\":{" - + " \"text\":{\"type\":\"text\"}," - + " \"keyword\":{\"type\":\"keyword\"}" - + " }" - + " }," - + " \"responsetime\": { \"type\":\"float\"}" - + " }" - + " }" - + "}" - ); + // space in 'time stamp' is intentional + createAirlineDataRequest.setJsonEntity(""" + { + "mappings": { + "runtime": { + "airline_lowercase_rt": { + "type": "keyword", + "script": { + "source": "emit(params._source.airline.toLowerCase())" + } + } + }, + "properties": { + "time stamp": { + "type": "date" + }, + "airline": { + "type": "text", + "fields": { + "text": { + "type": "text" + }, + "keyword": { + "type": "keyword" + } + } + }, + "responsetime": { + "type": "float" + } + } + } + }"""); client().performRequest(createAirlineDataRequest); - bulk.append("{\"index\": {\"_index\": \"airline-data\", \"_id\": 1}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:00:00Z\",\"airline\":\"AAA\",\"responsetime\":135.22}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data\", \"_id\": 2}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:59:00Z\",\"airline\":\"AAA\",\"responsetime\":541.76}\n"); + bulk.append(""" + {"index": {"_index": "airline-data", "_id": 1}} + {"time stamp":"2016-06-01T00:00:00Z","airline":"AAA","responsetime":135.22} + {"index": {"_index": "airline-data", "_id": 2}} + {"time stamp":"2016-06-01T01:59:00Z","airline":"AAA","responsetime":541.76} + """); // Create index with source = enabled, doc_values = disabled (except time), stored = false Request createAirlineDataDisabledDocValues = new Request("PUT", "/airline-data-disabled-doc-values"); - createAirlineDataDisabledDocValues.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time stamp\": { \"type\":\"date\"}," - + " \"airline\": { \"type\":\"keyword\", \"doc_values\":false}," - + " \"responsetime\": { \"type\":\"float\", \"doc_values\":false}" - + " }" - + " }" - + "}" - ); + createAirlineDataDisabledDocValues.setJsonEntity(""" + { + "mappings": { + "properties": { + "time stamp": { + "type": "date" + }, + "airline": { + "type": "keyword", + "doc_values": false + }, + "responsetime": { + "type": "float", + "doc_values": false + } + } + } + }"""); client().performRequest(createAirlineDataDisabledDocValues); - bulk.append("{\"index\": {\"_index\": \"airline-data-disabled-doc-values\", \"_id\": 1}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:00:00Z\",\"airline\":\"AAA\",\"responsetime\":135.22}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-disabled-doc-values\", \"_id\": 2}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:59:00Z\",\"airline\":\"AAA\",\"responsetime\":541.76}\n"); + bulk.append(""" + {"index": {"_index": "airline-data-disabled-doc-values", "_id": 1}} + {"time stamp":"2016-06-01T00:00:00Z","airline":"AAA","responsetime":135.22} + {"index": {"_index": "airline-data-disabled-doc-values", "_id": 2}} + {"time stamp":"2016-06-01T01:59:00Z","airline":"AAA","responsetime":541.76} + """); // Create index with source = disabled, doc_values = enabled (except time), stored = true Request createAirlineDataDisabledSource = new Request("PUT", "/airline-data-disabled-source"); - createAirlineDataDisabledSource.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"_source\":{\"enabled\":false}," - + " \"properties\": {" - + " \"time stamp\": { \"type\":\"date\", \"store\":true}," - + " \"airline\": { \"type\":\"keyword\", \"store\":true}," - + " \"responsetime\": { \"type\":\"float\", \"store\":true}" - + " }" - + " }" - + "}" - ); - - bulk.append("{\"index\": {\"_index\": \"airline-data-disabled-source\", \"_id\": 1}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:00:00Z\",\"airline\":\"AAA\",\"responsetime\":135.22}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-disabled-source\", \"_id\": 2}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:59:00Z\",\"airline\":\"AAA\",\"responsetime\":541.76}\n"); + createAirlineDataDisabledSource.setJsonEntity(""" + { + "mappings": { + "_source": { + "enabled": false + }, + "properties": { + "time stamp": { + "type": "date", + "store": true + }, + "airline": { + "type": "keyword", + "store": true + }, + "responsetime": { + "type": "float", + "store": true + } + } + } + }"""); + + bulk.append(""" + {"index": {"_index": "airline-data-disabled-source", "_id": 1}} + {"time stamp":"2016-06-01T00:00:00Z","airline":"AAA","responsetime":135.22} + {"index": {"_index": "airline-data-disabled-source", "_id": 2}} + {"time stamp":"2016-06-01T01:59:00Z","airline":"AAA","responsetime":541.76} + """); // Create index with nested documents Request createAirlineDataNested = new Request("PUT", "/nested-data"); - createAirlineDataNested.setJsonEntity( - "{" + " \"mappings\": {" + " \"properties\": {" + " \"time\": { \"type\":\"date\"}" + " }" + " }" + "}" - ); + createAirlineDataNested.setJsonEntity(""" + { + "mappings": { + "properties": { + "time": { + "type": "date" + } + } + } + }"""); client().performRequest(createAirlineDataNested); - bulk.append("{\"index\": {\"_index\": \"nested-data\", \"_id\": 1}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:00:00Z\", \"responsetime\":{\"millis\":135.22}, \"airline\":[{\"name\": \"foo\"}]}\n"); - bulk.append("{\"index\": {\"_index\": \"nested-data\", \"_id\": 2}}\n"); - bulk.append("{\"time\":\"2016-06-01T01:59:00Z\", \"responsetime\":{\"millis\":222.00}, \"airline\":[{\"name\": \"bar\"}]}\n"); + bulk.append(""" + {"index": {"_index": "nested-data", "_id": 1}} + {"time":"2016-06-01T00:00:00Z", "responsetime":{"millis":135.22}, "airline":[{"name": "foo"}]} + {"index": {"_index": "nested-data", "_id": 2}} + {"time":"2016-06-01T01:59:00Z", "responsetime":{"millis":222.00}, "airline":[{"name": "bar"}]} + """); // Create index with multiple docs per time interval for aggregation testing Request createAirlineDataAggs = new Request("PUT", "/airline-data-aggs"); - createAirlineDataAggs.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time stamp\": { \"type\":\"date\"}," // space in 'time stamp' is intentional - + " \"airline\": { \"type\":\"keyword\"}," - + " \"responsetime\": { \"type\":\"float\"}" - + " }" - + " }" - + "}" - ); + // space in 'time stamp' is intentional + createAirlineDataAggs.setJsonEntity(""" + { + "mappings": { + "properties": { + "time stamp": { + "type": "date" + }, + "airline": { + "type": "keyword" + }, + "responsetime": { + "type": "float" + } + } + } + }"""); client().performRequest(createAirlineDataAggs); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 1}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:00:00Z\",\"airline\":\"AAA\",\"responsetime\":100.0}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 2}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:01:00Z\",\"airline\":\"AAA\",\"responsetime\":200.0}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 3}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:00:00Z\",\"airline\":\"BBB\",\"responsetime\":1000.0}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 4}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:01:00Z\",\"airline\":\"BBB\",\"responsetime\":2000.0}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 5}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:00:00Z\",\"airline\":\"AAA\",\"responsetime\":300.0}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 6}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:01:00Z\",\"airline\":\"AAA\",\"responsetime\":400.0}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 7}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:00:00Z\",\"airline\":\"BBB\",\"responsetime\":3000.0}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data-aggs\", \"_id\": 8}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:01:00Z\",\"airline\":\"BBB\",\"responsetime\":4000.0}\n"); + bulk.append(""" + {"index": {"_index": "airline-data-aggs", "_id": 1}} + {"time stamp":"2016-06-01T00:00:00Z","airline":"AAA","responsetime":100.0} + {"index": {"_index": "airline-data-aggs", "_id": 2}} + {"time stamp":"2016-06-01T00:01:00Z","airline":"AAA","responsetime":200.0} + {"index": {"_index": "airline-data-aggs", "_id": 3}} + {"time stamp":"2016-06-01T00:00:00Z","airline":"BBB","responsetime":1000.0} + {"index": {"_index": "airline-data-aggs", "_id": 4}} + {"time stamp":"2016-06-01T00:01:00Z","airline":"BBB","responsetime":2000.0} + {"index": {"_index": "airline-data-aggs", "_id": 5}} + {"time stamp":"2016-06-01T01:00:00Z","airline":"AAA","responsetime":300.0} + {"index": {"_index": "airline-data-aggs", "_id": 6}} + {"time stamp":"2016-06-01T01:01:00Z","airline":"AAA","responsetime":400.0} + {"index": {"_index": "airline-data-aggs", "_id": 7}} + {"time stamp":"2016-06-01T01:00:00Z","airline":"BBB","responsetime":3000.0} + {"index": {"_index": "airline-data-aggs", "_id": 8}} + {"time stamp":"2016-06-01T01:01:00Z","airline":"BBB","responsetime":4000.0} + """); bulkIndex(bulk.toString()); } @@ -248,27 +297,35 @@ private void addAirlineData() throws IOException { private void addNetworkData(String index) throws IOException { // Create index with source = enabled, doc_values = enabled, stored = false + multi-field Request createIndexRequest = new Request("PUT", index); - createIndexRequest.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"timestamp\": { \"type\":\"date\"}," - + " \"host\": {" - + " \"type\":\"text\"," - + " \"fields\":{" - + " \"text\":{\"type\":\"text\"}," - + " \"keyword\":{\"type\":\"keyword\"}" - + " }" - + " }," - + " \"network_bytes_out\": { \"type\":\"long\"}" - + " }" - + " }" - + "}" - ); + createIndexRequest.setJsonEntity(""" + { + "mappings": { + "properties": { + "timestamp": { + "type": "date" + }, + "host": { + "type": "text", + "fields": { + "text": { + "type": "text" + }, + "keyword": { + "type": "keyword" + } + } + }, + "network_bytes_out": { + "type": "long" + } + } + } + }"""); client().performRequest(createIndexRequest); StringBuilder bulk = new StringBuilder(); - String docTemplate = "{\"timestamp\":%d,\"host\":\"%s\",\"network_bytes_out\":%d}"; + String docTemplate = """ + {"timestamp":%d,"host":"%s","network_bytes_out":%d}"""; Date date = new Date(1464739200735L); for (int i = 0; i < 120; i++) { long byteCount = randomNonNegativeLong(); @@ -310,9 +367,10 @@ public void testLookbackOnlyWithSourceDisabled() throws Exception { } public void testLookbackOnlyWithScriptFields() throws Exception { - new LookbackOnlyTestHelper("test-lookback-only-with-script-fields", "airline-data").setScriptedFields( - "{\"scripted_airline\":{\"script\":{\"lang\":\"painless\",\"source\":\"doc['airline.keyword'].value\"}}}" - ).setAirlineVariant("scripted_airline").execute(); + new LookbackOnlyTestHelper("test-lookback-only-with-script-fields", "airline-data").setScriptedFields(""" + {"scripted_airline":{"script":{"lang":"painless","source":"doc['airline.keyword'].value"}}}""") + .setAirlineVariant("scripted_airline") + .execute(); } public void testLookbackOnlyWithRuntimeFields() throws Exception { @@ -324,22 +382,20 @@ public void testLookbackOnlyWithRuntimeFields() throws Exception { public void testLookbackonlyWithNestedFields() throws Exception { String jobId = "test-lookback-only-with-nested-fields"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Nested job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime.millis\",\n" - + " \"by_field_name\": \"airline.name\"\n" - + " }\n" - + " ]\n" - + " }," - + " \"data_description\": {\"time_field\": \"time\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Nested job", + "analysis_config": { + "bucket_span": "1h", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime.millis", + "by_field_name": "airline.name" + } + ] + }, "data_description": {"time_field": "time"} + }"""); client().performRequest(createJobRequest); String datafeedId = jobId + "-datafeed"; @@ -360,21 +416,19 @@ public void testLookbackonlyWithNestedFields() throws Exception { public void testLookbackWithGeo() throws Exception { String jobId = "test-lookback-only-with-geo"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"lat_long with geo_point\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"15m\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"lat_long\",\n" - + " \"field_name\": \"location\"\n" - + " }\n" - + " ]\n" - + " }," - + " \"data_description\": {\"time_field\": \"time\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "lat_long with geo_point", + "analysis_config": { + "bucket_span": "15m", + "detectors": [ + { + "function": "lat_long", + "field_name": "location" + } + ] + }, "data_description": {"time_field": "time"} + }"""); client().performRequest(createJobRequest); String datafeedId = jobId + "-datafeed"; new DatafeedBuilder(datafeedId, jobId, "geo-data").build(); @@ -382,36 +436,41 @@ public void testLookbackWithGeo() throws Exception { StringBuilder bulk = new StringBuilder(); Request createGeoData = new Request("PUT", "/geo-data"); - createGeoData.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time\": { \"type\":\"date\"}," - + " \"location\": { \"type\":\"geo_point\"}" - + " }" - + " }" - + "}" - ); + createGeoData.setJsonEntity(""" + { + "mappings": { + "properties": { + "time": { + "type": "date" + }, + "location": { + "type": "geo_point" + } + } + } + }"""); client().performRequest(createGeoData); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 1}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:00:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 2}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:05:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 3}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:10:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 4}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:15:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 5}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:20:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 6}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:25:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 7}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:30:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 8}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:40:00Z\",\"location\":{\"lat\":90.0,\"lon\":-77.03653}}\n"); - bulk.append("{\"index\": {\"_index\": \"geo-data\", \"_id\": 9}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:41:00Z\",\"location\":{\"lat\":38.897676,\"lon\":-77.03653}}\n"); + bulk.append(""" + {"index": {"_index": "geo-data", "_id": 1}} + {"time":"2016-06-01T00:00:00Z","location":{"lat":38.897676,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 2}} + {"time":"2016-06-01T00:05:00Z","location":{"lat":38.897676,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 3}} + {"time":"2016-06-01T00:10:00Z","location":{"lat":38.897676,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 4}} + {"time":"2016-06-01T00:15:00Z","location":{"lat":38.897676,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 5}} + {"time":"2016-06-01T00:20:00Z","location":{"lat":38.897676,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 6}} + {"time":"2016-06-01T00:25:00Z","location":{"lat":38.897676,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 7}} + {"time":"2016-06-01T00:30:00Z","location":{"lat":38.897676,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 8}} + {"time":"2016-06-01T00:40:00Z","location":{"lat":90.0,"lon":-77.03653}} + {"index": {"_index": "geo-data", "_id": 9}} + {"time":"2016-06-01T00:41:00Z","location":{"lat":38.897676,"lon":-77.03653}} + """); bulkIndex(bulk.toString()); openJob(client(), jobId); @@ -430,59 +489,64 @@ public void testLookbackWithGeo() throws Exception { public void testLookbackWithIndicesOptions() throws Exception { String jobId = "test-lookback-only-with-indices-options"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"custom indices options\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"15m\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"count\"\n" - + " }\n" - + " ]\n" - + " }," - + " \"data_description\": {\"time_field\": \"time\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "custom indices options", + "analysis_config": { + "bucket_span": "15m", + "detectors": [ + { + "function": "count" + } + ] + }, "data_description": {"time_field": "time"} + }"""); client().performRequest(createJobRequest); String datafeedId = jobId + "-datafeed"; - new DatafeedBuilder(datafeedId, jobId, "*hidden-*").setIndicesOptions( - "{" + "\"expand_wildcards\": [\"all\"]," + "\"allow_no_indices\": true" + "}" - ).build(); + new DatafeedBuilder(datafeedId, jobId, "*hidden-*").setIndicesOptions(""" + {"expand_wildcards": ["all"],"allow_no_indices": true}""").build(); StringBuilder bulk = new StringBuilder(); Request createGeoData = new Request("PUT", "/.hidden-index"); - createGeoData.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time\": { \"type\":\"date\"}," - + " \"value\": { \"type\":\"long\"}" - + " }" - + " }, \"settings\": {\"index.hidden\": true} " - + "}" - ); + createGeoData.setJsonEntity(""" + { + "mappings": { + "properties": { + "time": { + "type": "date" + }, + "value": { + "type": "long" + } + } + }, + "settings": { + "index.hidden": true + } + }"""); client().performRequest(createGeoData); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 1}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:00:00Z\",\"value\": 1000}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 2}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:05:00Z\",\"value\":1500}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 3}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:10:00Z\",\"value\":1600}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 4}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:15:00Z\",\"value\":100}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 5}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:20:00Z\",\"value\":1}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 6}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:25:00Z\",\"value\":1500}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 7}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:30:00Z\",\"value\":1500}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 8}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:40:00Z\",\"value\":2100}\n"); - bulk.append("{\"index\": {\"_index\": \".hidden-index\", \"_id\": 9}}\n"); - bulk.append("{\"time\":\"2016-06-01T00:41:00Z\",\"value\":0}\n"); + bulk.append(""" + {"index": {"_index": ".hidden-index", "_id": 1}} + {"time":"2016-06-01T00:00:00Z","value": 1000} + {"index": {"_index": ".hidden-index", "_id": 2}} + {"time":"2016-06-01T00:05:00Z","value":1500} + {"index": {"_index": ".hidden-index", "_id": 3}} + {"time":"2016-06-01T00:10:00Z","value":1600} + {"index": {"_index": ".hidden-index", "_id": 4}} + {"time":"2016-06-01T00:15:00Z","value":100} + {"index": {"_index": ".hidden-index", "_id": 5}} + {"time":"2016-06-01T00:20:00Z","value":1} + {"index": {"_index": ".hidden-index", "_id": 6}} + {"time":"2016-06-01T00:25:00Z","value":1500} + {"index": {"_index": ".hidden-index", "_id": 7}} + {"time":"2016-06-01T00:30:00Z","value":1500} + {"index": {"_index": ".hidden-index", "_id": 8}} + {"time":"2016-06-01T00:40:00Z","value":2100} + {"index": {"_index": ".hidden-index", "_id": 9}} + {"time":"2016-06-01T00:41:00Z","value":0} + """); bulkIndex(bulk.toString()); openJob(client(), jobId); @@ -507,23 +571,22 @@ public void testLookbackOnlyGivenEmptyIndex() throws Exception { public void testInsufficientSearchPrivilegesOnPut() throws Exception { String jobId = "privs-put-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n " - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\":\"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\" : {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "analysis_config": { + "bucket_span": "1h", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime", + "by_field_name":"airline" + } + ] + }, + "data_description" : {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; @@ -542,24 +605,23 @@ public void testInsufficientSearchPrivilegesOnPut() throws Exception { public void testInsufficientSearchPrivilegesOnPutWithJob() { String jobId = "privs-failed-put-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"datafeed_config\": {\"indexes\": [\"airline-data-aggs\"]},\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n " - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\":\"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\" : {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "datafeed_config": {"indexes": ["airline-data-aggs"]}, + "analysis_config": { + "bucket_span": "1h", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime", + "by_field_name":"airline" + } + ] + }, + "data_description" : {"time_field": "time stamp"} + }"""); RequestOptions.Builder options = createJobRequest.getOptions().toBuilder(); options.addHeader("Authorization", BASIC_AUTH_VALUE_ML_ADMIN); createJobRequest.setOptions(options); @@ -590,23 +652,22 @@ public void testCreationOnPutWithRollup() throws Exception { public void testInsufficientSearchPrivilegesOnPreview() throws Exception { String jobId = "privs-preview-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\" : {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "analysis_config": { + "bucket_span": "1h", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime", + "by_field_name": "airline" + } + ] + }, + "data_description" : {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; @@ -667,31 +728,54 @@ public void testSecondaryAuthSearchPrivilegesOnPreview() throws Exception { public void testLookbackOnlyGivenAggregationsWithHistogram() throws Exception { String jobId = "aggs-histogram-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "analysis_config": { + "bucket_span": "1h", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime", + "by_field_name": "airline" + } + ] + }, + "data_description": {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"buckets\":{\"histogram\":{\"field\":\"time stamp\",\"interval\":3600000}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"airline\":{\"terms\":{\"field\":\"airline\",\"size\":10}," - + " \"aggregations\":{\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}}}"; + String aggregations = """ + { + "buckets": { + "histogram": { + "field": "time stamp", + "interval": 3600000 + }, + "aggregations": { + "time stamp": { + "max": { + "field": "time stamp" + } + }, + "airline": { + "terms": { + "field": "airline", + "size": 10 + }, + "aggregations": { + "responsetime": { + "avg": { + "field": "responsetime" + } + } + } + } + } + } + }"""; new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs").setAggregations(aggregations).build(); openJob(client(), jobId); @@ -709,31 +793,54 @@ public void testLookbackOnlyGivenAggregationsWithHistogram() throws Exception { public void testLookbackOnlyGivenAggregationsWithDateHistogram() throws Exception { String jobId = "aggs-date-histogram-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"3600s\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "analysis_config": { + "bucket_span": "3600s", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime", + "by_field_name": "airline" + } + ] + }, + "data_description": {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"time stamp\":{\"date_histogram\":{\"field\":\"time stamp\",\"calendar_interval\":\"1h\"}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"airline\":{\"terms\":{\"field\":\"airline\",\"size\":10}," - + " \"aggregations\":{\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}}}"; + String aggregations = """ + { + "time stamp": { + "date_histogram": { + "field": "time stamp", + "calendar_interval": "1h" + }, + "aggregations": { + "time stamp": { + "max": { + "field": "time stamp" + } + }, + "airline": { + "terms": { + "field": "airline", + "size": 10 + }, + "aggregations": { + "responsetime": { + "avg": { + "field": "responsetime" + } + } + } + } + } + } + }"""; new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs").setAggregations(aggregations).build(); openJob(client(), jobId); @@ -751,30 +858,58 @@ public void testLookbackOnlyGivenAggregationsWithDateHistogram() throws Exceptio public void testLookbackUsingDerivativeAggWithLargerHistogramBucketThanDataRate() throws Exception { String jobId = "derivative-agg-network-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"300s\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"bytes-delta\",\n" - + " \"by_field_name\": \"hostname\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"timestamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "analysis_config": { + "bucket_span": "300s", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "bytes-delta", + "by_field_name": "hostname" + } + ] + }, + "data_description": {"time_field": "timestamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," - + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"60s\"}," - + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," - + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," - + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; + String aggregations = """ + { + "hostname": { + "terms": { + "field": "host.keyword", + "size": 10 + }, + "aggs": { + "buckets": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "60s" + }, + "aggs": { + "timestamp": { + "max": { + "field": "timestamp" + } + }, + "bytes-delta": { + "derivative": { + "buckets_path": "avg_bytes_out" + } + }, + "avg_bytes_out": { + "avg": { + "field": "network_bytes_out" + } + } + } + } + } + } + }"""; new DatafeedBuilder(datafeedId, jobId, "network-data").setAggregations(aggregations).setChunkingTimespan("300s").build(); openJob(client(), jobId); @@ -796,30 +931,58 @@ public void testLookbackUsingDerivativeAggWithLargerHistogramBucketThanDataRate( public void testLookbackUsingDerivativeAggWithSmallerHistogramBucketThanDataRate() throws Exception { String jobId = "derivative-agg-network-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"300s\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"bytes-delta\",\n" - + " \"by_field_name\": \"hostname\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"timestamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "analysis_config": { + "bucket_span": "300s", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "bytes-delta", + "by_field_name": "hostname" + } + ] + }, + "data_description": {"time_field": "timestamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," - + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"5s\"}," - + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," - + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," - + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; + String aggregations = """ + { + "hostname": { + "terms": { + "field": "host.keyword", + "size": 10 + }, + "aggs": { + "buckets": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "5s" + }, + "aggs": { + "timestamp": { + "max": { + "field": "timestamp" + } + }, + "bytes-delta": { + "derivative": { + "buckets_path": "avg_bytes_out" + } + }, + "avg_bytes_out": { + "avg": { + "field": "network_bytes_out" + } + } + } + } + } + } + }"""; new DatafeedBuilder(datafeedId, jobId, "network-data").setAggregations(aggregations).setChunkingTimespan("300s").build(); openJob(client(), jobId); @@ -837,30 +1000,58 @@ public void testLookbackUsingDerivativeAggWithSmallerHistogramBucketThanDataRate public void testLookbackWithoutPermissions() throws Exception { String jobId = "permission-test-network-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"300s\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"bytes-delta\",\n" - + " \"by_field_name\": \"hostname\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"timestamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "analysis_config": { + "bucket_span": "300s", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "bytes-delta", + "by_field_name": "hostname" + } + ] + }, + "data_description": {"time_field": "timestamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"hostname\": {\"terms\" : {\"field\": \"host.keyword\", \"size\":10}," - + "\"aggs\": {\"buckets\": {\"date_histogram\":{\"field\":\"timestamp\",\"fixed_interval\":\"5s\"}," - + "\"aggs\": {\"timestamp\":{\"max\":{\"field\":\"timestamp\"}}," - + "\"bytes-delta\":{\"derivative\":{\"buckets_path\":\"avg_bytes_out\"}}," - + "\"avg_bytes_out\":{\"avg\":{\"field\":\"network_bytes_out\"}} }}}}}"; + String aggregations = """ + { + "hostname": { + "terms": { + "field": "host.keyword", + "size": 10 + }, + "aggs": { + "buckets": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "5s" + }, + "aggs": { + "timestamp": { + "max": { + "field": "timestamp" + } + }, + "bytes-delta": { + "derivative": { + "buckets_path": "avg_bytes_out" + } + }, + "avg_bytes_out": { + "avg": { + "field": "network_bytes_out" + } + } + } + } + } + } + }"""; // At the time we create the datafeed the user can access the network-data index that we have access to new DatafeedBuilder(datafeedId, jobId, "network-data").setAggregations(aggregations) @@ -904,30 +1095,51 @@ public void testLookbackWithoutPermissions() throws Exception { public void testLookbackWithPipelineBucketAgg() throws Exception { String jobId = "pipeline-bucket-agg-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"percentile95_airlines_count\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "analysis_config": { + "bucket_span": "1h", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "percentile95_airlines_count" + } + ] + }, + "data_description": {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"buckets\":{\"date_histogram\":{\"field\":\"time stamp\",\"fixed_interval\":\"15m\"}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"airlines\":{\"terms\":{\"field\":\"airline.keyword\",\"size\":10}}," - + "\"percentile95_airlines_count\":{\"percentiles_bucket\":" - + "{\"buckets_path\":\"airlines._count\", \"percents\": [95]}}}}}"; + String aggregations = """ + { + "buckets": { + "date_histogram": { + "field": "time stamp", + "fixed_interval": "15m" + }, + "aggregations": { + "time stamp": { + "max": { + "field": "time stamp" + } + }, + "airlines": { + "terms": { + "field": "airline.keyword", + "size": 10 + } + }, + "percentile95_airlines_count": { + "percentiles_bucket": { + "buckets_path": "airlines._count", + "percents": [ 95 ] + } + } + } + } + }"""; new DatafeedBuilder(datafeedId, jobId, "airline-data").setAggregations(aggregations).build(); openJob(client(), jobId); @@ -949,55 +1161,52 @@ public void testLookbackWithPipelineBucketAgg() throws Exception { public void testLookbackOnlyGivenAggregationsWithHistogramAndRollupIndex() throws Exception { String jobId = "aggs-histogram-rollup-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "analysis_config": { + "bucket_span": "1h", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime", + "by_field_name": "airline" + } + ] + }, + "data_description": {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String rollupJobId = "rollup-" + jobId; Request createRollupRequest = new Request("PUT", "/_rollup/job/" + rollupJobId); - createRollupRequest.setJsonEntity( - "{\n" - + "\"index_pattern\": \"airline-data-aggs\",\n" - + " \"rollup_index\": \"airline-data-aggs-rollup\",\n" - + " \"cron\": \"*/30 * * * * ?\",\n" - + " \"page_size\" :1000,\n" - + " \"groups\" : {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time stamp\",\n" - + " \"fixed_interval\": \"2m\",\n" - + " \"delay\": \"7d\"\n" - + " },\n" - + " \"terms\": {\n" - + " \"fields\": [\"airline\"]\n" - + " }" - + " },\n" - + " \"metrics\": [\n" - + " {\n" - + " \"field\": \"responsetime\",\n" - + " \"metrics\": [\"avg\",\"min\",\"max\",\"sum\"]\n" - + " },\n" - + " {\n" - + " \"field\": \"time stamp\",\n" - + " \"metrics\": [\"min\",\"max\"]\n" - + " }\n" - + " ]\n" - + "}" - ); + createRollupRequest.setJsonEntity(""" + { + "index_pattern": "airline-data-aggs", + "rollup_index": "airline-data-aggs-rollup", + "cron": "*/30 * * * * ?", + "page_size" :1000, + "groups" : { + "date_histogram": { + "field": "time stamp", + "fixed_interval": "2m", + "delay": "7d" + }, + "terms": { + "fields": ["airline"] + } }, + "metrics": [ + { + "field": "responsetime", + "metrics": ["avg","min","max","sum"] + }, + { + "field": "time stamp", + "metrics": ["min","max"] + } + ] + }"""); client().performRequest(createRollupRequest); client().performRequest(new Request("POST", "/_rollup/job/" + rollupJobId + "/_start")); @@ -1018,10 +1227,27 @@ public void testLookbackOnlyGivenAggregationsWithHistogramAndRollupIndex() throw client().performRequest(refreshRollupIndex); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"buckets\":{\"date_histogram\":{\"field\":\"time stamp\",\"fixed_interval\":\"3600000ms\"}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}"; + String aggregations = """ + { + "buckets": { + "date_histogram": { + "field": "time stamp", + "fixed_interval": "3600000ms" + }, + "aggregations": { + "time stamp": { + "max": { + "field": "time stamp" + } + }, + "responsetime": { + "avg": { + "field": "responsetime" + } + } + } + } + }"""; new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs-rollup").setAggregations(aggregations).build(); openJob(client(), jobId); @@ -1069,30 +1295,53 @@ public void testLookbackWithoutPermissionsAndRollup() throws Exception { public void testLookbackWithSingleBucketAgg() throws Exception { String jobId = "aggs-date-histogram-with-single-bucket-agg-job"; Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"3600s\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\"" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "analysis_config": { + "bucket_span": "3600s", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime" } + ] + }, + "data_description": {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String datafeedId = "datafeed-" + jobId; - String aggregations = "{\"time stamp\":{\"date_histogram\":{\"field\":\"time stamp\",\"calendar_interval\":\"1h\"}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"airlineFilter\":{\"filter\":{\"term\": {\"airline\":\"AAA\"}}," - + " \"aggregations\":{\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}}}"; + String aggregations = """ + { + "time stamp": { + "date_histogram": { + "field": "time stamp", + "calendar_interval": "1h" + }, + "aggregations": { + "time stamp": { + "max": { + "field": "time stamp" + } + }, + "airlineFilter": { + "filter": { + "term": { + "airline": "AAA" + } + }, + "aggregations": { + "responsetime": { + "avg": { + "field": "responsetime" + } + } + } + } + } + } + }"""; new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs").setAggregations(aggregations).build(); openJob(client(), jobId); @@ -1335,28 +1584,19 @@ private void waitUntilJobIsClosed(String jobId) throws Exception { private Response createJob(String id, String airlineVariant) throws Exception { Request request = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + id); - request.setJsonEntity( - "{\n" - + " \"description\": \"Analysis of response time by airline\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"detectors\" :[\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"" - + airlineVariant - + "\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\n" - + " \"format\": \"xcontent\",\n" - + " \"time_field\": \"time stamp\",\n" - + " \"time_format\": \"yyyy-MM-dd'T'HH:mm:ssX\"\n" - + " }\n" - + "}" - ); + request.setJsonEntity(""" + { + "description": "Analysis of response time by airline", + "analysis_config": { + "bucket_span": "1h", + "detectors": [ { "function": "mean", "field_name": "responsetime", "by_field_name": "%s" } ] + }, + "data_description": { + "format": "xcontent", + "time_field": "time stamp", + "time_format": "yyyy-MM-dd'T'HH:mm:ssX" + } + }""".formatted(airlineVariant)); return client().performRequest(request); } @@ -1428,21 +1668,28 @@ DatafeedBuilder setIndicesOptions(String indicesOptions) { Response build() throws IOException { Request request = new Request("PUT", MachineLearning.BASE_PATH + "datafeeds/" + datafeedId); request.setJsonEntity( - "{" - + "\"job_id\": \"" - + jobId - + "\",\"indexes\":[\"" - + index - + "\"]" - + (source ? ",\"_source\":true" : "") - + (scriptedFields == null ? "" : ",\"script_fields\":" + scriptedFields) - + (aggregations == null ? "" : ",\"aggs\":" + aggregations) - + (frequency == null ? "" : ",\"frequency\":\"" + frequency + "\"") - + (indicesOptions == null ? "" : ",\"indices_options\":" + indicesOptions) - + (chunkingTimespan == null - ? "" - : ",\"chunking_config\":{\"mode\":\"MANUAL\",\"time_span\":\"" + chunkingTimespan + "\"}") - + "}" + """ + { + "job_id": "%s", + "indexes":["%s"] + %s + %s + %s + %s + %s + %s + }""".formatted( + jobId, + index, + source ? ",\"_source\":true" : "", + scriptedFields == null ? "" : ",\"script_fields\":" + scriptedFields, + aggregations == null ? "" : ",\"aggs\":" + aggregations, + frequency == null ? "" : ",\"frequency\":\"" + frequency + "\"", + indicesOptions == null ? "" : ",\"indices_options\":" + indicesOptions, + chunkingTimespan == null ? "" : """ + ,"chunking_config":{"mode":"MANUAL","time_span":"%s"} + """.formatted(chunkingTimespan) + ) ); RequestOptions.Builder options = request.getOptions().toBuilder(); options.addHeader("Authorization", authHeader); @@ -1465,61 +1712,75 @@ private void bulkIndex(String bulk) throws IOException { private Response createJobAndDataFeed(String jobId, String datafeedId) throws IOException { Request createJobRequest = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\": \"Aggs job\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"1h\",\n" - + " \"summary_count_field_name\": \"doc_count\",\n" - + " \"detectors\": [\n" - + " {\n" - + " \"function\": \"mean\",\n" - + " \"field_name\": \"responsetime\",\n" - + " \"by_field_name\": \"airline\"\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"data_description\": {\"time_field\": \"time stamp\"}\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description": "Aggs job", + "analysis_config": { + "bucket_span": "1h", + "summary_count_field_name": "doc_count", + "detectors": [ + { + "function": "mean", + "field_name": "responsetime", + "by_field_name": "airline" + } + ] + }, + "data_description": {"time_field": "time stamp"} + }"""); client().performRequest(createJobRequest); String rollupJobId = "rollup-" + jobId; Request createRollupRequest = new Request("PUT", "/_rollup/job/" + rollupJobId); - createRollupRequest.setJsonEntity( - "{\n" - + "\"index_pattern\": \"airline-data-aggs\",\n" - + " \"rollup_index\": \"airline-data-aggs-rollup\",\n" - + " \"cron\": \"*/30 * * * * ?\",\n" - + " \"page_size\" :1000,\n" - + " \"groups\" : {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time stamp\",\n" - + " \"fixed_interval\": \"2m\",\n" - + " \"delay\": \"7d\"\n" - + " },\n" - + " \"terms\": {\n" - + " \"fields\": [\"airline\"]\n" - + " }" - + " },\n" - + " \"metrics\": [\n" - + " {\n" - + " \"field\": \"responsetime\",\n" - + " \"metrics\": [\"avg\",\"min\",\"max\",\"sum\"]\n" - + " },\n" - + " {\n" - + " \"field\": \"time stamp\",\n" - + " \"metrics\": [\"min\",\"max\"]\n" - + " }\n" - + " ]\n" - + "}" - ); + createRollupRequest.setJsonEntity(""" + { + "index_pattern": "airline-data-aggs", + "rollup_index": "airline-data-aggs-rollup", + "cron": "*/30 * * * * ?", + "page_size" :1000, + "groups" : { + "date_histogram": { + "field": "time stamp", + "fixed_interval": "2m", + "delay": "7d" + }, + "terms": { + "fields": ["airline"] + } }, + "metrics": [ + { + "field": "responsetime", + "metrics": ["avg","min","max","sum"] + }, + { + "field": "time stamp", + "metrics": ["min","max"] + } + ] + }"""); client().performRequest(createRollupRequest); - String aggregations = "{\"buckets\":{\"date_histogram\":{\"field\":\"time stamp\",\"fixed_interval\":\"3600000ms\"}," - + "\"aggregations\":{" - + "\"time stamp\":{\"max\":{\"field\":\"time stamp\"}}," - + "\"responsetime\":{\"avg\":{\"field\":\"responsetime\"}}}}}"; + String aggregations = """ + { + "buckets": { + "date_histogram": { + "field": "time stamp", + "fixed_interval": "3600000ms" + }, + "aggregations": { + "time stamp": { + "max": { + "field": "time stamp" + } + }, + "responsetime": { + "avg": { + "field": "responsetime" + } + } + } + } + }"""; return new DatafeedBuilder(datafeedId, jobId, "airline-data-aggs-rollup").setAggregations(aggregations) .setAuthHeader(BASIC_AUTH_VALUE_ML_ADMIN_WITH_SOME_DATA_ACCESS) diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java index b321f2f899638..ff5fd2e54aade 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsIT.java @@ -230,19 +230,20 @@ public void testSimultaneousExplainSameConfig() throws IOException { public void testRuntimeFields() { String sourceIndex = "test-explain-runtime-fields"; - String mapping = "{\n" - + " \"properties\": {\n" - + " \"mapped_field\": {\n" - + " \"type\": \"double\"\n" - + " }\n" - + " },\n" - + " \"runtime\": {\n" - + " \"mapped_runtime_field\": {\n" - + " \"type\": \"double\"\n," - + " \"script\": \"emit(doc['mapped_field'].value + 10.0)\"\n" - + " }\n" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "mapped_field": { + "type": "double" + } + }, + "runtime": { + "mapped_runtime_field": { + "type": "double", + "script": "emit(doc['mapped_field'].value + 10.0)" + } + } + }"""; client().admin().indices().prepareCreate(sourceIndex).setMapping(mapping).get(); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); for (int i = 0; i < 10; i++) { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java index eabcc647c3faf..d2dc304860004 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ExplainDataFrameAnalyticsRestIT.java @@ -56,16 +56,9 @@ private void setupUser(String user, List roles) throws IOException { String password = new String(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING.getChars()); Request request = new Request("PUT", "/_security/user/" + user); - request.setJsonEntity( - "{" - + " \"password\" : \"" - + password - + "\"," - + " \"roles\" : [ " - + roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")) - + " ]" - + "}" - ); + request.setJsonEntity(""" + { "password" : "%s", "roles" : [ %s ] } + """.formatted(password, roles.stream().map(unquoted -> "\"" + unquoted + "\"").collect(Collectors.joining(", ")))); client().performRequest(request); } @@ -82,40 +75,48 @@ private void addAirlineData() throws IOException { // Create index with source = enabled, doc_values = enabled, stored = false + multi-field Request createAirlineDataRequest = new Request("PUT", "/airline-data"); - createAirlineDataRequest.setJsonEntity( - "{" - + " \"mappings\": {" - + " \"properties\": {" - + " \"time stamp\": { \"type\":\"date\"}," // space in 'time stamp' is intentional - + " \"airline\": {" - + " \"type\":\"keyword\"" - + " }," - + " \"responsetime\": { \"type\":\"float\"}" - + " }" - + " }" - + "}" - ); + // space in 'time stamp' is intentional + createAirlineDataRequest.setJsonEntity(""" + { + "mappings": { + "properties": { + "time stamp": { + "type": "date" + }, + "airline": { + "type": "keyword" + }, + "responsetime": { + "type": "float" + } + } + } + }"""); client().performRequest(createAirlineDataRequest); - bulk.append("{\"index\": {\"_index\": \"airline-data\", \"_id\": 1}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T00:00:00Z\",\"airline\":\"AAA\",\"responsetime\":135.22}\n"); - bulk.append("{\"index\": {\"_index\": \"airline-data\", \"_id\": 2}}\n"); - bulk.append("{\"time stamp\":\"2016-06-01T01:59:00Z\",\"airline\":\"AAA\",\"responsetime\":541.76}\n"); + bulk.append(""" + {"index": {"_index": "airline-data", "_id": 1}} + {"time stamp":"2016-06-01T00:00:00Z","airline":"AAA","responsetime":135.22} + {"index": {"_index": "airline-data", "_id": 2}} + {"time stamp":"2016-06-01T01:59:00Z","airline":"AAA","responsetime":541.76} + + """); bulkIndex(bulk.toString()); } public void testExplain_GivenSecondaryHeadersAndConfig() throws IOException { - String config = "{\n" - + " \"source\": {\n" - + " \"index\": \"airline-data\"\n" - + " },\n" - + " \"analysis\": {\n" - + " \"regression\": {\n" - + " \"dependent_variable\": \"responsetime\"\n" - + " }\n" - + " }\n" - + "}"; + String config = """ + { + "source": { + "index": "airline-data" + }, + "analysis": { + "regression": { + "dependent_variable": "responsetime" + } + } + }"""; { // Request with secondary headers without perms Request explain = explainRequestViaConfig(config); @@ -138,20 +139,21 @@ public void testExplain_GivenSecondaryHeadersAndConfig() throws IOException { } public void testExplain_GivenSecondaryHeadersAndPreviouslyStoredConfig() throws IOException { - String config = "{\n" - + " \"source\": {\n" - + " \"index\": \"airline-data\"\n" - + " },\n" - + " \"dest\": {\n" - + " \"index\": \"response_prediction\"\n" - + " },\n" - + " \"analysis\":\n" - + " {\n" - + " \"regression\": {\n" - + " \"dependent_variable\": \"responsetime\"\n" - + " }\n" - + " }\n" - + "}"; + String config = """ + { + "source": { + "index": "airline-data" + }, + "dest": { + "index": "response_prediction" + }, + "analysis": + { + "regression": { + "dependent_variable": "responsetime" + } + } + }"""; String configId = "explain_test"; diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java index e9dc6ed52ef06..fa661ac28d9e2 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/ForecastIT.java @@ -624,14 +624,9 @@ private void createDataWithLotsOfClientIps(TimeValue bucketSpan, Job.Builder job double value = 10.0 + h; for (int i = 1; i < 101; i++) { for (int j = 1; j < 81; j++) { - String json = String.format( - Locale.ROOT, - "{\"time\": %d, \"value\": %f, \"clientIP\": \"192.168.%d.%d\"}\n", - timestamp, - value, - i, - j - ); + String json = String.format(Locale.ROOT, """ + {"time": %s, "value": %f, "clientIP": "192.168.%d.%d"} + """, timestamp, value, i, j); data.add(json); } } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java index ca09424b2b017..a7ac49d564130 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceIngestIT.java @@ -62,9 +62,10 @@ public class InferenceIngestIT extends ESRestTestCase { @Before public void setup() throws Exception { Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity( - "" + "{" + "\"persistent\" : {\n" + " \"logger.org.elasticsearch.xpack.ml.inference\" : \"TRACE\"\n" + " }" + "}" - ); + loggingSettings.setJsonEntity(""" + {"persistent" : { + "logger.org.elasticsearch.xpack.ml.inference" : "TRACE" + }}"""); client().performRequest(loggingSettings); client().performRequest(new Request("GET", "/_cluster/health?wait_for_status=green&timeout=30s")); } @@ -78,9 +79,10 @@ protected Settings restClientSettings() { public void cleanUpData() throws Exception { new MlRestTestStateCleaner(logger, adminClient()).resetFeatures(); Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity( - "" + "{" + "\"persistent\" : {\n" + " \"logger.org.elasticsearch.xpack.ml.inference\" : null\n" + " }" + "}" - ); + loggingSettings.setJsonEntity(""" + {"persistent" : { + "logger.org.elasticsearch.xpack.ml.inference" : null + }}"""); client().performRequest(loggingSettings); } @@ -199,15 +201,16 @@ public void testPipelineIngestWithModelAliases() throws Exception { putModelAlias(modelAlias, regressionModelId2); // Need to assert busy as loading the model and then switching the model alias can take time assertBusy(() -> { - String source = "{\n" - + " \"docs\": [\n" - + " {\"_source\": {\n" - + " \"col1\": \"female\",\n" - + " \"col2\": \"M\",\n" - + " \"col3\": \"none\",\n" - + " \"col4\": 10\n" - + " }}]\n" - + "}"; + String source = """ + { + "docs": [ + {"_source": { + "col1": "female", + "col2": "M", + "col3": "none", + "col4": 10 + }}] + }"""; Request request = new Request("POST", "_ingest/pipeline/simple_regression_pipeline/_simulate"); request.setJsonEntity(source); Response response = client().performRequest(request); @@ -289,53 +292,58 @@ public void testSimulate() throws IOException { String regressionModelId = "test_regression_simulate"; putModel(regressionModelId, REGRESSION_CONFIG); - String source = "{\n" - + " \"pipeline\": {\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"target_field\": \"ml.classification\",\n" - + " \"inference_config\": {\"classification\": " - + " {\"num_top_classes\":0, " - + " \"top_classes_results_field\": \"result_class_prob\"," - + " \"num_top_feature_importance_values\": 2" - + " }},\n" - + " \"model_id\": \"" - + classificationModelId - + "\",\n" - + " \"field_map\": {\n" - + " \"col1\": \"col1\",\n" - + " \"col2\": \"col2\",\n" - + " \"col3\": \"col3\",\n" - + " \"col4\": \"col4\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"inference\": {\n" - + " \"target_field\": \"ml.regression\",\n" - + " \"model_id\": \"" - + regressionModelId - + "\",\n" - + " \"inference_config\": {\"regression\":{}},\n" - + " \"field_map\": {\n" - + " \"col1\": \"col1\",\n" - + " \"col2\": \"col2\",\n" - + " \"col3\": \"col3\",\n" - + " \"col4\": \"col4\"\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"docs\": [\n" - + " {\"_source\": {\n" - + " \"col1\": \"female\",\n" - + " \"col2\": \"M\",\n" - + " \"col3\": \"none\",\n" - + " \"col4\": 10\n" - + " }}]\n" - + "}"; + String source = """ + { + "pipeline": { + "processors": [ + { + "inference": { + "target_field": "ml.classification", + "inference_config": { + "classification": { + "num_top_classes": 0, + "top_classes_results_field": "result_class_prob", + "num_top_feature_importance_values": 2 + } + }, + "model_id": "%s", + "field_map": { + "col1": "col1", + "col2": "col2", + "col3": "col3", + "col4": "col4" + } + } + }, + { + "inference": { + "target_field": "ml.regression", + "model_id": "%s", + "inference_config": { + "regression": {} + }, + "field_map": { + "col1": "col1", + "col2": "col2", + "col3": "col3", + "col4": "col4" + } + } + } + ] + }, + "docs": [ + { + "_source": { + "col1": "female", + "col2": "M", + "col3": "none", + "col4": 10 + } + } + ] + } + """.formatted(classificationModelId, regressionModelId); Response response = client().performRequest(simulateRequest(source)); String responseString = EntityUtils.toString(response.getEntity()); @@ -348,31 +356,32 @@ public void testSimulate() throws IOException { assertThat(responseString, containsString("\"importance\":0.944")); assertThat(responseString, containsString("\"importance\":0.19999")); - String sourceWithMissingModel = "{\n" - + " \"pipeline\": {\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"model_id\": \"test_classification_missing\",\n" - + " \"inference_config\": {\"classification\":{}},\n" - + " \"field_map\": {\n" - + " \"col1\": \"col1\",\n" - + " \"col2\": \"col2\",\n" - + " \"col3\": \"col3\",\n" - + " \"col4\": \"col4\"\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"docs\": [\n" - + " {\"_source\": {\n" - + " \"col1\": \"female\",\n" - + " \"col2\": \"M\",\n" - + " \"col3\": \"none\",\n" - + " \"col4\": 10\n" - + " }}]\n" - + "}"; + String sourceWithMissingModel = """ + { + "pipeline": { + "processors": [ + { + "inference": { + "model_id": "test_classification_missing", + "inference_config": {"classification":{}}, + "field_map": { + "col1": "col1", + "col2": "col2", + "col3": "col3", + "col4": "col4" + } + } + } + ] + }, + "docs": [ + {"_source": { + "col1": "female", + "col2": "M", + "col3": "none", + "col4": 10 + }}] + }"""; response = client().performRequest(simulateRequest(sourceWithMissingModel)); responseString = EntityUtils.toString(response.getEntity()); @@ -383,33 +392,37 @@ public void testSimulate() throws IOException { public void testSimulateWithDefaultMappedField() throws IOException { String classificationModelId = "test_classification_default_mapped_field"; putModel(classificationModelId, CLASSIFICATION_CONFIG); - String source = "{\n" - + " \"pipeline\": {\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"target_field\": \"ml.classification\",\n" - + " \"inference_config\": {\"classification\": " - + " {\"num_top_classes\":2, " - + " \"top_classes_results_field\": \"result_class_prob\"," - + " \"num_top_feature_importance_values\": 2" - + " }},\n" - + " \"model_id\": \"" - + classificationModelId - + "\",\n" - + " \"field_map\": {}\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"docs\": [\n" - + " {\"_source\": {\n" - + " \"col_1_alias\": \"female\",\n" - + " \"col2\": \"M\",\n" - + " \"col3\": \"none\",\n" - + " \"col4\": 10\n" - + " }}]\n" - + "}"; + String source = """ + { + "pipeline": { + "processors": [ + { + "inference": { + "target_field": "ml.classification", + "inference_config": { + "classification": { + "num_top_classes": 2, + "top_classes_results_field": "result_class_prob", + "num_top_feature_importance_values": 2 + } + }, + "model_id": "%s", + "field_map": {} + } + } + ] + }, + "docs": [ + { + "_source": { + "col_1_alias": "female", + "col2": "M", + "col3": "none", + "col4": 10 + } + } + ] + }""".formatted(classificationModelId); Response response = client().performRequest(simulateRequest(source)); String responseString = EntityUtils.toString(response.getEntity()); @@ -421,68 +434,69 @@ public void testSimulateWithDefaultMappedField() throws IOException { } public void testSimulateLangIdent() throws IOException { - String source = "{\n" - + " \"pipeline\": {\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"inference_config\": {\"classification\":{}},\n" - + " \"model_id\": \"lang_ident_model_1\",\n" - + " \"field_map\": {}\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"docs\": [\n" - + " {\"_source\": {\n" - + " \"text\": \"this is some plain text.\"\n" - + " }}]\n" - + "}"; + String source = """ + { + "pipeline": { + "processors": [ + { + "inference": { + "inference_config": {"classification":{}}, + "model_id": "lang_ident_model_1", + "field_map": {} + } + } + ] + }, + "docs": [ + {"_source": { + "text": "this is some plain text." + }}] + }"""; Response response = client().performRequest(simulateRequest(source)); assertThat(EntityUtils.toString(response.getEntity()), containsString("\"predicted_value\":\"en\"")); } public void testSimulateLangIdentForeach() throws IOException { - String source = "{" - + " \"pipeline\": {\n" - + " \"description\": \"detect text lang\",\n" - + " \"processors\": [\n" - + " {\n" - + " \"foreach\": {\n" - + " \"field\": \"greetings\",\n" - + " \"processor\": {\n" - + " \"inference\": {\n" - + " \"model_id\": \"lang_ident_model_1\",\n" - + " \"inference_config\": {\n" - + " \"classification\": {\n" - + " \"num_top_classes\": 5\n" - + " }\n" - + " },\n" - + " \"field_map\": {\n" - + " \"_ingest._value.text\": \"text\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"docs\": [\n" - + " {\n" - + " \"_source\": {\n" - + " \"greetings\": [\n" - + " {\n" - + " \"text\": \" a backup credit card by visiting your billing preferences page or visit the adwords help\"\n" - + " },\n" - + " {\n" - + " \"text\": \" 개별적으로 리포트 액세스 권한을 부여할 수 있습니다 액세스 권한 부여사용자에게 프로필 리포트에 \"\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + " ]\n" - + "}"; + String source = """ + { "pipeline": { + "description": "detect text lang", + "processors": [ + { + "foreach": { + "field": "greetings", + "processor": { + "inference": { + "model_id": "lang_ident_model_1", + "inference_config": { + "classification": { + "num_top_classes": 5 + } + }, + "field_map": { + "_ingest._value.text": "text" + } + } + } + } + } + ] + }, + "docs": [ + { + "_source": { + "greetings": [ + { + "text": " a backup credit card by visiting your billing preferences page or visit the adwords help" + }, + { + "text": " 개별적으로 리포트 액세스 권한을 부여할 수 있습니다 액세스 권한 부여사용자에게 프로필 리포트에 " + } + ] + } + } + ] + }"""; Response response = client().performRequest(simulateRequest(source)); String stringResponse = EntityUtils.toString(response.getEntity()); assertThat(stringResponse, containsString("\"predicted_value\":\"en\"")); @@ -537,176 +551,189 @@ private Map generateSourceDoc() { }; } - private static final String REGRESSION_DEFINITION = "{" - + " \"preprocessors\": [\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"col1\",\n" - + " \"hot_map\": {\n" - + " \"male\": \"col1_male\",\n" - + " \"female\": \"col1_female\"\n" - + " }\n" - + " }\n" - + " },\n" - + " {\n" - + " \"target_mean_encoding\": {\n" - + " \"field\": \"col2\",\n" - + " \"feature_name\": \"col2_encoded\",\n" - + " \"target_map\": {\n" - + " \"S\": 5.0,\n" - + " \"M\": 10.0,\n" - + " \"L\": 20\n" - + " },\n" - + " \"default_value\": 5.0\n" - + " }\n" - + " },\n" - + " {\n" - + " \"frequency_encoding\": {\n" - + " \"field\": \"col3\",\n" - + " \"feature_name\": \"col3_encoded\",\n" - + " \"frequency_map\": {\n" - + " \"none\": 0.75,\n" - + " \"true\": 0.10,\n" - + " \"false\": 0.15\n" - + " }\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"trained_model\": {\n" - + " \"ensemble\": {\n" - + " \"feature_names\": [\n" - + " \"col1_male\",\n" - + " \"col1_female\",\n" - + " \"col2_encoded\",\n" - + " \"col3_encoded\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"aggregate_output\": {\n" - + " \"weighted_sum\": {\n" - + " \"weights\": [\n" - + " 0.5,\n" - + " 0.5\n" - + " ]\n" - + " }\n" - + " },\n" - + " \"target_type\": \"regression\",\n" - + " \"trained_models\": [\n" - + " {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"col1_male\",\n" - + " \"col1_female\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"split_gain\": 12.0,\n" - + " \"threshold\": 10.0,\n" - + " \"decision_type\": \"lte\",\n" - + " \"number_samples\": 300,\n" - + " \"default_left\": true,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"number_samples\": 100,\n" - + " \"leaf_value\": 1\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"number_samples\": 200,\n" - + " \"leaf_value\": 2\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " },\n" - + " {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"col2_encoded\",\n" - + " \"col3_encoded\",\n" - + " \"col4\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"split_gain\": 12.0,\n" - + " \"threshold\": 10.0,\n" - + " \"decision_type\": \"lte\",\n" - + " \"default_left\": true,\n" - + " \"number_samples\": 150,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"number_samples\": 50,\n" - + " \"leaf_value\": 1\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"number_samples\": 100,\n" - + " \"leaf_value\": 2\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + "}"; - - private static final String REGRESSION_CONFIG = "{" - + " \"input\":{\"field_names\":[\"col1\",\"col2\",\"col3\",\"col4\"]}," - + " \"description\": \"test model for regression\",\n" - + " \"inference_config\": {\"regression\": {}},\n" - + " \"definition\": " - + REGRESSION_DEFINITION - + "}"; + private static final String REGRESSION_DEFINITION = """ + { "preprocessors": [ + { + "one_hot_encoding": { + "field": "col1", + "hot_map": { + "male": "col1_male", + "female": "col1_female" + } + } + }, + { + "target_mean_encoding": { + "field": "col2", + "feature_name": "col2_encoded", + "target_map": { + "S": 5.0, + "M": 10.0, + "L": 20 + }, + "default_value": 5.0 + } + }, + { + "frequency_encoding": { + "field": "col3", + "feature_name": "col3_encoded", + "frequency_map": { + "none": 0.75, + "true": 0.10, + "false": 0.15 + } + } + } + ], + "trained_model": { + "ensemble": { + "feature_names": [ + "col1_male", + "col1_female", + "col2_encoded", + "col3_encoded", + "col4" + ], + "aggregate_output": { + "weighted_sum": { + "weights": [ + 0.5, + 0.5 + ] + } + }, + "target_type": "regression", + "trained_models": [ + { + "tree": { + "feature_names": [ + "col1_male", + "col1_female", + "col4" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "split_gain": 12.0, + "threshold": 10.0, + "decision_type": "lte", + "number_samples": 300, + "default_left": true, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "number_samples": 100, + "leaf_value": 1 + }, + { + "node_index": 2, + "number_samples": 200, + "leaf_value": 2 + } + ], + "target_type": "regression" + } + }, + { + "tree": { + "feature_names": [ + "col2_encoded", + "col3_encoded", + "col4" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "split_gain": 12.0, + "threshold": 10.0, + "decision_type": "lte", + "default_left": true, + "number_samples": 150, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "number_samples": 50, + "leaf_value": 1 + }, + { + "node_index": 2, + "number_samples": 100, + "leaf_value": 2 + } + ], + "target_type": "regression" + } + } + ] + } + } + }"""; + + private static final String REGRESSION_CONFIG = """ + { + "input": { + "field_names": [ + "col1", + "col2", + "col3", + "col4" + ] + }, + "description": "test model for regression", + "inference_config": { + "regression": {} + }, + "definition": %s + }""".formatted(REGRESSION_DEFINITION); @Override protected NamedXContentRegistry xContentRegistry() { return new NamedXContentRegistry(new MlInferenceNamedXContentProvider().getNamedXContentParsers()); } - private static final String CLASSIFICATION_CONFIG = "" - + "{\n" - + " \"input\":{\"field_names\":[\"col1\",\"col2\",\"col3\",\"col4\"]}," - + " \"description\": \"test model for classification\",\n" - + " \"default_field_map\": {\"col_1_alias\": \"col1\"},\n" - + " \"inference_config\": {\"classification\": {}},\n" - + " \"definition\": " - + InferenceDefinitionTests.getClassificationDefinition(false) - + "}"; + private static final String CLASSIFICATION_CONFIG = """ + { + "input": { + "field_names": [ "col1", "col2", "col3", "col4" ] + }, + "description": "test model for classification", + "default_field_map": { + "col_1_alias": "col1" + }, + "inference_config": { + "classification": {} + }, + "definition": %s + }""".formatted(InferenceDefinitionTests.getClassificationDefinition(false)); private static String pipelineDefinition(String modelId, String inferenceConfig) { - return "{" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"model_id\": \"" - + modelId - + "\",\n" - + " \"tag\": \"" - + inferenceConfig - + "\",\n" - + " \"inference_config\": {\"" - + inferenceConfig - + "\": {}},\n" - + " \"field_map\": {\n" - + " \"col1\": \"col1\",\n" - + " \"col2\": \"col2\",\n" - + " \"col3\": \"col3\",\n" - + " \"col4\": \"col4\"\n" - + " }\n" - + " }\n" - + " }]}\n"; + return """ + { + "processors": [ + { + "inference": { + "model_id": "%s", + "tag": "%s", + "inference_config": { + "%s": {} + }, + "field_map": { + "col1": "col1", + "col2": "col2", + "col3": "col3", + "col4": "col4" + } + } + } + ] + }""".formatted(modelId, inferenceConfig, inferenceConfig); } private void putModel(String modelId, String modelConfiguration) throws IOException { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java index fd394354fbb1f..4679b3f4d5610 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InterimResultsIT.java @@ -68,9 +68,11 @@ public void testInterimResultsUpdates() throws Exception { assertThat(time, equalTo(1400040000L)); // push some data up to a 1/4 bucket boundary, flush (with interim), check interim results - String data = "{\"time\":1400040000,\"value\":14}\n" - + "{\"time\":1400040500,\"value\":12}\n" - + "{\"time\":1400040510,\"value\":16}\n"; + String data = """ + {"time":1400040000,"value":14} + {"time":1400040500,"value":12} + {"time":1400040510,"value":16} + """; assertThat(postData(job.getId(), data).getProcessedRecordCount(), equalTo(3L)); flushJob(job.getId(), true); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java index 4ee80fb30e381..75a16e393af0b 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java @@ -151,29 +151,28 @@ public void testUsage() throws IOException { } private Response createFarequoteJob(String jobId) throws IOException { - return putJob( - jobId, - "{\n" - + " \"description\":\"Analysis of response time by airline\",\n" - + " \"analysis_config\" : {\n" - + " \"bucket_span\": \"3600s\",\n" - + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\",\"by_field_name\":\"airline\"}]\n" - + " },\n" - + " \"data_description\" : {\n" - + " \"time_field\":\"time\",\n" - + " \"time_format\":\"yyyy-MM-dd HH:mm:ssX\"\n" - + " }\n" - + "}" - ); + return putJob(jobId, """ + { + "description":"Analysis of response time by airline", + "analysis_config" : { + "bucket_span": "3600s", + "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}] + }, + "data_description" : { + "time_field":"time", + "time_format":"yyyy-MM-dd HH:mm:ssX" + } + }"""); } public void testCantCreateJobWithSameID() throws Exception { - String jobTemplate = "{\n" - + " \"analysis_config\" : {\n" - + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\"}]\n" - + " },\n" - + " \"data_description\": {},\n" - + " \"results_index_name\" : \"%s\"}"; + String jobTemplate = """ + { + "analysis_config" : { + "detectors" :[{"function":"metric","field_name":"responsetime"}] + }, + "data_description": {}, + "results_index_name" : "%s"}"""; String jobId = "cant-create-job-with-same-id-job"; putJob(jobId, String.format(Locale.ROOT, jobTemplate, "index-1")); @@ -187,12 +186,13 @@ public void testCantCreateJobWithSameID() throws Exception { } public void testCreateJobsWithIndexNameOption() throws Exception { - String jobTemplate = "{\n" - + " \"analysis_config\" : {\n" - + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\"}]\n" - + " },\n" - + " \"data_description\": {},\n" - + " \"results_index_name\" : \"%s\"}"; + String jobTemplate = """ + { + "analysis_config" : { + "detectors" :[{"function":"metric","field_name":"responsetime"}] + }, + "data_description": {}, + "results_index_name" : "%s"}"""; String jobId1 = "create-jobs-with-index-name-option-job-1"; String indexName = "non-default-index"; @@ -207,38 +207,32 @@ public void testCreateJobsWithIndexNameOption() throws Exception { assertBusy(() -> { try { String aliasesResponse = getAliases(); - assertThat( - aliasesResponse, - containsString("\"" + AnomalyDetectorsIndex.jobResultsAliasedName("custom-" + indexName) + "\":{\"aliases\":{") - ); + assertThat(aliasesResponse, containsString(""" + "%s":{"aliases":{""".formatted(AnomalyDetectorsIndex.jobResultsAliasedName("custom-" + indexName)))); assertThat( aliasesResponse, containsString( - "\"" - + AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) - + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" - + jobId1 - + "\",\"boost\":1.0}}},\"is_hidden\":true}" + """ + "%s":{"filter":{"term":{"job_id":{"value":"%s","boost":1.0}}},"is_hidden":true}""".formatted( + AnomalyDetectorsIndex.jobResultsAliasedName(jobId1), + jobId1 + ) ) ); - assertThat( - aliasesResponse, - containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId1) + "\":{\"is_hidden\":true}") - ); + assertThat(aliasesResponse, containsString(""" + "%s":{"is_hidden":true}""".formatted(AnomalyDetectorsIndex.resultsWriteAlias(jobId1)))); assertThat( aliasesResponse, containsString( - "\"" - + AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) - + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" - + jobId2 - + "\",\"boost\":1.0}}},\"is_hidden\":true}" + """ + "%s":{"filter":{"term":{"job_id":{"value":"%s","boost":1.0}}},"is_hidden":true}""".formatted( + AnomalyDetectorsIndex.jobResultsAliasedName(jobId2), + jobId2 + ) ) ); - assertThat( - aliasesResponse, - containsString("\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId2) + "\":{\"is_hidden\":true}") - ); + assertThat(aliasesResponse, containsString(""" + "%s":{"is_hidden":true}""".formatted(AnomalyDetectorsIndex.resultsWriteAlias(jobId2)))); } catch (ResponseException e) { throw new AssertionError(e); } @@ -256,28 +250,14 @@ public void testCreateJobsWithIndexNameOption() throws Exception { { // create jobId1 docs String id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId1, "1234", 300); Request createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) + "/_doc/" + id); - createResultRequest.setJsonEntity( - String.format( - Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId1, - "1234", - 1 - ) - ); + createResultRequest.setJsonEntity(String.format(Locale.ROOT, """ + {"job_id":"%s", "timestamp": "%s", "result_type":"bucket", "bucket_span": "%s"}""", jobId1, "1234", 1)); client().performRequest(createResultRequest); id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId1, "1236", 300); createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId1) + "/_doc/" + id); - createResultRequest.setJsonEntity( - String.format( - Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId1, - "1236", - 1 - ) - ); + createResultRequest.setJsonEntity(String.format(Locale.ROOT, """ + {"job_id":"%s", "timestamp": "%s", "result_type":"bucket", "bucket_span": "%s"}""", jobId1, "1236", 1)); client().performRequest(createResultRequest); refreshAllIndices(); @@ -296,28 +276,14 @@ public void testCreateJobsWithIndexNameOption() throws Exception { { // create jobId2 docs String id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId2, "1234", 300); Request createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) + "/_doc/" + id); - createResultRequest.setJsonEntity( - String.format( - Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId2, - "1234", - 1 - ) - ); + createResultRequest.setJsonEntity(String.format(Locale.ROOT, """ + {"job_id":"%s", "timestamp": "%s", "result_type":"bucket", "bucket_span": "%s"}""", jobId2, "1234", 1)); client().performRequest(createResultRequest); id = String.format(Locale.ROOT, "%s_bucket_%s_%s", jobId2, "1236", 300); createResultRequest = new Request("PUT", AnomalyDetectorsIndex.jobResultsAliasedName(jobId2) + "/_doc/" + id); - createResultRequest.setJsonEntity( - String.format( - Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"result_type\":\"bucket\", \"bucket_span\": \"%s\"}", - jobId2, - "1236", - 1 - ) - ); + createResultRequest.setJsonEntity(String.format(Locale.ROOT, """ + {"job_id":"%s", "timestamp": "%s", "result_type":"bucket", "bucket_span": "%s"}""", jobId2, "1236", 1)); client().performRequest(createResultRequest); refreshAllIndices(); @@ -370,12 +336,13 @@ public void testCreateJobsWithIndexNameOption() throws Exception { } public void testCreateJobInSharedIndexUpdatesMapping() throws Exception { - String jobTemplate = "{\n" - + " \"analysis_config\" : {\n" - + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" - + " },\n" - + " \"data_description\": {}\n" - + "}"; + String jobTemplate = """ + { + "analysis_config" : { + "detectors" :[{"function":"metric","field_name":"metric", "by_field_name":"%s"}] + }, + "data_description": {} + }"""; String jobId1 = "create-job-in-shared-index-updates-mapping-job-1"; String byFieldName1 = "responsetime"; @@ -402,12 +369,13 @@ public void testCreateJobInSharedIndexUpdatesMapping() throws Exception { } public void testCreateJobInCustomSharedIndexUpdatesMapping() throws Exception { - String jobTemplate = "{\n" - + " \"analysis_config\" : {\n" - + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" - + " },\n" - + " \"data_description\": {},\n" - + " \"results_index_name\" : \"shared-index\"}"; + String jobTemplate = """ + { + "analysis_config" : { + "detectors" :[{"function":"metric","field_name":"metric", "by_field_name":"%s"}] + }, + "data_description": {}, + "results_index_name" : "shared-index"}"""; String jobId1 = "create-job-in-custom-shared-index-updates-mapping-job-1"; String byFieldName1 = "responsetime"; @@ -435,12 +403,13 @@ public void testCreateJobInCustomSharedIndexUpdatesMapping() throws Exception { } public void testCreateJob_WithClashingFieldMappingsFails() throws Exception { - String jobTemplate = "{\n" - + " \"analysis_config\" : {\n" - + " \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"metric\", \"by_field_name\":\"%s\"}]\n" - + " },\n" - + " \"data_description\": {}\n" - + "}"; + String jobTemplate = """ + { + "analysis_config" : { + "detectors" :[{"function":"metric","field_name":"metric", "by_field_name":"%s"}] + }, + "data_description": {} + }"""; String jobId1 = "job-with-response-field"; String byFieldName1; @@ -475,9 +444,12 @@ public void testOpenJobFailsWhenPersistentTaskAssignmentDisabled() throws Except createFarequoteJob(jobId); Request disablePersistentTaskAssignmentRequest = new Request("PUT", "_cluster/settings"); - disablePersistentTaskAssignmentRequest.setJsonEntity( - "{\n" + " \"persistent\": {\n" + " \"cluster.persistent_tasks.allocation.enable\": \"none\"\n" + " }\n" + "}" - ); + disablePersistentTaskAssignmentRequest.setJsonEntity(""" + { + "persistent": { + "cluster.persistent_tasks.allocation.enable": "none" + } + }"""); Response disablePersistentTaskAssignmentResponse = client().performRequest(disablePersistentTaskAssignmentRequest); assertThat(entityAsMap(disablePersistentTaskAssignmentResponse), hasEntry("acknowledged", true)); @@ -495,9 +467,12 @@ public void testOpenJobFailsWhenPersistentTaskAssignmentDisabled() throws Except // Try to revert the cluster setting change even if the test fails, // because otherwise this setting will cause many other tests to fail Request enablePersistentTaskAssignmentRequest = new Request("PUT", "_cluster/settings"); - enablePersistentTaskAssignmentRequest.setJsonEntity( - "{\n" + " \"persistent\": {\n" + " \"cluster.persistent_tasks.allocation.enable\": \"all\"\n" + " }\n" + "}" - ); + enablePersistentTaskAssignmentRequest.setJsonEntity(""" + { + "persistent": { + "cluster.persistent_tasks.allocation.enable": "all" + } + }"""); Response enablePersistentTaskAssignmentResponse = client().performRequest(disablePersistentTaskAssignmentRequest); assertThat(entityAsMap(enablePersistentTaskAssignmentResponse), hasEntry("acknowledged", true)); } @@ -584,9 +559,11 @@ public void testDeleteJob_TimingStatsDocumentIsDeleted() throws Exception { Request postDataRequest = new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId + "/_data"); // Post data is deprecated, so expect a deprecation warning postDataRequest.setOptions(POST_DATA); - postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"response_time\":100, \"time\":\"2019-07-01 00:00:00Z\" }"); + postDataRequest.setJsonEntity(""" + { "airline":"LOT", "response_time":100, "time":"2019-07-01 00:00:00Z" }"""); client().performRequest(postDataRequest); - postDataRequest.setJsonEntity("{ \"airline\":\"LOT\", \"response_time\":100, \"time\":\"2019-07-01 02:00:00Z\" }"); + postDataRequest.setJsonEntity(""" + { "airline":"LOT", "response_time":100, "time":"2019-07-01 02:00:00Z" }"""); client().performRequest(postDataRequest); Response flushResponse = client().performRequest( @@ -749,44 +726,34 @@ public void testMultiIndexDelete() throws Exception { // Make the job's results span an extra two indices, i.e. three in total. // To do this the job's results alias needs to encompass all three indices. Request extraIndex1 = new Request("PUT", indexName + "-001"); - extraIndex1.setJsonEntity( - "{\n" - + " \"aliases\" : {\n" - + " \"" - + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) - + "\" : {\n" - + " \"is_hidden\" : true,\n" - + " \"filter\" : {\n" - + " \"term\" : {\"" - + Job.ID - + "\" : \"" - + jobId - + "\" }\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + extraIndex1.setJsonEntity(""" + { + "aliases": { + "%s": { + "is_hidden": true, + "filter": { + "term": { + "%s": "%s" + } + } + } + } + }""".formatted(AnomalyDetectorsIndex.jobResultsAliasedName(jobId), Job.ID, jobId)); client().performRequest(extraIndex1); Request extraIndex2 = new Request("PUT", indexName + "-002"); - extraIndex2.setJsonEntity( - "{\n" - + " \"aliases\" : {\n" - + " \"" - + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) - + "\" : {\n" - + " \"is_hidden\" : true,\n" - + " \"filter\" : {\n" - + " \"term\" : {\"" - + Job.ID - + "\" : \"" - + jobId - + "\" }\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + extraIndex2.setJsonEntity(""" + { + "aliases": { + "%s": { + "is_hidden": true, + "filter": { + "term": { + "%s": "%s" + } + } + } + } + }""".formatted(AnomalyDetectorsIndex.jobResultsAliasedName(jobId), Job.ID, jobId)); client().performRequest(extraIndex2); // Use _cat/indices/.ml-anomalies-* instead of _cat/indices/_all to workaround https://github.com/elastic/elasticsearch/issues/45652 @@ -800,15 +767,8 @@ public void testMultiIndexDelete() throws Exception { // Add some documents to each index to make sure the DBQ clears them out Request createDoc0 = new Request("PUT", indexName + "/_doc/" + 123); - createDoc0.setJsonEntity( - String.format( - Locale.ROOT, - "{\"job_id\":\"%s\", \"timestamp\": \"%s\", \"bucket_span\":%d, \"result_type\":\"record\"}", - jobId, - 123, - 1 - ) - ); + createDoc0.setJsonEntity(String.format(Locale.ROOT, """ + {"job_id":"%s", "timestamp": "%s", "bucket_span":%d, "result_type":"record"}""", jobId, 123, 1)); client().performRequest(createDoc0); Request createDoc1 = new Request("PUT", indexName + "-001/_doc/" + 123); createDoc1.setEntity(createDoc0.getEntity()); @@ -947,12 +907,13 @@ public void testDelete_multipleRequest() throws Exception { assertNull(recreationException.get().getMessage(), recreationException.get()); } - String expectedReadAliasString = "\"" - + AnomalyDetectorsIndex.jobResultsAliasedName(jobId) - + "\":{\"filter\":{\"term\":{\"job_id\":{\"value\":\"" - + jobId - + "\",\"boost\":1.0}}},\"is_hidden\":true}"; - String expectedWriteAliasString = "\"" + AnomalyDetectorsIndex.resultsWriteAlias(jobId) + "\":{\"is_hidden\":true}"; + String expectedReadAliasString = """ + "%s":{"filter":{"term":{"job_id":{"value":"%s","boost":1.0}}},"is_hidden":true}""".formatted( + AnomalyDetectorsIndex.jobResultsAliasedName(jobId), + jobId + ); + String expectedWriteAliasString = """ + "%s":{"is_hidden":true}""".formatted(AnomalyDetectorsIndex.resultsWriteAlias(jobId)); try { // The idea of the code above is that the deletion is sufficiently time-consuming that // all threads enter the deletion call before the first one exits it. Usually this happens, diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java index c759f114825fe..6090648310b14 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/PyTorchModelIT.java @@ -118,16 +118,12 @@ protected Settings restClientSettings() { @Before public void setLogging() throws IOException { Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity( - "" - + "{" - + "\"persistent\" : {\n" - + " \"logger.org.elasticsearch.xpack.ml.inference.allocation\" : \"TRACE\",\n" - + " \"logger.org.elasticsearch.xpack.ml.inference.deployment\" : \"TRACE\",\n" - + " \"logger.org.elasticsearch.xpack.ml.process.logging\" : \"TRACE\"\n" - + " }" - + "}" - ); + loggingSettings.setJsonEntity(""" + {"persistent" : { + "logger.org.elasticsearch.xpack.ml.inference.allocation" : "TRACE", + "logger.org.elasticsearch.xpack.ml.inference.deployment" : "TRACE", + "logger.org.elasticsearch.xpack.ml.process.logging" : "TRACE" + }}"""); client().performRequest(loggingSettings); } @@ -136,16 +132,12 @@ public void cleanup() throws Exception { terminate(executorService); Request loggingSettings = new Request("PUT", "_cluster/settings"); - loggingSettings.setJsonEntity( - "" - + "{" - + "\"persistent\" : {\n" - + " \"logger.org.elasticsearch.xpack.ml.inference.allocation\": null,\n" - + " \"logger.org.elasticsearch.xpack.ml.inference.deployment\" : null,\n" - + " \"logger.org.elasticsearch.xpack.ml.process.logging\" : null\n" - + " }" - + "}" - ); + loggingSettings.setJsonEntity(""" + {"persistent" : { + "logger.org.elasticsearch.xpack.ml.inference.allocation": null, + "logger.org.elasticsearch.xpack.ml.inference.deployment" : null, + "logger.org.elasticsearch.xpack.ml.process.logging" : null + }}"""); client().performRequest(loggingSettings); new MlRestTestStateCleaner(logger, adminClient()).resetFeatures(); @@ -418,18 +410,8 @@ public void testStartDeploymentWithTruncatedDefinition() throws IOException { createTrainedModel(model); putVocabulary(List.of("once", "twice"), model); Request request = new Request("PUT", "_ml/trained_models/" + model + "/definition/0"); - request.setJsonEntity( - "{ " - + "\"total_definition_length\":" - + RAW_MODEL_SIZE - + 2L - + "," - + "\"definition\": \"" - + BASE_64_ENCODED_MODEL - + "\"," - + "\"total_parts\": 1" - + "}" - ); + request.setJsonEntity(""" + {"total_definition_length":%s2,"definition": "%s","total_parts": 1}""".formatted(RAW_MODEL_SIZE, BASE_64_ENCODED_MODEL)); client().performRequest(request); Exception ex = expectThrows(Exception.class, () -> startDeployment(model)); assertThat( @@ -444,24 +426,25 @@ public void testInferencePipelineAgainstUnallocatedModel() throws IOException { putVocabulary(List.of("once", "twice"), model); putModelDefinition(model); - String source = "{\n" - + " \"pipeline\": {\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"model_id\": \"not-deployed\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " },\n" - + " \"docs\": [\n" - + " {\n" - + " \"_source\": {\n" - + " \"input\": \"my words\"\n" - + " }\n" - + " }\n" - + " ]\n" - + "}"; + String source = """ + { + "pipeline": { + "processors": [ + { + "inference": { + "model_id": "not-deployed" + } + } + ] + }, + "docs": [ + { + "_source": { + "input": "my words" + } + } + ] + }"""; String response = EntityUtils.toString(client().performRequest(simulateRequest(source)).getEntity()); assertThat( @@ -473,23 +456,22 @@ public void testInferencePipelineAgainstUnallocatedModel() throws IOException { ) ); - client().performRequest( - putPipeline( - "my_pipeline", - "{" - + "\"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"model_id\": \"not-deployed\"\n" - + " }\n" - + " }\n" - + " ]\n" - + "}" - ) - ); + client().performRequest(putPipeline("my_pipeline", """ + {"processors": [ + { + "inference": { + "model_id": "not-deployed" + } + } + ] + }""")); Request request = new Request("PUT", "undeployed_model_index/_doc/1?pipeline=my_pipeline&refresh=true"); - request.setJsonEntity("{\n" + " \"input\": \"my words\"\n" + " }\n"); + request.setJsonEntity(""" + { + "input": "my words" + } + """); Exception ex = expectThrows(Exception.class, () -> client().performRequest(request)); assertThat( ex.getMessage(), @@ -501,23 +483,22 @@ public void testTruncation() throws IOException { String modelId = "no-truncation"; Request request = new Request("PUT", "/_ml/trained_models/" + modelId); - request.setJsonEntity( - "{ " - + " \"description\": \"simple model for testing\",\n" - + " \"model_type\": \"pytorch\",\n" - + " \"inference_config\": {\n" - + " \"pass_through\": {\n" - + " \"tokenization\": {" - + " \"bert\": {" - + " \"with_special_tokens\": false," - + " \"truncate\": \"none\"," - + " \"max_sequence_length\": 2" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + request.setJsonEntity(""" + { + "description": "simple model for testing", + "model_type": "pytorch", + "inference_config": { + "pass_through": { + "tokenization": { + "bert": { + "with_special_tokens": false, + "truncate": "none", + "max_sequence_length": 2 + } + } + } + } + }"""); client().performRequest(request); putVocabulary(List.of("once", "twice", "thrice"), modelId); @@ -531,18 +512,23 @@ public void testTruncation() throws IOException { ); request = new Request("POST", "/_ml/trained_models/" + modelId + "/deployment/_infer"); - request.setJsonEntity( - "{" - + "\"docs\": [{\"input\":\"" - + input - + "\"}]," - + "\"inference_config\": { " - + " \"pass_through\": {" - + " \"tokenization\": {\"bert\": {\"truncate\": \"first\"}}" - + " }" - + " }" - + "}" - ); + request.setJsonEntity(""" + { + "docs": [ + { + "input": "%s" + } + ], + "inference_config": { + "pass_through": { + "tokenization": { + "bert": { + "truncate": "first" + } + } + } + } + }""".formatted(input)); client().performRequest(request); } @@ -553,22 +539,16 @@ public void testStopUsedDeploymentByIngestProcessor() throws IOException { putVocabulary(List.of("these", "are", "my", "words"), modelId); startDeployment(modelId); - client().performRequest( - putPipeline( - "my_pipeline", - "{" - + "\"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"model_id\": \"" - + modelId - + "\"\n" - + " }\n" - + " }\n" - + " ]\n" - + "}" - ) - ); + client().performRequest(putPipeline("my_pipeline", """ + { + "processors": [ + { + "inference": { + "model_id": "%s" + } + } + ] + }""".formatted(modelId))); ResponseException ex = expectThrows(ResponseException.class, () -> stopDeployment(modelId)); assertThat(ex.getResponse().getStatusLine().getStatusCode(), equalTo(409)); assertThat( @@ -670,17 +650,8 @@ private int sumInferenceCountOnNodes(List> nodes) { private void putModelDefinition(String modelId) throws IOException { Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/definition/0"); - request.setJsonEntity( - "{ " - + "\"total_definition_length\":" - + RAW_MODEL_SIZE - + "," - + "\"definition\": \"" - + BASE_64_ENCODED_MODEL - + "\"," - + "\"total_parts\": 1" - + "}" - ); + request.setJsonEntity(""" + {"total_definition_length":%s,"definition": "%s","total_parts": 1}""".formatted(RAW_MODEL_SIZE, BASE_64_ENCODED_MODEL)); client().performRequest(request); } @@ -692,25 +663,28 @@ private void putVocabulary(List vocabulary, String modelId) throws IOExc String quotedWords = vocabularyWithPad.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(",")); Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/vocabulary"); - request.setJsonEntity("{ " + "\"vocabulary\": [" + quotedWords + "]\n" + "}"); + request.setJsonEntity(""" + { "vocabulary": [%s] } + """.formatted(quotedWords)); client().performRequest(request); } private void createTrainedModel(String modelId) throws IOException { Request request = new Request("PUT", "/_ml/trained_models/" + modelId); - request.setJsonEntity( - "{ " - + " \"description\": \"simple model for testing\",\n" - + " \"model_type\": \"pytorch\",\n" - + " \"inference_config\": {\n" - + " \"pass_through\": {\n" - + " \"tokenization\": {" - + " \"bert\": {\"with_special_tokens\": false}\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + request.setJsonEntity(""" + { + "description": "simple model for testing", + "model_type": "pytorch", + "inference_config": { + "pass_through": { + "tokenization": { + "bert": { + "with_special_tokens": false + } + } + } + } + }"""); client().performRequest(request); } @@ -750,28 +724,31 @@ private Response getTrainedModelStats(String modelId) throws IOException { private Response infer(String input, String modelId, TimeValue timeout) throws IOException { Request request = new Request("POST", "/_ml/trained_models/" + modelId + "/deployment/_infer?timeout=" + timeout.toString()); - request.setJsonEntity("{ " + "\"docs\": [{\"input\":\"" + input + "\"}]\n" + "}"); + request.setJsonEntity(""" + { "docs": [{"input":"%s"}] } + """.formatted(input)); return client().performRequest(request); } private Response infer(String input, String modelId) throws IOException { Request request = new Request("POST", "/_ml/trained_models/" + modelId + "/deployment/_infer"); - request.setJsonEntity("{ " + "\"docs\": [{\"input\":\"" + input + "\"}]\n" + "}"); + request.setJsonEntity(""" + { "docs": [{"input":"%s"}] } + """.formatted(input)); return client().performRequest(request); } private Response infer(String input, String modelId, String resultsField) throws IOException { Request request = new Request("POST", "/_ml/trained_models/" + modelId + "/deployment/_infer"); - request.setJsonEntity( - "{ " - + "\"docs\": [{\"input\":\"" - + input - + "\"}],\n" - + "\"inference_config\": {\"pass_through\":{\"results_field\": \"" - + resultsField - + "\"}}\n" - + "}" - ); + request.setJsonEntity(""" + { + "docs": [ { "input": "%s" } ], + "inference_config": { + "pass_through": { + "results_field": "%s" + } + } + }""".formatted(input, resultsField)); return client().performRequest(request); } diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java index 478c15d9237b0..a846087972aee 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RegressionIT.java @@ -562,20 +562,21 @@ public void testAliasFields() throws Exception { initialize("regression_alias_fields"); String predictionField = "field_2_prediction"; - String mapping = "{\n" - + " \"properties\": {\n" - + " \"field_1\": {\n" - + " \"type\": \"integer\"\n" - + " }," - + " \"field_2\": {\n" - + " \"type\": \"integer\"\n" - + " }," - + " \"field_1_alias\": {\n" - + " \"type\": \"alias\",\n" - + " \"path\": \"field_1\"\n" - + " }" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "field_1": { + "type": "integer" + }, + "field_2": { + "type": "integer" + }, + "field_1_alias": { + "type": "alias", + "path": "field_1" + } + } + }"""; client().admin().indices().prepareCreate(sourceIndex).setMapping(mapping).get(); int totalDocCount = 300; @@ -878,28 +879,23 @@ static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainin } static void indexData(String sourceIndex, int numTrainingRows, int numNonTrainingRows, boolean dataStream) { - String mapping = "{\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }," - + " \"" - + NUMERICAL_FEATURE_FIELD - + "\": {\n" - + " \"type\": \"double\"\n" - + " }," - + " \"" - + DISCRETE_NUMERICAL_FEATURE_FIELD - + "\": {\n" - + " \"type\": \"unsigned_long\"\n" - + " }," - + " \"" - + DEPENDENT_VARIABLE_FIELD - + "\": {\n" - + " \"type\": \"double\"\n" - + " }" - + " }\n" - + " }"; + String mapping = """ + { + "properties": { + "@timestamp": { + "type": "date" + }, + "%s": { + "type": "double" + }, + "%s": { + "type": "unsigned_long" + }, + "%s": { + "type": "double" + } + } + }""".formatted(NUMERICAL_FEATURE_FIELD, DISCRETE_NUMERICAL_FEATURE_FIELD, DEPENDENT_VARIABLE_FIELD); if (dataStream) { try { createDataStreamAndTemplate(sourceIndex, mapping); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java index cb931bedce339..4cb39bfaeee12 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/RunDataFrameAnalyticsIT.java @@ -829,8 +829,19 @@ public void testOutlierDetectionWithCustomParams() throws Exception { public void testOutlierDetection_GivenIndexWithRuntimeFields() throws Exception { String sourceIndex = "test-outlier-detection-with-index-with-runtime-fields"; - String mappings = "{\"dynamic\":false, \"runtime\": { \"runtime_numeric\": " - + "{ \"type\": \"double\", \"script\": { \"source\": \"emit(params._source.numeric)\", \"lang\": \"painless\" } } }}"; + String mappings = """ + { + "dynamic": false, + "runtime": { + "runtime_numeric": { + "type": "double", + "script": { + "source": "emit(params._source.numeric)", + "lang": "painless" + } + } + } + }"""; client().admin().indices().prepareCreate(sourceIndex).setMapping(mappings).get(); diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java index 4a32d3bd139bb..f763a37681a34 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureResetIT.java @@ -290,26 +290,18 @@ private void startRealtime(String jobId) throws Exception { } private void putTrainedModelIngestPipeline(String pipelineId) throws Exception { - client().execute( - PutPipelineAction.INSTANCE, - new PutPipelineRequest( - pipelineId, - new BytesArray( - "{\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"inference_config\": {\"classification\":{}},\n" - + " \"model_id\": \"lang_ident_model_1\",\n" - + " \"field_map\": {}\n" - + " }\n" - + " }\n" - + " ]\n" - + " }" - ), - XContentType.JSON - ) - ).actionGet(); + client().execute(PutPipelineAction.INSTANCE, new PutPipelineRequest(pipelineId, new BytesArray(""" + { + "processors": [ + { + "inference": { + "inference_config": {"classification":{}}, + "model_id": "lang_ident_model_1", + "field_map": {} + } + } + ] + }"""), XContentType.JSON)).actionGet(); } private void indexDocForInference(String pipelineId) { diff --git a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java index 5dcfa682ef5f4..3072504be5399 100644 --- a/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java +++ b/x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/TrainedModelIT.java @@ -341,31 +341,25 @@ public void testStartDeploymentWithInconsistentTotalLengths() throws IOException private void putPyTorchModel(String modelId) throws IOException { Request request = new Request("PUT", "/_ml/trained_models/" + modelId); - request.setJsonEntity( - "{ " - + " \"description\": \"simple model for testing\",\n" - + " \"model_type\": \"pytorch\",\n" - + " \"inference_config\": {\n" - + " \"pass_through\": {\n" - + " }\n" - + " }\n" - + "}" - ); + request.setJsonEntity(""" + { + "description": "simple model for testing", + "model_type": "pytorch", + "inference_config": { + "pass_through": {} + } + }"""); client().performRequest(request); } private void putModelDefinitionPart(String modelId, int totalSize, int numParts, int partNumber) throws IOException { Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/definition/" + partNumber); - request.setJsonEntity( - "{ " - + "\"total_definition_length\": " - + totalSize - + "," - + "\"definition\": \"UEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAUAA4Ac2ltcGxlbW9kZW==\"," - + "\"total_parts\": " - + numParts - + "}" - ); + request.setJsonEntity(""" + { + "total_definition_length": %s, + "definition": "UEsDBAAACAgAAAAAAAAAAAAAAAAAAAAAAAAUAA4Ac2ltcGxlbW9kZW==", + "total_parts": %s + }""".formatted(totalSize, numParts)); client().performRequest(request); } diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java index 6df3ecec39976..8695bf036976e 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferencePipelineAggIT.java @@ -25,76 +25,70 @@ public class InferencePipelineAggIT extends InferenceTestCase { @Before public void setupModelAndData() throws IOException { - putRegressionModel( - MODEL_ID, - "{\n" - + " \"description\": \"super complex model for tests\",\n" - + " \"input\": {\"field_names\": [\"avg_cost\", \"item\"]},\n" - + " \"inference_config\": {\n" - + " \"regression\": {\n" - + " \"results_field\": \"regression-value\",\n" - + " \"num_top_feature_importance_values\": 2\n" - + " }\n" - + " },\n" - + " \"definition\": {\n" - + " \"preprocessors\" : [{\n" - + " \"one_hot_encoding\": {\n" - + " \"field\": \"product_type\",\n" - + " \"hot_map\": {\n" - + " \"TV\": \"type_tv\",\n" - + " \"VCR\": \"type_vcr\",\n" - + " \"Laptop\": \"type_laptop\"\n" - + " }\n" - + " }\n" - + " }],\n" - + " \"trained_model\": {\n" - + " \"ensemble\": {\n" - + " \"feature_names\": [],\n" - + " \"target_type\": \"regression\",\n" - + " \"trained_models\": [\n" - + " {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\n" - + " \"avg_cost\", \"type_tv\", \"type_vcr\", \"type_laptop\"\n" - + " ],\n" - + " \"tree_structure\": [\n" - + " {\n" - + " \"node_index\": 0,\n" - + " \"split_feature\": 0,\n" - + " \"split_gain\": 12,\n" - + " \"threshold\": 38,\n" - + " \"decision_type\": \"lte\",\n" - + " \"default_left\": true,\n" - + " \"left_child\": 1,\n" - + " \"right_child\": 2\n" - + " },\n" - + " {\n" - + " \"node_index\": 1,\n" - + " \"leaf_value\": 5.0\n" - + " },\n" - + " {\n" - + " \"node_index\": 2,\n" - + " \"leaf_value\": 2.0\n" - + " }\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " }\n" - + " ]\n" - + " }\n" - + " }\n" - + " }\n" - + " }" - ); - createIndex( - INDEX_NAME, - Settings.EMPTY, - "\"properties\":{\n" - + " \"product\":{\"type\": \"keyword\"},\n" - + " \"cost\":{\"type\": \"integer\"},\n" - + " \"time\": {\"type\": \"date\"}" - + "}" - ); + putRegressionModel(MODEL_ID, """ + { + "description": "super complex model for tests", + "input": {"field_names": ["avg_cost", "item"]}, + "inference_config": { + "regression": { + "results_field": "regression-value", + "num_top_feature_importance_values": 2 + } + }, + "definition": { + "preprocessors" : [{ + "one_hot_encoding": { + "field": "product_type", + "hot_map": { + "TV": "type_tv", + "VCR": "type_vcr", + "Laptop": "type_laptop" + } + } + }], + "trained_model": { + "ensemble": { + "feature_names": [], + "target_type": "regression", + "trained_models": [ + { + "tree": { + "feature_names": [ + "avg_cost", "type_tv", "type_vcr", "type_laptop" + ], + "tree_structure": [ + { + "node_index": 0, + "split_feature": 0, + "split_gain": 12, + "threshold": 38, + "decision_type": "lte", + "default_left": true, + "left_child": 1, + "right_child": 2 + }, + { + "node_index": 1, + "leaf_value": 5.0 + }, + { + "node_index": 2, + "leaf_value": 2.0 + } + ], + "target_type": "regression" + } + } + ] + } + } + } + }"""); + createIndex(INDEX_NAME, Settings.EMPTY, """ + "properties":{ + "product":{"type": "keyword"}, + "cost":{"type": "integer"}, + "time": {"type": "date"}}"""); indexData("{ \"product\": \"TV\", \"cost\": 300, \"time\": 1587501233000 }"); indexData("{ \"product\": \"TV\", \"cost\": 400, \"time\": 1587501233000}"); indexData("{ \"product\": \"VCR\", \"cost\": 150, \"time\": 1587501233000 }"); @@ -117,39 +111,38 @@ private Response search(String searchBody) throws IOException { @SuppressWarnings("unchecked") public void testPipelineRegressionSimple() throws Exception { - Response searchResponse = search( - "{\n" - + " \"size\": 0,\n" - + " \"aggs\": {\n" - + " \"good\": {\n" - + " \"terms\": {\n" - + " \"field\": \"product\",\n" - + " \"size\": 10\n" - + " },\n" - + " \"aggs\": {\n" - + " \"avg_cost_agg\": {\n" - + " \"avg\": {\n" - + " \"field\": \"cost\"\n" - + " }\n" - + " },\n" - + " \"regression_agg\": {\n" - + " \"inference\": {\n" - + " \"model_id\": \"a-complex-regression-model\",\n" - + " \"inference_config\": {\n" - + " \"regression\": {\n" - + " \"results_field\": \"value\"\n" - + " }\n" - + " },\n" - + " \"buckets_path\": {\n" - + " \"avg_cost\": \"avg_cost_agg\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }" - ); + Response searchResponse = search(""" + { + "size": 0, + "aggs": { + "good": { + "terms": { + "field": "product", + "size": 10 + }, + "aggs": { + "avg_cost_agg": { + "avg": { + "field": "cost" + } + }, + "regression_agg": { + "inference": { + "model_id": "a-complex-regression-model", + "inference_config": { + "regression": { + "results_field": "value" + } + }, + "buckets_path": { + "avg_cost": "avg_cost_agg" + } + } + } + } + } + } + }"""); assertThat( (List) XContentMapValues.extractValue("aggregations.good.buckets.regression_agg.value", responseAsMap(searchResponse)), contains(2.0, 2.0, 2.0) @@ -158,46 +151,45 @@ public void testPipelineRegressionSimple() throws Exception { @SuppressWarnings("unchecked") public void testPipelineAggReferencingSingleBucket() throws Exception { - Response searchResponse = search( - "{\n" - + " \"size\": 0,\n" - + " \"query\": {\n" - + " \"match_all\": {}\n" - + " },\n" - + " \"aggs\": {\n" - + " \"date_histo\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"fixed_interval\": \"1d\"\n" - + " },\n" - + " \"aggs\": {\n" - + " \"good\": {\n" - + " \"terms\": {\n" - + " \"field\": \"product\",\n" - + " \"size\": 10\n" - + " },\n" - + " \"aggs\": {\n" - + " \"avg_cost_agg\": {\n" - + " \"avg\": {\n" - + " \"field\": \"cost\"\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"regression_agg\": {\n" - + " \"inference\": {\n" - + " \"model_id\": \"a-complex-regression-model\",\n" - + " \"buckets_path\": {\n" - + " \"avg_cost\": \"good['TV']>avg_cost_agg\",\n" - + " \"product_type\": \"good['TV']\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }" - ); + Response searchResponse = search(""" + { + "size": 0, + "query": { + "match_all": {} + }, + "aggs": { + "date_histo": { + "date_histogram": { + "field": "time", + "fixed_interval": "1d" + }, + "aggs": { + "good": { + "terms": { + "field": "product", + "size": 10 + }, + "aggs": { + "avg_cost_agg": { + "avg": { + "field": "cost" + } + } + } + }, + "regression_agg": { + "inference": { + "model_id": "a-complex-regression-model", + "buckets_path": { + "avg_cost": "good['TV']>avg_cost_agg", + "product_type": "good['TV']" + } + } + } + } + } + } + }"""); assertThat( (List) XContentMapValues.extractValue( "aggregations.date_histo.buckets.regression_agg.value", @@ -209,35 +201,34 @@ public void testPipelineAggReferencingSingleBucket() throws Exception { @SuppressWarnings("unchecked") public void testAllFieldsMissingWarning() throws IOException { - Response searchResponse = search( - "{\n" - + " \"size\": 0,\n" - + " \"query\": { \"match_all\" : { } },\n" - + " \"aggs\": {\n" - + " \"good\": {\n" - + " \"terms\": {\n" - + " \"field\": \"product\",\n" - + " \"size\": 10\n" - + " },\n" - + " \"aggs\": {\n" - + " \"avg_cost_agg\": {\n" - + " \"avg\": {\n" - + " \"field\": \"cost\"\n" - + " }\n" - + " },\n" - + " \"regression_agg\" : {\n" - + " \"inference\": {\n" - + " \"model_id\": \"a-complex-regression-model\",\n" - + " \"buckets_path\": {\n" - + " \"cost\" : \"avg_cost_agg\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }" - ); + Response searchResponse = search(""" + { + "size": 0, + "query": { "match_all" : { } }, + "aggs": { + "good": { + "terms": { + "field": "product", + "size": 10 + }, + "aggs": { + "avg_cost_agg": { + "avg": { + "field": "cost" + } + }, + "regression_agg" : { + "inference": { + "model_id": "a-complex-regression-model", + "buckets_path": { + "cost" : "avg_cost_agg" + } + } + } + } + } + } + }"""); assertThat( (List) XContentMapValues.extractValue( "aggregations.good.buckets.regression_agg.warning", diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java index ab118e7122355..1d66ef14cd24a 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceProcessorIT.java @@ -220,21 +220,18 @@ public void testCreateProcessorWithDeprecatedFields() throws Exception { createdPipelines.add("regression-model-deprecated-pipeline"); Request putPipeline = new Request("PUT", "_ingest/pipeline/regression-model-deprecated-pipeline"); - putPipeline.setJsonEntity( - "{\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\" : {\n" - + " \"model_id\" : \"" - + MODEL_ID - + "\",\n" - + " \"inference_config\": {\"regression\": {}},\n" - + " \"field_mappings\": {}\n" - + " }\n" - + " }\n" - + " ]\n" - + "}" - ); + putPipeline.setJsonEntity(""" + { + "processors": [ + { + "inference" : { + "model_id" : "%s", + "inference_config": {"regression": {}}, + "field_mappings": {} + } + } + ] + }""".formatted(MODEL_ID)); RequestOptions ro = expectWarnings("Deprecated field [field_mappings] used, expected [field_map] instead"); putPipeline.setOptions(ro); @@ -286,22 +283,21 @@ private void infer(String pipelineId) throws IOException { private void putPipeline(String modelId, String pipelineName) throws IOException { Request putPipeline = new Request("PUT", "_ingest/pipeline/" + pipelineName); - putPipeline.setJsonEntity( - " {\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\" : {\n" - + " \"model_id\" : \"" - + modelId - + "\",\n" - + " \"inference_config\": {\"regression\": {}},\n" - + " \"target_field\": \"regression_field\",\n" - + " \"field_map\": {}\n" - + " }\n" - + " }\n" - + " ]\n" - + " }" - ); + putPipeline.setJsonEntity(""" + { + "processors": [ + { + "inference": { + "model_id": "%s", + "inference_config": { + "regression": {} + }, + "target_field": "regression_field", + "field_map": {} + } + } + ] + }""".formatted(modelId)); assertThat(client().performRequest(putPipeline).getStatusLine().getStatusCode(), equalTo(200)); } diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java index 46c5f7dedd908..d95e3166ce4cb 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/InferenceTestCase.java @@ -78,27 +78,26 @@ Map getStats(String modelId) throws IOException { } void putRegressionModel(String modelId) throws IOException { - putRegressionModel( - modelId, - " {\n" - + " \"description\": \"empty model for tests\",\n" - + " \"tags\": [\"regression\", \"tag1\"],\n" - + " \"input\": {\"field_names\": [\"field1\", \"field2\"]},\n" - + " \"inference_config\": { \"regression\": {\"results_field\": \"my_regression\"}},\n" - + " \"definition\": {\n" - + " \"preprocessors\": [],\n" - + " \"trained_model\": {\n" - + " \"tree\": {\n" - + " \"feature_names\": [\"field1\", \"field2\"],\n" - + " \"tree_structure\": [\n" - + " {\"node_index\": 0, \"leaf_value\": 42}\n" - + " ],\n" - + " \"target_type\": \"regression\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }" - ); + putRegressionModel(modelId, """ + { + "description": "empty model for tests", + "tags": ["regression", "tag1"], + "input": {"field_names": ["field1", "field2"]}, + "inference_config": { "regression": {"results_field": "my_regression"}}, + "definition": { + "preprocessors": [], + "trained_model": { + "tree": { + "feature_names": ["field1", "field2"], + "tree_structure": [ + {"node_index": 0, "leaf_value": 42} + ], + "target_type": "regression" + } + } + } + } + """); } void putRegressionModel(String modelId, String body) throws IOException { diff --git a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java index b26d9f5dd6405..023ba147328e5 100644 --- a/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java +++ b/x-pack/plugin/ml/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/transforms/PainlessDomainSplitIT.java @@ -205,25 +205,21 @@ public void testIsolated() throws Exception { logger.info("params={}", mapAsJson); Request searchRequest = new Request("GET", "/painless/_search"); - searchRequest.setJsonEntity( - "{\n" - + " \"query\" : {\n" - + " \"match_all\": {}\n" - + " },\n" - + " \"script_fields\" : {\n" - + " \"domain_split\" : {\n" - + " \"script\" : {\n" - + " \"lang\": \"painless\",\n" - + " \"source\": \"" - + " return domainSplit(params['host']); \",\n" - + " \"params\": " - + mapAsJson - + "\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + searchRequest.setJsonEntity(""" + { + "query" : { + "match_all": {} + }, + "script_fields" : { + "domain_split" : { + "script" : { + "lang": "painless", + "source": " return domainSplit(params['host']); ", + "params": %s + } + } + } + }""".formatted(mapAsJson)); String responseBody = EntityUtils.toString(client().performRequest(searchRequest).getEntity()); Matcher m = pattern.matcher(responseBody); @@ -271,18 +267,17 @@ public void testIsolated() throws Exception { public void testHRDSplit() throws Exception { // Create job Request createJobRequest = new Request("PUT", BASE_PATH + "anomaly_detectors/hrd-split-job"); - createJobRequest.setJsonEntity( - "{\n" - + " \"description\":\"Domain splitting\",\n" - + " \"analysis_config\" : {\n" - + " \"bucket_span\":\"3600s\",\n" - + " \"detectors\" :[{\"function\":\"count\", \"by_field_name\" : \"domain_split\"}]\n" - + " },\n" - + " \"data_description\" : {\n" - + " \"time_field\":\"time\"\n" - + " }\n" - + "}" - ); + createJobRequest.setJsonEntity(""" + { + "description":"Domain splitting", + "analysis_config" : { + "bucket_span":"3600s", + "detectors" :[{"function":"count", "by_field_name" : "domain_split"}] + }, + "data_description" : { + "time_field":"time" + } + }"""); client().performRequest(createJobRequest); client().performRequest(new Request("POST", BASE_PATH + "anomaly_detectors/hrd-split-job/_open")); @@ -291,11 +286,8 @@ public void testHRDSplit() throws Exception { .put(IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), 1) .put(IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0); - createIndex( - "painless", - settings.build(), - "\"properties\": { \"domain\": { \"type\": \"keyword\" }," + "\"time\": { \"type\": \"date\" } }" - ); + createIndex("painless", settings.build(), """ + "properties": { "domain": { "type": "keyword" },"time": { "type": "date" } }"""); // Index some data ZonedDateTime baseTime = ZonedDateTime.now(ZoneOffset.UTC).minusYears(1); @@ -313,13 +305,17 @@ public void testHRDSplit() throws Exception { // Anomaly has 100 docs, but we don't care about the value for (int j = 0; j < 100; j++) { Request createDocRequest = new Request("POST", "/painless/_doc"); - createDocRequest.setJsonEntity("{\"domain\": \"" + "bar.bar.com\", \"time\": \"" + formattedTime + "\"}"); + createDocRequest.setJsonEntity(""" + {"domain": "bar.bar.com", "time": "%s"} + """.formatted(formattedTime)); client().performRequest(createDocRequest); } } else { // Non-anomalous values will be what's seen when the anomaly is reported Request createDocRequest = new Request("PUT", "/painless/_doc/" + formattedTime); - createDocRequest.setJsonEntity("{\"domain\": \"" + test.hostName + "\", \"time\": \"" + formattedTime + "\"}"); + createDocRequest.setJsonEntity(""" + {"domain": "%s", "time": "%s"} + """.formatted(test.hostName, formattedTime)); client().performRequest(createDocRequest); } } @@ -328,17 +324,16 @@ public void testHRDSplit() throws Exception { // Create and start datafeed Request createFeedRequest = new Request("PUT", BASE_PATH + "datafeeds/hrd-split-datafeed"); - createFeedRequest.setJsonEntity( - "{\n" - + " \"job_id\":\"hrd-split-job\",\n" - + " \"indexes\":[\"painless\"],\n" - + " \"script_fields\": {\n" - + " \"domain_split\": {\n" - + " \"script\": \"return domainSplit(doc['domain'].value, params);\"\n" - + " }\n" - + " }\n" - + "}" - ); + createFeedRequest.setJsonEntity(""" + { + "job_id":"hrd-split-job", + "indexes":["painless"], + "script_fields": { + "domain_split": { + "script": "return domainSplit(doc['domain'].value, params);" + } + } + }"""); client().performRequest(createFeedRequest); Request startDatafeedRequest = new Request("POST", BASE_PATH + "datafeeds/hrd-split-datafeed/_start"); diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java index 7d58c2c0baffc..5ef7fa710f465 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/license/MachineLearningLicensingIT.java @@ -200,7 +200,8 @@ public void testAutoCloseJobWithDatafeed() throws Exception { String datafeedId = jobId + "-datafeed"; assertMLAllowed(true); String datafeedIndex = jobId + "-data"; - prepareCreate(datafeedIndex).setMapping("{\"_doc\":{\"properties\":{\"time\":{\"type\":\"date\"}}}}").get(); + prepareCreate(datafeedIndex).setMapping(""" + {"_doc":{"properties":{"time":{"type":"date"}}}}""").get(); // put job PlainActionFuture putJobListener = PlainActionFuture.newFuture(); @@ -299,7 +300,8 @@ public void testMachineLearningStartDatafeedActionRestricted() throws Exception String datafeedId = jobId + "-datafeed"; assertMLAllowed(true); String datafeedIndex = jobId + "-data"; - prepareCreate(datafeedIndex).setMapping("{\"_doc\":{\"properties\":{\"time\":{\"type\":\"date\"}}}}").get(); + prepareCreate(datafeedIndex).setMapping(""" + {"_doc":{"properties":{"time":{"type":"date"}}}}""").get(); // test that license restricted apis do now work PlainActionFuture putJobListener = PlainActionFuture.newFuture(); client().execute(PutJobAction.INSTANCE, new PutJobAction.Request(createJob(jobId)), putJobListener); @@ -364,7 +366,8 @@ public void testMachineLearningStopDatafeedActionNotRestricted() throws Exceptio String datafeedId = jobId + "-datafeed"; assertMLAllowed(true); String datafeedIndex = jobId + "-data"; - prepareCreate(datafeedIndex).setMapping("{\"_doc\":{\"properties\":{\"time\":{\"type\":\"date\"}}}}").get(); + prepareCreate(datafeedIndex).setMapping(""" + {"_doc":{"properties":{"time":{"type":"date"}}}}""").get(); // test that license restricted apis do now work PlainActionFuture putJobListener = PlainActionFuture.newFuture(); client().execute(PutJobAction.INSTANCE, new PutJobAction.Request(createJob(jobId)), putJobListener); @@ -505,16 +508,17 @@ public void testMachineLearningCreateInferenceProcessorRestricted() { assertMLAllowed(true); putInferenceModel(modelId); - String pipeline = "{" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"target_field\": \"regression_value\",\n" - + " \"model_id\": \"modelprocessorlicensetest\",\n" - + " \"inference_config\": {\"regression\": {}},\n" - + " \"field_map\": {}\n" - + " }\n" - + " }]}\n"; + String pipeline = """ + { "processors": [ + { + "inference": { + "target_field": "regression_value", + "model_id": "modelprocessorlicensetest", + "inference_config": {"regression": {}}, + "field_map": {} + } + }]} + """; // Creating a pipeline should work PlainActionFuture putPipelineListener = PlainActionFuture.newFuture(); client().execute( @@ -535,18 +539,17 @@ public void testMachineLearningCreateInferenceProcessorRestricted() { .execute() .actionGet(); - String simulateSource = "{\n" - + " \"pipeline\": \n" - + pipeline - + " ,\n" - + " \"docs\": [\n" - + " {\"_source\": {\n" - + " \"col1\": \"female\",\n" - + " \"col2\": \"M\",\n" - + " \"col3\": \"none\",\n" - + " \"col4\": 10\n" - + " }}]\n" - + "}"; + String simulateSource = """ + { + "pipeline": %s, + "docs": [ + {"_source": { + "col1": "female", + "col2": "M", + "col3": "none", + "col4": 10 + }}] + }""".formatted(pipeline); PlainActionFuture simulatePipelineListener = PlainActionFuture.newFuture(); client().execute( SimulatePipelineAction.INSTANCE, diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java index dcdd4fcd96531..e5d63ef0b0fe0 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/BasicDistributedJobsIT.java @@ -416,13 +416,9 @@ public void testMlStateAndResultsIndicesNotAvailable() throws Exception { client().execute(OpenJobAction.INSTANCE, openJobRequest).actionGet(); PostDataAction.Request postDataRequest = new PostDataAction.Request(jobId); - postDataRequest.setContent( - new BytesArray( - "{\"airline\":\"AAL\",\"responsetime\":\"132.2046\",\"sourcetype\":\"farequote\",\"time\":\"1403481600\"}\n" - + "{\"airline\":\"JZA\",\"responsetime\":\"990.4628\",\"sourcetype\":\"farequote\",\"time\":\"1403481700\"}" - ), - XContentType.JSON - ); + postDataRequest.setContent(new BytesArray(""" + {"airline":"AAL","responsetime":"132.2046","sourcetype":"farequote","time":"1403481600"} + {"airline":"JZA","responsetime":"990.4628","sourcetype":"farequote","time":"1403481700"}"""), XContentType.JSON); PostDataAction.Response response = client().execute(PostDataAction.INSTANCE, postDataRequest).actionGet(); assertEquals(2, response.getDataCounts().getProcessedRecordCount()); diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java index e1c7910726c0f..c68f78a3b8258 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/JobResultsProviderIT.java @@ -675,10 +675,8 @@ public void testGetSnapshots() { // Add a snapshot WITHOUT a min version. client().prepareIndex(AnomalyDetectorsIndex.jobResultsAliasedName("other_job")) .setId(ModelSnapshot.documentId("other_job", "11")) - .setSource( - "{\"job_id\":\"other_job\"," + "\"snapshot_id\":\"11\", \"snapshot_doc_count\":1,\"retain\":false}", - XContentType.JSON - ) + .setSource(""" + {"job_id":"other_job","snapshot_id":"11", "snapshot_doc_count":1,"retain":false}""", XContentType.JSON) .get(); client().admin() diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java index f3e0ea2cbd179..317740efe4850 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/MlAutoUpdateServiceIT.java @@ -45,27 +45,28 @@ public void createComponents() throws Exception { waitForMlTemplates(); } - private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = "{\n" - + " \"datafeed_id\": \"farequote-datafeed-with-old-agg\",\n" - + " \"job_id\": \"farequote\",\n" - + " \"frequency\": \"1h\",\n" - + " \"config_type\": \"datafeed\",\n" - + " \"indices\": [\"farequote1\", \"farequote2\"],\n" - + " \"aggregations\": {\n" - + " \"buckets\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"time\",\n" - + " \"interval\": \"360s\",\n" - + " \"time_zone\": \"UTC\"\n" - + " },\n" - + " \"aggregations\": {\n" - + " \"time\": {\n" - + " \"max\": {\"field\": \"time\"}\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + private static final String AGG_WITH_OLD_DATE_HISTOGRAM_INTERVAL = """ + { + "datafeed_id": "farequote-datafeed-with-old-agg", + "job_id": "farequote", + "frequency": "1h", + "config_type": "datafeed", + "indices": ["farequote1", "farequote2"], + "aggregations": { + "buckets": { + "date_histogram": { + "field": "time", + "interval": "360s", + "time_zone": "UTC" + }, + "aggregations": { + "time": { + "max": {"field": "time"} + } + } + } + } + }"""; public void testAutomaticModelUpdate() throws Exception { ensureGreen("_all"); diff --git a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java index 1f334599a80da..9651115c658ee 100644 --- a/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java +++ b/x-pack/plugin/ml/src/internalClusterTest/java/org/elasticsearch/xpack/ml/integration/TestFeatureLicenseTrackingIT.java @@ -206,28 +206,18 @@ private List getJobStats(String jobId) { } private void putTrainedModelIngestPipeline(String pipelineId, String modelId) throws Exception { - client().execute( - PutPipelineAction.INSTANCE, - new PutPipelineRequest( - pipelineId, - new BytesArray( - "{\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"inference_config\": {\"classification\":{}},\n" - + " \"model_id\": \"" - + modelId - + "\",\n" - + " \"field_map\": {}\n" - + " }\n" - + " }\n" - + " ]\n" - + " }" - ), - XContentType.JSON - ) - ).actionGet(); + client().execute(PutPipelineAction.INSTANCE, new PutPipelineRequest(pipelineId, new BytesArray(""" + { + "processors": [ + { + "inference": { + "inference_config": {"classification":{}}, + "model_id": "%s", + "field_map": {} + } + } + ] + }""".formatted(modelId)), XContentType.JSON)).actionGet(); } } diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java index 1a2c97c082e7b..9cc91cc55b6df 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/inference/TrainedModelStatsService.java @@ -59,12 +59,12 @@ public class TrainedModelStatsService { private static final Logger logger = LogManager.getLogger(TrainedModelStatsService.class); private static final TimeValue PERSISTENCE_INTERVAL = TimeValue.timeValueSeconds(1); - private static final String STATS_UPDATE_SCRIPT_TEMPLATE = "" - + " ctx._source.{0} += params.{0};\n" - + " ctx._source.{1} += params.{1};\n" - + " ctx._source.{2} += params.{2};\n" - + " ctx._source.{3} += params.{3};\n" - + " ctx._source.{4} = params.{4};"; + private static final String STATS_UPDATE_SCRIPT_TEMPLATE = """ + ctx._source.{0} += params.{0}; + ctx._source.{1} += params.{1}; + ctx._source.{2} += params.{2}; + ctx._source.{3} += params.{3}; + ctx._source.{4} = params.{4};""".indent(4); // Script to only update if stats have increased since last persistence private static final String STATS_UPDATE_SCRIPT = Messages.getMessage( STATS_UPDATE_SCRIPT_TEMPLATE, diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java index e05111e8000c9..29b6d3056701c 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationDataExtractorTests.java @@ -145,10 +145,11 @@ public void testExtraction() throws IOException { assertThat(extractor.hasNext(), is(true)); Optional stream = extractor.next(); assertThat(stream.isPresent(), is(true)); - String expectedStream = "{\"time\":1999,\"airline\":\"a\",\"responsetime\":11.0,\"doc_count\":1} " - + "{\"time\":1999,\"airline\":\"b\",\"responsetime\":12.0,\"doc_count\":2} " - + "{\"time\":3999,\"airline\":\"c\",\"responsetime\":31.0,\"doc_count\":4} " - + "{\"time\":3999,\"airline\":\"b\",\"responsetime\":32.0,\"doc_count\":3}"; + String expectedStream = """ + {"time":1999,"airline":"a","responsetime":11.0,"doc_count":1} \ + {"time":1999,"airline":"b","responsetime":12.0,"doc_count":2} \ + {"time":3999,"airline":"c","responsetime":31.0,"doc_count":4} \ + {"time":3999,"airline":"b","responsetime":32.0,"doc_count":3}"""; assertThat(asString(stream.get()), equalTo(expectedStream)); assertThat(extractor.hasNext(), is(false)); assertThat(capturedSearchRequests.size(), equalTo(1)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java index 37e4d447521a5..47ed20ce7cbb9 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/AggregationToJsonProcessorTests.java @@ -240,17 +240,13 @@ public void testProcessGivenTopLevelAggIsNotHistogram() throws IOException { ); String json = aggToString(Sets.newHashSet("my_value", "my_field"), createAggs(Collections.singletonList(terms))); - assertThat( - json, - equalTo( - "{\"my_field\":\"A\",\"time\":1000,\"my_value\":1.0,\"doc_count\":3} " - + "{\"my_field\":\"B\",\"time\":1000,\"my_value\":10.0,\"doc_count\":6} " - + "{\"my_field\":\"A\",\"time\":2000,\"my_value\":2.0,\"doc_count\":4} " - + "{\"my_field\":\"B\",\"time\":2000,\"my_value\":20.0,\"doc_count\":7} " - + "{\"my_field\":\"A\",\"time\":3000,\"my_value\":3.0,\"doc_count\":5} " - + "{\"my_field\":\"B\",\"time\":3000,\"my_value\":30.0,\"doc_count\":8}" - ) - ); + assertThat(json, equalTo(""" + {"my_field":"A","time":1000,"my_value":1.0,"doc_count":3} \ + {"my_field":"B","time":1000,"my_value":10.0,"doc_count":6} \ + {"my_field":"A","time":2000,"my_value":2.0,"doc_count":4} \ + {"my_field":"B","time":2000,"my_value":20.0,"doc_count":7} \ + {"my_field":"A","time":3000,"my_value":3.0,"doc_count":5} \ + {"my_field":"B","time":3000,"my_value":30.0,"doc_count":8}""")); } public void testProcessGivenSingleMetricPerHistogram() throws IOException { @@ -266,14 +262,10 @@ public void testProcessGivenSingleMetricPerHistogram() throws IOException { String json = aggToString(Sets.newHashSet("my_value"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":1000,\"my_value\":1.0,\"doc_count\":3} " - + "{\"time\":2000,\"doc_count\":3} " - + "{\"time\":3000,\"my_value\":3.0,\"doc_count\":5}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"my_value":1.0,"doc_count":3} \ + {"time":2000,"doc_count":3} \ + {"time":3000,"my_value":3.0,"doc_count":5}""")); } public void testProcessGivenTermsPerHistogram() throws IOException { @@ -298,18 +290,14 @@ public void testProcessGivenTermsPerHistogram() throws IOException { String json = aggToString(Sets.newHashSet("time", "my_field"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":1100,\"my_field\":\"a\",\"doc_count\":1} " - + "{\"time\":1100,\"my_field\":\"b\",\"doc_count\":2} " - + "{\"time\":1100,\"my_field\":\"c\",\"doc_count\":1} " - + "{\"time\":2200,\"my_field\":\"a\",\"doc_count\":5} " - + "{\"time\":2200,\"my_field\":\"b\",\"doc_count\":2} " - + "{\"time\":4400,\"my_field\":\"c\",\"doc_count\":4} " - + "{\"time\":4400,\"my_field\":\"b\",\"doc_count\":3}" - ) - ); + assertThat(json, equalTo(""" + {"time":1100,"my_field":"a","doc_count":1} \ + {"time":1100,"my_field":"b","doc_count":2} \ + {"time":1100,"my_field":"c","doc_count":1} \ + {"time":2200,"my_field":"a","doc_count":5} \ + {"time":2200,"my_field":"b","doc_count":2} \ + {"time":4400,"my_field":"c","doc_count":4} \ + {"time":4400,"my_field":"b","doc_count":3}""")); } public void testProcessGivenSingleMetricPerSingleTermsPerHistogram() throws IOException { @@ -348,18 +336,14 @@ public void testProcessGivenSingleMetricPerSingleTermsPerHistogram() throws IOEx String json = aggToString(Sets.newHashSet("my_field", "my_value"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":1000,\"my_field\":\"a\",\"my_value\":11.0,\"doc_count\":1} " - + "{\"time\":1000,\"my_field\":\"b\",\"my_value\":12.0,\"doc_count\":2} " - + "{\"time\":1000,\"my_field\":\"c\",\"my_value\":13.0,\"doc_count\":1} " - + "{\"time\":2000,\"my_field\":\"a\",\"my_value\":21.0,\"doc_count\":5} " - + "{\"time\":2000,\"my_field\":\"b\",\"my_value\":22.0,\"doc_count\":2} " - + "{\"time\":4000,\"my_field\":\"c\",\"my_value\":41.0,\"doc_count\":4} " - + "{\"time\":4000,\"my_field\":\"b\",\"my_value\":42.0,\"doc_count\":3}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"my_field":"a","my_value":11.0,"doc_count":1} \ + {"time":1000,"my_field":"b","my_value":12.0,"doc_count":2} \ + {"time":1000,"my_field":"c","my_value":13.0,"doc_count":1} \ + {"time":2000,"my_field":"a","my_value":21.0,"doc_count":5} \ + {"time":2000,"my_field":"b","my_value":22.0,"doc_count":2} \ + {"time":4000,"my_field":"c","my_value":41.0,"doc_count":4} \ + {"time":4000,"my_field":"b","my_value":42.0,"doc_count":3}""")); } public void testProcessGivenMultipleSingleMetricPerSingleTermsPerHistogram() throws IOException { @@ -420,18 +404,14 @@ public void testProcessGivenMultipleSingleMetricPerSingleTermsPerHistogram() thr includeDocCount = false; String json = aggToString(Sets.newHashSet("my_field", "my_value", "my_value2"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":1000,\"my_field\":\"a\",\"my_value\":111.0,\"my_value2\":112.0} " - + "{\"time\":1000,\"my_field\":\"b\",\"my_value2\":122.0} " - + "{\"time\":1000,\"my_field\":\"c\",\"my_value\":131.0,\"my_value2\":132.0} " - + "{\"time\":2000,\"my_field\":\"a\",\"my_value\":211.0,\"my_value2\":212.0} " - + "{\"time\":2000,\"my_field\":\"b\",\"my_value\":221.0,\"my_value2\":222.0} " - + "{\"time\":4000,\"my_field\":\"c\",\"my_value\":411.0,\"my_value2\":412.0} " - + "{\"time\":4000,\"my_field\":\"b\",\"my_value\":421.0,\"my_value2\":422.0}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"my_field":"a","my_value":111.0,"my_value2":112.0} \ + {"time":1000,"my_field":"b","my_value2":122.0} \ + {"time":1000,"my_field":"c","my_value":131.0,"my_value2":132.0} \ + {"time":2000,"my_field":"a","my_value":211.0,"my_value2":212.0} \ + {"time":2000,"my_field":"b","my_value":221.0,"my_value2":222.0} \ + {"time":4000,"my_field":"c","my_value":411.0,"my_value2":412.0} \ + {"time":4000,"my_field":"b","my_value":421.0,"my_value2":422.0}""")); } public void testProcessGivenUnsupportedAggregationUnderHistogram() { @@ -470,13 +450,9 @@ public void testProcessGivenMixedBucketAndLeafAggregationsAtSameLevel_BucketFirs Histogram.Bucket histogramBucket = createHistogramBucket(1000L, 2, Arrays.asList(terms, createMax("time", 1000), maxAgg)); String json = aggToString(Sets.newHashSet("terms", "max_value"), histogramBucket); - assertThat( - json, - equalTo( - "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"a\",\"doc_count\":1} " - + "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"b\",\"doc_count\":2}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"max_value":1200.0,"terms":"a","doc_count":1} \ + {"time":1000,"max_value":1200.0,"terms":"b","doc_count":2}""")); } public void testProcessGivenMixedBucketAndLeafAggregationsAtSameLevel_LeafFirst() throws IOException { @@ -485,13 +461,9 @@ public void testProcessGivenMixedBucketAndLeafAggregationsAtSameLevel_LeafFirst( Histogram.Bucket histogramBucket = createHistogramBucket(1000L, 2, Arrays.asList(createMax("time", 1000), maxAgg, terms)); String json = aggToString(Sets.newHashSet("terms", "max_value"), histogramBucket); - assertThat( - json, - equalTo( - "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"a\",\"doc_count\":1} " - + "{\"time\":1000,\"max_value\":1200.0,\"terms\":\"b\",\"doc_count\":2}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"max_value":1200.0,"terms":"a","doc_count":1} \ + {"time":1000,"max_value":1200.0,"terms":"b","doc_count":2}""")); } public void testProcessGivenBucketAndLeafAggregationsButBucketNotInFields() throws IOException { @@ -528,14 +500,10 @@ public void testProcessGivenBucketAndLeafAggregationsButBucketNotInFields() thro String json = aggToString(Sets.newHashSet("time", "my_value"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":1100,\"my_value\":1.0,\"doc_count\":4} " - + "{\"time\":2200,\"my_value\":2.0,\"doc_count\":5} " - + "{\"time\":4400,\"my_value\":4.0,\"doc_count\":7}" - ) - ); + assertThat(json, equalTo(""" + {"time":1100,"my_value":1.0,"doc_count":4} \ + {"time":2200,"my_value":2.0,"doc_count":5} \ + {"time":4400,"my_value":4.0,"doc_count":7}""")); } public void testProcessGivenSinglePercentilesPerHistogram() throws IOException { @@ -552,15 +520,11 @@ public void testProcessGivenSinglePercentilesPerHistogram() throws IOException { String json = aggToString(Sets.newHashSet("my_field"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":1000,\"my_field\":1.0,\"doc_count\":4} " - + "{\"time\":2000,\"my_field\":2.0,\"doc_count\":7} " - + "{\"time\":3000,\"doc_count\":10} " - + "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"my_field":1.0,"doc_count":4} \ + {"time":2000,"my_field":2.0,"doc_count":7} \ + {"time":3000,"doc_count":10} \ + {"time":4000,"my_field":4.0,"doc_count":14}""")); } public void testProcessGivenMultiplePercentilesPerHistogram() { @@ -620,14 +584,10 @@ public void testBucketBeforeStartIsPruned() throws IOException { startTime = 2000; String json = aggToString(Sets.newHashSet("my_field"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":2000,\"my_field\":2.0,\"doc_count\":7} " - + "{\"time\":3000,\"my_field\":3.0,\"doc_count\":10} " - + "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}" - ) - ); + assertThat(json, equalTo(""" + {"time":2000,"my_field":2.0,"doc_count":7} \ + {"time":3000,"my_field":3.0,"doc_count":10} \ + {"time":4000,"my_field":4.0,"doc_count":14}""")); } public void testBucketsBeforeStartArePruned() throws IOException { @@ -641,10 +601,9 @@ public void testBucketsBeforeStartArePruned() throws IOException { startTime = 3000; String json = aggToString(Sets.newHashSet("my_field"), histogramBuckets); - assertThat( - json, - equalTo("{\"time\":3000,\"my_field\":3.0,\"doc_count\":10} " + "{\"time\":4000,\"my_field\":4.0,\"doc_count\":14}") - ); + assertThat(json, equalTo(""" + {"time":3000,"my_field":3.0,"doc_count":10} \ + {"time":4000,"my_field":4.0,"doc_count":14}""")); } public void testSingleBucketAgg() throws IOException { @@ -671,13 +630,9 @@ public void testSingleBucketAgg() throws IOException { String json = aggToString(Sets.newHashSet("field1", "field2"), histogramBuckets); - assertThat( - json, - equalTo( - "{\"time\":1000,\"field1\":5.0,\"field2\":3.0,\"doc_count\":4}" - + " {\"time\":2000,\"field2\":1.0,\"field1\":7.0,\"doc_count\":7}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"field1":5.0,"field2":3.0,"doc_count":4} \ + {"time":2000,"field2":1.0,"field1":7.0,"doc_count":7}""")); } public void testSingleBucketAgg_failureWithSubMultiBucket() { @@ -712,7 +667,9 @@ public void testGeoCentroidAgg() throws IOException { ); String json = aggToString(Sets.newHashSet("geo_field"), histogramBuckets); - assertThat(json, equalTo("{\"time\":1000,\"geo_field\":\"92.1,93.1\",\"doc_count\":4}" + " {\"time\":2000,\"doc_count\":7}")); + assertThat(json, equalTo(""" + {"time":1000,"geo_field":"92.1,93.1","doc_count":4} \ + {"time":2000,"doc_count":7}""")); } private String aggToString(Set fields, Histogram.Bucket bucket) throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java index 65799a0eba4b6..69427430b34ec 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/aggregation/CompositeAggregationDataExtractorTests.java @@ -171,10 +171,11 @@ public void testExtraction() throws IOException { assertThat(extractor.hasNext(), is(true)); Optional stream = extractor.next(); assertThat(stream.isPresent(), is(true)); - String expectedStream = "{\"airline\":\"a\",\"time\":1999,\"responsetime\":11.0,\"doc_count\":1} " - + "{\"airline\":\"b\",\"time\":1999,\"responsetime\":12.0,\"doc_count\":2} " - + "{\"airline\":\"c\",\"time\":3999,\"responsetime\":31.0,\"doc_count\":4} " - + "{\"airline\":\"b\",\"time\":3999,\"responsetime\":32.0,\"doc_count\":3}"; + String expectedStream = """ + {"airline":"a","time":1999,"responsetime":11.0,"doc_count":1} \ + {"airline":"b","time":1999,"responsetime":12.0,"doc_count":2} \ + {"airline":"c","time":3999,"responsetime":31.0,"doc_count":4} \ + {"airline":"b","time":3999,"responsetime":32.0,"doc_count":3}"""; assertThat(asString(stream.get()), equalTo(expectedStream)); assertThat(capturedSearchRequests.size(), equalTo(1)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java index 7472a7b6649c5..24943a785b138 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/ScrollDataExtractorTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.common.document.DocumentField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.core.Tuple; import org.elasticsearch.index.query.QueryBuilder; @@ -223,7 +224,8 @@ public void testMultiplePageExtraction() throws IOException { assertThat(extractor.hasNext(), is(true)); Optional stream = extractor.next(); assertThat(stream.isPresent(), is(true)); - String expectedStream = "{\"time\":1000,\"field_1\":\"a1\"} {\"time\":2000,\"field_1\":\"a2\"}"; + String expectedStream = """ + {"time":1000,"field_1":"a1"} {"time":2000,"field_1":"a2"}"""; assertThat(asString(stream.get()), equalTo(expectedStream)); SearchResponse response2 = createSearchResponse(Arrays.asList(3000L, 4000L), Arrays.asList("a3", "a4"), Arrays.asList("b3", "b4")); @@ -232,7 +234,8 @@ public void testMultiplePageExtraction() throws IOException { assertThat(extractor.hasNext(), is(true)); stream = extractor.next(); assertThat(stream.isPresent(), is(true)); - expectedStream = "{\"time\":3000,\"field_1\":\"a3\"} {\"time\":4000,\"field_1\":\"a4\"}"; + expectedStream = """ + {"time":3000,"field_1":"a3"} {"time":4000,"field_1":"a4"}"""; assertThat(asString(stream.get()), equalTo(expectedStream)); SearchResponse response3 = createEmptySearchResponse(); @@ -272,7 +275,8 @@ public void testMultiplePageExtractionGivenCancel() throws IOException { assertThat(extractor.hasNext(), is(true)); Optional stream = extractor.next(); assertThat(stream.isPresent(), is(true)); - String expectedStream = "{\"time\":1000,\"field_1\":\"a1\"} {\"time\":2000,\"field_1\":\"a2\"}"; + String expectedStream = """ + {"time":1000,"field_1":"a1"} {"time":2000,"field_1":"a2"}"""; assertThat(asString(stream.get()), equalTo(expectedStream)); extractor.cancel(); @@ -288,7 +292,8 @@ public void testMultiplePageExtractionGivenCancel() throws IOException { assertThat(extractor.hasNext(), is(true)); stream = extractor.next(); assertThat(stream.isPresent(), is(true)); - expectedStream = "{\"time\":2000,\"field_1\":\"a3\"} {\"time\":2000,\"field_1\":\"a4\"}"; + expectedStream = """ + {"time":2000,"field_1":"a3"} {"time":2000,"field_1":"a4"}"""; assertThat(asString(stream.get()), equalTo(expectedStream)); assertThat(extractor.hasNext(), is(false)); @@ -461,7 +466,8 @@ public void testDomainSplitScriptField() throws IOException { assertThat(extractor.hasNext(), is(true)); Optional stream = extractor.next(); assertThat(stream.isPresent(), is(true)); - String expectedStream = "{\"time\":1100,\"field_1\":\"a1\"} {\"time\":1200,\"field_1\":\"a2\"}"; + String expectedStream = """ + {"time":1100,"field_1":"a1"} {"time":1200,"field_1":"a2"}"""; assertThat(asString(stream.get()), equalTo(expectedStream)); SearchResponse response2 = createEmptySearchResponse(); @@ -471,7 +477,7 @@ public void testDomainSplitScriptField() throws IOException { assertThat(extractor.hasNext(), is(false)); assertThat(capturedSearchRequests.size(), equalTo(1)); - String searchRequest = capturedSearchRequests.get(0).toString().replaceAll("\\s", ""); + String searchRequest = XContentHelper.stripWhitespace(capturedSearchRequests.get(0).toString()); assertThat(searchRequest, containsString("\"size\":1000")); assertThat( searchRequest, @@ -485,10 +491,8 @@ public void testDomainSplitScriptField() throws IOException { assertThat(searchRequest, containsString("\"stored_fields\":\"_none_\"")); // Check for the scripts - assertThat( - searchRequest, - containsString("{\"script\":{\"source\":\"return 1 + 1;\",\"lang\":\"mockscript\"}".replaceAll("\\s", "")) - ); + assertThat(searchRequest, containsString(""" + {"script":{"source":"return 1+1;","lang":"mockscript"}""")); assertThat(capturedContinueScrollIds.size(), equalTo(1)); assertThat(capturedContinueScrollIds.get(0), equalTo(response1.getScrollId())); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java index e47621bfecc1d..ecb49ef9fa713 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/extractor/scroll/SearchHitToJsonProcessorTests.java @@ -66,13 +66,9 @@ public void testProcessGivenMultipleHits() throws IOException { String json = searchHitToString(extractedFields, hit1, hit2); - assertThat( - json, - equalTo( - "{\"time\":1000,\"single\":\"a1\",\"array\":[\"b1\",\"c1\"]} " - + "{\"time\":2000,\"single\":\"a2\",\"array\":[\"b2\",\"c2\"]}" - ) - ); + assertThat(json, equalTo(""" + {"time":1000,"single":"a1","array":["b1","c1"]} \ + {"time":2000,"single":"a2","array":["b2","c2"]}""")); } private String searchHitToString(ExtractedFields fields, SearchHit... searchHits) throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java index c3ebfdc7905be..dc21f2151c93d 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/DataFrameDataExtractorTests.java @@ -272,7 +272,8 @@ public void testIncludeSourceIsFalseAndNoSourceFields() throws IOException { assertThat(dataExtractor.capturedSearchRequests.size(), equalTo(2)); String searchRequest = dataExtractor.capturedSearchRequests.get(0).request().toString().replaceAll("\\s", ""); - assertThat(searchRequest, containsString("\"docvalue_fields\":[{\"field\":\"field_1\"},{\"field\":\"field_2\"}]")); + assertThat(searchRequest, containsString(""" + "docvalue_fields":[{"field":"field_1"},{"field":"field_2"}]""")); assertThat(searchRequest, containsString("\"_source\":false")); } @@ -306,8 +307,10 @@ public void testIncludeSourceIsFalseAndAtLeastOneSourceField() throws IOExceptio assertThat(dataExtractor.capturedSearchRequests.size(), equalTo(2)); String searchRequest = dataExtractor.capturedSearchRequests.get(0).request().toString().replaceAll("\\s", ""); - assertThat(searchRequest, containsString("\"docvalue_fields\":[{\"field\":\"field_1\"}]")); - assertThat(searchRequest, containsString("\"_source\":{\"includes\":[\"field_2\"],\"excludes\":[]}")); + assertThat(searchRequest, containsString(""" + "docvalue_fields":[{"field":"field_1"}]""")); + assertThat(searchRequest, containsString(""" + "_source":{"includes":["field_2"],"excludes":[]}""")); } public void testCollectDataSummary_GivenAnalysisSupportsMissingFields() { @@ -341,14 +344,9 @@ public void testCollectDataSummary_GivenAnalysisDoesNotSupportMissingFields() { assertThat(dataExtractor.capturedSearchRequests.size(), equalTo(1)); String searchRequest = dataExtractor.capturedSearchRequests.get(0).request().toString().replaceAll("\\s", ""); - assertThat( - searchRequest, - containsString( - "\"query\":{\"bool\":{\"filter\":[{\"match_all\":{\"boost\":1.0}},{\"bool\":{\"filter\":" - + "[{\"exists\":{\"field\":\"field_1\",\"boost\":1.0}},{\"exists\":{\"field\":\"field_2\",\"boost\":1.0}}]," - + "\"boost\":1.0}}],\"boost\":1.0}" - ) - ); + assertThat(searchRequest, containsString(""" + "query":{"bool":{"filter":[{"match_all":{"boost":1.0}},{"bool":{"filter":[{"exists":{"field":"field_1","boost":1.0}},\ + {"exists":{"field":"field_2","boost":1.0}}],"boost":1.0}}],"boost":1.0}""")); } public void testMissingValues_GivenSupported() throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/SourceFieldTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/SourceFieldTests.java index b94a8c1a37b27..891033e76dfa1 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/SourceFieldTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/extractor/SourceFieldTests.java @@ -37,7 +37,8 @@ public void testSingleValue() { } public void testArray() { - SearchHit hit = new SearchHitBuilder(42).setSource("{\"array\":[\"a\",\"b\"]}").build(); + SearchHit hit = new SearchHitBuilder(42).setSource(""" + {"array":["a","b"]}""").build(); ExtractedField field = new SourceField("array", Collections.singleton("text")); @@ -54,7 +55,8 @@ public void testArray() { } public void testMissing() { - SearchHit hit = new SearchHitBuilder(42).setSource("{\"array\":[\"a\",\"b\"]}").build(); + SearchHit hit = new SearchHitBuilder(42).setSource(""" + {"array":["a","b"]}""").build(); ExtractedField missing = new SourceField("missing", Collections.singleton("text")); @@ -62,7 +64,8 @@ public void testMissing() { } public void testValueGivenNested() { - SearchHit hit = new SearchHitBuilder(42).setSource("{\"level_1\":{\"level_2\":{\"foo\":\"bar\"}}}").build(); + SearchHit hit = new SearchHitBuilder(42).setSource(""" + {"level_1":{"level_2":{"foo":"bar"}}}""").build(); ExtractedField nested = new SourceField("level_1.level_2.foo", Collections.singleton("text")); @@ -70,7 +73,8 @@ public void testValueGivenNested() { } public void testValueGivenNestedArray() { - SearchHit hit = new SearchHitBuilder(42).setSource("{\"level_1\":{\"level_2\":[{\"foo\":\"bar\"}]}}").build(); + SearchHit hit = new SearchHitBuilder(42).setSource(""" + {"level_1":{"level_2":[{"foo":"bar"}]}}""").build(); ExtractedField nested = new SourceField("level_1.level_2.foo", Collections.singleton("text")); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java index 6a6df6634a6fd..639f11c1468e3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/modelsize/ModelSizeInfoTests.java @@ -71,60 +71,60 @@ public void testParseDescribedFormat() throws IOException { doParseInstance(parser); } - private static final String FORMAT = "" - + "{\n" - + " \"trained_model_size\": {\n" - + " \"ensemble_model_size\": {\n" - + " \"tree_sizes\": [\n" - + " {\"num_nodes\": 7, \"num_leaves\": 8},\n" - + " {\"num_nodes\": 3, \"num_leaves\": 4},\n" - + " {\"num_leaves\": 1}\n" - + " ],\n" - + " \"feature_name_lengths\": [\n" - + " 14,\n" - + " 10,\n" - + " 11\n" - + " ],\n" - + " \"num_output_processor_weights\": 3,\n" - + " \"num_classification_weights\": 0,\n" - + " \"num_classes\": 0,\n" - + " \"num_operations\": 3\n" - + " }\n" - + " },\n" - + " \"preprocessors\": [\n" - + " {\n" - + " \"one_hot_encoding\": {\n" - + " \"field_length\": 10,\n" - + " \"field_value_lengths\": [\n" - + " 10,\n" - + " 20\n" - + " ],\n" - + " \"feature_name_lengths\": [\n" - + " 15,\n" - + " 25\n" - + " ]\n" - + " }\n" - + " },\n" - + " {\n" - + " \"frequency_encoding\": {\n" - + " \"field_length\": 10,\n" - + " \"feature_name_length\": 5,\n" - + " \"field_value_lengths\": [\n" - + " 10,\n" - + " 20\n" - + " ]\n" - + " }\n" - + " },\n" - + " {\n" - + " \"target_mean_encoding\": {\n" - + " \"field_length\": 6,\n" - + " \"feature_name_length\": 15,\n" - + " \"field_value_lengths\": [\n" - + " 10,\n" - + " 20\n" - + " ]\n" - + " }\n" - + " }\n" - + " ]\n" - + "} "; + private static final String FORMAT = """ + { + "trained_model_size": { + "ensemble_model_size": { + "tree_sizes": [ + {"num_nodes": 7, "num_leaves": 8}, + {"num_nodes": 3, "num_leaves": 4}, + {"num_leaves": 1} + ], + "feature_name_lengths": [ + 14, + 10, + 11 + ], + "num_output_processor_weights": 3, + "num_classification_weights": 0, + "num_classes": 0, + "num_operations": 3 + } + }, + "preprocessors": [ + { + "one_hot_encoding": { + "field_length": 10, + "field_value_lengths": [ + 10, + 20 + ], + "feature_name_lengths": [ + 15, + 25 + ] + } + }, + { + "frequency_encoding": { + "field_length": 10, + "feature_name_length": 5, + "field_value_lengths": [ + 10, + 20 + ] + } + }, + { + "target_mean_encoding": { + "field_length": 6, + "feature_name_length": 15, + "field_value_lengths": [ + 10, + 20 + ] + } + } + ] + }\s"""; } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDocTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDocTests.java index 311fe05dd5f88..29e1fc5aab527 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDocTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/inference/persistence/TrainedModelDefinitionDocTests.java @@ -25,16 +25,17 @@ public void testParsingDocWithCompressedStringDefinition() throws IOException { // The previous storage format was a base64 encoded string. // The new format should parse and decode the string storing the raw bytes. - String compressedStringDoc = "{\"doc_type\":\"trained_model_definition_doc\"," - + "\"model_id\":\"bntHUo\"," - + "\"doc_num\":6," - + "\"definition_length\":7," - + "\"total_definition_length\":13," - + "\"compression_version\":3," - + "\"definition\":\"" - + base64 - + "\"," - + "\"eos\":false}"; + String compressedStringDoc = """ + { + "doc_type": "trained_model_definition_doc", + "model_id": "bntHUo", + "doc_num": 6, + "definition_length": 7, + "total_definition_length": 13, + "compression_version": 3, + "definition": "%s", + "eos": false + }""".formatted(base64); try (XContentParser parser = createParser(JsonXContent.jsonXContent, compressedStringDoc)) { TrainedModelDefinitionDoc parsed = doParseInstance(parser); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java index 16b7f757a236b..4b016e433e39f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/CategorizationAnalyzerTests.java @@ -25,28 +25,17 @@ public class CategorizationAnalyzerTests extends ESTestCase { + @SuppressWarnings("checkstyle:linelength") private static final String NGINX_ERROR_EXAMPLE = - "a client request body is buffered to a temporary file /tmp/client-body/0000021894, client: 10.8.0.12, " - + "server: apm.35.205.226.121.ip.es.io, request: \"POST /intake/v2/events HTTP/1.1\", host: \"apm.35.205.226.121.ip.es.io\"\n" - + "10.8.0.12 - - [29/Nov/2020:21:34:55 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " - + "\"elasticapm-dotnet/1.5.1 System.Net.Http/4.6.28208.02 .NET_Core/2.2.8\" 27821 0.002 [default-apm-apm-server-8200] [] " - + "10.8.1.19:8200 0 0.001 202 f961c776ff732f5c8337530aa22c7216\n" - + "10.8.0.14 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " - + "\"elasticapm-python/5.10.0\" 3594 0.002 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 " - + "61feb8fb9232b1ebe54b588b95771ce4\n" - + "10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] \"OPTIONS /intake/v2/rum/events HTTP/2.0\" 200 0 " - + "\"http://opbeans-frontend:3000/dashboard\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " - + "Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36\" 292 0.001 [default-apm-apm-server-8200] [] " - + "10.8.1.19:8200 0 0.000 200 5fbe8cd4d217b932def1c17ed381c66b\n" - + "10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/rum/events HTTP/2.0\" 202 0 " - + "\"http://opbeans-frontend:3000/dashboard\" \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " - + "Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36\" 3004 0.001 [default-apm-apm-server-8200] [] " - + "10.8.1.18:8200 0 0.001 202 4735f571928595744ac6a9545c3ecdf5\n" - + "10.8.0.11 - - [29/Nov/2020:21:34:56 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " - + "\"elasticapm-node/3.8.0 elastic-apm-http-client/9.4.2 node/12.20.0\" 4913 10.006 [default-apm-apm-server-8200] [] " - + "10.8.1.18:8200 0 0.002 202 1eac41789ea9a60a8be4e476c54cbbc9\n" - + "10.8.0.14 - - [29/Nov/2020:21:34:57 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" \"elasticapm-python/5.10.0\" " - + "1025 0.001 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 d27088936cadd3b8804b68998a5f94fa"; + """ + a client request body is buffered to a temporary file /tmp/client-body/0000021894, client: 10.8.0.12, server: apm.35.205.226.121.ip.es.io, request: "POST /intake/v2/events HTTP/1.1", host: "apm.35.205.226.121.ip.es.io" + 10.8.0.12 - - [29/Nov/2020:21:34:55 +0000] "POST /intake/v2/events HTTP/1.1" 202 0 "-" "elasticapm-dotnet/1.5.1 System.Net.Http/4.6.28208.02 .NET_Core/2.2.8" 27821 0.002 [default-apm-apm-server-8200] [] 10.8.1.19:8200 0 0.001 202 f961c776ff732f5c8337530aa22c7216 + 10.8.0.14 - - [29/Nov/2020:21:34:56 +0000] "POST /intake/v2/events HTTP/1.1" 202 0 "-" "elasticapm-python/5.10.0" 3594 0.002 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 61feb8fb9232b1ebe54b588b95771ce4 + 10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] "OPTIONS /intake/v2/rum/events HTTP/2.0" 200 0 "http://opbeans-frontend:3000/dashboard" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36" 292 0.001 [default-apm-apm-server-8200] [] 10.8.1.19:8200 0 0.000 200 5fbe8cd4d217b932def1c17ed381c66b + 10.8.4.90 - - [29/Nov/2020:21:34:56 +0000] "POST /intake/v2/rum/events HTTP/2.0" 202 0 "http://opbeans-frontend:3000/dashboard" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/3.3.1 Chrome/61.0.3163.100 Electron/2.0.18 Safari/537.36" 3004 0.001 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 4735f571928595744ac6a9545c3ecdf5 + 10.8.0.11 - - [29/Nov/2020:21:34:56 +0000] "POST /intake/v2/events HTTP/1.1" 202 0 "-" "elasticapm-node/3.8.0 elastic-apm-http-client/9.4.2 node/12.20.0" 4913 10.006 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.002 202 1eac41789ea9a60a8be4e476c54cbbc9 + 10.8.0.14 - - [29/Nov/2020:21:34:57 +0000] "POST /intake/v2/events HTTP/1.1" 202 0 "-" "elasticapm-python/5.10.0" 1025 0.001 [default-apm-apm-server-8200] [] 10.8.1.18:8200 0 0.001 202 d27088936cadd3b8804b68998a5f94fa + """; private AnalysisRegistry analysisRegistry; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java index 0afb3d731406b..1c95e9b3f19b2 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/FirstLineWithLettersCharFilterTests.java @@ -109,11 +109,12 @@ public void testNoLinesWithLetters() throws IOException { public void testCorrect() throws IOException { - String input = " --------------------------------------------------------------------------------\n" - + "\n" - + "Alias 'foo' already exists and this prevents setting up ILM for logs\n" - + "\n" - + "--------------------------------------------------------------------------------"; + String input = """ + -------------------------------------------------------------------------------- + + Alias 'foo' already exists and this prevents setting up ILM for logs + + --------------------------------------------------------------------------------"""; FirstLineWithLettersCharFilter filter = new FirstLineWithLettersCharFilter(new StringReader(input)); String expectedOutput = "Alias 'foo' already exists and this prevents setting up ILM for logs"; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java index 21bb0948de1b3..1179392f22229 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/GrokPatternCreatorTests.java @@ -418,31 +418,29 @@ public void testFindBestGrokMatchFromExamplesGivenTruncated() { + "integer.+?integer.+?integer.+?line.+?at.+?SQL.+?statement.+?SQL.+?statement.+?SELECT.+?" + "probable_cause_list_common.+?evidenceIdIn.+?linkGroupId.+?timeSpanSeconds.+?PL.+?pgSQL.+?function.+?" + "probable_cause_list.+?integer.+?integer.+?line.+?at.+?PERFORM.*"; - Collection examples = Collections.singletonList( - "2013-05-16 12:13:45 BST:192.168.61.59(51438):dave:@bank3:[19084]: " - + "CONTEXT: SQL statement \"SELECT\n" - + " time_series_ids_tmp.evidence_id,\n" - + " time_series_ids_tmp.time_series_id,\n" - + " is_delta,\n" - + " GREATEST(usual_interval, 1)\n" - + " FROM\n" - + " time_series_ids_tmp\n" - + " WHERE\n" - + " found_peak_value = FALSE\n" - + " ORDER BY\n" - + " \n" - + " \n" - + " \n" - + " time_series_ids_tmp.magnitude DESC,\n" - + " time_series_ids_tmp.scaling_factor DESC,\n" - + " time_series_ids_tmp.significance DESC,\n" - + " time_series_ids_tmp.evidence_id DESC\n" - + " LIMIT\n" - + " 1\"\n" - + " PL/pgSQL function probable_cause_list_common(integer,integer,integer) line 255 at SQL statement\n" - + " SQL statement \"SELECT probable_cause_list_common(evidenceIdIn, linkGroupId, timeSpanSeconds)\"\n" - + " PL/pgSQL function probable_cause_list..." - ); + Collection examples = Collections.singletonList(""" + 2013-05-16 12:13:45 BST:192.168.61.59(51438):dave:@bank3:[19084]: CONTEXT: SQL statement "SELECT + time_series_ids_tmp.evidence_id, + time_series_ids_tmp.time_series_id, + is_delta, + GREATEST(usual_interval, 1) + FROM + time_series_ids_tmp + WHERE + found_peak_value = FALSE + ORDER BY + \s + \s + \s + time_series_ids_tmp.magnitude DESC, + time_series_ids_tmp.scaling_factor DESC, + time_series_ids_tmp.significance DESC, + time_series_ids_tmp.evidence_id DESC + LIMIT + 1" + PL/pgSQL function probable_cause_list_common(integer,integer,integer) line 255 at SQL statement + SQL statement "SELECT probable_cause_list_common(evidenceIdIn, linkGroupId, timeSpanSeconds)" + PL/pgSQL function probable_cause_list..."""); // Our algorithm for converting examples to Grok patterns that pick out useful fields doesn't work in // this case because the regex doesn't match the example (because the example has been truncated and // the regex contains pieces that would match parts of the original message beyond the truncation point) diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizerTests.java index ab79ac80d9e2a..b0a045f805f9a 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/categorization/MlStandardTokenizerTests.java @@ -149,9 +149,11 @@ public void testTokenizeWithWindowsPaths() throws IOException { } public void testTokenizeWithUrl() throws IOException { - String testData = "10.8.0.12 - - [29/Nov/2020:21:34:55 +0000] \"POST /intake/v2/events HTTP/1.1\" 202 0 \"-\" " - + "\"elasticapm-dotnet/1.5.1 System.Net.Http/4.6.28208.02 .NET_Core/2.2.8\" 27821 0.002 [default-apm-apm-server-8200] " - + "[] 10.8.1.19:8200 0 0.001 202 f961c776ff732f5c8337530aa22c7216\n"; + String testData = """ + 10.8.0.12 - - [29/Nov/2020:21:34:55 +0000] "POST /intake/v2/events HTTP/1.1" 202 0 "-" \ + "elasticapm-dotnet/1.5.1 System.Net.Http/4.6.28208.02 .NET_Core/2.2.8" 27821 0.002 [default-apm-apm-server-8200] [] \ + 10.8.1.19:8200 0 0.001 202 f961c776ff732f5c8337530aa22c7216 + """; try (Tokenizer tokenizer = new MlStandardTokenizer()) { tokenizer.setReader(new StringReader(testData)); tokenizer.reset(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java index d38b730949599..978ea72b5d501 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/AutodetectControlMsgWriterTests.java @@ -6,6 +6,7 @@ */ package org.elasticsearch.xpack.ml.job.process.autodetect.writer; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.TimeValue; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.core.ml.calendars.ScheduledEvent; @@ -185,8 +186,8 @@ public void testWriteUpdateModelPlotMessage() throws IOException { InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(4); inOrder.verify(lengthEncodedWriter, times(3)).writeField(""); - inOrder.verify(lengthEncodedWriter) - .writeField("u{\"model_plot_config\":{\"enabled\":true,\"terms\":\"foo,bar\",\"annotations_enabled\":false}}"); + inOrder.verify(lengthEncodedWriter).writeField(""" + u{"model_plot_config":{"enabled":true,"terms":"foo,bar","annotations_enabled":false}}"""); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -200,13 +201,38 @@ public void testWriteUpdateDetectorRulesMessage() throws IOException { InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(4); inOrder.verify(lengthEncodedWriter, times(3)).writeField(""); - inOrder.verify(lengthEncodedWriter) - .writeField( - "u{\"detector_rules\":{\"detector_index\":2," - + "\"custom_rules\":[{\"actions\":[\"skip_result\"]," - + "\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}," - + "{\"actions\":[\"skip_result\"],\"conditions\":[{\"applies_to\":\"actual\",\"operator\":\"gt\",\"value\":5.0}]}]}}" - ); + inOrder.verify(lengthEncodedWriter).writeField("u" + XContentHelper.stripWhitespace(""" + { + "detector_rules": { + "detector_index": 2, + "custom_rules": [ + { + "actions": [ + "skip_result" + ], + "conditions": [ + { + "applies_to": "actual", + "operator": "gt", + "value": 5.0 + } + ] + }, + { + "actions": [ + "skip_result" + ], + "conditions": [ + { + "applies_to": "actual", + "operator": "gt", + "value": 5.0 + } + ] + } + ] + } + }""")); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -221,10 +247,8 @@ public void testWriteUpdateFiltersMessage() throws IOException { InOrder inOrder = inOrder(lengthEncodedWriter); inOrder.verify(lengthEncodedWriter).writeNumFields(2); inOrder.verify(lengthEncodedWriter, times(1)).writeField(""); - inOrder.verify(lengthEncodedWriter) - .writeField( - "u{\"filters\":[{\"filter_id\":\"filter_1\",\"items\":[\"a\"]}," + "{\"filter_id\":\"filter_2\",\"items\":[\"b\",\"c\"]}]}" - ); + inOrder.verify(lengthEncodedWriter).writeField(""" + u{"filters":[{"filter_id":"filter_1","items":["a"]},{"filter_id":"filter_2","items":["b","c"]}]}"""); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -250,18 +274,57 @@ public void testWriteUpdateScheduledEventsMessage() throws IOException { inOrder.verify(lengthEncodedWriter, times(1)).writeField(""); ArgumentCaptor capturedMessage = ArgumentCaptor.forClass(String.class); inOrder.verify(lengthEncodedWriter).writeField(capturedMessage.capture()); - assertThat( - capturedMessage.getValue(), - equalTo( - "u{\"events\":[{\"description\":\"new year\"," - + "\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," - + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5147648E9}," - + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5148512E9}]}]}," - + "{\"description\":\"Jan maintenance day\",\"rules\":[{\"actions\":[\"skip_result\",\"skip_model_update\"]," - + "\"conditions\":[{\"applies_to\":\"time\",\"operator\":\"gte\",\"value\":1.5151968E9}," - + "{\"applies_to\":\"time\",\"operator\":\"lt\",\"value\":1.5152832E9}]}]}]}" - ) - ); + assertThat(capturedMessage.getValue(), equalTo("u" + XContentHelper.stripWhitespace(""" + { + "events": [ + { + "description": "new year", + "rules": [ + { + "actions": [ + "skip_result", + "skip_model_update" + ], + "conditions": [ + { + "applies_to": "time", + "operator": "gte", + "value": 1.5147648E9 + }, + { + "applies_to": "time", + "operator": "lt", + "value": 1.5148512E9 + } + ] + } + ] + }, + { + "description": "Jan maintenance day", + "rules": [ + { + "actions": [ + "skip_result", + "skip_model_update" + ], + "conditions": [ + { + "applies_to": "time", + "operator": "gte", + "value": 1.5151968E9 + }, + { + "applies_to": "time", + "operator": "lt", + "value": 1.5152832E9 + } + ] + } + ] + } + ] + }"""))); verifyNoMoreInteractions(lengthEncodedWriter); } @@ -316,13 +379,9 @@ public void testWriteForecastParamsMessage() throws IOException { inOrder.verify(lengthEncodedWriter).writeField(capturedMessage.capture()); assertThat(capturedMessage.getValue(), startsWith("p{\"forecast_id\":\"")); - assertThat( - capturedMessage.getValue(), - endsWith( - "\"duration\":10800,\"expires_in\":345600,\"tmp_storage\":\"/my_temp_dir\"," - + "\"max_model_memory\":12345,\"min_available_disk_space\":98765}" - ) - ); + assertThat(capturedMessage.getValue(), endsWith(""" + "duration":10800,"expires_in":345600,"tmp_storage":"/my_temp_dir",\ + "max_model_memory":12345,"min_available_disk_space":98765}""")); inOrder.verify(lengthEncodedWriter).writeNumFields(2); inOrder.verify(lengthEncodedWriter).writeField(""); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java index d5ea8d30b7644..21aa644b00ee8 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/JsonDataToProcessWriterTests.java @@ -90,10 +90,10 @@ public Void answer(InvocationOnMock invocation) throws Throwable { } public void testWrite_GivenTimeFormatIsEpochAndDataIsValid() throws Exception { - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"1\", \"metric\":\"foo\", \"value\":\"1.0\"}"); - input.append("{\"time\":\"2\", \"metric\":\"bar\", \"value\":\"2.0\"}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"1", "metric":"foo", "value":"1.0"}\ + {"time":"2", "metric":"bar", "value":"2.0"}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); @@ -117,10 +117,10 @@ public void testWrite_GivenTimeFormatIsEpochAndCategorization() throws Exception builder.setCategorizationAnalyzerConfig(CategorizationAnalyzerConfig.buildDefaultCategorizationAnalyzer(null)); analysisConfig = builder.build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"1\", \"message\":\"Node 1 started\"}"); - input.append("{\"time\":\"2\", \"message\":\"Node 2 started\"}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"1", "message":"Node 1 started"}\ + {"time":"2", "message":"Node 2 started"}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); try ( @@ -150,11 +150,11 @@ public void testWrite_GivenTimeFormatIsEpochAndCategorization() throws Exception } public void testWrite_GivenTimeFormatIsEpochAndTimestampsAreOutOfOrder() throws Exception { - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"3\", \"metric\":\"foo\", \"value\":\"3.0\"}"); - input.append("{\"time\":\"1\", \"metric\":\"bar\", \"value\":\"1.0\"}"); - input.append("{\"time\":\"2\", \"metric\":\"bar\", \"value\":\"2.0\"}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"3", "metric":"foo", "value":"3.0"}\ + {"time":"1", "metric":"bar", "value":"1.0"}\ + {"time":"2", "metric":"bar", "value":"2.0"}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); @@ -176,15 +176,15 @@ public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsOutOfOrderWithinBuc .setBucketSpan(TimeValue.timeValueSeconds(10)) .build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"4\", \"metric\":\"foo\", \"value\":\"4.0\"}"); - input.append("{\"time\":\"5\", \"metric\":\"foo\", \"value\":\"5.0\"}"); - input.append("{\"time\":\"3\", \"metric\":\"bar\", \"value\":\"3.0\"}"); - input.append("{\"time\":\"4\", \"metric\":\"bar\", \"value\":\"4.0\"}"); - input.append("{\"time\":\"2\", \"metric\":\"bar\", \"value\":\"2.0\"}"); - input.append("{\"time\":\"12\", \"metric\":\"bar\", \"value\":\"12.0\"}"); - input.append("{\"time\":\"2\", \"metric\":\"bar\", \"value\":\"2.0\"}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"4", "metric":"foo", "value":"4.0"}\ + {"time":"5", "metric":"foo", "value":"5.0"}\ + {"time":"3", "metric":"bar", "value":"3.0"}\ + {"time":"4", "metric":"bar", "value":"4.0"}\ + {"time":"2", "metric":"bar", "value":"2.0"}\ + {"time":"12", "metric":"bar", "value":"12.0"}\ + {"time":"2", "metric":"bar", "value":"2.0"}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); @@ -210,13 +210,13 @@ public void testWrite_GivenTimeFormatIsEpochAndSomeTimestampsWithinLatencySomeOu TimeValue.timeValueSeconds(2) ).setBucketSpan(TimeValue.timeValueSeconds(1)).setLatency(TimeValue.timeValueSeconds(2)).build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"4\", \"metric\":\"foo\", \"value\":\"4.0\"}"); - input.append("{\"time\":\"5\", \"metric\":\"foo\", \"value\":\"5.0\"}"); - input.append("{\"time\":\"3\", \"metric\":\"bar\", \"value\":\"3.0\"}"); - input.append("{\"time\":\"4\", \"metric\":\"bar\", \"value\":\"4.0\"}"); - input.append("{\"time\":\"2\", \"metric\":\"bar\", \"value\":\"2.0\"}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"4", "metric":"foo", "value":"4.0"}\ + {"time":"5", "metric":"foo", "value":"5.0"}\ + {"time":"3", "metric":"bar", "value":"3.0"}\ + {"time":"4", "metric":"bar", "value":"4.0"}\ + {"time":"2", "metric":"bar", "value":"2.0"}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); @@ -242,11 +242,11 @@ public void testWrite_GivenMalformedJsonWithoutNestedLevels() throws Exception { builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"1\", \"value\":\"1.0\"}"); - input.append("{\"time\":\"2\" \"value\":\"2.0\"}"); - input.append("{\"time\":\"3\", \"value\":\"3.0\"}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"1", "value":"1.0"}\ + {"time":"2" "value":"2.0"}\ + {"time":"3", "value":"3.0"}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); @@ -270,11 +270,11 @@ public void testWrite_GivenMalformedJsonWithNestedLevels() throws Exception { builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"1\", \"nested\":{\"value\":\"1.0\"}}"); - input.append("{\"time\":\"2\", \"nested\":{\"value\":\"2.0\"} \"foo\":\"bar\"}"); - input.append("{\"time\":\"3\", \"nested\":{\"value\":\"3.0\"}}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"1", "nested":{"value":"1.0"}}\ + {"time":"2", "nested":{"value":"2.0"} "foo":"bar"}\ + {"time":"3", "nested":{"value":"3.0"}}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); @@ -296,10 +296,9 @@ public void testWrite_GivenMalformedJsonThatNeverRecovers() throws Exception { builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"1\", \"value\":\"2.0\"}"); - input.append("{\"time"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"1", "value":"2.0"}{"time"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); @@ -313,10 +312,10 @@ public void testWrite_GivenJsonWithArrayField() throws Exception { builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"1\", \"array\":[\"foo\", \"bar\"], \"value\":\"1.0\"}"); - input.append("{\"time\":\"2\", \"array\":[], \"value\":\"2.0\"}"); - InputStream inputStream = createInputStream(input.toString()); + String input = """ + {"time":"1", "array":["foo", "bar"], "value":"1.0"}\ + {"time":"2", "array":[], "value":"2.0"}"""; + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); @@ -339,14 +338,14 @@ public void testWrite_GivenJsonWithMissingFields() throws Exception { builder.setLatency(TimeValue.timeValueSeconds(2)); analysisConfig = builder.build(); - StringBuilder input = new StringBuilder(); - input.append("{\"time\":\"1\", \"f1\":\"foo\", \"value\":\"1.0\"}"); - input.append("{\"time\":\"2\", \"value\":\"2.0\"}"); - input.append("{\"time\":\"3\", \"f1\":\"bar\"}"); - input.append("{}"); - input.append("{\"time\":\"4\", \"value\":\"3.0\"}"); + String input = """ + {"time":"1", "f1":"foo", "value":"1.0"}\ + {"time":"2", "value":"2.0"}\ + {"time":"3", "f1":"bar"}\ + {}\ + {"time":"4", "value":"3.0"}"""; - InputStream inputStream = createInputStream(input.toString()); + InputStream inputStream = createInputStream(input); JsonDataToProcessWriter writer = createWriter(); writer.writeHeader(); writer.write(inputStream, null, XContentType.JSON, (r, e) -> {}); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java index 426ee4cd673af..61dda713a3810 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/autodetect/writer/XContentRecordReaderTests.java @@ -32,7 +32,10 @@ public class XContentRecordReaderTests extends ESTestCase { public void testRead() throws JsonParseException, IOException { - String data = "{\"a\":10, \"b\":20, \"c\":30}\n{\"b\":21, \"a\":11, \"c\":31}\n"; + String data = """ + {"a":10, "b":20, "c":30} + {"b":21, "a":11, "c":31} + """; XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); @@ -55,7 +58,8 @@ public void testRead() throws JsonParseException, IOException { } public void testRead_GivenNestedField() throws JsonParseException, IOException { - String data = "{\"a\":10, \"b\":20, \"c\":{\"d\":30, \"e\":40}}"; + String data = """ + {"a":10, "b":20, "c":{"d":30, "e":40}}"""; XContentParser parser = createParser(data); Map fieldMap = new HashMap<>(); fieldMap.put("a", 0); @@ -76,7 +80,8 @@ public void testRead_GivenNestedField() throws JsonParseException, IOException { } public void testRead_GivenSingleValueArrays() throws JsonParseException, IOException { - String data = "{\"a\":[10], \"b\":20, \"c\":{\"d\":30, \"e\":[40]}}"; + String data = """ + {"a":[10], "b":20, "c":{"d":30, "e":[40]}}"""; XContentParser parser = createParser(data); Map fieldMap = new HashMap<>(); fieldMap.put("a", 0); @@ -97,7 +102,17 @@ public void testRead_GivenSingleValueArrays() throws JsonParseException, IOExcep } public void testRead_GivenMultiValueArrays() throws JsonParseException, IOException { - String data = "{\"a\":[10, 11], \"b\":20, \"c\":{\"d\":30, \"e\":[40, 50]}, " + "\"f\":[\"a\", \"a\", \"a\", \"a\"], \"g\":20}"; + String data = """ + { + "a": [ 10, 11 ], + "b": 20, + "c": { + "d": 30, + "e": [ 40, 50 ] + }, + "f": [ "a", "a", "a", "a" ], + "g": 20 + }"""; XContentParser parser = createParser(data); Map fieldMap = new HashMap<>(); fieldMap.put("a", 0); @@ -125,7 +140,10 @@ public void testRead_GivenMultiValueArrays() throws JsonParseException, IOExcept public void testRead_RecoverFromBadJson() throws JsonParseException, IOException { // no opening '{' - String data = "\"a\":10, \"b\":20, \"c\":30}\n{\"b\":21, \"a\":11, \"c\":31}\n" + "{\"c\":32, \"b\":22, \"a\":12}"; + String data = """ + "a":10, "b":20, "c":30} + {"b":21, "a":11, "c":31} + {"c":32, "b":22, "a":12}"""; XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); @@ -145,7 +163,9 @@ public void testRead_RecoverFromBadJson() throws JsonParseException, IOException public void testRead_RecoverFromBadNestedJson() throws JsonParseException, IOException { // nested object 'd' is missing a ',' - String data = "{\"a\":10, \"b\":20, \"c\":30}\n" + "{\"b\":21, \"d\" : {\"ee\": 1 \"ff\":0}, \"a\":11, \"c\":31}"; + String data = """ + {"a":10, "b":20, "c":30} + {"b":21, "d" : {"ee": 1 "ff":0}, "a":11, "c":31}"""; XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); @@ -167,7 +187,9 @@ public void testRead_RecoverFromBadNestedJson() throws JsonParseException, IOExc public void testRead_HitParseErrorsLimit() throws JsonParseException, IOException { // missing a ':' - String format = "{\"a\":1%1$d, \"b\"2%1$d, \"c\":3%1$d}\n"; + String format = """ + {"a":1%1$d, "b"2%1$d, "c":3%1$d} + """; StringBuilder builder = new StringBuilder(); for (int i = 0; i < XContentRecordReader.PARSE_ERRORS_LIMIT; i++) { builder.append(String.format(Locale.ROOT, format, i)); @@ -193,11 +215,11 @@ private void readUntilError(XContentRecordReader reader) throws IOException { public void testRead_givenControlCharacterInData() throws Exception { char controlChar = '\u0002'; - String data = "{\"a\":10, \"" - + controlChar - + "\" : 5, \"b\":20, \"c\":30}" - + "\n{\"b\":21, \"a\":11, \"c\":31}" - + "\n{\"c\":32, \"b\":22, \"a\":12}\n"; + String data = """ + {"a":10, "%s" : 5, "b":20, "c":30} + {"b":21, "a":11, "c":31} + {"c":32, "b":22, "a":12} + """.formatted(controlChar); XContentParser parser = createParser(data); Map fieldMap = createFieldMap(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java index c1da240b202a5..add86bcfe5953 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/process/normalizer/output/NormalizerResultHandlerTests.java @@ -20,16 +20,14 @@ public class NormalizerResultHandlerTests extends ESTestCase { private static final double EPSILON = 0.0000001; public void testParse() throws IOException { - - String testData = "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v1\"," - + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," - + "\"value_field_name\":\"x\",\"probability\":0.01,\"normalized_score\":88.88}\n" - + "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v2\"," - + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," - + "\"value_field_name\":\"x\",\"probability\":0.02,\"normalized_score\":44.44}\n" - + "{\"level\":\"leaf\",\"partition_field_name\":\"part\",\"partition_field_value\":\"v3\"," - + "\"person_field_name\":\"pers\",\"function_name\":\"f\"," - + "\"value_field_name\":\"x\",\"probability\":0.03,\"normalized_score\":22.22}\n"; + String testData = """ + {"level":"leaf","partition_field_name":"part","partition_field_value":"v1","person_field_name":"pers","function_name":"f",\ + "value_field_name":"x","probability":0.01,"normalized_score":88.88} + {"level":"leaf","partition_field_name":"part","partition_field_value":"v2","person_field_name":"pers","function_name":"f",\ + "value_field_name":"x","probability":0.02,"normalized_score":44.44} + {"level":"leaf","partition_field_name":"part","partition_field_value":"v3","person_field_name":"pers","function_name":"f",\ + "value_field_name":"x","probability":0.03,"normalized_score":22.22} + """; InputStream is = new ByteArrayInputStream(testData.getBytes(StandardCharsets.UTF_8)); NormalizerResultHandler handler = new NormalizerResultHandler(is); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java index 23fd71f529ea6..441ded7cfe3ff 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/BucketTests.java @@ -257,7 +257,9 @@ public void testCopyConstructor() { } public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp": 123544456, "bucket_span": 3600, "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Bucket.STRICT_PARSER.apply(parser, null)); @@ -266,7 +268,9 @@ public void testStrictParser() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\": 123544456, \"bucket_span\": 3600, \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp": 123544456, "bucket_span": 3600, "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { Bucket.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java index 16731029cd1cc..13d2ab999bb58 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/CategoryDefinitionTests.java @@ -153,7 +153,9 @@ private static CategoryDefinition createFullyPopulatedCategoryDefinition() { * For this class the strict parser is only used for parsing C++ output. */ public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, @@ -165,7 +167,9 @@ public void testStrictParser() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { CategoryDefinition.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java index 8361ecaa6b1f4..0f722e9d4beb5 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastRequestStatsTests.java @@ -84,7 +84,9 @@ protected ForecastRequestStats doParseInstance(XContentParser parser) { } public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"forecast_id\":\"forecast_1\", \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "forecast_id":"forecast_1", "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, @@ -96,7 +98,9 @@ public void testStrictParser() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"forecast_id\":\"forecast_1\", \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "forecast_id":"forecast_1", "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { ForecastRequestStats.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java index 57586bf86b63e..f1641960a0988 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ForecastTests.java @@ -87,8 +87,15 @@ public void testId() { } public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"forecast_id\":\"forecast_1\", \"timestamp\":12354667, \"bucket_span\": 3600," - + "\"detector_index\":3, \"foo\":\"bar\"}"; + String json = """ + { + "job_id": "job_1", + "forecast_id": "forecast_1", + "timestamp": 12354667, + "bucket_span": 3600, + "detector_index": 3, + "foo": "bar" + }"""; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Forecast.STRICT_PARSER.apply(parser, null)); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java index 4e1c59262e835..74e2a2aa7b0b2 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/InfluenceTests.java @@ -41,7 +41,9 @@ protected Influence doParseInstance(XContentParser parser) { } public void testStrictParser() throws IOException { - String json = "{\"influencer_field_name\":\"influencer_1\", \"influencer_field_values\":[], \"foo\":\"bar\"}"; + String json = """ + {"influencer_field_name":"influencer_1", "influencer_field_values":[], "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> Influence.STRICT_PARSER.apply(parser, null)); @@ -50,7 +52,9 @@ public void testStrictParser() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"influencer_field_name\":\"influencer_1\", \"influencer_field_values\":[], \"foo\":\"bar\"}"; + String json = """ + {"influencer_field_name":"influencer_1", "influencer_field_values":[], "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { Influence.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java index f14449691142c..65611b5034804 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/results/ModelPlotTests.java @@ -241,7 +241,9 @@ public void testId() { } public void testStrictParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\":12354667, \"bucket_span\": 3600, \"detector_index\":3, \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp":12354667, "bucket_span": 3600, "detector_index":3, "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ModelPlot.STRICT_PARSER.apply(parser, null)); @@ -250,7 +252,9 @@ public void testStrictParser() throws IOException { } public void testLenientParser() throws IOException { - String json = "{\"job_id\":\"job_1\", \"timestamp\":12354667, \"bucket_span\": 3600, \"detector_index\":3, \"foo\":\"bar\"}"; + String json = """ + {"job_id":"job_1", "timestamp":12354667, "bucket_span": 3600, "detector_index":3, "foo":"bar"} + """; try (XContentParser parser = createParser(JsonXContent.jsonXContent, json)) { ModelPlot.LENIENT_PARSER.apply(parser, null); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java index 27393b9ba7236..caedc7e3e0df3 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/IndexingStateProcessorTests.java @@ -50,17 +50,15 @@ */ public class IndexingStateProcessorTests extends ESTestCase { - private static final String STATE_SAMPLE = "" - + " \n" - + "{\"index\": {\"_index\": \"test\", \"_id\": \"1\"}}\n" - + "{ \"field\" : \"value1\" }\n" - + "\0" - + "{\"index\": {\"_index\": \"test\", \"_id\": \"2\"}}\n" - + "{ \"field\" : \"value2\" }\n" - + "\0" - + "{\"index\": {\"_index\": \"test\", \"_id\": \"3\"}}\n" - + "{ \"field\" : \"value3\" }\n" - + "\0"; + private static final String STATE_SAMPLE = """ + \s + {"index": {"_index": "test", "_id": "1"}} + { "field" : "value1" } + \0{"index": {"_index": "test", "_id": "2"}} + { "field" : "value2" } + \0{"index": {"_index": "test", "_id": "3"}} + { "field" : "value3" } + \0"""; private static final String JOB_ID = "state-processor-test-job"; @@ -88,8 +86,12 @@ public void verifyNoMoreClientInteractions() { } public void testExtractDocId() throws IOException { - assertThat(IndexingStateProcessor.extractDocId("{ \"index\": {\"_index\": \"test\", \"_id\": \"1\" } }\n"), equalTo("1")); - assertThat(IndexingStateProcessor.extractDocId("{ \"index\": {\"_id\": \"2\" } }\n"), equalTo("2")); + assertThat(IndexingStateProcessor.extractDocId(""" + { "index": {"_index": "test", "_id": "1" } } + """), equalTo("1")); + assertThat(IndexingStateProcessor.extractDocId(""" + { "index": {"_id": "2" } } + """), equalTo("2")); } private void testStateRead(SearchHits searchHits, String expectedIndexOrAlias) throws IOException { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java index f4ce7acc2de5f..9a851d5d8afa4 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/NativeControllerTests.java @@ -37,9 +37,11 @@ public class NativeControllerTests extends ESTestCase { private static final String NODE_NAME = "native-controller-tests-node"; - private static final String TEST_MESSAGE = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\",\"pid\":10211," - + "\"thread\":\"0x7fff7d2a8000\",\"message\":\"controller (64 bit): Version 6.0.0-alpha1-SNAPSHOT (Build a0d6ef8819418c) " - + "Copyright (c) 2017 Elasticsearch BV\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":123}\n"; + private static final String TEST_MESSAGE = """ + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":10211,"thread":"0x7fff7d2a8000",\ + "message":"controller (64 bit): Version 6.0.0-alpha1-SNAPSHOT (Build a0d6ef8819418c) Copyright (c) 2017 Elasticsearch BV",\ + "method":"main","file":"Main.cc","line":123} + """; private final Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build(); @@ -55,9 +57,8 @@ public void testStartProcessCommandSucceeds() throws Exception { when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))).thenReturn(logStream); ByteArrayOutputStream commandStream = new ByteArrayOutputStream(); when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))).thenReturn(commandStream); - ByteArrayInputStream outputStream = new ByteArrayInputStream( - "[{\"id\":1,\"success\":true,\"reason\":\"ok\"}]".getBytes(StandardCharsets.UTF_8) - ); + ByteArrayInputStream outputStream = new ByteArrayInputStream(""" + [{"id":1,"success":true,"reason":"ok"}]""".getBytes(StandardCharsets.UTF_8)); when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn(outputStream); List command = new ArrayList<>(); @@ -94,9 +95,8 @@ public void testStartProcessCommandFails() throws Exception { when(namedPipeHelper.openNamedPipeInputStream(contains("log"), any(Duration.class))).thenReturn(logStream); ByteArrayOutputStream commandStream = new ByteArrayOutputStream(); when(namedPipeHelper.openNamedPipeOutputStream(contains("command"), any(Duration.class))).thenReturn(commandStream); - ByteArrayInputStream outputStream = new ByteArrayInputStream( - "[{\"id\":1,\"success\":false,\"reason\":\"some problem\"}]".getBytes(StandardCharsets.UTF_8) - ); + ByteArrayInputStream outputStream = new ByteArrayInputStream(""" + [{"id":1,"success":false,"reason":"some problem"}]""".getBytes(StandardCharsets.UTF_8)); when(namedPipeHelper.openNamedPipeInputStream(contains("output"), any(Duration.class))).thenReturn(outputStream); List command = new ArrayList<>(); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java index 69296f3508030..55107bca776b2 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessPipesTests.java @@ -33,10 +33,10 @@ public class ProcessPipesTests extends ESTestCase { - private static final byte[] LOG_BYTES = ("\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151447,\"level\":\"INFO\"" - + ",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 5\",\"class\":\"ml\"," - + "\"method\":\"core::Something\",\"file\":\"Something.cc\",\"line\":555}\n").getBytes(StandardCharsets.UTF_8); + private static final byte[] LOG_BYTES = """ + {"logger":"controller","timestamp":1478261151447,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 5",\ + "class":"ml","method":"core::Something","file":"Something.cc","line":555} + """.getBytes(StandardCharsets.UTF_8); private static final byte[] OUTPUT_BYTES = { 3 }; private static final byte[] PERSIST_BYTES = { 6 }; diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java index 17edaa3a86ceb..9041ac19d20ed 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/ProcessResultsParserTests.java @@ -59,8 +59,8 @@ public void testParse_GivenArrayContainsAnotherArray() throws IOException { } public void testParseResults() throws IOException { - String input = "[{\"field_1\": \"a\", \"field_2\": 1.0}, {\"field_1\": \"b\", \"field_2\": 2.0}," - + " {\"field_1\": \"c\", \"field_2\": 3.0}]"; + String input = """ + [{"field_1": "a", "field_2": 1.0}, {"field_1": "b", "field_2": 2.0}, {"field_1": "c", "field_2": 3.0}]"""; try (InputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8))) { ProcessResultsParser parser = new ProcessResultsParser<>(TestResult.PARSER, NamedXContentRegistry.EMPTY); diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java index a3bb33f8119de..2ce5bf74cd9be 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageHandlerTests.java @@ -22,44 +22,50 @@ public class CppLogMessageHandlerTests extends ESTestCase { - private static final String TEST_MESSAGE_NOISE = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; - private static final String TEST_MESSAGE_NOISE_DIFFERENT_MESSAGE = "{\"logger\":\"controller\",\"timestamp\":1478261151445," - + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; - private static final String TEST_MESSAGE_NOISE_DIFFERENT_LEVEL = "{\"logger\":\"controller\",\"timestamp\":1478261151445," - + "\"level\":\"ERROR\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 3\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; - private static final String TEST_MESSAGE_OTHER_NOISE = "{\"logger\":\"controller\",\"timestamp\":1478261151446," - + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 4\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.h\",\"line\":333}\n"; - private static final String TEST_MESSAGE_SOMETHING = "{\"logger\":\"controller\",\"timestamp\":1478261151447,\"level\":\"INFO\"" - + ",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 5\",\"class\":\"ml\"," - + "\"method\":\"core::Something\",\"file\":\"Something.cc\",\"line\":555}\n"; - private static final String TEST_MESSAGE_NOISE_DEBUG = "{\"logger\":\"controller\",\"timestamp\":1478261151448,\"level\":\"DEBUG\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 6\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMake\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + private static final String TEST_MESSAGE_NOISE = """ + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 1",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + """; + private static final String TEST_MESSAGE_NOISE_DIFFERENT_MESSAGE = """ + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 2",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + """; + private static final String TEST_MESSAGE_NOISE_DIFFERENT_LEVEL = """ + {"logger":"controller","timestamp":1478261151445,"level":"ERROR","pid":42,"thread":"0x7fff7d2a8000","message":"message 3",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + """; + private static final String TEST_MESSAGE_OTHER_NOISE = """ + {"logger":"controller","timestamp":1478261151446,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 4",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.h","line":333} + """; + private static final String TEST_MESSAGE_SOMETHING = """ + {"logger":"controller","timestamp":1478261151447,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 5",\ + "class":"ml","method":"core::Something","file":"Something.cc","line":555} + """; + private static final String TEST_MESSAGE_NOISE_DEBUG = """ + {"logger":"controller","timestamp":1478261151448,"level":"DEBUG","pid":42,"thread":"0x7fff7d2a8000","message":"message 6",\ + "class":"ml","method":"core::SomeNoiseMake","file":"Noisemaker.cc","line":333} + """; private static final String TEST_MESSAGE_NON_JSON_FATAL_ERROR = "Segmentation fault core dumped"; public void testParse() throws IOException, TimeoutException { - - String testData = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\",\"pid\":10211," - + "\"thread\":\"0x7fff7d2a8000\",\"message\":\"uname -a : Darwin Davids-MacBook-Pro.local 15.6.0 Darwin Kernel " - + "Version 15.6.0: Thu Sep 1 15:01:16 PDT 2016; root:xnu-3248.60.11~2/RELEASE_X86_64 x86_64\",\"class\":\"ml\"," - + "\"method\":\"core::CLogger::reconfigureFromProps\",\"file\":\"CLogger.cc\",\"line\":452}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"DEBUG\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"Logger is logging to named pipe " - + "/var/folders/k5/5sqcdlps5sg3cvlp783gcz740000h0/T/controller_log_784\",\"class\":\"ml\"," - + "\"method\":\"core::CLogger::reconfigureLogToNamedPipe\",\"file\":\"CLogger.cc\",\"line\":333}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"controller (64 bit): Version based on 6.0.0-alpha1 (Build b0d6ef8819418c) " - + "Copyright (c) 2017 Elasticsearch BV\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":123}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261169065,\"level\":\"ERROR\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"Did not understand verb 'a'\",\"class\":\"ml\"," - + "\"method\":\"controller::CCommandProcessor::handleCommand\",\"file\":\"CCommandProcessor.cc\",\"line\":100}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261169065,\"level\":\"DEBUG\",\"pid\":10211,\"thread\":\"0x7fff7d2a8000\"," - + "\"message\":\"Ml controller exiting\",\"method\":\"main\",\"file\":\"Main.cc\",\"line\":147}\n"; + String testData = """ + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":10211,"thread":"0x7fff7d2a8000",\ + "message":"uname -a : Darwin Davids-MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Sep 1 15:01:16 PDT 2016; \ + root:xnu-3248.60.11~2/RELEASE_X86_64 x86_64","class":"ml","method":"core::CLogger::reconfigureFromProps",\ + "file":"CLogger.cc","line":452} + {"logger":"controller","timestamp":1478261151445,"level":"DEBUG","pid":10211,"thread":"0x7fff7d2a8000",\ + "message":"Logger is logging to named pipe /var/folders/k5/5sqcdlps5sg3cvlp783gcz740000h0/T/controller_log_784",\ + "class":"ml","method":"core::CLogger::reconfigureLogToNamedPipe","file":"CLogger.cc","line":333} + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":10211,"thread":"0x7fff7d2a8000",\ + "message":"controller (64 bit): Version based on 6.0.0-alpha1 (Build b0d6ef8819418c) Copyright (c) 2017 Elasticsearch BV",\ + "method":"main","file":"Main.cc","line":123} + {"logger":"controller","timestamp":1478261169065,"level":"ERROR","pid":10211,"thread":"0x7fff7d2a8000",\ + "message":"Did not understand verb 'a'","class":"ml","method":"controller::CCommandProcessor::handleCommand",\ + "file":"CCommandProcessor.cc","line":100} + {"logger":"controller","timestamp":1478261169065,"level":"DEBUG","pid":10211,"thread":"0x7fff7d2a8000",\ + "message":"Ml controller exiting","method":"main","file":"Main.cc","line":147} + """; // Try different buffer sizes to smoke out edge case problems in the buffer management for (int readBufSize : new int[] { 11, 42, 101, 1024, 9999 }) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java index 05699fce238bf..95e68036122bf 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/process/logging/CppLogMessageTests.java @@ -38,9 +38,18 @@ public void testParseWithMissingTimestamp() throws IOException { XContent xContent = XContentFactory.xContent(XContentType.JSON); Instant before = Instant.ofEpochMilli(Instant.now().toEpochMilli()); - String input = "{\"logger\":\"controller\",\"level\":\"INFO\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; + String input = """ + { + "logger": "controller", + "level": "INFO", + "pid": 42, + "thread": "0x7fff7d2a8000", + "message": "message 1", + "class": "ml", + "method": "core::SomeNoiseMaker", + "file": "Noisemaker.cc", + "line": 333 + }"""; XContentParser parser = xContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, input); CppLogMessage msg = CppLogMessage.PARSER.apply(parser, null); diff --git a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java index 62ceb677d6265..e0ccd95f44515 100644 --- a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java +++ b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java @@ -838,7 +838,8 @@ private void enqueueWatcherResponses( // if the remote cluster doesn't allow watcher, then we only check for it and we're done if (remoteClusterAllowsWatcher) { // X-Pack exists and Watcher can be used - enqueueResponse(webServer, 200, "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":true}}}"); + enqueueResponse(webServer, 200, """ + {"features":{"watcher":{"available":true,"enabled":true}}}"""); // if we have an active license that's not Basic, then we should add watches if (currentLicenseAllowsWatcher) { @@ -854,11 +855,9 @@ private void enqueueWatcherResponses( } else { // X-Pack exists but Watcher just cannot be used if (randomBoolean()) { - final String responseBody = randomFrom( - "{\"features\":{\"watcher\":{\"available\":false,\"enabled\":true}}}", - "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":false}}}", - "{}" - ); + final String responseBody = randomFrom(""" + {"features":{"watcher":{"available":false,"enabled":true}}}""", """ + {"features":{"watcher":{"available":true,"enabled":false}}}""", "{}"); enqueueResponse(webServer, 200, responseBody); } else { diff --git a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java index 6b0f44d626ef9..47a9e54644901 100644 --- a/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java +++ b/x-pack/plugin/monitoring/src/internalClusterTest/java/org/elasticsearch/xpack/monitoring/integration/MonitoringIT.java @@ -95,13 +95,15 @@ protected Collection> getPlugins() { } private String createBulkEntity() { - return "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" - + "{\"foo\":{\"bar\":0}}\n" - + "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" - + "{\"foo\":{\"bar\":1}}\n" - + "{\"index\":{\"_type\":\"monitoring_data_type\"}}\n" - + "{\"foo\":{\"bar\":2}}\n" - + "\n"; + return """ + {"index":{"_type":"monitoring_data_type"}} + {"foo":{"bar":0}} + {"index":{"_type":"monitoring_data_type"}} + {"foo":{"bar":1}} + {"index":{"_type":"monitoring_data_type"}} + {"foo":{"bar":2}} + + """; } /** diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java index 80ce7470f80f3..a3a19936822bb 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringBulkActionTests.java @@ -366,28 +366,26 @@ public void testAsyncActionCreateMonitoringDoc() throws Exception { ).createMonitoringDoc(monitoringBulkDoc); final BytesReference xContent = XContentHelper.toXContent(monitoringDoc, XContentType.JSON, randomBoolean()); - assertEquals( - "{" - + "\"cluster_uuid\":\"_cluster_uuid\"," - + "\"timestamp\":\"2017-08-07T12:03:22.133Z\"," - + "\"interval_ms\":15000," - + "\"type\":\"_type\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"_type\":{" - + "\"_foo\":{" - + "\"_bar\":\"_baz\"" - + "}" - + "}" - + "}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "cluster_uuid": "_cluster_uuid", + "timestamp": "2017-08-07T12:03:22.133Z", + "interval_ms": 15000, + "type": "_type", + "source_node": { + "uuid": "_uuid", + "host": "_host", + "transport_address": "_addr", + "ip": "_ip", + "name": "_name", + "timestamp": "2017-08-31T08:46:30.855Z" + }, + "_type": { + "_foo": { + "_bar": "_baz" + } + } + }"""), xContent.utf8ToString()); } @SuppressWarnings("unchecked") diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java index c242c871a50fd..d86e802ce5d17 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/action/TransportMonitoringMigrateAlertsActionTests.java @@ -433,7 +433,8 @@ public void testRemoteAlertsRemovalFailure() throws Exception { assertAcked(client().admin().cluster().prepareUpdateSettings().setPersistentSettings(exporterSettings)); // enqueue a "watcher available" response, but then a "failure to delete watch" response - enqueueResponse(webServer, 200, "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":true}}}"); + enqueueResponse(webServer, 200, """ + {"features":{"watcher":{"available":true,"enabled":true}}}"""); enqueueResponse(webServer, 500, "{\"error\":{}}"); // call migration api @@ -559,18 +560,17 @@ private void enqueueWatcherResponses(final MockWebServer mockWebServer, final bo // if the remote cluster doesn't allow watcher, then we only check for it and we're done if (remoteClusterAllowsWatcher) { // X-Pack exists and Watcher can be used - enqueueResponse(mockWebServer, 200, "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":true}}}"); + enqueueResponse(mockWebServer, 200, """ + {"features":{"watcher":{"available":true,"enabled":true}}}"""); // add delete responses enqueueDeleteClusterAlertResponses(mockWebServer); } else { // X-Pack exists but Watcher just cannot be used if (randomBoolean()) { - final String responseBody = randomFrom( - "{\"features\":{\"watcher\":{\"available\":false,\"enabled\":true}}}", - "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":false}}}", - "{}" - ); + final String responseBody = randomFrom(""" + {"features":{"watcher":{"available":false,"enabled":true}}}""", """ + {"features":{"watcher":{"available":true,"enabled":false}}}""", "{}"); enqueueResponse(mockWebServer, 200, responseBody); } else { 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 8372d3c0e74e1..7b0b24678129a 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 @@ -435,311 +435,306 @@ public void testToXContent() throws IOException { ); final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, false); - final String expectedJson = String.format( - Locale.ROOT, - "{" - + " \"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," - + " \"max_resource_units\": null," - + " \"issued_to\": \"customer\"," - + " \"issuer\": \"elasticsearch\"," - + " \"start_date_in_millis\": -1%s" - + " }," - + " \"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," - + " \"total_data_set_size_in_bytes\": 0," - + " \"reserved_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\": {}" - + " }," - + " \"mappings\":{" - + " \"field_types\":[]," - + " \"runtime_field_types\":[]" - + " }," - + " \"analysis\":{" - + " \"char_filter_types\":[]," - + " \"tokenizer_types\":[]," - + " \"filter_types\":[]," - + " \"analyzer_types\":[]," - + " \"built_in_char_filters\":[]," - + " \"built_in_tokenizers\":[]," - + " \"built_in_filters\":[]," - + " \"built_in_analyzers\":[]" - + " }," - + " \"versions\":[]" - + " }," - + " \"nodes\": {" - + " \"count\": {" - + " \"total\": 1," - + " \"coordinating_only\": 0," - + " \"data\": 0," - + " \"data_cold\": 0," - + " \"data_content\": 0," - + " \"data_frozen\": 0," - + " \"data_hot\": 0," - + " \"data_warm\": 0," - + " \"ingest\": 0," - + " \"master\": 1," - + " \"ml\": 0," - + " \"remote_cluster_client\": 0," - + " \"transform\": 0," - + " \"voting_only\": 0" - + " }," - + " \"versions\": [" - + " \"%s\"" - + " ]," - + " \"os\": {" - + " \"available_processors\": 32," - + " \"allocated_processors\": 16," - + " \"names\": [" - + " {" - + " \"name\": \"_os_name\"," - + " \"count\": 1" - + " }" - + " ]," - + " \"pretty_names\": [" - + " {" - + " \"pretty_name\": \"_pretty_os_name\"," - + " \"count\": 1" - + " }" - + " ]," - + " \"architectures\": [" - + " {" - + " \"arch\": \"_architecture\"," - + " \"count\": 1" - + " }" - + " ]," - + " \"mem\": {" - + " \"total_in_bytes\": 100," - + " \"adjusted_total_in_bytes\": 99," - + " \"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," - + " \"licensed\": false," - + " \"type\": \"isolated\"" - + " }" - + " ]," - + " \"network_types\": {" - + " \"transport_types\": {" - + " \"_transport\": 1" - + " }," - + " \"http_types\": {" - + " \"_http\": 1" - + " }" - + " }," - + " \"discovery_types\": {" - + " \"_disco\": 1" - + " }," - + " \"packaging_types\": [" - + " {" - + " \"flavor\": \"default\"," - + " \"type\": \"docker\"," - + " \"count\": 1" - + " }" - + " ]," - + " \"ingest\": {" - + " \"number_of_pipelines\": 0," - + " \"processor_stats\": {}" - + " }," - + " \"indexing_pressure\": {" - + " \"memory\": {" - + " \"current\" :{" - + " \"combined_coordinating_and_primary_in_bytes\": 0," - + " \"coordinating_in_bytes\": 0," - + " \"primary_in_bytes\": 0," - + " \"replica_in_bytes\": 0," - + " \"all_in_bytes\": 0" - + " }," - + " \"total\": {" - + " \"combined_coordinating_and_primary_in_bytes\": 0," - + " \"coordinating_in_bytes\": 0," - + " \"primary_in_bytes\": 0," - + " \"replica_in_bytes\": 0," - + " \"all_in_bytes\": 0," - + " \"coordinating_rejections\": 0," - + " \"primary_rejections\": 0," - + " \"replica_rejections\": 0" - + " }," - + " \"limit_in_bytes\": 0" - + " }" - + " }" - + " }" - + " }," - + " \"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\"" - + " }," - + " \"roles\" : [" - + " \"master\"" - + " ]" - + " }" - + " }" - + " }," - + " \"cluster_settings\": {" - + " \"cluster\": {" - + " \"metadata\": {" - + " \"display_name\": \"my_prod_cluster\"" - + " }" - + " }" - + " }," - + " \"stack_stats\": {" - + " \"apm\": {" - + " \"found\": %s" - + " }," - + " \"xpack\": {" - + " \"monitoring\": {" - + " \"available\": true," - + " \"enabled\": true," - + " \"collection_enabled\": false" - + " }" - + " }" - + " }" - + "}", - needToEnableTLS ? ",\"cluster_needs_tls\": true" : "", - mockNodeVersion, - Version.CURRENT, - apmIndicesExist - ); + final String expectedJson = """ + { + "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, + "max_resource_units": null, + "issued_to": "customer", + "issuer": "elasticsearch", + "start_date_in_millis": -1 + %s + }, + "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, + "total_data_set_size_in_bytes": 0, + "reserved_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": {} + }, + "mappings": { + "field_types": [], + "runtime_field_types": [] + }, + "analysis": { + "char_filter_types": [], + "tokenizer_types": [], + "filter_types": [], + "analyzer_types": [], + "built_in_char_filters": [], + "built_in_tokenizers": [], + "built_in_filters": [], + "built_in_analyzers": [] + }, + "versions": [] + }, + "nodes": { + "count": { + "total": 1, + "coordinating_only": 0, + "data": 0, + "data_cold": 0, + "data_content": 0, + "data_frozen": 0, + "data_hot": 0, + "data_warm": 0, + "ingest": 0, + "master": 1, + "ml": 0, + "remote_cluster_client": 0, + "transform": 0, + "voting_only": 0 + }, + "versions": [ + "%s" + ], + "os": { + "available_processors": 32, + "allocated_processors": 16, + "names": [ + { + "name": "_os_name", + "count": 1 + } + ], + "pretty_names": [ + { + "pretty_name": "_pretty_os_name", + "count": 1 + } + ], + "architectures": [ + { + "arch": "_architecture", + "count": 1 + } + ], + "mem": { + "total_in_bytes": 100, + "adjusted_total_in_bytes": 99, + "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, + "licensed": false, + "type": "isolated" + } + ], + "network_types": { + "transport_types": { + "_transport": 1 + }, + "http_types": { + "_http": 1 + } + }, + "discovery_types": { + "_disco": 1 + }, + "packaging_types": [ + { + "flavor": "default", + "type": "docker", + "count": 1 + } + ], + "ingest": { + "number_of_pipelines": 0, + "processor_stats": {} + }, + "indexing_pressure": { + "memory": { + "current": { + "combined_coordinating_and_primary_in_bytes": 0, + "coordinating_in_bytes": 0, + "primary_in_bytes": 0, + "replica_in_bytes": 0, + "all_in_bytes": 0 + }, + "total": { + "combined_coordinating_and_primary_in_bytes": 0, + "coordinating_in_bytes": 0, + "primary_in_bytes": 0, + "replica_in_bytes": 0, + "all_in_bytes": 0, + "coordinating_rejections": 0, + "primary_rejections": 0, + "replica_rejections": 0 + }, + "limit_in_bytes": 0 + } + } + } + }, + "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" + }, + "roles": [ + "master" + ] + } + } + }, + "cluster_settings": { + "cluster": { + "metadata": { + "display_name": "my_prod_cluster" + } + } + }, + "stack_stats": { + "apm": { + "found": %s + }, + "xpack": { + "monitoring": { + "available": true, + "enabled": true, + "collection_enabled": false + } + } + } + }""".formatted(needToEnableTLS ? ",\"cluster_needs_tls\": true" : "", mockNodeVersion, Version.CURRENT, apmIndicesExist); assertEquals(stripWhitespace(expectedJson), xContent.utf8ToString()); } 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 9ea4258b2761b..65e01772eb675 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 @@ -31,7 +31,6 @@ 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; @@ -132,85 +131,78 @@ public void testToXContent() throws IOException { ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - final String expected = XContentHelper.stripWhitespace( - String.format( - Locale.ROOT, - "{" - + " \"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," - + " \"recovered_from_snapshot_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() - ) - ); + final String expected = XContentHelper.stripWhitespace(""" + { + "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": %s, + "stop_time_in_millis": %s, + "total_time_in_millis": %s, + "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, + "recovered_from_snapshot_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 + } + } + ] + } + }""".formatted(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 68ab5e0b8b2df..2d85b0e41b366 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 @@ -155,146 +155,145 @@ public void testToXContent() throws IOException { xContent = BytesReference.bytes(builder); } + // indexStatsSummary() final String expected = stripWhitespace( - String.format( - Locale.ROOT, - "{" - + " \"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\": 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\": 22," - + " \"version_map_memory_in_bytes\": 23," - + " \"fixed_bit_set_memory_in_bytes\": 24" - + " }," - + " \"request_cache\": {" - + " \"memory_size_in_bytes\": 9," - + " \"evictions\": 10," - + " \"hit_count\": 11," - + " \"miss_count\": 12" - + " }," - + " \"bulk\": {" - + " \"total_operations\": 0," - + " \"total_time_in_millis\": 0," - + " \"total_size_in_bytes\": 0," - + " \"avg_time_in_millis\": 0," - + " \"avg_size_in_bytes\": 0" - + " }" - + " }," - + " \"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\": 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\": 22," - + " \"version_map_memory_in_bytes\": 23," - + " \"fixed_bit_set_memory_in_bytes\": 24" - + " }," - + " \"request_cache\": {" - + " \"memory_size_in_bytes\": 9," - + " \"evictions\": 10," - + " \"hit_count\": 11," - + " \"miss_count\": 12" - + " }," - + " \"bulk\": {" - + " \"total_operations\": 0," - + " \"total_time_in_millis\": 0," - + " \"total_size_in_bytes\": 0," - + " \"avg_time_in_millis\": 0," - + " \"avg_size_in_bytes\": 0" - + " }" - + " }" - + " }" - + "}", + """ + { + "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, + "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": 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": 22, + "version_map_memory_in_bytes": 23, + "fixed_bit_set_memory_in_bytes": 24 + }, + "request_cache": { + "memory_size_in_bytes": 9, + "evictions": 10, + "hit_count": 11, + "miss_count": 12 + }, + "bulk": { + "total_operations": 0, + "total_time_in_millis": 0, + "total_size_in_bytes": 0, + "avg_time_in_millis": 0, + "avg_size_in_bytes": 0 + } + }, + "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": 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": 22, + "version_map_memory_in_bytes": 23, + "fixed_bit_set_memory_in_bytes": 24 + }, + "request_cache": { + "memory_size_in_bytes": 9, + "evictions": 10, + "hit_count": 11, + "miss_count": 12 + }, + "bulk": { + "total_operations": 0, + "total_time_in_millis": 0, + "total_size_in_bytes": 0, + "avg_time_in_millis": 0, + "avg_size_in_bytes": 0 + } + } + }}""".formatted( // Since the summary is being merged with other data, remove the enclosing braces. indexStatsSummary().replaceAll("(^\\{|}$)", "") ) @@ -326,27 +325,22 @@ public void testToXContentWithNullStats() throws IOException { ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - final String expected = stripWhitespace( - String.format( - Locale.ROOT, - "{" - + " \"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() - ) - ); + final String expected = stripWhitespace(""" + { + "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 + }""".formatted(indexStatsSummary())); assertEquals(expected, xContent.utf8ToString()); } 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 02d18b61a4645..8ec4bd8141756 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 @@ -104,76 +104,75 @@ public void testToXContent() throws IOException { ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - final String expected = XContentHelper.stripWhitespace( - "{" - + " \"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" - + " }," - + " \"bulk\": {" - + " \"total_operations\": 0," - + " \"total_time_in_millis\": 0," - + " \"total_size_in_bytes\": 0," - + " \"avg_time_in_millis\": 0," - + " \"avg_size_in_bytes\": 0" - + " }" - + " }," - + " \"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" - + " }," - + " \"bulk\": {" - + " \"total_operations\": 0," - + " \"total_time_in_millis\": 0," - + " \"total_size_in_bytes\": 0," - + " \"avg_time_in_millis\": 0," - + " \"avg_size_in_bytes\": 0" - + " }" - + " }" - + " }" - + " }" - + "}" - ); + final String expected = XContentHelper.stripWhitespace(""" + { + "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 + }, + "bulk": { + "total_operations": 0, + "total_time_in_millis": 0, + "total_size_in_bytes": 0, + "avg_time_in_millis": 0, + "avg_size_in_bytes": 0 + } + }, + "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 + }, + "bulk": { + "total_operations": 0, + "total_time_in_millis": 0, + "total_size_in_bytes": 0, + "avg_time_in_millis": 0, + "avg_size_in_bytes": 0 + } + } + } + } + }"""); assertEquals(expected, xContent.utf8ToString()); } 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 f6bb35879162e..e22f3ab4a2065 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 @@ -164,90 +164,89 @@ public void testToXContent() throws IOException { final JobStatsMonitoringDoc document = new JobStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, jobStats); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - final String expected = XContentHelper.stripWhitespace( - "{" - + " \"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," - + " \"log_time\":1483751288007" - + " }," - + " \"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\"," - + " \"categorized_doc_count\": 42," - + " \"total_category_count\": 8," - + " \"frequent_category_count\": 4," - + " \"rare_category_count\": 2," - + " \"dead_category_count\": 1," - + " \"failed_category_count\": 3," - + " \"categorization_status\": \"warn\"," - + " \"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" - + " }" - + " }" - + "}" - ); + final String expected = XContentHelper.stripWhitespace(""" + { + "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, + "log_time": 1483751288007 + }, + "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", + "categorized_doc_count": 42, + "total_category_count": 8, + "frequent_category_count": 4, + "rare_category_count": 2, + "dead_category_count": 1, + "failed_category_count": 3, + "categorization_status": "warn", + "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 + } + } + }"""); 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 646d5dc569d20..4b22722e323fb 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 @@ -131,184 +131,183 @@ public void testToXContent() throws IOException { doc.toXContent(builder, ToXContent.EMPTY_PARAMS); xContent = BytesReference.bytes(builder); } - final String expected = XContentHelper.stripWhitespace( - "{" - + " \"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\": 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\": 20," - + " \"version_map_memory_in_bytes\": 21," - + " \"fixed_bit_set_memory_in_bytes\": 22" - + " }," - + " \"request_cache\": {" - + " \"memory_size_in_bytes\": 13," - + " \"evictions\": 14," - + " \"hit_count\": 15," - + " \"miss_count\": 16" - + " }," - + " \"bulk\": {" - + " \"total_operations\": 0," - + " \"total_time_in_millis\": 0," - + " \"total_size_in_bytes\": 0," - + " \"avg_time_in_millis\": 0," - + " \"avg_size_in_bytes\": 0" - + " }" - + " }," - + " \"os\": {" - + " \"cpu\": {" - + " \"load_average\": {" - + " \"1m\": 36.0," - + " \"5m\": 37.0," - + " \"15m\": 38.0" - + " }" - + " }," - + " \"cgroup\": {" - + " \"cpuacct\": {" - + " \"control_group\": \"_cpu_acct_ctrl_group\"," - + " \"usage_nanos\": 42" - + " }," - + " \"cpu\": {" - + " \"control_group\": \"_cpu_ctrl_group\"," - + " \"cfs_period_micros\": 43," - + " \"cfs_quota_micros\": 44," - + " \"stat\": {" - + " \"number_of_elapsed_periods\": 39," - + " \"number_of_times_throttled\": 40," - + " \"time_throttled_nanos\": 41" - + " }" - + " }," - + " \"memory\": {" - + " \"control_group\": \"_memory_ctrl_group\"," - + " \"limit_in_bytes\": \"2000000000\"," - + " \"usage_in_bytes\": \"1000000000\"" - + " }" - + " }" - + " }," - + " \"process\": {" - + " \"open_file_descriptors\": 46," - + " \"max_file_descriptors\": 47," - + " \"cpu\": {" - + " \"percent\": 45" - + " }" - + " }," - + " \"jvm\": {" - + " \"mem\": {" - + " \"heap_used_in_bytes\": 48," - + " \"heap_used_percent\": 97," - + " \"heap_max_in_bytes\": 49" - + " }," - + " \"gc\": {" - + " \"collectors\": {" - + " \"young\": {" - + " \"collection_count\": 50," - + " \"collection_time_in_millis\": 51" - + " }," - + " \"old\": {" - + " \"collection_count\": 52," - + " \"collection_time_in_millis\": 53" - + " }" - + " }" - + " }" - + " }," - + " \"thread_pool\": {" - + " \"generic\": {" - + " \"threads\": 54," - + " \"queue\": 55," - + " \"rejected\": 56" - + " }," - + " \"get\": {" - + " \"threads\": 57," - + " \"queue\": 58," - + " \"rejected\": 59" - + " }," - + " \"management\": {" - + " \"threads\": 60," - + " \"queue\": 61," - + " \"rejected\": 62" - + " }," - + " \"search\": {" - + " \"threads\": 63," - + " \"queue\": 64," - + " \"rejected\": 65" - + " }," - + " \"watcher\": {" - + " \"threads\": 66," - + " \"queue\": 67," - + " \"rejected\": 68" - + " }," - + " \"write\": {" - + " \"threads\": 69," - + " \"queue\": 70," - + " \"rejected\": 71" - + " }" - + " }," - + " \"fs\": {" - + " \"total\": {" - + " \"total_in_bytes\": 33," - + " \"free_in_bytes\": 34," - + " \"available_in_bytes\": 35" - + " }," - + " \"io_stats\": {" - + " \"total\": {" - + " \"operations\": 10," - + " \"read_operations\": 5," - + " \"write_operations\": 5," - + " \"read_kilobytes\": 2," - + " \"write_kilobytes\": 2" - + " }" - + " }" - + " }" - + " }" - + "}" - ); + final String expected = XContentHelper.stripWhitespace(""" + { + "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": 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": 20, + "version_map_memory_in_bytes": 21, + "fixed_bit_set_memory_in_bytes": 22 + }, + "request_cache": { + "memory_size_in_bytes": 13, + "evictions": 14, + "hit_count": 15, + "miss_count": 16 + }, + "bulk": { + "total_operations": 0, + "total_time_in_millis": 0, + "total_size_in_bytes": 0, + "avg_time_in_millis": 0, + "avg_size_in_bytes": 0 + } + }, + "os": { + "cpu": { + "load_average": { + "1m": 36.0, + "5m": 37.0, + "15m": 38.0 + } + }, + "cgroup": { + "cpuacct": { + "control_group": "_cpu_acct_ctrl_group", + "usage_nanos": 42 + }, + "cpu": { + "control_group": "_cpu_ctrl_group", + "cfs_period_micros": 43, + "cfs_quota_micros": 44, + "stat": { + "number_of_elapsed_periods": 39, + "number_of_times_throttled": 40, + "time_throttled_nanos": 41 + } + }, + "memory": { + "control_group": "_memory_ctrl_group", + "limit_in_bytes": "2000000000", + "usage_in_bytes": "1000000000" + } + } + }, + "process": { + "open_file_descriptors": 46, + "max_file_descriptors": 47, + "cpu": { + "percent": 45 + } + }, + "jvm": { + "mem": { + "heap_used_in_bytes": 48, + "heap_used_percent": 97, + "heap_max_in_bytes": 49 + }, + "gc": { + "collectors": { + "young": { + "collection_count": 50, + "collection_time_in_millis": 51 + }, + "old": { + "collection_count": 52, + "collection_time_in_millis": 53 + } + } + } + }, + "thread_pool": { + "generic": { + "threads": 54, + "queue": 55, + "rejected": 56 + }, + "get": { + "threads": 57, + "queue": 58, + "rejected": 59 + }, + "management": { + "threads": 60, + "queue": 61, + "rejected": 62 + }, + "search": { + "threads": 63, + "queue": 64, + "rejected": 65 + }, + "watcher": { + "threads": 66, + "queue": 67, + "rejected": 68 + }, + "write": { + "threads": 69, + "queue": 70, + "rejected": 71 + } + }, + "fs": { + "total": { + "total_in_bytes": 33, + "free_in_bytes": 34, + "available_in_bytes": 35 + }, + "io_stats": { + "total": { + "operations": 10, + "read_operations": 5, + "write_operations": 5, + "read_kilobytes": 2, + "write_kilobytes": 2 + } + } + } + } + }"""); assertEquals(expected, xContent.utf8ToString()); } 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 7d9483d33b296..b3db571aab7ae 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 @@ -123,29 +123,30 @@ public void testToXContent() throws IOException { ); final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, randomBoolean()); - 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\"" - + " }" - + "}"; + 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.stripWhitespace(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 316d2f825d7d6..e8f8bafea44ca 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 @@ -92,31 +92,28 @@ public void testFilteredMonitoringDocToXContent() throws IOException { ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - 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" - + " }" - + " ]" - + " }" - + "}"; + 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.stripWhitespace(expected), xContent.utf8ToString()); } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java index ed639a3a9b7b0..e4956e8ef0343 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java @@ -193,17 +193,15 @@ public void testMonitoringNodeToXContent() throws IOException { node = new MonitoringDoc.Node("_uuid", "_host", "_addr", "_ip", "_name", 1504169190855L); final BytesReference xContent = XContentHelper.toXContent(node, XContentType.JSON, randomBoolean()); - assertEquals( - "{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}", - xContent.utf8ToString() - ); + assertEquals(XContentHelper.stripWhitespace(""" + { + "uuid": "_uuid", + "host": "_host", + "transport_address": "_addr", + "ip": "_ip", + "name": "_name", + "timestamp": "2017-08-31T08:46:30.855Z" + }"""), xContent.utf8ToString()); } public void testMonitoringNodeEqualsAndHashcode() { 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 81c5f57462b17..dd0dc68da0cb7 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 @@ -114,23 +114,24 @@ public void testToXContent() throws IOException { ); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - 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\"" - + " }" - + "}"; + 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.stripWhitespace(expected), xContent.utf8ToString()); } 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 559db2ce3c6ae..da13b4707e3f4 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 @@ -78,21 +78,43 @@ public void testOnSuccessWithInnerErrors() { final String[] expectedErrors = new String[] { randomAlphaOfLengthBetween(4, 10), randomAlphaOfLengthBetween(5, 9) }; final AtomicInteger counter = new AtomicInteger(0); final Response response = mock(Response.class); - final StringEntity entity = new StringEntity( - "{\"took\":4,\"errors\":true,\"items\":[" - + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"123\"}}," - + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"456\"," - + "\"error\":\"" - + expectedErrors[0] - + "\"}}," - + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"789\"}}," - + "{\"index\":{\"_index\":\".monitoring-data-2\",\"_type\":\"node\",\"_id\":\"012\"," - + "\"error\":\"" - + expectedErrors[1] - + "\"}}" - + "]}", - ContentType.APPLICATION_JSON - ); + final StringEntity entity = new StringEntity(""" + { + "took": 4, + "errors": true, + "items": [ + { + "index": { + "_index": ".monitoring-data-2", + "_type": "node", + "_id": "123" + } + }, + { + "index": { + "_index": ".monitoring-data-2", + "_type": "node", + "_id": "456", + "error": "%s" + } + }, + { + "index": { + "_index": ".monitoring-data-2", + "_type": "node", + "_id": "789" + } + }, + { + "index": { + "_index": ".monitoring-data-2", + "_type": "node", + "_id": "012", + "error": "%s" + } + } + ] + }""".formatted(expectedErrors[0], expectedErrors[1]), ContentType.APPLICATION_JSON); when(response.getEntity()).thenReturn(entity); 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 785a0835c91d0..b09d8fb984fb4 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 @@ -649,8 +649,10 @@ private void whenWatcherCannotBeUsed() { final Response response; if (randomBoolean()) { final HttpEntity entity = randomFrom( - new StringEntity("{\"features\":{\"watcher\":{\"enabled\":false,\"available\":true}}}", ContentType.APPLICATION_JSON), - new StringEntity("{\"features\":{\"watcher\":{\"enabled\":true,\"available\":false}}}", ContentType.APPLICATION_JSON), + new StringEntity(""" + {"features":{"watcher":{"enabled":false,"available":true}}}""", ContentType.APPLICATION_JSON), + new StringEntity(""" + {"features":{"watcher":{"enabled":true,"available":false}}}""", ContentType.APPLICATION_JSON), new StringEntity("{}", ContentType.APPLICATION_JSON) ); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java index b4f729f7f360f..827e662a39e19 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/TemplateHttpResourceTests.java @@ -28,10 +28,52 @@ public class TemplateHttpResourceTests extends AbstractPublishableHttpResourceTe private final String templateName = ".my_template"; // the internal representation has the type, the external representation should not - private final String templateValueInternal = "{\"order\":0,\"index_patterns\":[\".xyz-*\"],\"settings\":{},\"mappings\":{\"_doc\"" - + ":{\"properties\":{\"one\":{\"properties\":{\"two\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}}},\"aliases\":{}}"; - private final String templateValueExternal = "{\"order\":0,\"index_patterns\":[\".xyz-*\"],\"settings\":{},\"mappings\"" - + ":{\"properties\":{\"one\":{\"properties\":{\"two\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}}}},\"aliases\":{}}"; + private final String templateValueInternal = """ + { + "order": 0, + "index_patterns": [ ".xyz-*" ], + "settings": {}, + "mappings": { + "_doc": { + "properties": { + "one": { + "properties": { + "two": { + "properties": { + "name": { + "type": "keyword" + } + } + } + } + } + } + } + }, + "aliases": {} + }"""; + private final String templateValueExternal = """ + { + "order": 0, + "index_patterns": [ ".xyz-*" ], + "settings": {}, + "mappings": { + "properties": { + "one": { + "properties": { + "two": { + "properties": { + "name": { + "type": "keyword" + } + } + } + } + } + } + }, + "aliases": {} + }"""; private final Supplier template = () -> templateValueInternal; private final int minimumVersion = Math.min(MonitoringTemplateUtils.LAST_UPDATED_VERSION, Version.CURRENT.id); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java index 39fca3f3b2872..5337e71b65404 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/WatcherExistsHttpResourceTests.java @@ -66,12 +66,11 @@ public void testDoCheckExistsFor400() { public void testDoCheckExistsAsElectedMaster() { whenElectedMaster(); - final String[] noWatcher = { - "{}", - "{\"features\":{\"watcher\":{\"available\":true,\"enabled\":false}}}", - "{\"features\":{\"watcher\":{\"available\":false,\"enabled\":true}}}", - "{\"features\":{\"watcher\":{\"available\":true}}}", - "{\"features\":{\"watcher\":{\"enabled\":true}}}" }; + final String[] noWatcher = { "{}", """ + {"features":{"watcher":{"available":true,"enabled":false}}}""", """ + {"features":{"watcher":{"available":false,"enabled":true}}}""", """ + {"features":{"watcher":{"available":true}}}""", """ + {"features":{"watcher":{"enabled":true}}}""" }; final String endpoint = "/_xpack"; // success only implies that it responded; it also needs to be available and enabled @@ -110,8 +109,10 @@ public void testDoCheckErrorWithDataException() { whenElectedMaster(); final String[] errorWatcher = { - "{\"features\":{}}", // missing watcher object 'string' - "{\"watcher\":{\"enabled\":true,\"available\":true}}", // missing features outer object + """ + {"features":{}}""", // missing watcher object 'string' + """ + {"watcher":{"enabled":true,"available":true}}""", // missing features outer object "{{}" // extra { }; diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java index 580d2c56306c3..e4a234cbf5197 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/util/Graphviz.java @@ -25,15 +25,12 @@ public abstract class Graphviz { public static String dot(String name, Node root) { StringBuilder sb = new StringBuilder(); // name - sb.append( - "digraph G { " - + "rankdir=BT; \n" - + "label=\"" - + name - + "\"; \n" - + "node[shape=plaintext, color=azure1];\n " - + "edge[color=black,arrowsize=0.5];\n" - ); + sb.append(""" + digraph G { rankdir=BT; + label="%s"; + node[shape=plaintext, color=azure1]; + edge[color=black,arrowsize=0.5]; + """.formatted(name)); handleNode(sb, root, new AtomicInteger(0), INDENT, true); sb.append("}"); return sb.toString(); @@ -44,13 +41,13 @@ public static String dot(Map> clusters, boolean drawSu StringBuilder sb = new StringBuilder(); // name - sb.append( - "digraph G { " - + "rankdir=BT;\n " - + "node[shape=plaintext, color=azure1];\n " - + "edge[color=black];\n " - + "graph[compound=true];\n\n" - ); + sb.append(""" + digraph G { rankdir=BT; + node[shape=plaintext, color=azure1]; + edge[color=black]; + graph[compound=true]; + + """); int clusterNodeStart = 1; int clusterId = 0; @@ -130,9 +127,13 @@ private static void handleNode(StringBuilder output, Node n, AtomicInteger no StringBuilder nodeInfo = new StringBuilder(); nodeInfo.append("\n"); indent(nodeInfo, currentIndent + NODE_LABEL_INDENT); - nodeInfo.append("\n"); + nodeInfo.append(""" +
    + """); indent(nodeInfo, currentIndent + NODE_LABEL_INDENT); - nodeInfo.append("\n"); + nodeInfo.append(""" + + """.formatted(n.nodeName())); indent(nodeInfo, currentIndent + NODE_LABEL_INDENT); List props = n.nodeProperties(); @@ -177,8 +178,10 @@ private static void handleNode(StringBuilder output, Node n, AtomicInteger no // check any subtrees if (subTrees.isEmpty() == false) { // write nested trees - output.append("subgraph cluster_" + thisId + " {"); - output.append("style=filled; color=white; fillcolor=azure2; label=\"\";\n"); + output.append(""" + subgraph cluster_%s{ + style=filled; color=white; fillcolor=azure2; label=""; + """.formatted(thisId)); } // write node info diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java index c107e5a256bde..4c65aebce12a6 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/action/QlStatusResponseTests.java @@ -8,6 +8,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.Writeable; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.AbstractWireSerializingTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -58,35 +59,25 @@ protected QlStatusResponse mutateInstance(QlStatusResponse instance) { public void testToXContent() throws IOException { QlStatusResponse response = createTestInstance(); try (XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent())) { - String expectedJson = "{\n" - + " \"id\" : \"" - + response.getId() - + "\",\n" - + " \"is_running\" : " - + response.isRunning() - + ",\n" - + " \"is_partial\" : " - + response.isPartial() - + ",\n"; - - if (response.getStartTime() != null) { - expectedJson = expectedJson + " \"start_time_in_millis\" : " + response.getStartTime() + ",\n"; - } - expectedJson = expectedJson + " \"expiration_time_in_millis\" : " + response.getExpirationTime(); - - if (response.getCompletionStatus() == null) { - expectedJson = expectedJson + "\n" + "}"; - } else { - expectedJson = expectedJson - + ",\n" - + " \"completion_status\" : " - + response.getCompletionStatus().getStatus() - + "\n" - + "}"; - } - builder.prettyPrint(); + String expectedJson = """ + { + "id" : "%s", + "is_running" : %s, + "is_partial" : %s, + %s + "expiration_time_in_millis" : %s + %s + } + """.formatted( + response.getId(), + response.isRunning(), + response.isPartial(), + response.getStartTime() != null ? "\"start_time_in_millis\" : " + response.getStartTime() + "," : "", + response.getExpirationTime(), + response.getCompletionStatus() != null ? ", \"completion_status\" : " + response.getCompletionStatus().getStatus() : "" + ); response.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertEquals(expectedJson, Strings.toString(builder)); + assertEquals(XContentHelper.stripWhitespace(expectedJson), Strings.toString(builder)); } } } diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java index f124a8894819c..3432c5ff75132 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/tree/NodeTests.java @@ -27,7 +27,10 @@ public void testToString() { new ChildrenAreAProperty(randomSource(), singletonList(empty), "single").toString() ); assertEquals( - "ChildrenAreAProperty[many]\n" + "|_ChildrenAreAProperty[thing]\n" + "\\_ChildrenAreAProperty[thing]", + """ + ChildrenAreAProperty[many] + |_ChildrenAreAProperty[thing] + \\_ChildrenAreAProperty[thing]""", new ChildrenAreAProperty(randomSource(), Arrays.asList(empty, empty), "many").toString() ); } diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java index 84583d12eb4c8..19ac90d0871fc 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.MockBigArrays; import org.elasticsearch.common.util.MockPageCacheRecycler; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.core.CheckedConsumer; import org.elasticsearch.core.TimeValue; import org.elasticsearch.index.IndexNotFoundException; @@ -694,15 +695,36 @@ public void testDateHistoWithGap() throws IOException { ); InternalAggregation reduced = ((InternalDateHistogram) unrolled).reduce(Collections.singletonList(unrolled), context); - assertThat( - reduced.toString(), - equalTo( - "{\"histo\":{\"buckets\":[{\"key_as_string\":\"1970-01-01T00:00:00.100Z\",\"key\":100," - + "\"doc_count\":1},{\"key_as_string\":\"1970-01-01T00:00:00.200Z\",\"key\":200,\"doc_count\":1}," - + "{\"key_as_string\":\"1970-01-01T00:00:00.300Z\",\"key\":300,\"doc_count\":0,\"histo._count\":{\"value\":0.0}}," - + "{\"key_as_string\":\"1970-01-01T00:00:00.400Z\",\"key\":400,\"doc_count\":1}]}}" - ) - ); + assertThat(reduced.toString(), equalTo(XContentHelper.stripWhitespace(""" + { + "histo": { + "buckets": [ + { + "key_as_string": "1970-01-01T00:00:00.100Z", + "key": 100, + "doc_count": 1 + }, + { + "key_as_string": "1970-01-01T00:00:00.200Z", + "key": 200, + "doc_count": 1 + }, + { + "key_as_string": "1970-01-01T00:00:00.300Z", + "key": 300, + "doc_count": 0, + "histo._count": { + "value": 0.0 + } + }, + { + "key_as_string": "1970-01-01T00:00:00.400Z", + "key": 400, + "doc_count": 1 + } + ] + } + }"""))); } public void testNonMatchingPartition() throws IOException { @@ -799,13 +821,23 @@ public void testNonMatchingPartition() throws IOException { assertThat(((InternalDateHistogram) unrolled).getBuckets().get(0).getDocCount(), equalTo(2L)); // two "a" at 100 assertThat(((InternalDateHistogram) unrolled).getBuckets().get(1).getDocCount(), equalTo(1L)); // one "a" at 200 assertThat(((InternalDateHistogram) unrolled).getBuckets().get(0).getKeyAsString(), equalTo("1970-01-01T00:00:00.100Z")); - assertThat( - unrolled.toString(), - equalTo( - "{\"histo\":{\"buckets\":[{\"key_as_string\":\"1970-01-01T00:00:00.100Z\"," - + "\"key\":100,\"doc_count\":2},{\"key_as_string\":\"1970-01-01T00:00:00.200Z\",\"key\":200,\"doc_count\":1}]}}" - ) - ); + assertThat(unrolled.toString(), equalTo(XContentHelper.stripWhitespace(""" + { + "histo": { + "buckets": [ + { + "key_as_string": "1970-01-01T00:00:00.100Z", + "key": 100, + "doc_count": 2 + }, + { + "key_as_string": "1970-01-01T00:00:00.200Z", + "key": 200, + "doc_count": 1 + } + ] + } + }"""))); assertThat(unrolled.toString(), not(equalTo(results.get(1).toString()))); } @@ -1252,10 +1284,8 @@ public void testOverlappingBuckets() throws IOException { assertThat(((InternalDateHistogram) unrolled).getBuckets().size(), equalTo(1)); assertThat(((InternalDateHistogram) unrolled).getBuckets().get(0).getDocCount(), equalTo(1L)); assertThat(((InternalDateHistogram) unrolled).getBuckets().get(0).getKeyAsString(), equalTo("1970-01-01T00:00:00.400Z")); - assertThat( - unrolled.toString(), - equalTo("{\"histo\":{\"buckets\":[{\"key_as_string\":\"1970-01-01T00:00:00.400Z\"," + "\"key\":400,\"doc_count\":1}]}}") - ); + assertThat(unrolled.toString(), equalTo(""" + {"histo":{"buckets":[{"key_as_string":"1970-01-01T00:00:00.400Z","key":400,"doc_count":1}]}}""")); assertThat(unrolled.toString(), not(equalTo(responses.get(1).toString()))); } diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java index 4252bfc6b213e..afabad1dc157c 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/v2/RollupActionSingleNodeTests.java @@ -564,11 +564,9 @@ public interface SourceSupplier { private String createDataStream() throws Exception { String dataStreamName = randomAlphaOfLength(10).toLowerCase(Locale.getDefault()); - Template idxTemplate = new Template( - null, - new CompressedXContent("{\"properties\":{\"" + timestampFieldName + "\":{\"type\":\"date\"},\"data\":{\"type\":\"keyword\"}}}"), - null - ); + Template idxTemplate = new Template(null, new CompressedXContent(""" + {"properties":{"%s":{"type":"date"},"data":{"type":"keyword"}}} + """.formatted(timestampFieldName)), null); ComposableIndexTemplate template = new ComposableIndexTemplate( List.of(dataStreamName + "*"), idxTemplate, diff --git a/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java b/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java index 8e0ea79fa4151..6682dcc1094c3 100644 --- a/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java +++ b/x-pack/plugin/search-business-rules/src/test/java/org/elasticsearch/xpack/searchbusinessrules/PinnedQueryBuilderTests.java @@ -150,20 +150,21 @@ public void testEmptyPinnedQuery() throws Exception { } public void testIdsFromJson() throws IOException { - String query = "{" - + "\"pinned\" : {" - + " \"organic\" : {" - + " \"term\" : {" - + " \"tag\" : {" - + " \"value\" : \"tech\"," - + " \"boost\" : 1.0" - + " }" - + " }" - + " }, " - + " \"ids\" : [ \"1\",\"2\" ]," - + " \"boost\":1.0 " - + "}" - + "}"; + String query = """ + { + "pinned": { + "organic": { + "term": { + "tag": { + "value": "tech", + "boost": 1.0 + } + } + }, + "ids": [ "1", "2" ], + "boost": 1.0 + } + }"""; PinnedQueryBuilder queryBuilder = (PinnedQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); @@ -173,20 +174,21 @@ public void testIdsFromJson() throws IOException { } public void testDocsFromJson() throws IOException { - String query = "{" - + "\"pinned\" : {" - + " \"organic\" : {" - + " \"term\" : {" - + " \"tag\" : {" - + " \"value\" : \"tech\"," - + " \"boost\" : 1.0" - + " }" - + " }" - + " }, " - + " \"docs\" : [{ \"_index\": \"test\", \"_id\": \"1\" }, { \"_index\": \"test\", \"_id\": \"2\" }]," - + " \"boost\":1.0 " - + "}" - + "}"; + String query = """ + { + "pinned": { + "organic": { + "term": { + "tag": { + "value": "tech", + "boost": 1.0 + } + } + }, + "docs": [ { "_index": "test", "_id": "1" }, { "_index": "test", "_id": "2" } ], + "boost": 1.0 + } + }"""; PinnedQueryBuilder queryBuilder = (PinnedQueryBuilder) parseQuery(query); checkGeneratedJson(query, queryBuilder); diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/RetrySearchIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/RetrySearchIntegTests.java index f3e54ca204410..a690def9d8a32 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/RetrySearchIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/RetrySearchIntegTests.java @@ -45,7 +45,8 @@ public void testSearcherId() throws Exception { .indices() .prepareCreate(indexName) .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards).build()) - .setMapping("{\"properties\":{\"created_date\":{\"type\": \"date\", \"format\": \"yyyy-MM-dd\"}}}") + .setMapping(""" + {"properties":{"created_date":{"type": "date", "format": "yyyy-MM-dd"}}}""") ); final List indexRequestBuilders = new ArrayList<>(); final int docCount = between(0, 100); @@ -113,7 +114,8 @@ public void testRetryPointInTime() throws Exception { .indices() .prepareCreate(indexName) .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)).build()) - .setMapping("{\"properties\":{\"created_date\":{\"type\": \"date\", \"format\": \"yyyy-MM-dd\"}}}") + .setMapping(""" + {"properties":{"created_date":{"type": "date", "format": "yyyy-MM-dd"}}}""") ); final List indexRequestBuilders = new ArrayList<>(); final int docCount = between(0, 100); diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java index fea09bf252063..c6d6f71b25690 100644 --- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java +++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java @@ -163,12 +163,10 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { super.execute(terminal, options); } catch (OptionException e) { if (e.options().size() == 1 && e.options().contains("keep-ca-key")) { - throw new UserException( - ExitCodes.USAGE, - "Generating certificates without providing a CA is no longer supported.\n" - + "Please first generate a CA with the 'ca' sub-command and provide the ca file \n" - + "with either --ca or --ca-cert/--ca-key to generate certificates." - ); + throw new UserException(ExitCodes.USAGE, """ + Generating certificates without providing a CA is no longer supported. + Please first generate a CA with the 'ca' sub-command and provide the ca file\s + with either --ca or --ca-cert/--ca-key to generate certificates."""); } else { throw e; } @@ -178,25 +176,27 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception { static final String INTRO_TEXT = "This tool assists you in the generation of X.509 certificates and certificate\n" + "signing requests for use with SSL/TLS in the Elastic stack."; - static final String INSTANCE_EXPLANATION = " * An instance is any piece of the Elastic Stack that requires an SSL certificate.\n" - + " Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats\n" - + " may all require a certificate and private key.\n" - + " * The minimum required value for each instance is a name. This can simply be the\n" - + " hostname, which will be used as the Common Name of the certificate. A full\n" - + " distinguished name may also be used.\n" - + " * A filename value may be required for each instance. This is necessary when the\n" - + " name would result in an invalid file or directory name. The name provided here\n" - + " is used as the directory name (within the zip) and the prefix for the key and\n" - + " certificate files. The filename is required if you are prompted and the name\n" - + " is not displayed in the prompt.\n" - + " * IP addresses and DNS names are optional. Multiple values can be specified as a\n" - + " comma separated string. If no IP addresses or DNS names are provided, you may\n" - + " disable hostname verification in your SSL configuration."; - - static final String CA_EXPLANATION = " * All certificates generated by this tool will be signed by a certificate authority (CA)\n" - + " unless the --self-signed command line option is specified.\n" - + " The tool can automatically generate a new CA for you, or you can provide your own with\n" - + " the --ca or --ca-cert command line options."; + static final String INSTANCE_EXPLANATION = """ + * An instance is any piece of the Elastic Stack that requires an SSL certificate. + Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats + may all require a certificate and private key. + * The minimum required value for each instance is a name. This can simply be the + hostname, which will be used as the Common Name of the certificate. A full + distinguished name may also be used. + * A filename value may be required for each instance. This is necessary when the + name would result in an invalid file or directory name. The name provided here + is used as the directory name (within the zip) and the prefix for the key and + certificate files. The filename is required if you are prompted and the name + is not displayed in the prompt. + * IP addresses and DNS names are optional. Multiple values can be specified as a + comma separated string. If no IP addresses or DNS names are provided, you may + disable hostname verification in your SSL configuration.""".indent(4); + + static final String CA_EXPLANATION = """ + * All certificates generated by this tool will be signed by a certificate authority (CA) + unless the --self-signed command line option is specified. + The tool can automatically generate a new CA for you, or you can provide your own with + the --ca or --ca-cert command line options.""".indent(4); abstract static class CertificateCommand extends EnvironmentAwareCommand { // Common option for multiple commands. diff --git a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java index 0bc91a8ee9344..1f8a821cf302a 100644 --- a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java +++ b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolTests.java @@ -48,7 +48,6 @@ import java.io.Reader; import java.net.InetAddress; import java.net.URI; -import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -541,26 +540,25 @@ private String getValidRandomInstanceName() { * Writes the description of instances to a given {@link Path} */ private Path writeInstancesTo(Path path) throws IOException { - Iterable instances = Arrays.asList( - "instances:", - " - name: \"node1\"", - " ip:", - " - \"127.0.0.1\"", - " dns: \"localhost\"", - " - name: \"node2\"", - " filename: \"node2\"", - " ip: \"::1\"", - " cn:", - " - \"node2.elasticsearch\"", - " - name: \"node3\"", - " filename: \"node3\"", - " - name: \"CN=different value\"", - " filename: \"different file\"", - " dns:", - " - \"node4.mydomain.com\"" - ); - - return Files.write(path, instances, StandardCharsets.UTF_8); + String instances = """ + instances: + - name: "node1" + ip: + - "127.0.0.1" + dns: "localhost" + - name: "node2" + filename: "node2" + ip: "::1" + cn: + - "node2.elasticsearch" + - name: "node3" + filename: "node3" + - name: "CN=different value" + filename: "different file" + dns: + - "node4.mydomain.com" + """; + return Files.writeString(path, instances); } @SuppressForbidden(reason = "resolve paths against CWD for a CLI tool") diff --git a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java index 664d682595da4..a44844a51876e 100644 --- a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java +++ b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/CertificateToolTests.java @@ -58,7 +58,6 @@ import java.io.Reader; import java.net.InetAddress; import java.net.URI; -import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -987,34 +986,37 @@ private String getValidRandomInstanceName() { * Writes the description of instances to a given {@link Path} */ private Path writeInstancesTo(Path path) throws IOException { - Iterable instances = Arrays.asList( - "instances:", - " - name: \"node1\"", - " ip:", - " - \"127.0.0.1\"", - " dns: \"localhost\"", - " - name: \"node2\"", - " filename: \"node2\"", - " ip: \"::1\"", - " cn:", - " - \"node2.elasticsearch\"", - " - name: \"node3\"", - " filename: \"node3\"", - " - name: \"CN=different value\"", - " filename: \"different file\"", - " dns:", - " - \"node4.mydomain.com\"" - ); - - return Files.write(path, instances, StandardCharsets.UTF_8); + String instances = """ + instances: + - name: "node1" + ip: + - "127.0.0.1" + dns: "localhost" + - name: "node2" + filename: "node2" + ip: "::1" + cn: + - "node2.elasticsearch" + - name: "node3" + filename: "node3" + - name: "CN=different value" + filename: "different file" + dns: + - "node4.mydomain.com" + """; + return Files.writeString(path, instances); } /** * Writes the description of instances to a given {@link Path} */ private Path writeInvalidInstanceInformation(Path path) throws IOException { - Iterable instances = Arrays.asList("instances:", " - name: \"THIS=not a,valid DN\"", " ip: \"127.0.0.1\""); - return Files.write(path, instances, StandardCharsets.UTF_8); + String instances = """ + instances: + - name: "THIS=not a,valid DN" + ip: "127.0.0.1" + """; + return Files.writeString(path, instances); } @SuppressForbidden(reason = "resolve paths against CWD for a CLI tool") diff --git a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java index 5e127e0c68a71..08d88b602b34f 100644 --- a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java +++ b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/QueryApiKeyIT.java @@ -48,7 +48,8 @@ public void testQuery() throws IOException { createUser("someone"); // Admin with manage_api_key can search for all keys - assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }", apiKeys -> { + assertQuery(API_KEY_ADMIN_AUTH_HEADER, """ + { "query": { "wildcard": {"name": "*alert*"} } }""", apiKeys -> { assertThat(apiKeys.size(), equalTo(2)); assertThat(apiKeys.get(0).get("name"), oneOf("my-org/alert-key-1", "my-alert-key-2")); assertThat(apiKeys.get(1).get("name"), oneOf("my-org/alert-key-1", "my-alert-key-2")); @@ -58,14 +59,15 @@ public void testQuery() throws IOException { // An empty request body means search for all keys assertQuery( API_KEY_ADMIN_AUTH_HEADER, - randomBoolean() ? "" : "{\"query\":{\"match_all\":{}}}", + randomBoolean() ? "" : """ + {"query":{"match_all":{}}}""", apiKeys -> assertThat(apiKeys.size(), equalTo(6)) ); assertQuery( API_KEY_ADMIN_AUTH_HEADER, - "{\"query\":{\"bool\":{\"must\":[" - + "{\"prefix\":{\"metadata.application\":\"fleet\"}},{\"term\":{\"metadata.environment.os\":\"Cat\"}}]}}}", + """ + {"query":{"bool":{"must":[{"prefix":{"metadata.application":"fleet"}},{"term":{"metadata.environment.os":"Cat"}}]}}}""", apiKeys -> { assertThat(apiKeys, hasSize(2)); assertThat( @@ -76,18 +78,21 @@ public void testQuery() throws IOException { } ); - assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{\"query\":{\"terms\":{\"metadata.tags\":[\"prod\",\"east\"]}}}", apiKeys -> { + assertQuery(API_KEY_ADMIN_AUTH_HEADER, """ + {"query":{"terms":{"metadata.tags":["prod","east"]}}}""", apiKeys -> { assertThat(apiKeys.size(), equalTo(5)); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); }); - assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{\"query\":{\"range\":{\"creation\":{\"lt\":\"now\"}}}}", apiKeys -> { + assertQuery(API_KEY_ADMIN_AUTH_HEADER, """ + {"query":{"range":{"creation":{"lt":"now"}}}}""", apiKeys -> { assertThat(apiKeys.size(), equalTo(6)); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); }); // Search for keys belong to an user - assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{ \"query\": { \"term\": {\"username\": \"api_key_user\"} } }", apiKeys -> { + assertQuery(API_KEY_ADMIN_AUTH_HEADER, """ + { "query": { "term": {"username": "api_key_user"} } }""", apiKeys -> { assertThat(apiKeys.size(), equalTo(2)); assertThat( apiKeys.stream().map(m -> m.get("name")).collect(Collectors.toSet()), @@ -97,7 +102,8 @@ public void testQuery() throws IOException { }); // Search for keys belong to users from a realm - assertQuery(API_KEY_ADMIN_AUTH_HEADER, "{ \"query\": { \"term\": {\"realm_name\": \"default_file\"} } }", apiKeys -> { + assertQuery(API_KEY_ADMIN_AUTH_HEADER, """ + { "query": { "term": {"realm_name": "default_file"} } }""", apiKeys -> { assertThat(apiKeys.size(), equalTo(6)); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); // search using explicit IDs @@ -106,9 +112,10 @@ public void testQuery() throws IOException { var subset = randomSubsetOf(randomIntBetween(1, 5), apiKeys); assertQuery( API_KEY_ADMIN_AUTH_HEADER, - "{ \"query\": { \"ids\": { \"values\": [" - + subset.stream().map(m -> "\"" + m.get("id") + "\"").collect(Collectors.joining(",")) - + "] } } }", + """ + { "query": { "ids": { "values": [%s] } } }""".formatted( + subset.stream().map(m -> "\"" + m.get("id") + "\"").collect(Collectors.joining(",")) + ), keys -> { assertThat(keys, hasSize(subset.size())); keys.forEach(k -> assertThat(k, not(hasKey("_sort")))); @@ -120,22 +127,17 @@ public void testQuery() throws IOException { }); // Search for fields outside of the allowlist fails - assertQueryError(API_KEY_ADMIN_AUTH_HEADER, 400, "{ \"query\": { \"prefix\": {\"api_key_hash\": \"{PBKDF2}10000$\"} } }"); + assertQueryError(API_KEY_ADMIN_AUTH_HEADER, 400, """ + { "query": { "prefix": {"api_key_hash": "{PBKDF2}10000$"} } }"""); // Search for fields that are not allowed in Query DSL but used internally by the service itself final String fieldName = randomFrom("doc_type", "api_key_invalidated"); - assertQueryError( - API_KEY_ADMIN_AUTH_HEADER, - 400, - "{ \"query\": { \"term\": {\"" + fieldName + "\": \"" + randomAlphaOfLengthBetween(3, 8) + "\"} } }" - ); + assertQueryError(API_KEY_ADMIN_AUTH_HEADER, 400, """ + { "query": { "term": {"%s": "%s"} } }""".formatted(fieldName, randomAlphaOfLengthBetween(3, 8))); // Search for api keys won't return other entities - assertQuery( - API_KEY_ADMIN_AUTH_HEADER, - "{ \"query\": { \"term\": {\"name\": \"someone\"} } }", - apiKeys -> { assertThat(apiKeys, empty()); } - ); + assertQuery(API_KEY_ADMIN_AUTH_HEADER, """ + { "query": { "term": {"name": "someone"} } }""", apiKeys -> { assertThat(apiKeys, empty()); }); // User with manage_own_api_key will only see its own keys assertQuery(API_KEY_USER_AUTH_HEADER, randomBoolean() ? "" : "{\"query\":{\"match_all\":{}}}", apiKeys -> { @@ -147,28 +149,29 @@ public void testQuery() throws IOException { apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); }); - assertQuery(API_KEY_USER_AUTH_HEADER, "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }", apiKeys -> { + assertQuery(API_KEY_USER_AUTH_HEADER, """ + { "query": { "wildcard": {"name": "*alert*"} } }""", apiKeys -> { assertThat(apiKeys.size(), equalTo(1)); assertThat(apiKeys.get(0).get("name"), equalTo("my-alert-key-2")); apiKeys.forEach(k -> assertThat(k, not(hasKey("_sort")))); }); // User without manage_api_key or manage_own_api_key gets 403 trying to search API keys - assertQueryError(TEST_USER_AUTH_HEADER, 403, "{ \"query\": { \"wildcard\": {\"name\": \"*alert*\"} } }"); + assertQueryError(TEST_USER_AUTH_HEADER, 403, """ + { "query": { "wildcard": {"name": "*alert*"} } }"""); // Invalidated API keys are returned by default, but can be filtered out final String authHeader = randomFrom(API_KEY_ADMIN_AUTH_HEADER, API_KEY_USER_AUTH_HEADER); final String invalidatedApiKeyId1 = createAndInvalidateApiKey("temporary-key-1", authHeader); - final String queryString = randomFrom( - "{ \"query\": { \"term\": {\"name\": \"temporary-key-1\"} } }", - "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"name\":{\"value\":\"temporary-key-1\"}}}," - + "{\"term\":{\"invalidated\":{\"value\":\"" - + randomBoolean() - + "\"}}}]}}}" - ); + final String queryString = randomFrom(""" + {"query": { "term": {"name": "temporary-key-1"} } }""", """ + {"query":{"bool":{"must":[{"term":{"name":{"value":"temporary-key-1"}}},\ + {"term":{"invalidated":{"value":"%s"}}}]}}} + """.formatted(randomBoolean())); assertQuery(authHeader, queryString, apiKeys -> { - if (queryString.contains("\"invalidated\":{\"value\":\"false\"")) { + if (queryString.contains(""" + "invalidated":{"value":"false\"""")) { assertThat(apiKeys, empty()); } else { assertThat(apiKeys.size(), equalTo(1)); @@ -257,7 +260,9 @@ public void testPagination() throws IOException, InterruptedException { } else { searchAfter.append(sortValues.get(0)); } - request2.setJsonEntity("{\"size\":" + size + ",\"sort\":[\"" + sortField + "\"],\"search_after\":[" + searchAfter + "]}"); + request2.setJsonEntity(""" + {"size":%s,"sort":["%s"],"search_after":[%s]} + """.formatted(size, sortField, searchAfter)); actualSize = collectApiKeys(apiKeyInfos, request2, total, size); if (actualSize == 0 && apiKeyInfos.size() < remaining) { fail("fail to retrieve all API keys, expect [" + remaining + "] keys, got [" + apiKeyInfos + "]"); @@ -308,7 +313,8 @@ public void testSort() throws IOException { apiKeyIds.add(createApiKey("k1", Map.of("letter", "b", "symbol", "2"), authHeader).v1()); apiKeyIds.add(createApiKey("k0", Map.of("letter", "c", "symbol", "1"), authHeader).v1()); - assertQuery(authHeader, "{\"sort\":[{\"creation\":{\"order\":\"desc\"}}]}", apiKeys -> { + assertQuery(authHeader, """ + {"sort":[{"creation":{"order":"desc"}}]}""", apiKeys -> { assertThat(apiKeys.size(), equalTo(3)); for (int i = 2, j = 0; i >= 0; i--, j++) { assertThat(apiKeys.get(i).get("id"), equalTo(apiKeyIds.get(j))); @@ -316,7 +322,8 @@ public void testSort() throws IOException { } }); - assertQuery(authHeader, "{\"sort\":[{\"name\":{\"order\":\"asc\"}}]}", apiKeys -> { + assertQuery(authHeader, """ + {"sort":[{"name":{"order":"asc"}}]}""", apiKeys -> { assertThat(apiKeys.size(), equalTo(3)); for (int i = 2, j = 0; i >= 0; i--, j++) { assertThat(apiKeys.get(i).get("id"), equalTo(apiKeyIds.get(j))); @@ -324,14 +331,16 @@ public void testSort() throws IOException { } }); - assertQuery(authHeader, "{\"sort\":[\"metadata.letter\"]}", apiKeys -> { + assertQuery(authHeader, """ + {"sort":["metadata.letter"]}""", apiKeys -> { assertThat(apiKeys.size(), equalTo(3)); for (int i = 0; i < 3; i++) { assertThat(apiKeys.get(i).get("id"), equalTo(apiKeyIds.get(i))); } }); - assertQuery(authHeader, "{\"sort\":[\"metadata.symbol\",\"metadata.letter\"]}", apiKeys -> { + assertQuery(authHeader, """ + {"sort":["metadata.symbol","metadata.letter"]}""", apiKeys -> { assertThat(apiKeys.size(), equalTo(3)); assertThat(apiKeys.get(0).get("id"), equalTo(apiKeyIds.get(2))); assertThat(apiKeys.get(1).get("id"), equalTo(apiKeyIds.get(0))); @@ -481,19 +490,12 @@ private Tuple createApiKey( final String metadataString = XContentTestUtils.convertToXContent(metadata == null ? Map.of() : metadata, XContentType.JSON) .utf8ToString(); if (randomBoolean()) { - request.setJsonEntity( - "{\"name\":\"" + name + "\", \"role_descriptors\":" + roleDescriptorsString + ", \"metadata\":" + metadataString + "}" - ); + request.setJsonEntity(""" + {"name":"%s", "role_descriptors":%s, "metadata":%s}""".formatted(name, roleDescriptorsString, metadataString)); } else { - request.setJsonEntity( - "{\"name\":\"" - + name - + "\", \"expiration\": \"10d\", \"role_descriptors\":" - + roleDescriptorsString - + ", \"metadata\":" - + metadataString - + "}" - ); + request.setJsonEntity(""" + {"name":"%s", "expiration": "10d", "role_descriptors":%s,\ + "metadata":%s}""".formatted(name, roleDescriptorsString, metadataString)); } request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader)); final Response response = client().performRequest(request); @@ -506,14 +508,16 @@ private String createAndInvalidateApiKey(String name, String authHeader) throws final Tuple tuple = createApiKey(name, null, authHeader); final Request request = new Request("DELETE", "/_security/api_key"); request.setOptions(request.getOptions().toBuilder().addHeader(HttpHeaders.AUTHORIZATION, authHeader)); - request.setJsonEntity("{\"ids\": [\"" + tuple.v1() + "\"],\"owner\":true}"); + request.setJsonEntity(""" + {"ids": ["%s"],"owner":true}""".formatted(tuple.v1())); assertOK(client().performRequest(request)); return tuple.v1(); } private void createUser(String name) throws IOException { final Request request = new Request("POST", "/_security/user/" + name); - request.setJsonEntity("{\"password\":\"super-strong-password\",\"roles\":[]}"); + request.setJsonEntity(""" + {"password":"super-strong-password","roles":[]}"""); assertOK(adminClient().performRequest(request)); } } diff --git a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java index 85a69c85002d3..0a2c3aa071de5 100644 --- a/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java +++ b/x-pack/plugin/security/qa/security-basic/src/javaRestTest/java/org/elasticsearch/xpack/security/SecurityWithBasicLicenseIT.java @@ -135,12 +135,16 @@ private void checkAuthentication() throws IOException { private void checkHasPrivileges() throws IOException { final Request request = new Request("GET", "/_security/user/_has_privileges"); - request.setJsonEntity( - "{" - + "\"cluster\": [ \"manage\", \"monitor\" ]," - + "\"index\": [{ \"names\": [ \"index_allowed\", \"index_denied\" ], \"privileges\": [ \"read\", \"all\" ] }]" - + "}" - ); + request.setJsonEntity(""" + { + "cluster": [ "manage", "monitor" ], + "index": [ + { + "names": [ "index_allowed", "index_denied" ], + "privileges": [ "read", "all" ] + } + ] + }"""); Response response = client().performRequest(request); final Map auth = entityAsMap(response); assertThat(ObjectPath.evaluate(auth, "username"), equalTo("security_test_user")); @@ -170,18 +174,21 @@ private void checkIndexWrite() throws IOException { private Request buildGetTokenRequest() { final Request getToken = new Request("POST", "/_security/oauth2/token"); - getToken.setJsonEntity( - "{\"grant_type\" : \"password\",\n" - + " \"username\" : \"security_test_user\",\n" - + " \"password\" : \"security-test-password\"\n" - + "}" - ); + getToken.setJsonEntity(""" + {"grant_type" : "password", + "username" : "security_test_user", + "password" : "security-test-password" + }"""); return getToken; } private Request buildGetApiKeyRequest() { final Request getApiKey = new Request("POST", "/_security/api_key"); - getApiKey.setJsonEntity("{\"name\" : \"my-api-key\",\n" + " \"expiration\" : \"2d\",\n" + " \"role_descriptors\" : {} \n" + "}"); + getApiKey.setJsonEntity(""" + {"name" : "my-api-key", + "expiration" : "2d", + "role_descriptors" : {}\s + }"""); return getApiKey; } @@ -262,26 +269,25 @@ private void assertAuthenticateWithServiceAccountToken(String bearerString) thro private void assertAddRoleWithDLS(boolean shouldSucceed) throws IOException { final Request addRole = new Request("POST", "/_security/role/dlsrole"); - addRole.setJsonEntity( - "{\n" - + " \"cluster\": [\"all\"],\n" - + " \"indices\": [\n" - + " {\n" - + " \"names\": [ \"index1\", \"index2\" ],\n" - + " \"privileges\": [\"all\"],\n" - + " \"query\": \"{\\\"match\\\": {\\\"title\\\": \\\"foo\\\"}}\" \n" - + " },\n" - + " {\n" - + " \"names\": [ \"index41\", \"index42\" ],\n" - + " \"privileges\": [\"read\"]\n" - + " }\n" - + " ],\n" - + " \"run_as\": [ \"other_user\" ],\n" - + " \"metadata\" : { // optional\n" - + " \"version\" : 1\n" - + " }\n" - + "}" - ); + addRole.setJsonEntity(""" + { + "cluster": ["all"], + "indices": [ + { + "names": [ "index1", "index2" ], + "privileges": ["all"], + "query": "{\\"match\\": {\\"title\\": \\"foo\\"}}"\s + }, + { + "names": [ "index41", "index42" ], + "privileges": ["read"] + } + ], + "run_as": [ "other_user" ], + "metadata" : { // optional + "version" : 1 + } + }"""); if (shouldSucceed) { Response addRoleResponse = adminClient().performRequest(addRole); assertThat(addRoleResponse.getStatusLine().getStatusCode(), equalTo(200)); @@ -294,28 +300,27 @@ private void assertAddRoleWithDLS(boolean shouldSucceed) throws IOException { private void assertAddRoleWithFLS(boolean shouldSucceed) throws IOException { final Request addRole = new Request("POST", "/_security/role/flsrole"); - addRole.setJsonEntity( - "{\n" - + " \"cluster\": [\"all\"],\n" - + " \"indices\": [\n" - + " {\n" - + " \"names\": [ \"index1\", \"index2\" ],\n" - + " \"privileges\": [\"all\"],\n" - + " \"field_security\" : { // optional\n" - + " \"grant\" : [ \"title\", \"body\" ]\n" - + " }\n" - + " },\n" - + " {\n" - + " \"names\": [ \"index41\", \"index42\" ],\n" - + " \"privileges\": [\"read\"]\n" - + " }\n" - + " ],\n" - + " \"run_as\": [ \"other_user\" ],\n" - + " \"metadata\" : { // optional\n" - + " \"version\" : 1\n" - + " }\n" - + "}" - ); + addRole.setJsonEntity(""" + { + "cluster": ["all"], + "indices": [ + { + "names": [ "index1", "index2" ], + "privileges": ["all"], + "field_security" : { // optional + "grant" : [ "title", "body" ] + } + }, + { + "names": [ "index41", "index42" ], + "privileges": ["read"] + } + ], + "run_as": [ "other_user" ], + "metadata" : { // optional + "version" : 1 + } + }"""); if (shouldSucceed) { Response addRoleResponse = adminClient().performRequest(addRole); assertThat(addRoleResponse.getStatusLine().getStatusCode(), equalTo(200)); @@ -341,29 +346,69 @@ private Tuple assertCreateApiKeyWithDlsFls() throws IOException final boolean keyRoleHasDlsFls = randomBoolean(); if (keyRoleHasDlsFls) { if (randomBoolean()) { - request.setJsonEntity( - "{\"name\":\"my-key\",\"role_descriptors\":" - + "{\"a\":{\"indices\":[" - + "{\"names\":[\"index41\"],\"privileges\":[\"read\"]," - + "\"query\":{\"term\":{\"tag\":{\"value\":\"prod\"}}}}," - + "{\"names\":[\"index1\",\"index2\",\"index42\"],\"privileges\":[\"read\"]}" - + "]}}}" - ); + request.setJsonEntity(""" + { + "name": "my-key", + "role_descriptors": { + "a": { + "indices": [ + { + "names": [ "index41" ], + "privileges": [ "read" ], + "query": { + "term": { + "tag": { + "value": "prod" + } + } + } + }, + { + "names": [ "index1", "index2", "index42" ], + "privileges": [ "read" ] + } + ] + } + } + }"""); } else { - request.setJsonEntity( - "{\"name\":\"my-key\",\"role_descriptors\":" - + "{\"a\":{\"indices\":[" - + "{\"names\":[\"index41\"],\"privileges\":[\"read\"]," - + "\"field_security\":{\"grant\":[\"tag\"]}}," - + "{\"names\":[\"index1\",\"index2\",\"index42\"],\"privileges\":[\"read\"]}" - + "]}}}" - ); + request.setJsonEntity(""" + { + "name": "my-key", + "role_descriptors": { + "a": { + "indices": [ + { + "names": [ "index41" ], + "privileges": [ "read" ], + "field_security": { + "grant": [ "tag" ] + } + }, + { + "names": [ "index1", "index2", "index42" ], + "privileges": [ "read" ] + } + ] + } + } + }"""); } } else { - request.setJsonEntity( - "{\"name\":\"my-key\",\"role_descriptors\":" - + "{\"a\":{\"indices\":[{\"names\":[\"index1\",\"index2\",\"index41\",\"index42\"],\"privileges\":[\"read\"]}]}}}" - ); + request.setJsonEntity(""" + { + "name": "my-key", + "role_descriptors": { + "a": { + "indices": [ + { + "names": [ "index1", "index2", "index41", "index42" ], + "privileges": [ "read" ] + } + ] + } + } + }"""); } request.setOptions( request.getOptions() diff --git a/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java b/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java index 5e11a01a04e25..9d8d7b0e147fd 100644 --- a/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java +++ b/x-pack/plugin/security/qa/security-disabled/src/javaRestTest/java/org/elasticsearch/xpack/security/SetSecurityUserProcessorWithSecurityDisabledIT.java @@ -26,16 +26,11 @@ public void testDefineAndUseProcessor() throws Exception { final String index = "index-" + getTestName(); { final Request putPipeline = new Request("PUT", "/_ingest/pipeline/" + pipeline); - putPipeline.setJsonEntity( - "{" - + " \"description\": \"Test pipeline (" - + getTestName() - + ")\"," - + " \"processors\":[{" - + " \"set_security_user\":{ \"field\": \"user\" }" - + " }]" - + "}" - ); + putPipeline.setJsonEntity(""" + { + "description": "Test pipeline (%s)", + "processors": [ { "set_security_user": { "field": "user" } } ] + }""".formatted(getTestName())); final Response response = client().performRequest(putPipeline); assertOK(response); } diff --git a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java index 6a34f9029910d..743700af81fd9 100644 --- a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java +++ b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/security/dlsfls/DlsRequestCacheIT.java @@ -57,18 +57,32 @@ public void testRequestCacheDisabledForDlsTemplateRoleWithPainless() throws IOEx final RestClient client = client(); final Request putScriptRequest = new Request("PUT", "_scripts/range-now"); - putScriptRequest.setJsonEntity( - "{\"script\":{\"lang\":\"painless\"," - + "\"source\":\"'{\\\"range\\\":{\\\"date\\\": {\\\"lte\\\": \\\"' + new Date().getTime() + '\\\"}}}' \"}}" - ); + putScriptRequest.setJsonEntity(""" + { + "script": { + "lang": "painless", + "source": "'{\\"range\\":{\\"date\\": {\\"lte\\": \\"' + new Date().getTime() + '\\"}}}' " + } + }"""); assertOK(adminClient.performRequest(putScriptRequest)); // Create the index with a date field and 1 primary shard with no replica final Request putIndexRequest = new Request("PUT", DLS_TEMPLATE_PAINLESS_INDEX); - putIndexRequest.setJsonEntity( - "{\"mappings\":{\"properties\":{\"date\":{\"type\":\"date\",\"format\":\"epoch_millis\"}}}," - + "\"settings\":{\"number_of_shards\":1,\"number_of_replicas\":0}}" - ); + putIndexRequest.setJsonEntity(""" + { + "mappings": { + "properties": { + "date": { + "type": "date", + "format": "epoch_millis" + } + } + }, + "settings": { + "number_of_shards": 1, + "number_of_replicas": 0 + } + }"""); assertOK(adminClient.performRequest(putIndexRequest)); // A doc in the past 1 min diff --git a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java index 62bc97366f7b3..4b08f046d69c7 100644 --- a/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java +++ b/x-pack/plugin/security/qa/security-trial/src/javaRestTest/java/org/elasticsearch/xpack/test/rest/CatIndicesWithSecurityIT.java @@ -60,10 +60,18 @@ public void testHiddenIndexWithHiddenAlias() throws IOException { // Create the index and alias { final Request createRequest = new Request("PUT", ".index_hidden"); - createRequest.setJsonEntity( - "{\"settings\": {\"index.hidden\": true, \"number_of_replicas\": 0}, " - + "\"aliases\": {\"index_allowed\": {\"is_hidden\": true}}}" - ); + createRequest.setJsonEntity(""" + { + "settings": { + "index.hidden": true, + "number_of_replicas": 0 + }, + "aliases": { + "index_allowed": { + "is_hidden": true + } + } + }"""); final Response createResponse = adminClient().performRequest(createRequest); assertOK(createResponse); ensureGreen("index_allowed"); @@ -94,9 +102,17 @@ public void testVisibleIndexWithHiddenAlias() throws IOException { // Create the index and alias { final Request createRequest = new Request("PUT", "visible_index"); - createRequest.setJsonEntity( - "{\"settings\": {\"number_of_replicas\": 0}, " + "\"aliases\": {\"index_allowed\": {\"is_hidden\": true}}}" - ); + createRequest.setJsonEntity(""" + { + "settings": { + "number_of_replicas": 0 + }, + "aliases": { + "index_allowed": { + "is_hidden": true + } + } + }"""); final Response createResponse = adminClient().performRequest(createRequest); assertOK(createResponse); ensureGreen("index_allowed"); diff --git a/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java b/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java index 99c9389014f44..b7ec3bba3d9ac 100644 --- a/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java +++ b/x-pack/plugin/security/qa/service-account/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/service/ServiceAccountIT.java @@ -52,86 +52,85 @@ public class ServiceAccountIT extends ESRestTestCase { private static final String INVALID_SERVICE_TOKEN = "AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuMTozYUpDTGFRV1JOMnNYbE9kdHhBMFNR"; private static Path caPath; - private static final String AUTHENTICATE_RESPONSE = "" - + "{\n" - + " \"username\": \"elastic/fleet-server\",\n" - + " \"roles\": [],\n" - + " \"full_name\": \"Service account - elastic/fleet-server\",\n" - + " \"email\": null,\n" - + " \"token\": {\n" - + " \"name\": \"%s\",\n" - + " \"type\": \"_service_account_%s\"\n" - + " },\n" - + " \"metadata\": {\n" - + " \"_elastic_service_account\": true\n" - + " },\n" - + " \"enabled\": true,\n" - + " \"authentication_realm\": {\n" - + " \"name\": \"_service_account\",\n" - + " \"type\": \"_service_account\"\n" - + " },\n" - + " \"lookup_realm\": {\n" - + " \"name\": \"_service_account\",\n" - + " \"type\": \"_service_account\"\n" - + " },\n" - + " \"authentication_type\": \"token\"\n" - + "}\n"; - - private static final String ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR = "" - + "{\n" - + " \"cluster\": [\n" - + " \"monitor\",\n" - + " \"manage_own_api_key\"\n" - + " ],\n" - + " \"indices\": [\n" - + " {\n" - + " \"names\": [\n" - + " \"logs-*\",\n" - + " \"metrics-*\",\n" - + " \"traces-*\",\n" - + " \"synthetics-*\",\n" - + " \".logs-endpoint.diagnostic.collection-*\",\n" - + " \".logs-endpoint.action.responses-*\"\n" - + " ],\n" - + " \"privileges\": [\n" - + " \"write\",\n" - + " \"create_index\",\n" - + " \"auto_configure\"\n" - + " ],\n" - + " \"allow_restricted_indices\": false\n" - + " },\n" - + " {\n" - + " \"names\": [\n" - + " \".fleet-*\"\n" - + " ],\n" - + " \"privileges\": [\n" - + " \"read\",\n" - + " \"write\",\n" - + " \"monitor\",\n" - + " \"create_index\",\n" - + " \"auto_configure\"\n" - + " ],\n" - + " \"allow_restricted_indices\": true\n" - + " }\n" - + " ],\n" - + " \"applications\": [" - + " {\n" - + " \"application\" : \"kibana-*\",\n" - + " \"privileges\" : [\n" - + " \"reserved_fleet-setup\"\n" - + " ],\n" - + " \"resources\" : [\n" - + " \"*\"\n" - + " ]\n" - + " }" - + " ],\n" - + " \"run_as\": [],\n" - + " \"metadata\": {},\n" - + " \"transient_metadata\": {\n" - + " \"enabled\": true\n" - + " }\n" - + " }\n" - + " }"; + private static final String AUTHENTICATE_RESPONSE = """ + { + "username": "elastic/fleet-server", + "roles": [], + "full_name": "Service account - elastic/fleet-server", + "email": null, + "token": { + "name": "%s", + "type": "_service_account_%s" + }, + "metadata": { + "_elastic_service_account": true + }, + "enabled": true, + "authentication_realm": { + "name": "_service_account", + "type": "_service_account" + }, + "lookup_realm": { + "name": "_service_account", + "type": "_service_account" + }, + "authentication_type": "token" + } + """; + + private static final String ELASTIC_FLEET_SERVER_ROLE_DESCRIPTOR = """ + { + "cluster": [ + "monitor", + "manage_own_api_key" + ], + "indices": [ + { + "names": [ + "logs-*", + "metrics-*", + "traces-*", + "synthetics-*", + ".logs-endpoint.diagnostic.collection-*", + ".logs-endpoint.action.responses-*" + ], + "privileges": [ + "write", + "create_index", + "auto_configure" + ], + "allow_restricted_indices": false + }, + { + "names": [ + ".fleet-*" + ], + "privileges": [ + "read", + "write", + "monitor", + "create_index", + "auto_configure" + ], + "allow_restricted_indices": true + } + ], + "applications": [ { + "application" : "kibana-*", + "privileges" : [ + "reserved_fleet-setup" + ], + "resources" : [ + "*" + ] + } ], + "run_as": [], + "metadata": {}, + "transient_metadata": { + "enabled": true + } + } + }"""; @BeforeClass public static void init() throws URISyntaxException, FileNotFoundException { @@ -251,7 +250,9 @@ public void testAuthenticateShouldWorkWithOAuthBearerToken() throws IOException final String refreshToken = (String) oauthTokenResponseMap.get("refresh_token"); final Request refreshTokenRequest = new Request("POST", "_security/oauth2/token"); - refreshTokenRequest.setJsonEntity("{\"grant_type\":\"refresh_token\",\"refresh_token\":\"" + refreshToken + "\"}"); + refreshTokenRequest.setJsonEntity(""" + {"grant_type":"refresh_token","refresh_token":"%s"} + """.formatted(refreshToken)); final Response refreshTokenResponse = adminClient().performRequest(refreshTokenRequest); assertOK(refreshTokenResponse); } @@ -414,7 +415,8 @@ public void testManageOwnApiKey() throws IOException { if (randomBoolean()) { createApiKeyRequest1.setJsonEntity("{\"name\":\"key-1\"}"); } else { - createApiKeyRequest1.setJsonEntity("{\"name\":\"key-1\",\"role_descriptors\":{\"a\":{\"cluster\":[\"all\"]}}}"); + createApiKeyRequest1.setJsonEntity(""" + {"name":"key-1","role_descriptors":{"a":{"cluster":["all"]}}}"""); } createApiKeyRequest1.setOptions(requestOptions); final Response createApiKeyResponse1 = client().performRequest(createApiKeyRequest1); @@ -440,7 +442,8 @@ public void testManageOwnApiKey() throws IOException { assertThat(e.getMessage(), containsString("is unauthorized for API key")); final Request invalidateApiKeysRequest = new Request("DELETE", "_security/api_key"); - invalidateApiKeysRequest.setJsonEntity("{\"ids\":[\"" + apiKeyId1 + "\"],\"owner\":true}"); + invalidateApiKeysRequest.setJsonEntity(""" + {"ids":["%s"],"owner":true}""".formatted(apiKeyId1)); invalidateApiKeysRequest.setOptions(requestOptions); final Response invalidateApiKeysResponse = client().performRequest(invalidateApiKeysRequest); assertOK(invalidateApiKeysResponse); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java index 6bc08fd6603ea..14e9056569533 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/BulkUpdateTests.java @@ -117,9 +117,10 @@ public void testThatBulkUpdateDoesNotLoseFieldsHttp() throws IOException { Request bulkRequest = new Request("POST", "/_bulk"); bulkRequest.setOptions(options); - bulkRequest.setJsonEntity( - "{\"update\": {\"_index\": \"index1\", \"_id\": \"1\"}}\n" + "{\"doc\": {\"bulk updated\":\"bulk updated\"}}\n" - ); + bulkRequest.setJsonEntity(""" + {"update": {"_index": "index1", "_id": "1"}} + {"doc": {"bulk updated":"bulk updated"}} + """); getRestClient().performRequest(bulkRequest); String afterBulk = EntityUtils.toString(getRestClient().performRequest(getRequest).getEntity()); diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java index 87c9804bc48cc..566de1b45ce0c 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/ClusterPrivilegeIntegrationTests.java @@ -24,27 +24,31 @@ public class ClusterPrivilegeIntegrationTests extends AbstractPrivilegeTestCase { - private static final String ROLES = "role_a:\n" - + " cluster: [ all ]\n" - + "\n" - + "role_b:\n" - + " cluster: [ monitor ]\n" - + "\n" - + "role_c:\n" - + " indices:\n" - + " - names: 'someindex'\n" - + " privileges: [ all ]\n" - + "role_d:\n" - + " cluster: [ create_snapshot ]\n" - + "\n" - + "role_e:\n" - + " cluster: [ monitor_snapshot]\n"; - - private static final String USERS_ROLES = "role_a:user_a\n" - + "role_b:user_b\n" - + "role_c:user_c\n" - + "role_d:user_d\n" - + "role_e:user_e\n"; + private static final String ROLES = """ + role_a: + cluster: [ all ] + + role_b: + cluster: [ monitor ] + + role_c: + indices: + - names: 'someindex' + privileges: [ all ] + role_d: + cluster: [ create_snapshot ] + + role_e: + cluster: [ monitor_snapshot] + """; + + private static final String USERS_ROLES = """ + role_a:user_a + role_b:user_b + role_c:user_c + role_d:user_d + role_e:user_e + """; private static Path repositoryLocation; diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java index b56f0d6dffaf6..fe9d11e891ca9 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/CreateDocsIndexPrivilegeTests.java @@ -17,15 +17,18 @@ public class CreateDocsIndexPrivilegeTests extends AbstractPrivilegeTestCase { private static final String INDEX_NAME = "index-1"; private static final String CREATE_DOC_USER = "create_doc_user"; - private String jsonDoc = "{ \"name\" : \"elasticsearch\", \"body\": \"foo bar\" }"; - private static final String ROLES = "all_indices_role:\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ all ]\n" - + "create_doc_role:\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ create_doc ]\n"; + private String jsonDoc = """ + { "name" : "elasticsearch", "body": "foo bar" }"""; + private static final String ROLES = """ + all_indices_role: + indices: + - names: '*' + privileges: [ all ] + create_doc_role: + indices: + - names: '*' + privileges: [ create_doc ] + """; private static final String USERS_ROLES = "all_indices_role:admin\n" + "create_doc_role:" + CREATE_DOC_USER + "\n"; @@ -61,76 +64,66 @@ public void insertBaseDocumentsAsAdmin() throws Exception { } public void testCreateDocUserCanIndexNewDocumentsWithAutoGeneratedId() throws IOException { - assertAccessIsAllowed(CREATE_DOC_USER, "POST", "/" + INDEX_NAME + "/_doc", "{ \"foo\" : \"bar\" }"); + assertAccessIsAllowed(CREATE_DOC_USER, "POST", "/" + INDEX_NAME + "/_doc", """ + { "foo" : "bar" }"""); } public void testCreateDocUserCanIndexNewDocumentsWithExternalIdAndOpTypeIsCreate() throws IOException { - assertAccessIsAllowed( - CREATE_DOC_USER, - randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_doc/2?op_type=create", - "{ \"foo\" : " + "\"bar\" }" - ); + assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_doc/2?op_type=create", """ + { "foo" : "bar" }"""); } public void testCreateDocUserIsDeniedToIndexNewDocumentsWithExternalIdAndOpTypeIsIndex() throws IOException { - assertAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_doc/3", "{ \"foo\" : \"bar\" }"); + assertAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_doc/3", """ + { "foo" : "bar" }"""); } public void testCreateDocUserIsDeniedToIndexUpdatesToExistingDocument() throws IOException { - assertAccessIsDenied(CREATE_DOC_USER, "POST", "/" + INDEX_NAME + "/_update/1", "{ \"doc\" : { \"foo\" : \"baz\" } }"); - assertAccessIsDenied(CREATE_DOC_USER, "PUT", "/" + INDEX_NAME + "/_doc/1", "{ \"foo\" : \"baz\" }"); + assertAccessIsDenied(CREATE_DOC_USER, "POST", "/" + INDEX_NAME + "/_update/1", """ + { "doc" : { "foo" : "baz" } } + """); + assertAccessIsDenied(CREATE_DOC_USER, "PUT", "/" + INDEX_NAME + "/_doc/1", """ + { "foo" : "baz" } + """); } public void testCreateDocUserCanIndexNewDocumentsWithAutoGeneratedIdUsingBulkApi() throws IOException { - assertAccessIsAllowed( - CREATE_DOC_USER, - randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", - "{ \"index\" : { } }\n{ \"foo\" : \"bar\" }\n" - ); + assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_bulk", """ + { "index" : { } } + { "foo" : "bar" } + """); } public void testCreateDocUserCanIndexNewDocumentsWithAutoGeneratedIdAndOpTypeCreateUsingBulkApi() throws IOException { - assertAccessIsAllowed( - CREATE_DOC_USER, - randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", - "{ \"create\" : { } }\n{ \"foo\" : \"bar\" }\n" - ); + assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_bulk", """ + { "create" : { } } + { "foo" : "bar" } + """); } public void testCreateDocUserCanIndexNewDocumentsWithExternalIdAndOpTypeIsCreateUsingBulkApi() throws IOException { - assertAccessIsAllowed( - CREATE_DOC_USER, - randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", - "{ \"create\" : { \"_id\" : \"4\" } }\n{ \"foo\" : \"bar\" }\n" - ); + assertAccessIsAllowed(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_bulk", """ + { "create" : { "_id" : "4" } } + { "foo" : "bar" } + """); } public void testCreateDocUserIsDeniedToIndexNewDocumentsWithExternalIdAndOpTypeIsIndexUsingBulkApi() throws IOException { - assertBodyHasAccessIsDenied( - CREATE_DOC_USER, - randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", - "{ \"index\" : { \"_id\" : \"5\" } }\n{ \"foo\" : \"bar\" }\n" - ); + assertBodyHasAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_bulk", """ + { "index" : { "_id" : "5" } } + { "foo" : "bar" } + """); } public void testCreateDocUserIsDeniedToIndexUpdatesToExistingDocumentUsingBulkApi() throws IOException { - assertBodyHasAccessIsDenied( - CREATE_DOC_USER, - randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", - "{ \"index\" : { \"_id\" : \"1\" } }\n{ \"doc\" : {\"foo\" : \"bazbaz\"} }\n" - ); - assertBodyHasAccessIsDenied( - CREATE_DOC_USER, - randomFrom("PUT", "POST"), - "/" + INDEX_NAME + "/_bulk", - "{ \"update\" : { \"_id\" : \"1\" } }\n{ \"doc\" : {\"foo\" : \"bazbaz\"} }\n" - ); + assertBodyHasAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_bulk", """ + { "index" : { "_id" : "1" } } + { "doc" : {"foo" : "bazbaz"} } + """); + assertBodyHasAccessIsDenied(CREATE_DOC_USER, randomFrom("PUT", "POST"), "/" + INDEX_NAME + "/_bulk", """ + { "update" : { "_id" : "1" } } + { "doc" : {"foo" : "bazbaz"} } + """); } } diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java index 89cdde42554b6..d4ac150452e3f 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DlsFlsRequestCacheTests.java @@ -96,72 +96,71 @@ protected String configUsers() { @Override protected String configRoles() { - return super.configRoles() - + DLS_FLS_USER - + ":\n" - + " cluster: [ \"manage_own_api_key\" ]\n" - + " indices:\n" - + " - names:\n" - + " - \"dls-index\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " query: \"{\\\"match\\\": {\\\"number\\\": 101}}\"\n" - + " - names:\n" - + " - \"dls-alias\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " query: \"{\\\"match\\\": {\\\"number\\\": 102}}\"\n" - + " - names:\n" - + " - \"fls-index\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " field_security:\n" - + " grant:\n" - + " - \"public\"\n" - + " - names:\n" - + " - \"fls-alias\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " field_security:\n" - + " grant:\n" - + " - \"private\"\n" - + " - names:\n" - + " - \"alias1\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " query: \"{\\\"match\\\": {\\\"number\\\": 1}}\"\n" - + " field_security:\n" - + " grant:\n" - + " - \"*\"\n" - + " except:\n" - + " - \"private\"\n" - + " - names:\n" - + " - \"alias2\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " query: \"{\\\"match\\\": {\\\"number\\\": 2}}\"\n" - + " field_security:\n" - + " grant:\n" - + " - \"*\"\n" - + " except:\n" - + " - \"public\"\n" - + " - names:\n" - + " - \"all-alias\"\n" - + " privileges:\n" - + " - \"read\"\n" - + DLS_TEMPLATE_ROLE_QUERY_ROLE - + ":\n" - + " indices:\n" - + " - names:\n" - + " - \"dls-template-role-query-index\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " query: {\"template\":{\"source\":{\"match\":{\"username\":\"{{_user.username}}\"}}}}\n" - + " - names:\n" - + " - \"dls-template-role-query-alias\"\n" - + " privileges:\n" - + " - \"read\"\n" - + " query: {\"template\":{\"id\":\"my-script\"}}\n"; + return """ + %s%s: + cluster: [ "manage_own_api_key" ] + indices: + - names: + - "dls-index" + privileges: + - "read" + query: "{\\"match\\": {\\"number\\": 101}}" + - names: + - "dls-alias" + privileges: + - "read" + query: "{\\"match\\": {\\"number\\": 102}}" + - names: + - "fls-index" + privileges: + - "read" + field_security: + grant: + - "public" + - names: + - "fls-alias" + privileges: + - "read" + field_security: + grant: + - "private" + - names: + - "alias1" + privileges: + - "read" + query: "{\\"match\\": {\\"number\\": 1}}" + field_security: + grant: + - "*" + except: + - "private" + - names: + - "alias2" + privileges: + - "read" + query: "{\\"match\\": {\\"number\\": 2}}" + field_security: + grant: + - "*" + except: + - "public" + - names: + - "all-alias" + privileges: + - "read" + %s: + indices: + - names: + - "dls-template-role-query-index" + privileges: + - "read" + query: {"template":{"source":{"match":{"username":"{{_user.username}}"}}}} + - names: + - "dls-template-role-query-alias" + privileges: + - "read" + query: {"template":{"id":"my-script"}} + """.formatted(super.configRoles(), DLS_FLS_USER, DLS_TEMPLATE_ROLE_QUERY_ROLE); } @Override @@ -389,10 +388,8 @@ private void prepareIndices() { .preparePutStoredScript() .setId("my-script") .setContent( - new BytesArray( - "{\"script\":{\"source\":" - + "\"{\\\"match\\\":{\\\"username\\\":\\\"{{_user.username}}\\\"}}\",\"lang\":\"mustache\"}}" - ), + new BytesArray(""" + {"script":{"source":"{\\"match\\":{\\"username\\":\\"{{_user.username}}\\"}}","lang":"mustache"}}"""), XContentType.JSON ) .get() @@ -459,13 +456,8 @@ private Client limitedClientApiKey() throws ExecutionException, InterruptedExcep randomAlphaOfLengthBetween(3, 8), null, new RoleDescriptor.IndicesPrivileges[] { - RoleDescriptor.IndicesPrivileges.builder() - .indices(ALL_ALIAS) - .privileges("read") - .query("{\"term\":{\"letter\":\"a\"}}") - .grantedFields("*") - .deniedFields("number") - .build() }, + RoleDescriptor.IndicesPrivileges.builder().indices(ALL_ALIAS).privileges("read").query(""" + {"term":{"letter":"a"}}""").grantedFields("*").deniedFields("number").build() }, null ) ), diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java index 09444a8914f10..f505773d36a37 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentAndFieldLevelSecurityTests.java @@ -49,23 +49,13 @@ public class DocumentAndFieldLevelSecurityTests extends SecurityIntegTestCase { @Override protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - - return super.configUsers() - + "user1:" - + usersPasswdHashed - + "\n" - + "user2:" - + usersPasswdHashed - + "\n" - + "user3:" - + usersPasswdHashed - + "\n" - + "user4:" - + usersPasswdHashed - + "\n" - + "user5:" - + usersPasswdHashed - + "\n"; + return super.configUsers() + """ + user1:%s + user2:%s + user3:%s + user4:%s + user5:%s + """.formatted(usersPasswdHashed, usersPasswdHashed, usersPasswdHashed, usersPasswdHashed, usersPasswdHashed); } @Override @@ -75,37 +65,39 @@ protected String configUsersRoles() { @Override protected String configRoles() { - return super.configRoles() - + "\nrole1:\n" - + " cluster: [ none ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ none ]\n" - + "role2:\n" - + " cluster:\n" - + " - all\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ field1, id ]\n" - + " query: '{\"term\" : {\"field1\" : \"value1\"}}'\n" - + "role3:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ field2, id ]\n" - + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" - + "role4:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ field1, id ]\n" - + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n"; + return super.configRoles() + """ + + role1: + cluster: [ none ] + indices: + - names: '*' + privileges: [ none ] + role2: + cluster: + - all + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ field1, id ] + query: '{"term" : {"field1" : "value1"}}' + role3: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ field2, id ] + query: '{"term" : {"field2" : "value2"}}' + role4: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ field1, id ] + query: '{"term" : {"field2" : "value2"}}' + """; } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java index be6db7edada74..4bd597b765296 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/DocumentLevelSecurityTests.java @@ -129,77 +129,71 @@ protected boolean addMockGeoShapeFieldMapper() { @Override protected String configUsers() { final String usersPasswdHashed = new String(getFastStoredHashAlgoForTests().hash(USERS_PASSWD)); - return super.configUsers() - + "user1:" - + usersPasswdHashed - + "\n" - + "user2:" - + usersPasswdHashed - + "\n" - + "user3:" - + usersPasswdHashed - + "\n" - + "user4:" - + usersPasswdHashed - + "\n" - + "user5:" - + usersPasswdHashed - + "\n"; + return super.configUsers() + """ + user1:%s + user2:%s + user3:%s + user4:%s + user5:%s + """.formatted(usersPasswdHashed, usersPasswdHashed, usersPasswdHashed, usersPasswdHashed, usersPasswdHashed); } @Override protected String configUsersRoles() { - return super.configUsersRoles() - + "role1:user1,user2,user3\n" - + "role2:user1,user3\n" - + "role3:user2,user3\n" - + "role4:user4\n" - + "role5:user5\n"; + return super.configUsersRoles() + """ + role1:user1,user2,user3 + role2:user1,user3 + role3:user2,user3 + role4:user4 + role5:user5 + """; } @Override protected String configRoles() { - return super.configRoles() - + "\nrole1:\n" - + " cluster: [ none ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ none ]\n" - + "\nrole2:\n" - + " cluster:\n" - + " - all\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges:\n" - + " - all\n" - + " query: \n" - + " term: \n" - + " field1: value1\n" - + "role3:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" - + // <-- query defined as json in a string - "role4:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + - // query that can match nested documents - " query: '{\"bool\": { \"must_not\": { \"term\" : {\"field1\" : \"value2\"}}}}'\n" - + "role5:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: [ 'test' ]\n" - + " privileges: [ read ]\n" - + " query: '{\"term\" : {\"field2\" : \"value2\"}}'\n" - + " - names: [ 'fls-index' ]\n" - + " privileges: [ read ]\n" - + " field_security:\n" - + " grant: [ 'field1', 'other_field', 'suggest_field2' ]\n"; + // <-- query defined as json in a string + // query that can match nested documents + return super.configRoles() + """ + + role1: + cluster: [ none ] + indices: + - names: '*' + privileges: [ none ] + + role2: + cluster: + - all + indices: + - names: '*' + privileges: + - all + query: + term: + field1: value1 + role3: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + query: '{"term" : {"field2" : "value2"}}' + role4: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + query: '{"bool": { "must_not": { "term" : {"field1" : "value2"}}}}' + role5: + cluster: [ all ] + indices: + - names: [ 'test' ] + privileges: [ read ] + query: '{"term" : {"field2" : "value2"}}' + - names: [ 'fls-index' ] + privileges: [ read ] + field_security: + grant: [ 'field1', 'other_field', 'suggest_field2' ] + """; } @Override @@ -553,10 +547,8 @@ public void testPercolateQueryWithIndexedDocWithDLS() { assertAcked(client().admin().indices().prepareCreate("doc_index").setMapping("message", "type=text", "field1", "type=text")); client().prepareIndex("query_index") .setId("1") - .setSource( - "{\"field1\": \"value1\", \"field2\": \"value2\", \"query\": " + "{\"match\": {\"message\": \"bonsai tree\"}}}", - XContentType.JSON - ) + .setSource(""" + {"field1": "value1", "field2": "value2", "query": {"match": {"message": "bonsai tree"}}}""", XContentType.JSON) .setRefreshPolicy(IMMEDIATE) .get(); client().prepareIndex("doc_index") @@ -603,18 +595,14 @@ public void testGeoQueryWithIndexedShapeWithDLS() { ); client().prepareIndex("search_index") .setId("1") - .setSource( - "{\"field1\": \"value1\", \"field2\": \"value2\", \"search_field\": " + "{ \"type\": \"point\", \"coordinates\":[1, 1] }}", - XContentType.JSON - ) + .setSource(""" + {"field1": "value1", "field2": "value2", "search_field": { "type": "point", "coordinates":[1, 1] }}""", XContentType.JSON) .setRefreshPolicy(IMMEDIATE) .get(); client().prepareIndex("shape_index") .setId("1") - .setSource( - "{\"field1\": \"value1\", \"shape_field\": " + "{ \"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", - XContentType.JSON - ) + .setSource(""" + {"field1": "value1", "shape_field": { "type": "envelope", "coordinates": [[0, 2], [2, 0]]}}""", XContentType.JSON) .setRefreshPolicy(IMMEDIATE) .get(); ShapeQueryBuilder shapeQuery = new ShapeQueryBuilder("search_field", "1").relation(ShapeRelation.WITHIN) diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java index 75fccb0eae9a9..bebbc0c2f24bb 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityRandomTests.java @@ -60,12 +60,13 @@ protected String configUsers() { @Override protected String configUsersRoles() { - return super.configUsersRoles() - + "role1:user1,user2,user3,user4\n" - + "role2:user1\n" - + "role3:user2\n" - + "role4:user3\n" - + "role5:user4\n"; + return super.configUsersRoles() + """ + role1:user1,user2,user3,user4 + role2:user1 + role3:user2 + role4:user3 + role5:user4 + """; } @Override @@ -90,43 +91,46 @@ protected String configRoles() { roleFields.append(" - ").append(field).append('\n'); } - return super.configRoles() - + "\nrole1:\n" - + " cluster: [ none ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ none ]\n" - + "\nrole2:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant:\n" - + roleFields.toString() - + "role3:\n" - + " cluster:\n" - + " - all\n" - + " indices:\n" - + " - names: test\n" - + " privileges:\n" - + " - all\n" - + " field_security:\n" - + " grant: [ id, field1 ]\n" - + "role4:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: test\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ id, field2 ]\n" - + "role5:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: test\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ id, field3 ]\n"; + return """ + %s + role1: + cluster: [ none ] + indices: + - names: '*' + privileges: [ none ] + + role2: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: + %s + role3: + cluster: + - all + indices: + - names: test + privileges: + - all + field_security: + grant: [ id, field1 ] + role4: + cluster: [ all ] + indices: + - names: test + privileges: [ ALL ] + field_security: + grant: [ id, field2 ] + role5: + cluster: [ all ] + indices: + - names: test + privileges: [ ALL ] + field_security: + grant: [ id, field3 ] + """.formatted(super.configRoles(), roleFields.toString()); } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java index f097196b087ae..a6447f9296357 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/FieldLevelSecurityTests.java @@ -144,76 +144,78 @@ protected String configUsers() { @Override protected String configUsersRoles() { - return super.configUsersRoles() - + "role1:user1\n" - + "role2:user1,user7,user8\n" - + "role3:user2,user7,user8\n" - + "role4:user3,user7\n" - + "role5:user4,user7\n" - + "role6:user5,user7\n" - + "role7:user6\n" - + "role8:user9"; + return super.configUsersRoles() + """ + role1:user1 + role2:user1,user7,user8 + role3:user2,user7,user8 + role4:user3,user7 + role5:user4,user7 + role6:user5,user7 + role7:user6 + role8:user9"""; } @Override protected String configRoles() { - return super.configRoles() - + "\nrole1:\n" - + " cluster: [ none ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ none ]\n" - + "role2:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ field1, join_field*, vector ]\n" - + "role3:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ field2, query* ]\n" - + "role4:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ field1, field2]\n" - + "role5:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ ]\n" - + "role6:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ALL]\n" - + "role7:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ 'field*' ]\n" - + "role8:\n" - + " indices:\n" - + " - names: 'doc_index'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ 'field*' ]\n" - + " except: [ 'field2' ]\n" - + " - names: 'query_index'\n" - + " privileges: [ ALL ]\n" - + " field_security:\n" - + " grant: [ 'field*', 'query' ]\n"; + return super.configRoles() + """ + %s + role1: + cluster: [ none ] + indices: + - names: '*' + privileges: [ none ] + role2: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ field1, join_field*, vector ] + role3: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ field2, query* ] + role4: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ field1, field2] + role5: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ ] + role6: + cluster: [ all ] + indices: + - names: '*' + privileges: [ALL] + role7: + cluster: [ all ] + indices: + - names: '*' + privileges: [ ALL ] + field_security: + grant: [ 'field*' ] + role8: + indices: + - names: 'doc_index' + privileges: [ ALL ] + field_security: + grant: [ 'field*' ] + except: [ 'field2' ] + - names: 'query_index' + privileges: [ ALL ] + field_security: + grant: [ 'field*', 'query' ] + """; } @Override @@ -430,16 +432,10 @@ public void testKnnSearch() throws IOException { public void testPercolateQueryWithIndexedDocWithFLS() { assertAcked(client().admin().indices().prepareCreate("query_index").setMapping("query", "type=percolator", "field2", "type=text")); assertAcked(client().admin().indices().prepareCreate("doc_index").setMapping("field2", "type=text", "field1", "type=text")); - client().prepareIndex("query_index") - .setId("1") - .setSource("{\"query\": {\"match\": {\"field2\": \"bonsai tree\"}}}", XContentType.JSON) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("doc_index") - .setId("1") - .setSource("{\"field1\": \"value1\", \"field2\": \"A new bonsai tree in the office\"}", XContentType.JSON) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("query_index").setId("1").setSource(""" + {"query": {"match": {"field2": "bonsai tree"}}}""", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("doc_index").setId("1").setSource(""" + {"field1": "value1", "field2": "A new bonsai tree in the office"}""", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); QueryBuilder percolateQuery = new PercolateQueryBuilder("query", "doc_index", "1", null, null, null); // user7 sees everything SearchResponse result = client().filterWithHeader( @@ -478,30 +474,47 @@ public void testPercolateQueryWithIndexedDocWithFLS() { public void testGeoQueryWithIndexedShapeWithFLS() { assertAcked(client().admin().indices().prepareCreate("search_index").setMapping("field", "type=shape", "other", "type=shape")); assertAcked(client().admin().indices().prepareCreate("shape_index").setMapping("field", "type=shape", "other", "type=shape")); - client().prepareIndex("search_index") - .setId("1") - .setSource("{\"field\": {\"type\": \"point\", \"coordinates\":[1, 1]}}", XContentType.JSON) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("search_index") - .setId("2") - .setSource("{\"other\": {\"type\": \"point\", \"coordinates\":[1, 1]}}", XContentType.JSON) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("shape_index") - .setId("1") - .setSource( - "{\"field\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}, " - + "\"field2\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", - XContentType.JSON - ) - .setRefreshPolicy(IMMEDIATE) - .get(); - client().prepareIndex("shape_index") - .setId("2") - .setSource("{\"other\": {\"type\": \"envelope\", \"coordinates\": [[0, 2], [2, 0]]}}", XContentType.JSON) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex("search_index").setId("1").setSource(""" + { + "field": { + "type": "point", + "coordinates": [ 1, 1 ] + } + }""", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("search_index").setId("2").setSource(""" + { + "other": { + "type": "point", + "coordinates": [ 1, 1 ] + } + }""", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("shape_index").setId("1").setSource(""" + { + "field": { + "type": "envelope", + "coordinates": [ + [ 0, 2 ], + [ 2, 0 ] + ] + }, + "field2": { + "type": "envelope", + "coordinates": [ + [ 0, 2 ], + [ 2, 0 ] + ] + } + }""", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); + client().prepareIndex("shape_index").setId("2").setSource(""" + { + "other": { + "type": "envelope", + "coordinates": [ + [ 0, 2 ], + [ 2, 0 ] + ] + } + }""", XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); SearchResponse result; // user sees both the querying shape and the queried point SearchRequestBuilder requestBuilder = client().filterWithHeader( diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java index 3364627c2c353..5ffb79133c1d1 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndexPrivilegeIntegTests.java @@ -27,88 +27,92 @@ public class IndexPrivilegeIntegTests extends AbstractPrivilegeTestCase { private final String jsonDoc = "{ \"name\" : \"elasticsearch\", \"body\": \"foo bar\" }"; - private static final String ROLES = "all_cluster_role:\n" - + " cluster: [ all ]\n" - + "all_indices_role:\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ all ]\n" - + "all_a_role:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [ all ]\n" - + "read_a_role:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [ read ]\n" - + "read_b_role:\n" - + " indices:\n" - + " - names: 'b'\n" - + " privileges: [ read ]\n" - + "write_a_role:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [ write ]\n" - + "read_ab_role:\n" - + " indices:\n" - + " - names: [ 'a', 'b' ]\n" - + " privileges: [ read ]\n" - + "all_regex_ab_role:\n" - + " indices:\n" - + " - names: '/a|b/'\n" - + " privileges: [ all ]\n" - + "manage_starts_with_a_role:\n" - + " indices:\n" - + " - names: 'a*'\n" - + " privileges: [ manage ]\n" - + "read_write_all_role:\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ read, write ]\n" - + "create_c_role:\n" - + " indices:\n" - + " - names: 'c'\n" - + " privileges: [ create_index ]\n" - + "monitor_b_role:\n" - + " indices:\n" - + " - names: 'b'\n" - + " privileges: [ monitor ]\n" - + "maintenance_a_view_meta_b_role:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [ maintenance ]\n" - + " - names: '*b'\n" - + " privileges: [ view_index_metadata ]\n" - + "read_write_a_role:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [ read, write ]\n" - + "delete_b_role:\n" - + " indices:\n" - + " - names: 'b'\n" - + " privileges: [ delete ]\n" - + "index_a_role:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [ index ]\n" - + "\n"; - - private static final String USERS_ROLES = "all_indices_role:admin,u8\n" - + "all_cluster_role:admin\n" - + "all_a_role:u1,u2,u6\n" - + "read_a_role:u1,u5,u14\n" - + "read_b_role:u3,u5,u6,u8,u13\n" - + "write_a_role:u9\n" - + "read_ab_role:u2,u4,u9\n" - + "all_regex_ab_role:u3\n" - + "manage_starts_with_a_role:u4\n" - + "read_write_all_role:u12\n" - + "create_c_role:u11\n" - + "monitor_b_role:u14\n" - + "maintenance_a_view_meta_b_role:u15\n" - + "read_write_a_role:u12\n" - + "delete_b_role:u11\n" - + "index_a_role:u13\n"; + private static final String ROLES = """ + all_cluster_role: + cluster: [ all ] + all_indices_role: + indices: + - names: '*' + privileges: [ all ] + all_a_role: + indices: + - names: 'a' + privileges: [ all ] + read_a_role: + indices: + - names: 'a' + privileges: [ read ] + read_b_role: + indices: + - names: 'b' + privileges: [ read ] + write_a_role: + indices: + - names: 'a' + privileges: [ write ] + read_ab_role: + indices: + - names: [ 'a', 'b' ] + privileges: [ read ] + all_regex_ab_role: + indices: + - names: '/a|b/' + privileges: [ all ] + manage_starts_with_a_role: + indices: + - names: 'a*' + privileges: [ manage ] + read_write_all_role: + indices: + - names: '*' + privileges: [ read, write ] + create_c_role: + indices: + - names: 'c' + privileges: [ create_index ] + monitor_b_role: + indices: + - names: 'b' + privileges: [ monitor ] + maintenance_a_view_meta_b_role: + indices: + - names: 'a' + privileges: [ maintenance ] + - names: '*b' + privileges: [ view_index_metadata ] + read_write_a_role: + indices: + - names: 'a' + privileges: [ read, write ] + delete_b_role: + indices: + - names: 'b' + privileges: [ delete ] + index_a_role: + indices: + - names: 'a' + privileges: [ index ] + + """; + + private static final String USERS_ROLES = """ + all_indices_role:admin,u8 + all_cluster_role:admin + all_a_role:u1,u2,u6 + read_a_role:u1,u5,u14 + read_b_role:u3,u5,u6,u8,u13 + write_a_role:u9 + read_ab_role:u2,u4,u9 + all_regex_ab_role:u3 + manage_starts_with_a_role:u4 + read_write_all_role:u12 + create_c_role:u11 + monitor_b_role:u14 + maintenance_a_view_meta_b_role:u15 + read_write_a_role:u12 + delete_b_role:u11 + index_a_role:u13 + """; @Override protected boolean addMockHttpTransport() { @@ -125,52 +129,23 @@ protected String configUsers() { final Hasher passwdHasher = getFastStoredHashAlgoForTests(); final String usersPasswdHashed = new String(passwdHasher.hash(SecuritySettingsSourceField.TEST_PASSWORD_SECURE_STRING)); - return super.configUsers() - + "admin:" - + usersPasswdHashed - + "\n" - + "u1:" - + usersPasswdHashed - + "\n" - + "u2:" - + usersPasswdHashed - + "\n" - + "u3:" - + usersPasswdHashed - + "\n" - + "u4:" - + usersPasswdHashed - + "\n" - + "u5:" - + usersPasswdHashed - + "\n" - + "u6:" - + usersPasswdHashed - + "\n" - + "u7:" - + usersPasswdHashed - + "\n" - + "u8:" - + usersPasswdHashed - + "\n" - + "u9:" - + usersPasswdHashed - + "\n" - + "u11:" - + usersPasswdHashed - + "\n" - + "u12:" - + usersPasswdHashed - + "\n" - + "u13:" - + usersPasswdHashed - + "\n" - + "u14:" - + usersPasswdHashed - + "\n" - + "u15:" - + usersPasswdHashed - + "\n"; + return super.configUsers() + """ + admin:%1$s + u1:%1$s + u2:%1$s + u3:%1$s + u4:%1$s + u5:%1$s + u6:%1$s + u7:%1$s + u8:%1$s + u9:%1$s + u11:%1$s + u12:%1$s + u13:%1$s + u14:%1$s + u15:%1$s + """.formatted(usersPasswdHashed); } @Override @@ -198,20 +173,18 @@ public void testUserU1() throws Exception { assertUserIsAllowed("u1", "all", "a"); assertUserIsDenied("u1", "all", "b"); assertUserIsDenied("u1", "all", "c"); - assertAccessIsAllowed("u1", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u1", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed( - "u1", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u1", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u1", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u1", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] }"""); + assertAccessIsAllowed("u1", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u1", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u1", randomFrom("GET", "POST"), "/" + "b" + "/_field_caps?fields=*"); assertAccessIsDenied("u1", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -224,20 +197,18 @@ public void testUserU2() throws Exception { assertUserIsDenied("u2", "monitor", "b"); assertUserIsDenied("u2", "create_index", "b"); assertUserIsDenied("u2", "all", "c"); - assertAccessIsAllowed("u2", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u2", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed( - "u2", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u2", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u2", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u2", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] }"""); + assertAccessIsAllowed("u2", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u2", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u2", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -246,20 +217,18 @@ public void testUserU3() throws Exception { assertUserIsAllowed("u3", "all", "a"); assertUserIsAllowed("u3", "all", "b"); assertUserIsDenied("u3", "all", "c"); - assertAccessIsAllowed("u3", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u3", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed( - "u3", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u3", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u3", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u3", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] }"""); + assertAccessIsAllowed("u3", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u3", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); } public void testUserU4() throws Exception { @@ -277,15 +246,18 @@ public void testUserU4() throws Exception { assertUserIsAllowed("u4", "create_index", "an_index"); assertUserIsAllowed("u4", "manage", "an_index"); - assertAccessIsAllowed("u4", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u4", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied("u4", "PUT", "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed( - "u4", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u4", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u4", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] }"""); + assertAccessIsDenied("u4", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u4", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u2", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -299,15 +271,18 @@ public void testUserU5() throws Exception { assertUserIsDenied("u5", "manage", "b"); assertUserIsDenied("u5", "write", "b"); - assertAccessIsAllowed("u5", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u5", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied("u5", "PUT", "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed( - "u5", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u5", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u5", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] }"""); + assertAccessIsDenied("u5", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u5", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); } public void testUserU6() throws Exception { @@ -317,20 +292,18 @@ public void testUserU6() throws Exception { assertUserIsDenied("u6", "manage", "b"); assertUserIsDenied("u6", "write", "b"); assertUserIsDenied("u6", "all", "c"); - assertAccessIsAllowed("u6", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u6", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed( - "u6", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u6", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u6", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u6", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] }"""); + assertAccessIsAllowed("u6", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u6", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); } public void testUserU7() throws Exception { @@ -338,15 +311,18 @@ public void testUserU7() throws Exception { assertUserIsDenied("u7", "all", "a"); assertUserIsDenied("u7", "all", "b"); assertUserIsDenied("u7", "all", "c"); - assertAccessIsDenied("u7", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsDenied("u7", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied("u7", "PUT", "/" + randomIndex() + "/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsDenied( - "u7", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsDenied("u7", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsDenied("u7", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] }"""); + assertAccessIsDenied("u7", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsDenied("u7", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u7", randomFrom("GET", "POST"), "/" + randomIndex() + "/_field_caps?fields=*"); } @@ -355,20 +331,19 @@ public void testUserU8() throws Exception { assertUserIsAllowed("u8", "all", "a"); assertUserIsAllowed("u8", "all", "b"); assertUserIsAllowed("u8", "all", "c"); - assertAccessIsAllowed("u8", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u8", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed( - "u8", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u8", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u8", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u8", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] } + """); + assertAccessIsAllowed("u8", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u8", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); } public void testUserU9() throws Exception { @@ -379,20 +354,19 @@ public void testUserU9() throws Exception { assertUserIsDenied("u9", "manage", "b"); assertUserIsDenied("u9", "write", "b"); assertUserIsDenied("u9", "all", "c"); - assertAccessIsAllowed("u9", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u9", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed( - "u9", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u9", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u9", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u9", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] } + """); + assertAccessIsAllowed("u9", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u9", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u9", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -412,20 +386,19 @@ public void testUserU11() throws Exception { assertUserIsDenied("u11", "monitor", "c"); assertUserIsDenied("u11", "maintenance", "c"); - assertAccessIsDenied("u11", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsDenied("u11", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertBodyHasAccessIsDenied( - "u11", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsDenied( - "u11", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsDenied("u11", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsDenied("u11", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] } + """); + assertBodyHasAccessIsDenied("u11", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsDenied("u11", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u11", randomFrom("GET", "POST"), "/" + "b" + "/_field_caps?fields=*"); assertAccessIsDenied("u11", randomFrom("GET", "POST"), "/" + "c" + "/_field_caps?fields=*"); } @@ -438,20 +411,19 @@ public void testUserU12() throws Exception { assertUserIsAllowed("u12", "data_access", "b"); assertUserIsDenied("u12", "manage", "c"); assertUserIsAllowed("u12", "data_access", "c"); - assertAccessIsAllowed("u12", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u12", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed( - "u12", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u12", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u12", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u12", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] } + """); + assertAccessIsAllowed("u12", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u12", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); } public void testUserU13() throws Exception { @@ -467,16 +439,23 @@ public void testUserU13() throws Exception { assertUserIsDenied("u13", "all", "c"); - assertAccessIsAllowed("u13", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); - assertAccessIsAllowed("u13", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsAllowed("u13", "PUT", "/a/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertBodyHasAccessIsDenied("u13", "PUT", "/b/_bulk", "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n"); - assertAccessIsAllowed( - "u13", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsAllowed("u13", "GET", "/" + randomIndex() + "/_msearch", """ + {} + { "query" : { "match_all" : {} } } + """); + assertAccessIsAllowed("u13", "POST", "/" + randomIndex() + "/_mget", """ + { "ids" : [ "1", "2" ] } + """); + assertAccessIsAllowed("u13", "PUT", "/a/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertBodyHasAccessIsDenied("u13", "PUT", "/b/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u13", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u13", randomFrom("GET", "POST"), "/" + "a" + "/_field_caps?fields=*"); } @@ -495,18 +474,12 @@ public void testUserU14() throws Exception { assertAccessIsAllowed("u14", "GET", "/" + randomIndex() + "/_msearch", "{}\n{ \"query\" : { \"match_all\" : {} } }\n"); assertAccessIsAllowed("u14", "POST", "/" + randomIndex() + "/_mget", "{ \"ids\" : [ \"1\", \"2\" ] } "); - assertAccessIsDenied( - "u14", - "PUT", - "/" + randomIndex() + "/_bulk", - "{ \"index\" : { \"_id\" : \"123\" } }\n{ \"foo\" : \"bar\" }\n" - ); - assertAccessIsAllowed( - "u14", - "GET", - "/" + randomIndex() + "/_mtermvectors", - "{ \"docs\" : [ { \"_id\": \"1\" }, { \"_id\": \"2\" } ] }" - ); + assertAccessIsDenied("u14", "PUT", "/" + randomIndex() + "/_bulk", """ + { "index" : { "_id" : "123" } } + { "foo" : "bar" } + """); + assertAccessIsAllowed("u14", "GET", "/" + randomIndex() + "/_mtermvectors", """ + { "docs" : [ { "_id": "1" }, { "_id": "2" } ] }"""); assertAccessIsDenied("u14", randomFrom("GET", "POST"), "/" + "b" + "/_field_caps?fields=*"); } @@ -640,7 +613,9 @@ private void assertUserExecutes(String user, String action, String index, boolea if (userIsAllowed) { assertUserIsAllowed(user, "read", index); assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/321", "{ \"foo\" : \"bar\" }"); - assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", "{ \"doc\" : { \"foo\" : \"baz\" } }"); + assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", """ + { "doc" : { "foo" : "baz" } } + """); } else { assertUserIsDenied(user, "read", index); assertUserIsDenied(user, "index", index); @@ -687,54 +662,36 @@ private void assertUserExecutes(String user, String action, String index, boolea if (userIsAllowed) { assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/321", "{ \"foo\" : \"bar\" }"); // test auto mapping update is allowed but deprecated - Response response = assertAccessIsAllowed( - user, - "PUT", - "/" + index + "/_doc/4321", - "{ \"" + UUIDs.randomBase64UUID() + "\" : \"foo\" }" - ); + Response response = assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/4321", """ + { "%s" : "foo" }""".formatted(UUIDs.randomBase64UUID())); String warningHeader = response.getHeader("Warning"); - assertThat( - warningHeader, - containsString( - "the index privilege [index] allowed the update mapping action [indices:admin/mapping/auto_put] on index [" - + index - + "], this privilege will not permit mapping updates in the next major release - users who require" - + " access to update mappings must be granted explicit privileges" - ) - ); - assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", "{ \"doc\" : { \"foo\" : \"baz\" } }"); - response = assertAccessIsAllowed( - user, - "POST", - "/" + index + "/_update/321", - "{ \"doc\" : { \"" + UUIDs.randomBase64UUID() + "\" : \"baz\" } }" - ); + assertThat(warningHeader, containsString(""" + the index privilege [index] allowed the update mapping action [indices:admin/mapping/auto_put] on index [%s], \ + this privilege will not permit mapping updates in the next major release - users who require access to update \ + mappings must be granted explicit privileges""".formatted(index))); + assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", """ + { "doc" : { "foo" : "baz" } } + """); + response = assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", """ + { "doc" : { "%s" : "baz" } } + """.formatted(UUIDs.randomBase64UUID())); warningHeader = response.getHeader("Warning"); - assertThat( - warningHeader, - containsString( - "the index privilege [index] allowed the update mapping action " - + "[indices:admin/mapping/auto_put] on index [" - + index - + "], this privilege will not permit mapping updates in" - + " the next major release - users who require access to update mappings must be" - + " granted explicit privileges" - ) - ); - assertThat( - warningHeader, - containsString( - "the index privilege [index] allowed the update mapping action [indices:admin/mapping/auto_put] on index [" - + index - + "], this privilege will not permit mapping updates in the next major release - users who require" - + " access to update mappings must be granted explicit privileges" - ) - ); + assertThat(warningHeader, containsString(""" + the index privilege [index] allowed the update mapping action [indices:admin/mapping/auto_put] on index [%s], \ + this privilege will not permit mapping updates in the next major release - users who require access to update \ + mappings must be granted explicit privileges\ + """.formatted(index))); + assertThat(warningHeader, containsString(""" + the index privilege [index] allowed the update mapping action [indices:admin/mapping/auto_put] on index [%s], \ + this privilege will not permit mapping updates in the next major release - users who require access to update \ + mappings must be granted explicit privileges\ + """.formatted(index))); } else { assertAccessIsDenied(user, "PUT", "/" + index + "/_doc/321", "{ \"foo\" : \"bar\" }"); assertAccessIsDenied(user, "PUT", "/" + index + "/_doc/321", "{ \"foo\" : \"bar\" }"); - assertAccessIsDenied(user, "POST", "/" + index + "/_update/321", "{ \"doc\" : { \"foo\" : \"baz\" } }"); + assertAccessIsDenied(user, "POST", "/" + index + "/_update/321", """ + { "doc" : { "foo" : "baz" } } + """); } break; @@ -755,12 +712,8 @@ private void assertUserExecutes(String user, String action, String index, boolea assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/321", "{ \"foo\" : \"bar\" }"); // test auto mapping update is allowed but deprecated - Response response = assertAccessIsAllowed( - user, - "PUT", - "/" + index + "/_doc/4321", - "{ \"" + UUIDs.randomBase64UUID() + "\" : \"foo\" }" - ); + Response response = assertAccessIsAllowed(user, "PUT", "/" + index + "/_doc/4321", """ + { "%s" : "foo" }""".formatted(UUIDs.randomBase64UUID())); String warningHeader = response.getHeader("Warning"); assertThat( warningHeader, @@ -771,13 +724,12 @@ private void assertUserExecutes(String user, String action, String index, boolea + "]" ) ); - assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", "{ \"doc\" : { \"foo\" : \"baz\" } }"); - response = assertAccessIsAllowed( - user, - "POST", - "/" + index + "/_update/321", - "{ \"doc\" : { \"" + UUIDs.randomBase64UUID() + "\" : \"baz\" } }" - ); + assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", """ + { "doc" : { "foo" : "baz" } } + """); + response = assertAccessIsAllowed(user, "POST", "/" + index + "/_update/321", """ + { "doc" : { "%s" : "baz" } } + """.formatted(UUIDs.randomBase64UUID())); warningHeader = response.getHeader("Warning"); assertThat( warningHeader, diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java index a624879e20635..5c88dc0652789 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/IndicesPermissionsWithAliasesWildcardsAndRegexsTests.java @@ -58,22 +58,24 @@ protected String configUsersRoles() { @Override protected String configRoles() { - return super.configRoles() - + "\nrole1:\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: 't*'\n" - + " privileges: [ALL]\n" - + " field_security:\n" - + " grant: [ field1 ]\n" - + " - names: 'my_alias'\n" - + " privileges: [ALL]\n" - + " field_security:\n" - + " grant: [ field2 ]\n" - + " - names: '/an_.*/'\n" - + " privileges: [ALL]\n" - + " field_security:\n" - + " grant: [ field3 ]\n"; + return """ + %s + role1: + cluster: [ all ] + indices: + - names: 't*' + privileges: [ALL] + field_security: + grant: [ field1 ] + - names: 'my_alias' + privileges: [ALL] + field_security: + grant: [ field2 ] + - names: '/an_.*/' + privileges: [ALL] + field_security: + grant: [ field3 ] + """.formatted(super.configRoles()); } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java index ad10488bf5f60..46fc650a80cf5 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/KibanaUserRoleIntegTests.java @@ -38,14 +38,15 @@ public class KibanaUserRoleIntegTests extends NativeRealmIntegTestCase { @Override public String configRoles() { - return super.configRoles() - + "\n" - + "my_kibana_user:\n" - + " indices:\n" - + " - names: 'logstash-*'\n" - + " privileges:\n" - + " - view_index_metadata\n" - + " - read\n"; + return """ + %s + my_kibana_user: + indices: + - names: 'logstash-*' + privileges: + - view_index_metadata + - read + """.formatted(super.configRoles()); } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java index c93f250e3f6d2..68ae1c4fd2e76 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/MultipleIndicesPermissionsTests.java @@ -83,36 +83,37 @@ protected boolean addMockHttpTransport() { @Override protected String configRoles() { - return SecuritySettingsSource.TEST_ROLE - + ":\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [manage]\n" - + " - names: '/.*/'\n" - + " privileges: [write]\n" - + " - names: 'test'\n" - + " privileges: [read]\n" - + " - names: 'test1'\n" - + " privileges: [read]\n" - + "\n" - + "role_a:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [all]\n" - + " - names: 'alias1'\n" - + " privileges: [read]\n" - + "\n" - + "role_monitor_all_unrestricted_indices:\n" - + " cluster: [monitor]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [monitor]\n" - + "\n" - + "role_b:\n" - + " indices:\n" - + " - names: 'b'\n" - + " privileges: [all]\n"; + return """ + %s: + cluster: [ all ] + indices: + - names: '*' + privileges: [manage] + - names: '/.*/' + privileges: [write] + - names: 'test' + privileges: [read] + - names: 'test1' + privileges: [read] + + role_a: + indices: + - names: 'a' + privileges: [all] + - names: 'alias1' + privileges: [read] + + role_monitor_all_unrestricted_indices: + cluster: [monitor] + indices: + - names: '*' + privileges: [monitor] + + role_b: + indices: + - names: 'b' + privileges: [all] + """.formatted(SecuritySettingsSource.TEST_ROLE); } @Override @@ -132,10 +133,11 @@ protected String configUsers() { @Override protected String configUsersRoles() { - return SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES - + "role_a:user_a,user_ab\n" - + "role_b:user_ab\n" - + "role_monitor_all_unrestricted_indices:user_monitor\n"; + return SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES + """ + role_a:user_a,user_ab + role_b:user_ab + role_monitor_all_unrestricted_indices:user_monitor + """; } public void testSingleRole() throws Exception { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java index 5c3ce15000a2b..e571cb7f6bfb4 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/PermissionPrecedenceTests.java @@ -37,16 +37,16 @@ public class PermissionPrecedenceTests extends SecurityIntegTestCase { @Override protected String configRoles() { - return "admin:\n" - + " cluster: [ all ] \n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ all ]" - + "\n" - + "user:\n" - + " indices:\n" - + " - names: 'test_*'\n" - + " privileges: [ all ]"; + return """ + admin: + cluster: [ all ]\s + indices: + - names: '*' + privileges: [ all ] + user: + indices: + - names: 'test_*' + privileges: [ all ]"""; } @Override @@ -59,7 +59,11 @@ protected String configUsers() { @Override protected String configUsersRoles() { - return "admin:admin\n" + "transport_client:client\n" + "user:user\n"; + return """ + admin:admin + transport_client:client + user:user + """; } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java index aa6ddc969f231..0a66f5fbeed37 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityClearScrollTests.java @@ -51,14 +51,16 @@ protected String configUsersRoles() { @Override protected String configRoles() { - return super.configRoles() - + "\nallowed_role:\n" - + " cluster:\n" - + " - cluster:admin/indices/scroll/clear_all \n" - + "denied_role:\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ALL]\n"; + return """ + %s + allowed_role: + cluster: + - cluster:admin/indices/scroll/clear_all\s + denied_role: + indices: + - names: '*' + privileges: [ALL] + """.formatted(super.configRoles()); } @Before diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java index ab0611f617fee..25560dbd907fa 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/integration/SecurityFeatureStateIntegTests.java @@ -78,29 +78,30 @@ public void testSecurityFeatureStateSnapshotAndRestore() throws Exception { final String roleName = "extra_role"; final Request createRoleRequest = new Request("PUT", "/_security/role/" + roleName); createRoleRequest.addParameter("refresh", "wait_for"); - createRoleRequest.setJsonEntity( - "{" - + " \"indices\": [" - + " {" - + " \"names\": [ \"test_index\" ]," - + " \"privileges\" : [ \"create\", \"create_index\", \"create_doc\" ]" - + " }" - + " ]" - + "}" - ); + createRoleRequest.setJsonEntity(""" + { + "indices": [ + { + "names": [ "test_index" ], + "privileges": [ "create", "create_index", "create_doc" ] + } + ] + }"""); performSuperuserRequest(createRoleRequest); // create a test user final Request createUserRequest = new Request("PUT", "/_security/user/" + LOCAL_TEST_USER_NAME); createUserRequest.addParameter("refresh", "wait_for"); - createUserRequest.setJsonEntity( - "{" + " \"password\": \"" + LOCAL_TEST_USER_PASSWORD + "\"," + " \"roles\": [ \"" + roleName + "\" ]" + "}" - ); + createUserRequest.setJsonEntity(""" + { "password": "%s", "roles": [ "%s" ]} + """.formatted(LOCAL_TEST_USER_PASSWORD, roleName)); performSuperuserRequest(createUserRequest); // test user posts a document final Request postTestDocument1 = new Request("POST", "/test_index/_doc"); - postTestDocument1.setJsonEntity("{\"message\": \"before snapshot\"}"); + postTestDocument1.setJsonEntity(""" + {"message": "before snapshot"} + """); performTestUserRequest(postTestDocument1); // snapshot state diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java index 1ab39d461e9ae..8cb1e61cbc3a9 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java @@ -71,35 +71,40 @@ public class LicensingTests extends SecurityIntegTestCase { private static final SecureString HASH_PASSWD = new SecureString(Hasher.BCRYPT4.hash(new SecureString("passwd".toCharArray()))); - private static final String ROLES = SecuritySettingsSource.TEST_ROLE - + ":\n" - + " cluster: [ all ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [manage]\n" - + " - names: '/.*/'\n" - + " privileges: [write]\n" - + " - names: 'test'\n" - + " privileges: [read]\n" - + " - names: 'test1'\n" - + " privileges: [read]\n" - + "\n" - + "role_a:\n" - + " indices:\n" - + " - names: 'a'\n" - + " privileges: [all]\n" - + " - names: 'test-dls'\n" - + " privileges: [read]\n" - + " query: '{\"term\":{\"field\":\"value\"} }'\n" - + "\n" - + "role_b:\n" - + " indices:\n" - + " - names: 'b'\n" - + " privileges: [all]\n"; - - private static final String USERS_ROLES = SecuritySettingsSource.CONFIG_STANDARD_USER_ROLES - + "role_a:user_a,user_b\n" - + "role_b:user_b\n"; + private static final String ROLES = SecuritySettingsSource.TEST_ROLE + """ + : + cluster: [ all ] + indices: + - names: '*' + privileges: [manage] + - names: '/.*/' + privileges: [write] + - names: 'test' + privileges: [read] + - names: 'test1' + privileges: [read] + + role_a: + indices: + - names: 'a' + privileges: [all] + - names: 'test-dls' + privileges: [read] + query: '{"term":{"field":"value"} }' + + role_b: + indices: + - names: 'b' + privileges: [all] + """; + + private static final String USERS_ROLES = """ + user:test_user,test_trans_client_user + transport_client:test_trans_client_user + superuser:test_superuser + role_a:user_a,user_b + role_b:user_b + """; @Override protected String configRoles() { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java index 9bc349edcd73b..e2227f10e7da4 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/ApiKeyIntegTests.java @@ -133,16 +133,17 @@ public void wipeSecurityIndex() throws Exception { @Override public String configRoles() { - return super.configRoles() - + "\n" - + "no_api_key_role:\n" - + " cluster: [\"manage_token\"]\n" - + "manage_api_key_role:\n" - + " cluster: [\"manage_api_key\"]\n" - + "manage_own_api_key_role:\n" - + " cluster: [\"manage_own_api_key\"]\n" - + "run_as_role:\n" - + " run_as: [\"user_with_manage_own_api_key_role\"]\n"; + return super.configRoles() + """ + + no_api_key_role: + cluster: ["manage_token"] + manage_api_key_role: + cluster: ["manage_api_key"] + manage_own_api_key_role: + cluster: ["manage_own_api_key"] + run_as_role: + run_as: ["user_with_manage_own_api_key_role"] + """; } @Override @@ -162,10 +163,11 @@ public String configUsers() { @Override public String configUsersRoles() { - return super.configUsersRoles() - + "no_api_key_role:user_with_no_api_key_role\n" - + "manage_api_key_role:user_with_manage_api_key_role\n" - + "manage_own_api_key_role:user_with_manage_own_api_key_role\n"; + return super.configUsersRoles() + """ + no_api_key_role:user_with_no_api_key_role + manage_api_key_role:user_with_manage_api_key_role + manage_own_api_key_role:user_with_manage_own_api_key_role + """; } private void awaitApiKeysRemoverCompletion() throws Exception { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java index 4d7102fd59b96..f0749679888e5 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authc/pki/PkiAuthDelegationIntegTests.java @@ -106,30 +106,32 @@ protected String configUsers() { @Override protected String configRoles() { - return super.configRoles() - + "\n" - + "role_manage:\n" - + " cluster: [ manage ]\n" - + "\n" - + "role_manage_security:\n" - + " cluster: [ manage_security ]\n" - + "\n" - + "role_delegate_pki:\n" - + " cluster: [ delegate_pki ]\n" - + "\n" - + "role_all:\n" - + " cluster: [ all ]\n"; + return """ + %s + role_manage: + cluster: [ manage ] + + role_manage_security: + cluster: [ manage_security ] + + role_delegate_pki: + cluster: [ delegate_pki ] + + role_all: + cluster: [ all ] + """.formatted(super.configRoles()); } @Override protected String configUsersRoles() { - return super.configUsersRoles() - + "\n" - + "role_manage:user_manage\n" - + "role_manage_security:user_manage_security\n" - + "role_delegate_pki:user_delegate_pki\n" - + "role_all:user_all\n" - + "kibana_system:my_kibana_system\n"; + return """ + %s + role_manage:user_manage + role_manage_security:user_manage_security + role_delegate_pki:user_delegate_pki + role_all:user_all + kibana_system:my_kibana_system + """.formatted(super.configUsersRoles()); } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java index c05855563197b..9c219ed3f1949 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/AnalyzeTests.java @@ -34,15 +34,17 @@ protected String configUsersRoles() { @Override protected String configRoles() { - return super.configRoles() + "\n" + // role that has analyze indices privileges only - "analyze_indices:\n" - + " indices:\n" - + " - names: 'test_*'\n" - + " privileges: [ 'indices:admin/analyze' ]\n" - + "analyze_cluster:\n" - + " cluster:\n" - + " - cluster:admin/analyze\n"; + return """ + %s + analyze_indices: + indices: + - names: 'test_*' + privileges: [ 'indices:admin/analyze' ] + analyze_cluster: + cluster: + - cluster:admin/analyze + """.formatted(super.configRoles()); } public void testAnalyzeWithIndices() { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java index 639e5c446d20e..a63b2394f3ba6 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/IndexAliasesTests.java @@ -76,44 +76,42 @@ protected String configUsersRoles() { @Override protected String configRoles() { - return super.configRoles() + "\n" + // role that has create index only privileges - "create_only:\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ create_index ]\n" - + "all_on_test:\n" - + " indices:\n" - + " - names: 'test_*'\n" - + " privileges: [ all ]\n" - + - // role that has create index and manage_aliases on test_*, not enough to manage_aliases aliases outside of test_* namespace - "create_test_aliases_test:\n" - + " indices:\n" - + " - names: 'test_*'\n" - + " privileges: [ create_index, 'indices:admin/aliases*' ]\n" - + - // role that has create index on test_* and manage_aliases on alias_*, can't create aliases pointing to test_* though - "create_test_aliases_alias:\n" - + " indices:\n" - + " - names: 'test_*'\n" - + " privileges: [ create_index ]\n" - + " - names: 'alias_*'\n" - + " privileges: [ 'indices:admin/aliases*' ]\n" - + - // role that has create index on test_* and manage_aliases on both alias_* and test_* - "create_test_aliases_test_alias:\n" - + " indices:\n" - + " - names: 'test_*'\n" - + " privileges: [ create_index ]\n" - + " - names: [ 'alias_*', 'test_*' ]\n" - + " privileges: [ 'indices:admin/aliases*' ]\n" - + - // role that has manage_aliases only on both test_* and alias_* - "aliases_only:\n" - + " indices:\n" - + " - names: [ 'alias_*', 'test_*']\n" - + " privileges: [ 'indices:admin/aliases*' ]\n"; + // role that has create index and manage_aliases on test_*, not enough to manage_aliases aliases outside of test_* namespace + // role that has create index on test_* and manage_aliases on alias_*, can't create aliases pointing to test_* though + // role that has create index on test_* and manage_aliases on both alias_* and test_* + // role that has manage_aliases only on both test_* and alias_* + return super.configRoles() + """ + + create_only: + indices: + - names: '*' + privileges: [ create_index ] + all_on_test: + indices: + - names: 'test_*' + privileges: [ all ] + create_test_aliases_test: + indices: + - names: 'test_*' + privileges: [ create_index, 'indices:admin/aliases*' ] + create_test_aliases_alias: + indices: + - names: 'test_*' + privileges: [ create_index ] + - names: 'alias_*' + privileges: [ 'indices:admin/aliases*' ] + create_test_aliases_test_alias: + indices: + - names: 'test_*' + privileges: [ create_index ] + - names: [ 'alias_*', 'test_*' ] + privileges: [ 'indices:admin/aliases*' ] + aliases_only: + indices: + - names: [ 'alias_*', 'test_*'] + privileges: [ 'indices:admin/aliases*' ] + """; } @Before diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java index f54543be98eff..b1b2376d364b3 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/ReadActionsTests.java @@ -39,14 +39,15 @@ public class ReadActionsTests extends SecurityIntegTestCase { @Override protected String configRoles() { - return SecuritySettingsSource.TEST_ROLE - + ":\n" - + " cluster: [ ALL ]\n" - + " indices:\n" - + " - names: '*'\n" - + " privileges: [ manage, write ]\n" - + " - names: ['/test.*/', '/-alias.*/']\n" - + " privileges: [ read ]\n"; + return """ + %s: + cluster: [ ALL ] + indices: + - names: '*' + privileges: [ manage, write ] + - names: ['/test.*/', '/-alias.*/'] + privileges: [ read ] + """.formatted(SecuritySettingsSource.TEST_ROLE); } public void testSearchForAll() { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java index 5ac35a9a8c935..4aecedf1e35e4 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/WriteActionsTests.java @@ -33,19 +33,19 @@ public class WriteActionsTests extends SecurityIntegTestCase { @Override protected String configRoles() { - return SecuritySettingsSource.TEST_ROLE - + ":\n" - + " cluster: [ ALL ]\n" - + " indices:\n" - + " - names: 'missing'\n" - + " privileges: [ 'indices:admin/create', 'indices:admin/auto_create', " - + "'indices:admin/delete' ]\n" - + " - names: ['/index.*/']\n" - + " privileges: [ manage ]\n" - + " - names: ['/test.*/']\n" - + " privileges: [ manage, write ]\n" - + " - names: '/test.*/'\n" - + " privileges: [ read ]\n"; + return """ + %s: + cluster: [ ALL ] + indices: + - names: 'missing' + privileges: [ 'indices:admin/create', 'indices:admin/auto_create', 'indices:admin/delete' ] + - names: ['/index.*/'] + privileges: [ manage ] + - names: ['/test.*/'] + privileges: [ manage, write ] + - names: '/test.*/' + privileges: [ read ] + """.formatted(SecuritySettingsSource.TEST_ROLE); } public void testIndex() { diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java index 328f4d28b7d0f..7617da3ce8e1d 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreCacheTests.java @@ -67,19 +67,20 @@ protected String configUsers() { @Override protected String configRoles() { - return super.configRoles() - + "app_role:\n" - + " cluster: ['monitor']\n" - + " indices:\n" - + " - names: ['*']\n" - + " privileges: ['read']\n" - + " applications:\n" - + " - application: 'app-1'\n" - + " privileges: ['read', 'check']\n" - + " resources: ['foo']\n" - + " - application: 'app-2'\n" - + " privileges: ['check']\n" - + " resources: ['foo']\n"; + return super.configRoles() + """ + app_role: + cluster: ['monitor'] + indices: + - names: ['*'] + privileges: ['read'] + applications: + - application: 'app-1' + privileges: ['read', 'check'] + resources: ['foo'] + - application: 'app-2' + privileges: ['check'] + resources: ['foo'] + """; } @Override diff --git a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java index f745d033ff306..35e978f59f925 100644 --- a/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java +++ b/x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/operator/OperatorPrivilegesSingleNodeTests.java @@ -38,12 +38,13 @@ protected String configUsers() { @Override protected String configRoles() { - return super.configRoles() - + "limited_operator:\n" - + " cluster:\n" - + " - 'cluster:admin/voting_config/clear_exclusions'\n" - + " - 'cluster:admin/settings/update'\n" - + " - 'monitor'\n"; + return super.configRoles() + """ + limited_operator: + cluster: + - 'cluster:admin/voting_config/clear_exclusions' + - 'cluster:admin/settings/update' + - 'monitor' + """; } @Override diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java index 9032e0c008cef..9356c850c6468 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/filter/SecurityActionFilter.java @@ -88,12 +88,10 @@ public void app - cluster:monitor/nodes/stats* */ if (licenseState.isActive() == false && LICENSE_EXPIRATION_ACTION_MATCHER.test(action)) { - logger.error( - "blocking [{}] operation due to expired license. Cluster health, cluster stats and indices stats \n" - + "operations are blocked on license expiration. All data operations (read and write) continue to work. \n" - + "If you have a new license, please update it. Otherwise, please reach out to your support contact.", - action - ); + logger.error(""" + blocking [{}] operation due to expired license. Cluster health, cluster stats and indices stats\s + operations are blocked on license expiration. All data operations (read and write) continue to work.\s + If you have a new license, please update it. Otherwise, please reach out to your support contact.""", action); throw LicenseUtils.newComplianceException(XPackField.SECURITY); } diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java index 8a2cdc37f272e..63128c1dd9918 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ApiKeyService.java @@ -202,15 +202,42 @@ public class ApiKeyService { // This following fixed role descriptor is for fleet-server BWC on and before 7.14. // It is fixed and must NOT be updated when the fleet-server service account updates. - private static final BytesArray FLEET_SERVER_ROLE_DESCRIPTOR_BYTES_V_7_14 = new BytesArray( - "{\"elastic/fleet-server\":{\"cluster\":[\"monitor\",\"manage_own_api_key\"]," - + "\"indices\":[{\"names\":[\"logs-*\",\"metrics-*\",\"traces-*\",\"synthetics-*\"," - + "\".logs-endpoint.diagnostic.collection-*\"]," - + "\"privileges\":[\"write\",\"create_index\",\"auto_configure\"],\"allow_restricted_indices\":false}," - + "{\"names\":[\".fleet-*\"],\"privileges\":[\"read\",\"write\",\"monitor\",\"create_index\",\"auto_configure\"]," - + "\"allow_restricted_indices\":false}],\"applications\":[],\"run_as\":[],\"metadata\":{}," - + "\"transient_metadata\":{\"enabled\":true}}}" - ); + private static final BytesArray FLEET_SERVER_ROLE_DESCRIPTOR_BYTES_V_7_14 = new BytesArray(""" + { + "elastic/fleet-server": { + "cluster": [ "monitor", "manage_own_api_key" ], + "indices": [ + { + "names": [ + "logs-*", + "metrics-*", + "traces-*", + "synthetics-*", + ".logs-endpoint.diagnostic.collection-*" + ], + "privileges": [ "write", "create_index", "auto_configure" ], + "allow_restricted_indices": false + }, + { + "names": [ ".fleet-*" ], + "privileges": [ + "read", + "write", + "monitor", + "create_index", + "auto_configure" + ], + "allow_restricted_indices": false + } + ], + "applications": [], + "run_as": [], + "metadata": {}, + "transient_metadata": { + "enabled": true + } + } + }"""); private final Clock clock; private final Client client; diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java index cab1c8fe508a4..e8996062f66aa 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/TokenService.java @@ -175,12 +175,10 @@ public final class TokenService { static final int IV_BYTES = 12; private static final int VERSION_BYTES = 4; private static final String ENCRYPTION_CIPHER = "AES/GCM/NoPadding"; - private static final String EXPIRED_TOKEN_WWW_AUTH_VALUE = "Bearer realm=\"" - + XPackField.SECURITY - + "\", error=\"invalid_token\", error_description=\"The access token expired\""; - private static final String MALFORMED_TOKEN_WWW_AUTH_VALUE = "Bearer realm=\"" - + XPackField.SECURITY - + "\", error=\"invalid_token\", error_description=\"The access token is malformed\""; + private static final String EXPIRED_TOKEN_WWW_AUTH_VALUE = """ + Bearer realm="%s", error="invalid_token", error_description="The access token expired\"""".formatted(XPackField.SECURITY); + private static final String MALFORMED_TOKEN_WWW_AUTH_VALUE = """ + Bearer realm="%s", error="invalid_token", error_description="The access token is malformed\"""".formatted(XPackField.SECURITY); private static final BackoffPolicy DEFAULT_BACKOFF = BackoffPolicy.exponentialBackoff(); public static final String THREAD_POOL_NAME = XPackField.SECURITY + "-token-key"; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java index fcfa47f80720b..cc946acc90d6c 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/test/SecuritySettingsSource.java @@ -98,13 +98,14 @@ public class SecuritySettingsSource extends NodeConfigurationSource { + TEST_SUPERUSER + "\n"; - public static final String CONFIG_ROLE_ALLOW_ALL = TEST_ROLE - + ":\n" - + " cluster: [ ALL ]\n" - + " indices:\n" - + " - names: '*'\n" - + " allow_restricted_indices: true\n" - + " privileges: [ ALL ]\n"; + public static final String CONFIG_ROLE_ALLOW_ALL = """ + %s: + cluster: [ ALL ] + indices: + - names: '*' + allow_restricted_indices: true + privileges: [ ALL ] + """.formatted(TEST_ROLE); private final Path parentFolder; private final String subfolderPrefix; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java index c8e763d0911ce..b2824323651f0 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/core/security/action/privilege/PutPrivilegesRequestBuilderTests.java @@ -25,23 +25,43 @@ public class PutPrivilegesRequestBuilderTests extends ESTestCase { public void testBuildRequestWithMultipleElements() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - builder.source( - new BytesArray( - "{ " - + "\"foo\":{" - + " \"read\":{ \"application\":\"foo\", \"name\":\"read\", \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," - + " \"write\":{ \"application\":\"foo\", \"name\":\"write\", \"actions\":[ \"data:/write/*\", \"admin:*\" ] }," - + " \"all\":{ \"application\":\"foo\", \"name\":\"all\", \"actions\":[ \"*\" ] }" - + " }, " - + "\"bar\":{" - + " \"read\":{ \"application\":\"bar\", \"name\":\"read\", \"actions\":[ \"read/*\" ] }," - + " \"write\":{ \"application\":\"bar\", \"name\":\"write\", \"actions\":[ \"write/*\" ] }," - + " \"all\":{ \"application\":\"bar\", \"name\":\"all\", \"actions\":[ \"*\" ] }" - + " } " - + "}" - ), - XContentType.JSON - ); + builder.source(new BytesArray(""" + { + "foo": { + "read": { + "application": "foo", + "name": "read", + "actions": [ "data:/read/*", "admin:/read/*" ] + }, + "write": { + "application": "foo", + "name": "write", + "actions": [ "data:/write/*", "admin:*" ] + }, + "all": { + "application": "foo", + "name": "all", + "actions": [ "*" ] + } + }, + "bar": { + "read": { + "application": "bar", + "name": "read", + "actions": [ "read/*" ] + }, + "write": { + "application": "bar", + "name": "write", + "actions": [ "write/*" ] + }, + "all": { + "application": "bar", + "name": "all", + "actions": [ "*" ] + } + } + }"""), XContentType.JSON); final List privileges = builder.request().getPrivileges(); assertThat(privileges, iterableWithSize(6)); assertThat( @@ -63,53 +83,67 @@ private ApplicationPrivilegeDescriptor descriptor(String app, String name, Strin public void testPrivilegeNameValidationOfMultipleElement() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - final IllegalArgumentException exception = expectThrows( - IllegalArgumentException.class, - () -> builder.source( - new BytesArray( - "{ \"foo\":{" - + "\"write\":{ \"application\":\"foo\", \"name\":\"read\", \"actions\":[\"data:/read/*\",\"admin:/read/*\"] }," - + "\"all\":{ \"application\":\"foo\", \"name\":\"all\", \"actions\":[ \"/*\" ] }" - + "} }" - ), - XContentType.JSON - ) - ); + final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> builder.source(new BytesArray(""" + { + "foo": { + "write": { + "application": "foo", + "name": "read", + "actions": [ "data:/read/*", "admin:/read/*" ] + }, + "all": { + "application": "foo", + "name": "all", + "actions": [ "/*" ] + } + } + }"""), XContentType.JSON)); assertThat(exception.getMessage(), containsString("write")); assertThat(exception.getMessage(), containsString("read")); } public void testApplicationNameValidationOfMultipleElement() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - final IllegalArgumentException exception = expectThrows( - IllegalArgumentException.class, - () -> builder.source( - new BytesArray( - "{ \"bar\":{" - + "\"read\":{ \"application\":\"foo\", \"name\":\"read\", \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," - + "\"write\":{ \"application\":\"foo\", \"name\":\"write\", \"actions\":[ \"data:/write/*\", \"admin:/*\" ] }," - + "\"all\":{ \"application\":\"foo\", \"name\":\"all\", \"actions\":[ \"/*\" ] }" - + "} }" - ), - XContentType.JSON - ) - ); + final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> builder.source(new BytesArray(""" + { + "bar": { + "read": { + "application": "foo", + "name": "read", + "actions": [ "data:/read/*", "admin:/read/*" ] + }, + "write": { + "application": "foo", + "name": "write", + "actions": [ "data:/write/*", "admin:/*" ] + }, + "all": { + "application": "foo", + "name": "all", + "actions": [ "/*" ] + } + } + }"""), XContentType.JSON)); assertThat(exception.getMessage(), containsString("bar")); assertThat(exception.getMessage(), containsString("foo")); } public void testInferApplicationNameAndPrivilegeName() throws Exception { final PutPrivilegesRequestBuilder builder = new PutPrivilegesRequestBuilder(null); - builder.source( - new BytesArray( - "{ \"foo\":{" - + "\"read\":{ \"actions\":[ \"data:/read/*\", \"admin:/read/*\" ] }," - + "\"write\":{ \"actions\":[ \"data:/write/*\", \"admin:/*\" ] }," - + "\"all\":{ \"actions\":[ \"*\" ] }" - + "} }" - ), - XContentType.JSON - ); + builder.source(new BytesArray(""" + { + "foo": { + "read": { + "actions": [ "data:/read/*", "admin:/read/*" ] + }, + "write": { + "actions": [ "data:/write/*", "admin:/*" ] + }, + "all": { + "actions": [ "*" ] + } + } + }"""), XContentType.JSON); assertThat(builder.request().getPrivileges(), iterableWithSize(3)); for (ApplicationPrivilegeDescriptor p : builder.request().getPrivileges()) { assertThat(p.getApplication(), equalTo("foo")); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java index 9a516e75a6975..061bc22c28fe8 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/SecurityContextTests.java @@ -208,11 +208,13 @@ public void testExecuteAfterRewritingAuthenticationWillConditionallyRewriteOldAp securityContext.executeAfterRewritingAuthentication(originalCtx -> { Authentication authentication = securityContext.getAuthentication(); assertEquals( - "{\"a role\":{\"cluster\":[\"all\"]}}", + """ + {"a role":{"cluster":["all"]}}""", ((BytesReference) authentication.getMetadata().get(AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY)).utf8ToString() ); assertEquals( - "{\"limitedBy role\":{\"cluster\":[\"all\"]}}", + """ + {"limitedBy role":{"cluster":["all"]}}""", ((BytesReference) authentication.getMetadata().get(AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY)).utf8ToString() ); }, VersionUtils.randomVersionBetween(random(), VERSION_API_KEY_ROLES_AS_BYTES, Version.CURRENT)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java index 2988c880aa0ec..6b8da84b7ebfd 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/role/PutRoleBuilderTests.java @@ -31,13 +31,9 @@ public void testBWCFieldPermissions() throws Exception { ElasticsearchParseException.class, () -> new PutRoleRequestBuilder(client).source("role1", new BytesArray(roleString), XContentType.JSON) ); - assertThat( - e.getDetailedMessage(), - containsString( - "\"fields\": [...]] format has changed for field permissions in role " - + "[role1], use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" - ) - ); + assertThat(e.getDetailedMessage(), containsString(""" + "fields": [...]] format has changed for field permissions in role [role1], \ + use ["field_security": {"grant":[...],"except":[...]}] instead""")); } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java index c0a9d1c239dae..326dbbdee224d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/ChangePasswordRequestBuilderTests.java @@ -32,7 +32,10 @@ public class ChangePasswordRequestBuilderTests extends ESTestCase { public void testWithCleartextPassword() throws IOException { final Hasher hasher = getFastStoredHashAlgoForTests(); - final String json = "{\n" + " \"password\": \"superlongpassword\"" + "}"; + final String json = """ + { + "password": "superlongpassword" + }"""; ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); ChangePasswordRequest request = builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher) .request(); @@ -42,7 +45,10 @@ public void testWithCleartextPassword() throws IOException { public void testWithHashedPassword() throws IOException { final Hasher hasher = getFastStoredHashAlgoForTests(); final char[] hash = hasher.hash(new SecureString("superlongpassword".toCharArray())); - final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"" + "}"; + final String json = """ + { + "password_hash": "%s" + }""".formatted(new String(hash)); ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); ChangePasswordRequest request = builder.source(new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, hasher) .request(); @@ -56,7 +62,9 @@ public void testWithHashedPasswordWithWrongAlgo() { userHasher = getFastStoredHashAlgoForTests(); } final char[] hash = userHasher.hash(new SecureString("superlongpassword".toCharArray())); - final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"" + "}"; + final String json = """ + {"password_hash": "%s"} + """.formatted(new String(hash)); ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, @@ -69,7 +77,10 @@ public void testWithHashedPasswordWithWrongAlgo() { public void testWithHashedPasswordNotHash() { final Hasher systemHasher = getFastStoredHashAlgoForTests(); final char[] hash = randomAlphaOfLength(20).toCharArray(); - final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"" + "}"; + final String json = """ + { + "password_hash": "%s" + }""".formatted(new String(hash)); ChangePasswordRequestBuilder builder = new ChangePasswordRequestBuilder(mock(Client.class)); final IllegalArgumentException e = expectThrows( IllegalArgumentException.class, diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java index 0e7c690cea1d3..d77ddfe2720ab 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/HasPrivilegesRequestBuilderTests.java @@ -27,15 +27,20 @@ public class HasPrivilegesRequestBuilderTests extends ESTestCase { public void testParseValidJsonWithClusterAndIndexPrivileges() throws Exception { - String json = "{ " - + " \"cluster\":[ \"all\"]," - + " \"index\":[ " - + " { \"names\": [ \".kibana\", \".reporting\" ], " - + " \"privileges\" : [ \"read\", \"write\" ] }, " - + " { \"names\": [ \".security\" ], " - + " \"privileges\" : [ \"manage\" ] } " - + " ]" - + "}"; + String json = """ + { + "cluster": [ "all" ], + "index": [ + { + "names": [ ".kibana", ".reporting" ], + "privileges": [ "read", "write" ] + }, + { + "names": [ ".security" ], + "privileges": [ "manage" ] + } + ] + }"""; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); @@ -56,12 +61,19 @@ public void testParseValidJsonWithClusterAndIndexPrivileges() throws Exception { } public void testParseValidJsonWithJustIndexPrivileges() throws Exception { - String json = "{ \"index\":[ " - + "{ \"names\": [ \".kibana\", \".reporting\" ], " - + " \"privileges\" : [ \"read\", \"write\" ] }, " - + "{ \"names\": [ \".security\" ], " - + " \"privileges\" : [ \"manage\" ] } " - + "] }"; + String json = """ + { + "index": [ + { + "names": [ ".kibana", ".reporting" ], + "privileges": [ "read", "write" ] + }, + { + "names": [ ".security" ], + "privileges": [ "manage" ] + } + ] + }"""; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); @@ -80,15 +92,8 @@ public void testParseValidJsonWithJustIndexPrivileges() throws Exception { } public void testParseValidJsonWithJustClusterPrivileges() throws Exception { - String json = "{ \"cluster\":[ " - + "\"manage\"," - + "\"" - + ClusterHealthAction.NAME - + "\"," - + "\"" - + ClusterStatsAction.NAME - + "\"" - + "] }"; + String json = """ + { "cluster": [ "manage","%s","%s"] }""".formatted(ClusterHealthAction.NAME, ClusterStatsAction.NAME); final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); builder.source("elastic", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON); @@ -99,12 +104,18 @@ public void testParseValidJsonWithJustClusterPrivileges() throws Exception { } public void testUseOfFieldLevelSecurityThrowsException() throws Exception { - String json = "{ \"index\":[ " - + "{" - + " \"names\": [ \"employees\" ], " - + " \"privileges\" : [ \"read\", \"write\" ] ," - + " \"field_security\": { \"grant\": [ \"name\", \"department\", \"title\" ] }" - + "} ] }"; + String json = """ + { + "index": [ + { + "names": [ "employees" ], + "privileges": [ "read", "write" ], + "field_security": { + "grant": [ "name", "department", "title" ] + } + } + ] + }"""; final HasPrivilegesRequestBuilder builder = new HasPrivilegesRequestBuilder(mock(Client.class)); final ElasticsearchParseException parseException = expectThrows( diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java index a58c898d0c2fc..777299bc2ad5f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/user/PutUserRequestBuilderTests.java @@ -35,14 +35,15 @@ public class PutUserRequestBuilderTests extends ESTestCase { public void testNullValuesForEmailAndFullName() throws IOException { - final String json = "{\n" - + " \"roles\": [\n" - + " \"kibana4\"\n" - + " ],\n" - + " \"full_name\": null,\n" - + " \"email\": null,\n" - + " \"metadata\": {}\n" - + "}"; + final String json = """ + { + "roles": [ + "kibana4" + ], + "full_name": null, + "email": null, + "metadata": {} + }"""; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT); @@ -57,7 +58,13 @@ public void testNullValuesForEmailAndFullName() throws IOException { } public void testMissingEmailFullName() throws Exception { - final String json = "{\n" + " \"roles\": [\n" + " \"kibana4\"\n" + " ],\n" + " \"metadata\": {}\n" + "}"; + final String json = """ + { + "roles": [ + "kibana4" + ], + "metadata": {} + }"""; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT); @@ -71,14 +78,15 @@ public void testMissingEmailFullName() throws Exception { } public void testWithFullNameAndEmail() throws IOException { - final String json = "{\n" - + " \"roles\": [\n" - + " \"kibana4\"\n" - + " ],\n" - + " \"full_name\": \"Kibana User\",\n" - + " \"email\": \"kibana@elastic.co\",\n" - + " \"metadata\": {}\n" - + "}"; + final String json = """ + { + "roles": [ + "kibana4" + ], + "full_name": "Kibana User", + "email": "kibana@elastic.co", + "metadata": {} + }"""; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); builder.source("kibana4", new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON, Hasher.BCRYPT); @@ -92,14 +100,15 @@ public void testWithFullNameAndEmail() throws IOException { } public void testInvalidFullname() throws IOException { - final String json = "{\n" - + " \"roles\": [\n" - + " \"kibana4\"\n" - + " ],\n" - + " \"full_name\": [ \"Kibana User\" ],\n" - + " \"email\": \"kibana@elastic.co\",\n" - + " \"metadata\": {}\n" - + "}"; + final String json = """ + { + "roles": [ + "kibana4" + ], + "full_name": [ "Kibana User" ], + "email": "kibana@elastic.co", + "metadata": {} + }"""; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); ElasticsearchParseException e = expectThrows( @@ -110,14 +119,15 @@ public void testInvalidFullname() throws IOException { } public void testInvalidEmail() throws IOException { - final String json = "{\n" - + " \"roles\": [\n" - + " \"kibana4\"\n" - + " ],\n" - + " \"full_name\": \"Kibana User\",\n" - + " \"email\": [ \"kibana@elastic.co\" ],\n" - + " \"metadata\": {}\n" - + "}"; + final String json = """ + { + "roles": [ + "kibana4" + ], + "full_name": "Kibana User", + "email": [ "kibana@elastic.co" ], + "metadata": {} + }"""; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); ElasticsearchParseException e = expectThrows( @@ -128,15 +138,16 @@ public void testInvalidEmail() throws IOException { } public void testWithEnabled() throws IOException { - final String json = "{\n" - + " \"roles\": [\n" - + " \"kibana4\"\n" - + " ],\n" - + " \"full_name\": \"Kibana User\",\n" - + " \"email\": \"kibana@elastic.co\",\n" - + " \"metadata\": {}\n," - + " \"enabled\": false\n" - + "}"; + final String json = """ + { + "roles": [ + "kibana4" + ], + "full_name": "Kibana User", + "email": "kibana@elastic.co", + "metadata": {} + , "enabled": false + }"""; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); PutUserRequest request = builder.source( @@ -151,7 +162,11 @@ public void testWithEnabled() throws IOException { public void testWithValidPasswordHash() throws IOException { final Hasher hasher = getFastStoredHashAlgoForTests(); final char[] hash = hasher.hash(new SecureString("secretpassword".toCharArray())); - final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"," + " \"roles\": []\n" + "}"; + final String json = """ + { + "password_hash": "%s", + "roles": [] + }""".formatted(new String(hash)); PutUserRequestBuilder requestBuilder = new PutUserRequestBuilder(mock(Client.class)); PutUserRequest request = requestBuilder.source( @@ -171,7 +186,11 @@ public void testWithMismatchedPasswordHashingAlgorithm() throws IOException { userHasher = getFastStoredHashAlgoForTests(); } final char[] hash = userHasher.hash(new SecureString("secretpassword".toCharArray())); - final String json = "{\n" + " \"password_hash\": \"" + new String(hash) + "\"," + " \"roles\": []\n" + "}"; + final String json = """ + { + "password_hash": "%s", + "roles": [] + }""".formatted(new String(hash)); PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); final IllegalArgumentException ex = expectThrows( @@ -187,7 +206,11 @@ public void testWithMismatchedPasswordHashingAlgorithm() throws IOException { public void testWithPasswordHashThatsNotReallyAHash() throws IOException { final Hasher systemHasher = Hasher.PBKDF2; - final String json = "{\n" + " \"password_hash\": \"not-a-hash\"," + " \"roles\": []\n" + "}"; + final String json = """ + { + "password_hash": "not-a-hash", + "roles": [] + }"""; PutUserRequestBuilder builder = new PutUserRequestBuilder(mock(Client.class)); final IllegalArgumentException ex = expectThrows( diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java index 25682e7fc7fda..e41447b1a077e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java @@ -563,13 +563,9 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest(keyName, keyRoleDescriptors, expiration); createApiKeyRequest.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())); auditTrail.accessGranted(requestId, authentication, CreateApiKeyAction.NAME, createApiKeyRequest, authorizationInfo); - String expectedCreateKeyAuditEventString = "\"create\":{\"apikey\":{\"name\":\"" - + keyName - + "\",\"expiration\":" - + (expiration != null ? "\"" + expiration.toString() + "\"" : "null") - + "," - + roleDescriptorsStringBuilder - + "}}"; + String expectedCreateKeyAuditEventString = """ + "create":{"apikey":{"name":"%s","expiration":%s,%s}}\ + """.formatted(keyName, expiration != null ? "\"" + expiration + "\"" : "null", roleDescriptorsStringBuilder); List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedCreateKeyAuditEventString = output.get(1); @@ -648,12 +644,9 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedPutRoleAuditEventString = output.get(1); - String expectedPutRoleAuditEventString = "\"put\":{\"role\":{\"name\":\"" - + putRoleRequest.name() - + "\"," - + "\"role_descriptor\":" - + auditedRolesMap.get(putRoleRequest.name()) - + "}}"; + String expectedPutRoleAuditEventString = """ + "put":{"role":{"name":"%s","role_descriptor":%s}}\ + """.formatted(putRoleRequest.name(), auditedRolesMap.get(putRoleRequest.name())); assertThat(generatedPutRoleAuditEventString, containsString(expectedPutRoleAuditEventString)); generatedPutRoleAuditEventString = generatedPutRoleAuditEventString.replace(", " + expectedPutRoleAuditEventString, ""); checkedFields = new MapBuilder<>(commonFields); @@ -712,7 +705,8 @@ public void testSecurityConfigChangeEventFormattingForApiKeyInvalidation() throw List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedInvalidateKeyAuditEventString = output.get(1); - StringBuilder invalidateKeyEventStringBuilder = new StringBuilder().append("\"invalidate\":{\"apikeys\":{"); + StringBuilder invalidateKeyEventStringBuilder = new StringBuilder().append(""" + "invalidate":{"apikeys":{"""); if (invalidateApiKeyRequest.getIds() != null && invalidateApiKeyRequest.getIds().length > 0) { invalidateKeyEventStringBuilder.append("\"ids\":["); for (String apiKeyId : invalidateApiKeyRequest.getIds()) { @@ -728,7 +722,8 @@ public void testSecurityConfigChangeEventFormattingForApiKeyInvalidation() throw invalidateKeyEventStringBuilder.append("\"owned_by_authenticated_user\":") .append(invalidateApiKeyRequest.ownedByAuthenticatedUser()); if (Strings.hasLength(invalidateApiKeyRequest.getUserName()) || Strings.hasLength(invalidateApiKeyRequest.getRealmName())) { - invalidateKeyEventStringBuilder.append(",\"user\":{\"name\":"); + invalidateKeyEventStringBuilder.append(""" + ,"user":{"name":"""); if (Strings.hasLength(invalidateApiKeyRequest.getUserName())) { invalidateKeyEventStringBuilder.append("\"").append(invalidateApiKeyRequest.getUserName()).append("\""); } else { @@ -769,11 +764,13 @@ public void testSecurityConfigChangeEventFormattingForApplicationPrivileges() th if (randomBoolean()) { metadata.put("test", true); metadata.put("ans", 42); - serializedMetadata = ",\"metadata\":{\"ans\":42,\"test\":true}"; + serializedMetadata = """ + ,"metadata":{"ans":42,"test":true}"""; } else { metadata.put("ans", List.of(42, true)); metadata.put("other", Map.of("42", true)); - serializedMetadata = ",\"metadata\":{\"ans\":[42,true],\"other\":{\"42\":true}}"; + serializedMetadata = """ + ,"metadata":{"ans":[42,true],"other":{"42":true}}"""; } PutPrivilegesRequest putPrivilegesRequest = new PutPrivilegesRequest(); @@ -786,7 +783,8 @@ public void testSecurityConfigChangeEventFormattingForApplicationPrivileges() th List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedPutPrivilegesAuditEventString = output.get(1); - StringBuilder putPrivilegesAuditEventStringBuilder = new StringBuilder().append("\"put\":{\"privileges\":["); + StringBuilder putPrivilegesAuditEventStringBuilder = new StringBuilder().append(""" + "put":{"privileges":["""); if (false == putPrivilegesRequest.getPrivileges().isEmpty()) { for (ApplicationPrivilegeDescriptor appPriv : putPrivilegesRequest.getPrivileges()) { putPrivilegesAuditEventStringBuilder.append("{\"application\":\"") @@ -837,7 +835,8 @@ public void testSecurityConfigChangeEventFormattingForApplicationPrivileges() th output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDeletePrivilegesAuditEventString = output.get(1); - StringBuilder deletePrivilegesAuditEventStringBuilder = new StringBuilder().append("\"delete\":{\"privileges\":{\"application\":"); + StringBuilder deletePrivilegesAuditEventStringBuilder = new StringBuilder().append(""" + "delete":{"privileges":{"application":"""); if (deletePrivilegesRequest.application() != null) { deletePrivilegesAuditEventStringBuilder.append("\"").append(deletePrivilegesRequest.application()).append("\""); } else { @@ -940,7 +939,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws List output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedPutRoleMappingAuditEventString = output.get(1); - StringBuilder putRoleMappingAuditEventStringBuilder = new StringBuilder().append("\"put\":{\"role_mapping\":{\"name\":"); + StringBuilder putRoleMappingAuditEventStringBuilder = new StringBuilder().append(""" + "put":{"role_mapping":{"name":"""); if (putRoleMappingRequest.getName() != null) { putRoleMappingAuditEventStringBuilder.append("\"").append(putRoleMappingRequest.getName()).append("\""); } else { @@ -969,13 +969,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws putRoleMappingAuditEventStringBuilder.append("]"); } if (hasRules) { - putRoleMappingAuditEventStringBuilder.append(",\"rules\":{\"mock\":\"A mock role mapper expression\"}"); + putRoleMappingAuditEventStringBuilder.append(""" + ,"rules":{"mock":"A mock role mapper expression"}"""); } else { - putRoleMappingAuditEventStringBuilder.append(",\"rules\":null"); + putRoleMappingAuditEventStringBuilder.append(""" + ,"rules":null"""); } - putRoleMappingAuditEventStringBuilder.append(",\"enabled\":").append(putRoleMappingRequest.isEnabled()); + putRoleMappingAuditEventStringBuilder.append(""" + ,"enabled":""").append(putRoleMappingRequest.isEnabled()); if (hasMetadata) { - putRoleMappingAuditEventStringBuilder.append(",\"metadata\":{\"list\":[\"42\",13],\"smth\":42}}}"); + putRoleMappingAuditEventStringBuilder.append(""" + ,"metadata":{"list":["42",13],"smth":42}}}"""); } else { putRoleMappingAuditEventStringBuilder.append("}}"); } @@ -1003,7 +1007,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDeleteRoleMappingAuditEventString = output.get(1); - StringBuilder deleteRoleMappingStringBuilder = new StringBuilder().append("\"delete\":{\"role_mapping\":{\"name\":"); + StringBuilder deleteRoleMappingStringBuilder = new StringBuilder().append(""" + "delete":{"role_mapping":{"name":"""); if (deleteRoleMappingRequest.getName() == null) { deleteRoleMappingStringBuilder.append("null"); } else { @@ -1060,7 +1065,8 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException assertThat(output.size(), is(2)); String generatedPutUserAuditEventString = output.get(1); - StringBuilder putUserAuditEventStringBuilder = new StringBuilder().append("\"put\":{\"user\":{\"name\":") + StringBuilder putUserAuditEventStringBuilder = new StringBuilder().append(""" + "put":{"user":{"name":""") .append("\"") .append(putUserRequest.username()) .append("\"") @@ -1088,7 +1094,8 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException } putUserAuditEventStringBuilder.append(",\"has_password\":").append(putUserRequest.passwordHash() != null); if (hasMetadata) { - putUserAuditEventStringBuilder.append(",\"metadata\":{\"list\":[\"42\",13],\"smth\":42}}}"); + putUserAuditEventStringBuilder.append(""" + ,"metadata":{"list":["42",13],"smth":42}}}"""); } else { putUserAuditEventStringBuilder.append("}}"); } @@ -1115,7 +1122,9 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedEnableUserAuditEventString = output.get(1); - String expectedEnableUserAuditEventString = "\"change\":{\"enable\":{\"user\":{\"name\":\"" + username + "\"}}}"; + String expectedEnableUserAuditEventString = """ + "change":{"enable":{"user":{"name":"%s"}}}\ + """.formatted(username); assertThat(generatedEnableUserAuditEventString, containsString(expectedEnableUserAuditEventString)); generatedEnableUserAuditEventString = generatedEnableUserAuditEventString.replace(", " + expectedEnableUserAuditEventString, ""); checkedFields = new MapBuilder<>(commonFields); @@ -1138,7 +1147,9 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDisableUserAuditEventString = output.get(1); - String expectedDisableUserAuditEventString = "\"change\":{\"disable\":{\"user\":{\"name\":\"" + username + "\"}}}"; + String expectedDisableUserAuditEventString = """ + "change":{"disable":{"user":{"name":"%s"}}}\ + """.formatted(username); assertThat(generatedDisableUserAuditEventString, containsString(expectedDisableUserAuditEventString)); generatedDisableUserAuditEventString = generatedDisableUserAuditEventString.replace(", " + expectedDisableUserAuditEventString, ""); checkedFields = new MapBuilder<>(commonFields); @@ -1160,7 +1171,9 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedChangePasswordAuditEventString = output.get(1); - String expectedChangePasswordAuditEventString = "\"change\":{\"password\":{\"user\":{\"name\":\"" + username + "\"}}}"; + String expectedChangePasswordAuditEventString = """ + "change":{"password":{"user":{"name":"%s"}}}\ + """.formatted(username); assertThat(generatedChangePasswordAuditEventString, containsString(expectedChangePasswordAuditEventString)); generatedChangePasswordAuditEventString = generatedChangePasswordAuditEventString.replace( ", " + expectedChangePasswordAuditEventString, @@ -1184,7 +1197,9 @@ public void testSecurityConfigChangeEventFormattingForUsers() throws IOException output = CapturingLogger.output(logger.getName(), Level.INFO); assertThat(output.size(), is(2)); String generatedDeleteUserAuditEventString = output.get(1); - String expectedDeleteUserAuditEventString = "\"delete\":{\"user\":{\"name\":\"" + username + "\"}}"; + String expectedDeleteUserAuditEventString = """ + "delete":{"user":{"name":"%s"}}\ + """.formatted(username); assertThat(generatedDeleteUserAuditEventString, containsString(expectedDeleteUserAuditEventString)); generatedDeleteUserAuditEventString = generatedDeleteUserAuditEventString.replace(", " + expectedDeleteUserAuditEventString, ""); checkedFields = new MapBuilder<>(commonFields); @@ -1223,13 +1238,8 @@ public void testSecurityConfigChangeEventFormattingForServiceAccountToken() { assertThat(output.size(), is(2)); String generatedCreateServiceAccountTokenAuditEventString = output.get(1); - final String expectedCreateServiceAccountTokenAuditEventString = String.format( - Locale.ROOT, - "\"create\":{\"service_token\":{\"namespace\":\"%s\",\"service\":\"%s\",\"name\":\"%s\"}}", - namespace, - serviceName, - tokenName - ); + final String expectedCreateServiceAccountTokenAuditEventString = String.format(Locale.ROOT, """ + "create":{"service_token":{"namespace":"%s","service":"%s","name":"%s"}}""", namespace, serviceName, tokenName); assertThat(generatedCreateServiceAccountTokenAuditEventString, containsString(expectedCreateServiceAccountTokenAuditEventString)); generatedCreateServiceAccountTokenAuditEventString = generatedCreateServiceAccountTokenAuditEventString.replace( ", " + expectedCreateServiceAccountTokenAuditEventString, @@ -1263,13 +1273,8 @@ public void testSecurityConfigChangeEventFormattingForServiceAccountToken() { assertThat(output.size(), is(2)); String generatedDeleteServiceAccountTokenAuditEventString = output.get(1); - final String expectedDeleteServiceAccountTokenAuditEventString = String.format( - Locale.ROOT, - "\"delete\":{\"service_token\":{\"namespace\":\"%s\",\"service\":\"%s\",\"name\":\"%s\"}}", - namespace, - serviceName, - tokenName - ); + final String expectedDeleteServiceAccountTokenAuditEventString = String.format(Locale.ROOT, """ + "delete":{"service_token":{"namespace":"%s","service":"%s","name":"%s"}}""", namespace, serviceName, tokenName); assertThat(generatedDeleteServiceAccountTokenAuditEventString, containsString(expectedDeleteServiceAccountTokenAuditEventString)); generatedDeleteServiceAccountTokenAuditEventString = generatedDeleteServiceAccountTokenAuditEventString.replace( ", " + expectedDeleteServiceAccountTokenAuditEventString, diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java index 0caa4cff53802..bd2247a8d6a71 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ApiKeyServiceTests.java @@ -1298,13 +1298,38 @@ public void testCachedApiKeyValidationWillNotBeBlockedByUnCachedApiKey() throws @SuppressWarnings("unchecked") public void testApiKeyDocDeserialization() throws IOException { - final String apiKeyDocumentSource = - "{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":1591919944599,\"api_key_invalidated\":false," - + "\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{\"a\":{\"cluster\":[\"all\"]}}," - + "\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]," - + "\"metadata\":{\"_reserved\":true},\"type\":\"role\"}}," - + "\"name\":\"key-1\",\"version\":7000099," - + "\"creator\":{\"principal\":\"admin\",\"metadata\":{\"foo\":\"bar\"},\"realm\":\"file1\",\"realm_type\":\"file\"}}"; + final String apiKeyDocumentSource = """ + { + "doc_type": "api_key", + "creation_time": 1591919944598, + "expiration_time": 1591919944599, + "api_key_invalidated": false, + "api_key_hash": "{PBKDF2}10000$abc", + "role_descriptors": { + "a": { + "cluster": [ "all" ] + } + }, + "limited_by_role_descriptors": { + "limited_by": { + "cluster": [ "all" ], + "metadata": { + "_reserved": true + }, + "type": "role" + } + }, + "name": "key-1", + "version": 7000099, + "creator": { + "principal": "admin", + "metadata": { + "foo": "bar" + }, + "realm": "file1", + "realm_type": "file" + } + }"""; final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent( XContentHelper.createParser( NamedXContentRegistry.EMPTY, @@ -1320,11 +1345,10 @@ public void testApiKeyDocDeserialization() throws IOException { assertEquals("{PBKDF2}10000$abc", apiKeyDoc.hash); assertEquals("key-1", apiKeyDoc.name); assertEquals(7000099, apiKeyDoc.version); - assertEquals(new BytesArray("{\"a\":{\"cluster\":[\"all\"]}}"), apiKeyDoc.roleDescriptorsBytes); - assertEquals( - new BytesArray("{\"limited_by\":{\"cluster\":[\"all\"],\"metadata\":{\"_reserved\":true},\"type\":\"role\"}}"), - apiKeyDoc.limitedByRoleDescriptorsBytes - ); + assertEquals(new BytesArray(""" + {"a":{"cluster":["all"]}}"""), apiKeyDoc.roleDescriptorsBytes); + assertEquals(new BytesArray(""" + {"limited_by":{"cluster":["all"],"metadata":{"_reserved":true},"type":"role"}}"""), apiKeyDoc.limitedByRoleDescriptorsBytes); final Map creator = apiKeyDoc.creator; assertEquals("admin", creator.get("principal")); @@ -1334,12 +1358,27 @@ public void testApiKeyDocDeserialization() throws IOException { } public void testApiKeyDocDeserializationWithNullValues() throws IOException { - final String apiKeyDocumentSource = - "{\"doc_type\":\"api_key\",\"creation_time\":1591919944598,\"expiration_time\":null,\"api_key_invalidated\":false," - + "\"api_key_hash\":\"{PBKDF2}10000$abc\",\"role_descriptors\":{}," - + "\"limited_by_role_descriptors\":{\"limited_by\":{\"cluster\":[\"all\"]}}," - + "\"name\":null,\"version\":7000099," - + "\"creator\":{\"principal\":\"admin\",\"metadata\":{},\"realm\":\"file1\"}}"; + final String apiKeyDocumentSource = """ + { + "doc_type": "api_key", + "creation_time": 1591919944598, + "expiration_time": null, + "api_key_invalidated": false, + "api_key_hash": "{PBKDF2}10000$abc", + "role_descriptors": {}, + "limited_by_role_descriptors": { + "limited_by": { + "cluster": [ "all" ] + } + }, + "name": null, + "version": 7000099, + "creator": { + "principal": "admin", + "metadata": {}, + "realm": "file1" + } + }"""; final ApiKeyDoc apiKeyDoc = ApiKeyDoc.fromXContent( XContentHelper.createParser( NamedXContentRegistry.EMPTY, diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRealmTests.java index 552d9756729f1..16c60f0c7cfd0 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/ActiveDirectoryRealmTests.java @@ -438,21 +438,16 @@ public void testRealmWithTemplatedRoleMapping() throws Exception { NativeRoleMappingStore roleMapper = new NativeRoleMappingStore(settings, mockClient, mockSecurityIndex, scriptService) { @Override protected void loadMappings(ActionListener> listener) { - listener.onResponse( - Arrays.asList( - this.buildMapping( - "m1", - new BytesArray( - "{" - + "\"role_templates\":[{\"template\":{\"source\":\"_role_{{metadata.departmentNumber}}\"}}]," - + "\"enabled\":true," - + "\"rules\":{ " - + " \"field\":{\"realm.name\":\"testrealmwithtemplatedrolemapping\"}" - + "}}" - ) - ) - ) - ); + listener.onResponse(Arrays.asList(this.buildMapping("m1", new BytesArray(""" + { + "role_templates": [ { "template": { "source": "_role_{{metadata.departmentNumber}}" } } ], + "enabled": true, + "rules": { + "field": { + "realm.name": "testrealmwithtemplatedrolemapping" + } + } + }""")))); } }; LdapRealm realm = new LdapRealm(config, sessionFactory, roleMapper, threadPool); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java index 7647bc4dae9a8..742c5685db57d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapRealmTests.java @@ -484,45 +484,30 @@ public void testLdapRealmWithTemplatedRoleMapping() throws Exception { ) { @Override protected void loadMappings(ActionListener> listener) { - listener.onResponse( - Arrays.asList( - this.buildMapping( - "m1", - new BytesArray( - "{" - + "\"role_templates\":[{\"template\":{\"source\":\"_user_{{metadata.uid}}\"}}]," - + "\"enabled\":true," - + "\"rules\":{ \"any\":[" - + " { \"field\":{\"realm.name\":\"ldap1\"}}," - + " { \"field\":{\"realm.name\":\"ldap2\"}}" - + "]}}" - ) - ), - this.buildMapping( - "m2", - new BytesArray( - "{" - + "\"roles\":[\"should_not_happen\"]," - + "\"enabled\":true," - + "\"rules\":{ \"all\":[" - + " { \"field\":{\"realm.name\":\"ldap1\"}}," - + " { \"field\":{\"realm.name\":\"ldap2\"}}" - + "]}}" - ) - ), - this.buildMapping( - "m3", - new BytesArray( - "{" - + "\"roles\":[\"sales_admin\"]," - + "\"enabled\":true," - + "\"rules\":" - + " { \"field\":{\"dn\":\"*,ou=people,o=sevenSeas\"}}" - + "}" - ) - ) - ) - ); + listener.onResponse(Arrays.asList(this.buildMapping("m1", new BytesArray(""" + { + "role_templates": [ { "template": { "source": "_user_{{metadata.uid}}" } } ], + "enabled": true, + "rules": { + "any": [ { "field": { "realm.name": "ldap1" } }, { "field": { "realm.name": "ldap2" } } ] + } + }""")), this.buildMapping("m2", new BytesArray(""" + { + "roles": [ "should_not_happen" ], + "enabled": true, + "rules": { + "all": [ { "field": { "realm.name": "ldap1" } }, { "field": { "realm.name": "ldap2" } } ] + } + }""")), this.buildMapping("m3", new BytesArray(""" + { + "roles": [ "sales_admin" ], + "enabled": true, + "rules": { + "field": { + "dn": "*,ou=people,o=sevenSeas" + } + } + }""")))); } }; LdapSessionFactory ldapFactory = new LdapSessionFactory(config, sslService, threadPool); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java index 5c161a516a258..5c55552838801 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectTestCase.java @@ -102,29 +102,24 @@ protected RealmConfig buildConfig(Settings realmSettings, ThreadContext threadCo } public static void writeJwkSetToFile(Path file) throws IOException { - Files.write( - file, - Arrays.asList( - "{\n" - + " \"keys\": [\n" - + " {\n" - + " \"kty\": \"RSA\",\n" - + " \"d\": \"lT2V49RNsu0eTroQDqFCiHY-CkPWdKfKAf66sJrWPNpSX8URa6pTCruFQMsb9ZSqQ8eIvqys9I9rq6Wpaxn1aGRahVzxp7nsBPZYw" - + "SY09LRzhvAxJwWdwtF-ogrV5-p99W9mhEa0khot3myzzfWNnGzcf1IudqvkqE9zrlUJg-kvA3icbs6HgaZVAevb_mx-bgbtJdnUxyPGwXLyQ7g6hlntQ" - + "R_vpzTnK7XFU6fvkrojh7UPJkanKAH0gf3qPrB-Y2gQML7RSlKo-ZfJNHa83G4NRLHKuWTI6dSKJlqmS9zWGmyC3dx5kGjgqD6YgwtWlip8q-U839zxt" - + "z25yeslsQ\",\n" - + " \"e\": \"AQAB\",\n" - + " \"use\": \"sig\",\n" - + " \"kid\": \"testkey\",\n" - + " \"alg\": \"RS256\",\n" - + " \"n\": \"lXBe4UngWJiUfbqbeOvwbH04kYLCpeH4k0o3ngScZDo6ydc_gBDEVwPLQpi8D930aIzr3XHP3RCj0hnpxUun7MNMhWxJZVOd1eg5u" - + "uO-nPIhkqr9iGKV5srJk0Dvw0wBaGZuXMBheY2ViNaKTR9EEtjNwU2d2-I5U3YlrnFR6nj-Pn_hWaiCbb_pSFM4w9QpoLDmuwMRanHY_YK7Td2WMICSG" - + "P3IRGmbecRZCqgkWVZk396EMoMLNxi8WcErYknyY9r-QeJMruRkr27kgx78L7KZ9uBmu9oKXRQl15ZDYe7Bnt9E5wSdOCV9R9h5VRVUur-_129XkDeAX" - + "-6re63_Mw\"\n" - + " }\n" - + " ]\n" - + "}" - ) - ); + Files.write(file, Arrays.asList(""" + { + "keys": [ + { + "kty": "RSA", + "d": "lT2V49RNsu0eTroQDqFCiHY-CkPWdKfKAf66sJrWPNpSX8URa6pTCruFQMsb9ZSqQ8eIvqys9I9rq6Wpaxn1aGRahVzxp7nsBPZYwSY09L\ + RzhvAxJwWdwtF-ogrV5-p99W9mhEa0khot3myzzfWNnGzcf1IudqvkqE9zrlUJg-kvA3icbs6HgaZVAevb_mx-bgbtJdnUxyPGwXLyQ7g6hlntQR_vpzTnK\ + 7XFU6fvkrojh7UPJkanKAH0gf3qPrB-Y2gQML7RSlKo-ZfJNHa83G4NRLHKuWTI6dSKJlqmS9zWGmyC3dx5kGjgqD6YgwtWlip8q-U839zxtz25yeslsQ", + "e": "AQAB", + "use": "sig", + "kid": "testkey", + "alg": "RS256", + "n": "lXBe4UngWJiUfbqbeOvwbH04kYLCpeH4k0o3ngScZDo6ydc_gBDEVwPLQpi8D930aIzr3XHP3RCj0hnpxUun7MNMhWxJZVOd1eg5uuO-nP\ + Ihkqr9iGKV5srJk0Dvw0wBaGZuXMBheY2ViNaKTR9EEtjNwU2d2-I5U3YlrnFR6nj-Pn_hWaiCbb_pSFM4w9QpoLDmuwMRanHY_YK7Td2WMICSGP\ + 3IRGmbecRZCqgkWVZk396EMoMLNxi8WcErYknyY9r-QeJMruRkr27kgx78L7KZ9uBmu9oKXRQl15ZDYe7Bnt9E5wSdOCV9R9h5VRVUur-_129XkD\ + eAX-6re63_Mw" + } + ] + }""")); } } 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 aa6aa2e7cadb2..96a53fca4f352 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 @@ -1087,11 +1087,12 @@ public void testXXE() throws Exception { public void testBillionLaughsAttack() throws Exception { // There is no need to go up to N iterations - String xml = "\n" - + " \n" - + "]>\n" - + "&lol1;"; + String xml = """ + + + ]> + &lol1;"""; final SamlToken token = token(xml); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getCause(), instanceOf(SAXException.class)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java index f138118954067..b44a5ed32a400 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlLogoutResponseHandlerHttpPostTests.java @@ -66,17 +66,18 @@ public void testHandlerWillThrowWhenStatusIsNotSuccess() throws Exception { } private String buildLogoutResponsePayload(Map data, boolean shouldSign) throws Exception { - final String template = "\n" - + "\n" - + " %(IDP_ENTITY_ID)\n" - + " \n" - + " \n" - + " \n" - + ""; + final String template = """ + + + %(IDP_ENTITY_ID) + + + + """; Map replacements = new HashMap<>(data); replacements.putIfAbsent("IDP_ENTITY_ID", IDP_ENTITY_ID); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java index eab7e0ca40470..aa5ea2405f0e2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/TokensInvalidationResultTests.java @@ -9,6 +9,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xcontent.ToXContent; @@ -36,31 +37,30 @@ public void testToXcontent() throws Exception { try (XContentBuilder builder = JsonXContent.contentBuilder()) { result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat( - Strings.toString(builder), - equalTo( - "{\"invalidated_tokens\":2," - + "\"previously_invalidated_tokens\":2," - + "\"error_count\":2," - + "\"error_details\":[" - + "{\"type\":\"exception\"," - + "\"reason\":\"foo\"," - + "\"caused_by\":{" - + "\"type\":\"illegal_state_exception\"," - + "\"reason\":\"bar\"" - + "}" - + "}," - + "{\"type\":\"exception\"," - + "\"reason\":\"boo\"," - + "\"caused_by\":{" - + "\"type\":\"illegal_state_exception\"," - + "\"reason\":\"far\"" - + "}" - + "}" - + "]" - + "}" - ) - ); + assertThat(Strings.toString(builder), equalTo(XContentHelper.stripWhitespace(""" + { + "invalidated_tokens": 2, + "previously_invalidated_tokens": 2, + "error_count": 2, + "error_details": [ + { + "type": "exception", + "reason": "foo", + "caused_by": { + "type": "illegal_state_exception", + "reason": "bar" + } + }, + { + "type": "exception", + "reason": "boo", + "caused_by": { + "type": "illegal_state_exception", + "reason": "far" + } + } + ] + }"""))); } } @@ -73,10 +73,8 @@ public void testToXcontentWithNoErrors() throws Exception { ); try (XContentBuilder builder = JsonXContent.contentBuilder()) { result.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat( - Strings.toString(builder), - equalTo("{\"invalidated_tokens\":2," + "\"previously_invalidated_tokens\":0," + "\"error_count\":0" + "}") - ); + assertThat(Strings.toString(builder), equalTo(""" + {"invalidated_tokens":2,"previously_invalidated_tokens":0,"error_count":0}""")); } } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java index 9cf64e1d9e48c..7cf5c9823e2cd 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/mapper/ExpressionRoleMappingTests.java @@ -18,6 +18,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.VersionUtils; @@ -69,15 +70,27 @@ public void setupMapping() throws Exception { } public void testValidExpressionWithFixedRoleNames() throws Exception { - String json = "{" - + "\"roles\": [ \"kibana_user\", \"sales\" ], " - + "\"enabled\": true, " - + "\"rules\": { " - + " \"all\": [ " - + " { \"field\": { \"dn\" : \"*,ou=sales,dc=example,dc=com\" } }, " - + " { \"except\": { \"field\": { \"metadata.active\" : false } } }" - + " ]}" - + "}"; + String json = """ + { + "roles": [ "kibana_user", "sales" ], + "enabled": true, + "rules": { + "all": [ + { + "field": { + "dn": "*,ou=sales,dc=example,dc=com" + } + }, + { + "except": { + "field": { + "metadata.active": false + } + } + } + ] + } + }"""; ExpressionRoleMapping mapping = parse(json, "ldap_sales"); assertThat(mapping.getRoles(), Matchers.containsInAnyOrder("kibana_user", "sales")); assertThat(mapping.getExpression(), instanceOf(AllExpression.class)); @@ -137,15 +150,25 @@ public void testValidExpressionWithFixedRoleNames() throws Exception { assertThat(mapping.getExpression().match(user4.asModel()), equalTo(false)); // dn == null // expression without dn - json = "{" - + "\"roles\": [ \"superuser\", \"system_admin\", \"admin\" ], " - + "\"enabled\": true, " - + "\"rules\": { " - + " \"any\": [ " - + " { \"field\": { \"username\" : \"tony.stark\" } }, " - + " { \"field\": { \"groups\": \"cn=admins,dc=stark-enterprises,dc=com\" } }" - + " ]}" - + "}"; + json = """ + { + "roles": [ "superuser", "system_admin", "admin" ], + "enabled": true, + "rules": { + "any": [ + { + "field": { + "username": "tony.stark" + } + }, + { + "field": { + "groups": "cn=admins,dc=stark-enterprises,dc=com" + } + } + ] + } + }"""; mapping = parse(json, "stark_admin"); assertThat(mapping.getRoles(), Matchers.containsInAnyOrder("superuser", "system_admin", "admin")); assertThat(mapping.getExpression(), instanceOf(AnyExpression.class)); @@ -177,19 +200,44 @@ public void testValidExpressionWithFixedRoleNames() throws Exception { } public void testParseValidJsonWithTemplatedRoleNames() throws Exception { - String json = "{" - + "\"role_templates\": [ " - + " { \"template\" : { \"source\":\"kibana_user\"} }," - + " { \"template\" : { \"source\":\"sales\"} }," - + " { \"template\" : { \"source\":\"_user_{{username}}\" }, \"format\":\"string\" }" - + " ], " - + "\"enabled\": true, " - + "\"rules\": { " - + " \"all\": [ " - + " { \"field\": { \"dn\" : \"*,ou=sales,dc=example,dc=com\" } }, " - + " { \"except\": { \"field\": { \"metadata.active\" : false } } }" - + " ]}" - + "}"; + String json = """ + { + "role_templates": [ + { + "template": { + "source": "kibana_user" + } + }, + { + "template": { + "source": "sales" + } + }, + { + "template": { + "source": "_user_{{username}}" + }, + "format": "string" + } + ], + "enabled": true, + "rules": { + "all": [ + { + "field": { + "dn": "*,ou=sales,dc=example,dc=com" + } + }, + { + "except": { + "field": { + "metadata.active": false + } + } + } + ] + } + }"""; final ExpressionRoleMapping mapping = parse(json, "ldap_sales"); assertThat(mapping.getRoleTemplates(), iterableWithSize(3)); assertThat(mapping.getRoleTemplates().get(0).getTemplate().utf8ToString(), equalTo("{\"source\":\"kibana_user\"}")); @@ -201,56 +249,74 @@ public void testParseValidJsonWithTemplatedRoleNames() throws Exception { } public void testParsingFailsIfRulesAreMissing() throws Exception { - String json = "{" + "\"roles\": [ \"kibana_user\", \"sales\" ], " + "\"enabled\": true " + "}"; + String json = """ + { + "roles": [ "kibana_user", "sales" ], + "enabled": true + }"""; ParsingException ex = expectThrows(ParsingException.class, () -> parse(json, "bad_json")); assertThat(ex.getMessage(), containsString("rules")); } public void testParsingFailsIfRolesMissing() throws Exception { - String json = "{" - + "\"enabled\": true, " - + "\"rules\": " - + " { \"field\": { \"dn\" : \"*,ou=sales,dc=example,dc=com\" } } " - + "}"; + String json = """ + { + "enabled": true, + "rules": { + "field": { + "dn": "*,ou=sales,dc=example,dc=com" + } + } + }"""; ParsingException ex = expectThrows(ParsingException.class, () -> parse(json, "bad_json")); assertThat(ex.getMessage(), containsString("role")); } public void testParsingFailsIfThereAreUnrecognisedFields() throws Exception { - String json = "{" - + "\"disabled\": false, " - + "\"roles\": [ \"kibana_user\", \"sales\" ], " - + "\"rules\": " - + " { \"field\": { \"dn\" : \"*,ou=sales,dc=example,dc=com\" } } " - + "}"; + String json = """ + { + "disabled": false, + "roles": [ "kibana_user", "sales" ], + "rules": { + "field": { + "dn": "*,ou=sales,dc=example,dc=com" + } + } + }"""; ParsingException ex = expectThrows(ParsingException.class, () -> parse(json, "bad_json")); assertThat(ex.getMessage(), containsString("disabled")); } public void testParsingIgnoresTypeFields() throws Exception { - String json = "{" - + "\"enabled\": true, " - + "\"roles\": [ \"kibana_user\", \"sales\" ], " - + "\"rules\": " - + " { \"field\": { \"dn\" : \"*,ou=sales,dc=example,dc=com\" } }, " - + "\"doc_type\": \"role-mapping\", " - + "\"type\": \"doc\"" - + "}"; + String json = """ + { + "enabled": true, + "roles": [ "kibana_user", "sales" ], + "rules": { + "field": { + "dn": "*,ou=sales,dc=example,dc=com" + } + }, + "doc_type": "role-mapping", + "type": "doc" + }"""; final ExpressionRoleMapping mapping = parse(json, "from_index", true); assertThat(mapping.isEnabled(), equalTo(true)); assertThat(mapping.getRoles(), Matchers.containsInAnyOrder("kibana_user", "sales")); } public void testParsingOfBothRoleNamesAndTemplates() throws Exception { - String json = "{" - + "\"enabled\": true, " - + "\"roles\": [ \"kibana_user\", \"sales\" ], " - + "\"role_templates\": [" - + " { \"template\" : \"{ \\\"source\\\":\\\"_user_{{username}}\\\" }\", \"format\":\"string\" }" - + "]," - + "\"rules\": " - + " { \"field\": { \"dn\" : \"*,ou=sales,dc=example,dc=com\" } }" - + "}"; + String json = """ + { + "enabled": true, + "roles": [ "kibana_user", "sales" ], + "role_templates": [ { "template": "{ \\"source\\":\\"_user_{{username}}\\" }", "format": "string" } ], + "rules": { + "field": { + "dn": "*,ou=sales,dc=example,dc=com" + } + } + }"""; // This is rejected when validating a request, but is valid when parsing the mapping final ExpressionRoleMapping mapping = parse(json, "from_api", false); @@ -259,63 +325,78 @@ public void testParsingOfBothRoleNamesAndTemplates() throws Exception { } public void testToXContentWithRoleNames() throws Exception { - String source = "{" - + "\"roles\": [ " - + " \"kibana_user\"," - + " \"sales\"" - + " ], " - + "\"enabled\": true, " - + "\"rules\": { \"field\": { \"realm.name\" : \"saml1\" } }" - + "}"; + String source = """ + { + "roles": [ "kibana_user", "sales" ], + "enabled": true, + "rules": { + "field": { + "realm.name": "saml1" + } + } + }"""; final ExpressionRoleMapping mapping = parse(source, getTestName()); assertThat(mapping.getRoles(), iterableWithSize(2)); final String xcontent = Strings.toString(mapping); - assertThat( - xcontent, - equalTo( - "{" - + "\"enabled\":true," - + "\"roles\":[" - + "\"kibana_user\"," - + "\"sales\"" - + "]," - + "\"rules\":{\"field\":{\"realm.name\":\"saml1\"}}," - + "\"metadata\":{}" - + "}" - ) - ); + assertThat(xcontent, equalTo(""" + {"enabled":true,"roles":["kibana_user","sales"],"rules":{"field":{"realm.name":"saml1"}},"metadata":{}}""")); } public void testToXContentWithTemplates() throws Exception { - String source = "{" - + "\"metadata\" : { \"answer\":42 }," - + "\"role_templates\": [ " - + " { \"template\" : { \"source\":\"_user_{{username}}\" }, \"format\":\"string\" }," - + " { \"template\" : { \"source\":\"{{#tojson}}groups{{/tojson}}\" }, \"format\":\"json\" }" - + " ], " - + "\"enabled\": false, " - + "\"rules\": { \"field\": { \"realm.name\" : \"saml1\" } }" - + "}"; + String source = """ + { + "metadata": { + "answer": 42 + }, + "role_templates": [ + { + "template": { + "source": "_user_{{username}}" + }, + "format": "string" + }, + { + "template": { + "source": "{{#tojson}}groups{{/tojson}}" + }, + "format": "json" + } + ], + "enabled": false, + "rules": { + "field": { + "realm.name": "saml1" + } + } + }"""; final ExpressionRoleMapping mapping = parse(source, getTestName()); assertThat(mapping.getRoleTemplates(), iterableWithSize(2)); final String xcontent = Strings.toString(mapping.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS, true)); - assertThat( - xcontent, - equalTo( - "{" - + "\"enabled\":false," - + "\"role_templates\":[" - + "{\"template\":\"{\\\"source\\\":\\\"_user_{{username}}\\\"}\",\"format\":\"string\"}," - + "{\"template\":\"{\\\"source\\\":\\\"{{#tojson}}groups{{/tojson}}\\\"}\",\"format\":\"json\"}" - + "]," - + "\"rules\":{\"field\":{\"realm.name\":\"saml1\"}}," - + "\"metadata\":{\"answer\":42}," - + "\"doc_type\":\"role-mapping\"" - + "}" - ) - ); + assertThat(xcontent, equalTo(XContentHelper.stripWhitespace(""" + { + "enabled": false, + "role_templates": [ + { + "template": "{\\"source\\":\\"_user_{{username}}\\"}", + "format": "string" + }, + { + "template": "{\\"source\\":\\"{{#tojson}}groups{{/tojson}}\\"}", + "format": "json" + } + ], + "rules": { + "field": { + "realm.name": "saml1" + } + }, + "metadata": { + "answer": 42 + }, + "doc_type": "role-mapping" + }"""))); final ExpressionRoleMapping parsed = parse(xcontent, getTestName(), true); assertThat(parsed.getRoles(), iterableWithSize(0)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java index 39259d5f097ea..8e4239ac2ce7f 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/DlsFlsRequestCacheDifferentiatorTests.java @@ -66,9 +66,8 @@ public void init() throws IOException { flsIndexName = "fls-" + randomAlphaOfLengthBetween(3, 8); dlsFlsIndexName = "dls-fls-" + randomAlphaOfLengthBetween(3, 8); - final DocumentPermissions documentPermissions1 = DocumentPermissions.filteredBy( - Set.of(new BytesArray("{\"term\":{\"number\":1}}")) - ); + final DocumentPermissions documentPermissions1 = DocumentPermissions.filteredBy(Set.of(new BytesArray(""" + {"term":{"number":1}}"""))); threadContext.putTransient( AuthorizationServiceField.INDICES_PERMISSIONS_KEY, diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java index 092f3bb76bdc4..181d9bf4f8cdb 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RBACEngineTests.java @@ -1320,7 +1320,8 @@ public void testIsCompleteMatch() throws Exception { public void testBuildUserPrivilegeResponse() { final ManageApplicationPrivileges manageApplicationPrivileges = new ManageApplicationPrivileges(Sets.newHashSet("app01", "app02")); - final BytesArray query = new BytesArray("{\"term\":{\"public\":true}}"); + final BytesArray query = new BytesArray(""" + {"term":{"public":true}}"""); final Role role = Role.builder(RESTRICTED_INDICES_AUTOMATON, "test", "role") .cluster(Sets.newHashSet("monitor", "manage_watcher"), Collections.singleton(manageApplicationPrivileges)) .add(IndexPrivilege.get(Sets.newHashSet("read", "write")), "index-1") diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java index a4916e5548ed5..a82f4fbd0837e 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/RoleDescriptorTests.java @@ -159,26 +159,65 @@ public void testParse() throws Exception { assertEquals(0, rd.getIndicesPrivileges().length); assertArrayEquals(Strings.EMPTY_ARRAY, rd.getRunAs()); - q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"]}"; + q = """ + { + "cluster": [ "a", "b" ], + "run_as": [ "m", "n" ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); assertEquals(0, rd.getIndicesPrivileges().length); assertArrayEquals(new String[] { "m", "n" }, rd.getRunAs()); - q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": \"idx1\", \"privileges\": [\"p1\", " - + "\"p2\"]}, {\"names\": \"idx2\", \"allow_restricted_indices\": true, \"privileges\": [\"p3\"], \"field_security\": " - + "{\"grant\": [\"f1\", \"f2\"]}}, {\"names\": " - + "\"idx2\", \"allow_restricted_indices\": false," - + "\"privileges\": [\"p3\"], \"field_security\": {\"grant\": [\"f1\", \"f2\"]}, \"query\": {\"match_all\": {}} }]}"; + q = """ + { + "cluster": [ "a", "b" ], + "run_as": [ "m", "n" ], + "index": [ + { + "names": "idx1", + "privileges": [ "p1", "p2" ] + }, + { + "names": "idx2", + "allow_restricted_indices": true, + "privileges": [ "p3" ], + "field_security": { + "grant": [ "f1", "f2" ] + } + }, + { + "names": "idx2", + "allow_restricted_indices": false, + "privileges": [ "p3" ], + "field_security": { + "grant": [ "f1", "f2" ] + }, + "query": { + "match_all": {} + } + } + ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); assertEquals(3, rd.getIndicesPrivileges().length); assertArrayEquals(new String[] { "m", "n" }, rd.getRunAs()); - q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": " - + "[\"p1\", \"p2\"], \"allow_restricted_indices\": true}]}"; + q = """ + { + "cluster": [ "a", "b" ], + "run_as": [ "m", "n" ], + "index": [ + { + "names": [ "idx1", "idx2" ], + "privileges": [ "p1", "p2" ], + "allow_restricted_indices": true + } + ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -188,7 +227,8 @@ public void testParse() throws Exception { assertArrayEquals(new String[] { "m", "n" }, rd.getRunAs()); assertNull(rd.getIndicesPrivileges()[0].getQuery()); - q = "{\"cluster\":[\"a\", \"b\"], \"metadata\":{\"foo\":\"bar\"}}"; + q = """ + {"cluster":["a", "b"], "metadata":{"foo":"bar"}}"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -198,14 +238,37 @@ public void testParse() throws Exception { assertThat(rd.getMetadata().size(), is(1)); assertThat(rd.getMetadata().get("foo"), is("bar")); - q = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"]," - + " \"index\": [{\"names\": [\"idx1\",\"idx2\"], \"allow_restricted_indices\": false, \"privileges\": [\"p1\", \"p2\"]}]," - + " \"applications\": [" - + " {\"resources\": [\"object-123\",\"object-456\"], \"privileges\":[\"read\", \"delete\"], \"application\":\"app1\"}," - + " {\"resources\": [\"*\"], \"privileges\":[\"admin\"], \"application\":\"app2\" }" - + " ]," - + " \"global\": { \"application\": { \"manage\": { \"applications\" : [ \"kibana\", \"logstash\" ] } } }" - + "}"; + q = """ + { + "cluster": [ "a", "b" ], + "run_as": [ "m", "n" ], + "index": [ + { + "names": [ "idx1", "idx2" ], + "allow_restricted_indices": false, + "privileges": [ "p1", "p2" ] + } + ], + "applications": [ + { + "resources": [ "object-123", "object-456" ], + "privileges": [ "read", "delete" ], + "application": "app1" + }, + { + "resources": [ "*" ], + "privileges": [ "admin" ], + "application": "app2" + } + ], + "global": { + "application": { + "manage": { + "applications": [ "kibana", "logstash" ] + } + } + } + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertThat(rd.getName(), equalTo("test")); assertThat(rd.getClusterPrivileges(), arrayContaining("a", "b")); @@ -231,7 +294,8 @@ public void testParse() throws Exception { containsInAnyOrder("kibana", "logstash") ); - q = "{\"applications\": [{\"application\": \"myapp\", \"resources\": [\"*\"], \"privileges\": [\"login\" ]}] }"; + q = """ + {"applications": [{"application": "myapp", "resources": ["*"], "privileges": ["login" ]}] }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertThat(rd.getName(), equalTo("test")); assertThat(rd.getClusterPrivileges(), emptyArray()); @@ -242,8 +306,8 @@ public void testParse() throws Exception { assertThat(rd.getApplicationPrivileges()[0].getApplication(), equalTo("myapp")); assertThat(rd.getConditionalClusterPrivileges(), Matchers.arrayWithSize(0)); - final String badJson = - "{\"applications\":[{\"not_supported\": true, \"resources\": [\"*\"], \"privileges\": [\"my-app:login\" ]}] }"; + final String badJson = """ + {"applications":[{"not_supported": true, "resources": ["*"], "privileges": ["my-app:login" ]}] }"""; final IllegalArgumentException ex = expectThrows( IllegalArgumentException.class, () -> RoleDescriptor.parse("test", new BytesArray(badJson), false, XContentType.JSON) @@ -295,8 +359,18 @@ public void testSerializationForCurrentVersion() throws Exception { } public void testParseEmptyQuery() throws Exception { - String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], " - + "\"privileges\": [\"p1\", \"p2\"], \"query\": \"\"}]}"; + String json = """ + { + "cluster": [ "a", "b" ], + "run_as": [ "m", "n" ], + "index": [ + { + "names": [ "idx1", "idx2" ], + "privileges": [ "p1", "p2" ], + "query": "" + } + ] + }"""; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -307,8 +381,18 @@ public void testParseEmptyQuery() throws Exception { } public void testParseNullQuery() throws Exception { - String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"index\": [{\"names\": [\"idx1\",\"idx2\"], " - + "\"privileges\": [\"p1\", \"p2\"], \"query\": null}]}"; + String json = """ + { + "cluster": [ "a", "b" ], + "run_as": [ "m", "n" ], + "index": [ + { + "names": [ "idx1", "idx2" ], + "privileges": [ "p1", "p2" ], + "query": null + } + ] + }"""; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -319,8 +403,18 @@ public void testParseNullQuery() throws Exception { } public void testParseEmptyQueryUsingDeprecatedIndicesField() throws Exception { - String json = "{\"cluster\":[\"a\", \"b\"], \"run_as\": [\"m\", \"n\"], \"indices\": [{\"names\": [\"idx1\",\"idx2\"], " - + "\"privileges\": [\"p1\", \"p2\"], \"query\": \"\"}]}"; + String json = """ + { + "cluster": [ "a", "b" ], + "run_as": [ "m", "n" ], + "indices": [ + { + "names": [ "idx1", "idx2" ], + "privileges": [ "p1", "p2" ], + "query": "" + } + ] + }"""; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertArrayEquals(new String[] { "a", "b" }, rd.getClusterPrivileges()); @@ -354,12 +448,19 @@ public void testParseIndicesPrivilegesSucceedsWhenExceptFieldsIsSubsetOfGrantedF final String grant = grantAll ? "\"*\"" : "\"f1\",\"f2\""; final String except = grantAll ? "\"_fx\",\"f8\"" : "\"f1\""; - final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " - + "\"grant\" : [" - + grant - + "], \"except\" : [" - + except - + "] } }] }"; + final String json = """ + { + "indices": [ + { + "names": [ "idx1", "idx2" ], + "privileges": [ "p1", "p2" ], + "field_security": { + "grant": [ %s ], + "except": [ %s ] + } + } + ] + }""".formatted(grant, except); final RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON); assertEquals("test", rd.getName()); assertEquals(1, rd.getIndicesPrivileges().length); @@ -372,8 +473,19 @@ public void testParseIndicesPrivilegesSucceedsWhenExceptFieldsIsSubsetOfGrantedF } public void testParseIndicesPrivilegesFailsWhenExceptFieldsAreNotSubsetOfGrantedFields() { - final String json = "{ \"indices\": [{\"names\": [\"idx1\",\"idx2\"], \"privileges\": [\"p1\", \"p2\"], \"field_security\" : { " - + "\"grant\" : [\"f1\",\"f2\"], \"except\" : [\"f3\"] } }] }"; + final String json = """ + { + "indices": [ + { + "names": [ "idx1", "idx2" ], + "privileges": [ "p1", "p2" ], + "field_security": { + "grant": [ "f1", "f2" ], + "except": [ "f3" ] + } + } + ] + }"""; final ElasticsearchParseException epe = expectThrows( ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(json), false, XContentType.JSON) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java index bbca31604bff5..339e8d82197dd 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/interceptor/ShardSearchRequestInterceptorTests.java @@ -65,9 +65,8 @@ private void configureMinMondeVersion(Version version) { public void testRequestCacheWillBeDisabledWhenDlsUsesStoredScripts() { configureMinMondeVersion(Version.CURRENT); - final DocumentPermissions documentPermissions = DocumentPermissions.filteredBy( - Set.of(new BytesArray("{\"template\":{\"id\":\"my-script\"}}")) - ); + final DocumentPermissions documentPermissions = DocumentPermissions.filteredBy(Set.of(new BytesArray(""" + {"template":{"id":"my-script"}}"""))); final ShardSearchRequest shardSearchRequest = mock(ShardSearchRequest.class); final String index = randomAlphaOfLengthBetween(3, 8); when(shardSearchRequest.shardId()).thenReturn(new ShardId(index, randomAlphaOfLength(22), randomInt(3))); @@ -83,9 +82,8 @@ public void testRequestCacheWillBeDisabledWhenDlsUsesStoredScripts() { public void testRequestWillNotBeDisabledCacheWhenDlsUsesInlineScripts() { configureMinMondeVersion(Version.CURRENT); - final DocumentPermissions documentPermissions = DocumentPermissions.filteredBy( - Set.of(new BytesArray("{\"term\":{\"username\":\"foo\"}}")) - ); + final DocumentPermissions documentPermissions = DocumentPermissions.filteredBy(Set.of(new BytesArray(""" + {"term":{"username":"foo"}}"""))); final ShardSearchRequest shardSearchRequest = mock(ShardSearchRequest.class); final String index = randomAlphaOfLengthBetween(3, 8); when(shardSearchRequest.shardId()).thenReturn(new ShardId(index, randomAlphaOfLength(22), randomInt(3))); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java index 11941423f05c9..b97d71466f181 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/permission/FieldPermissionsTests.java @@ -24,50 +24,101 @@ public class FieldPermissionsTests extends ESTestCase { public void testParseFieldPermissions() throws Exception { - String q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " - + "\"field_security\": {" - + "\"grant\": [\"f1\", \"f2\", \"f3\", \"f4\"]," - + "\"except\": [\"f3\",\"f4\"]" - + "}}]}"; + String q = """ + { + "indices": [ + { + "names": "idx2", + "privileges": [ "p3" ], + "field_security": { + "grant": [ "f1", "f2", "f3", "f4" ], + "except": [ "f3", "f4" ] + } + } + ] + }"""; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2", "f3", "f4" }); assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), new String[] { "f3", "f4" }); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " - + "\"field_security\": {" - + "\"except\": [\"f3\",\"f4\"]," - + "\"grant\": [\"f1\", \"f2\", \"f3\", \"f4\"]" - + "}}]}"; + q = """ + { + "indices": [ + { + "names": "idx2", + "privileges": [ "p3" ], + "field_security": { + "except": [ "f3", "f4" ], + "grant": [ "f1", "f2", "f3", "f4" ] + } + } + ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2", "f3", "f4" }); assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), new String[] { "f3", "f4" }); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " - + "\"field_security\": {" - + "\"grant\": [\"f1\", \"f2\"]" - + "}}]}"; + q = """ + { + "indices": [ + { + "names": "idx2", + "privileges": [ "p3" ], + "field_security": { + "grant": [ "f1", "f2" ] + } + } + ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2" }); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"field_security\": {" + "\"grant\": []" + "}}]}"; + q = """ + { + "indices": [ + { + "names": "idx2", + "privileges": [ "p3" ], + "field_security": { + "grant": [] + } + } + ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " - + "\"field_security\": {" - + "\"except\": []," - + "\"grant\": []" - + "}}]}"; + q = """ + { + "indices": [ + { + "names": "idx2", + "privileges": [ "p3" ], + "field_security": { + "except": [], + "grant": [] + } + } + ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertArrayEquals(rd.getIndicesPrivileges()[0].getDeniedFields(), new String[] {}); - final String exceptWithoutGrant = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\":" - + " [\"p3\"], \"field_security\": {" - + "\"except\": [\"f1\"]" - + "}}]}"; + final String exceptWithoutGrant = """ + { + "indices": [ + { + "names": "idx2", + "privileges": [ "p3" ], + "field_security": { + "except": [ "f1" ] + } + } + ] + }"""; ElasticsearchParseException e = expectThrows( ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(exceptWithoutGrant), false, XContentType.JSON) @@ -77,10 +128,8 @@ public void testParseFieldPermissions() throws Exception { containsString("failed to parse indices privileges for role [test]. field_security" + " requires grant if except is given") ); - final String grantNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"]," - + " \"field_security\": {" - + "\"grant\": null" - + "}}]}"; + final String grantNull = """ + {"indices": [ {"names": "idx2", "privileges": ["p3"], "field_security": {"grant": null}}]}"""; e = expectThrows( ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(grantNull), false, XContentType.JSON) @@ -90,11 +139,8 @@ public void testParseFieldPermissions() throws Exception { containsString("failed to parse indices privileges for" + " role [test]. grant must not be null.") ); - final String exceptNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " - + "[\"p3\"], \"field_security\": {" - + "\"grant\": [\"*\"]," - + "\"except\": null" - + "}}]}"; + final String exceptNull = """ + {"indices": [ {"names": "idx2", "privileges": ["p3"], "field_security": {"grant": ["*"],"except": null}}]}"""; e = expectThrows( ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(exceptNull), false, XContentType.JSON) @@ -104,11 +150,8 @@ public void testParseFieldPermissions() throws Exception { containsString("failed to parse indices privileges for role [test]. except must" + " not be null.") ); - final String exceptGrantNull = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " - + "[\"p3\"], \"field_security\": {" - + "\"grant\": null," - + "\"except\": null" - + "}}]}"; + final String exceptGrantNull = """ + {"indices": [ {"names": "idx2", "privileges": ["p3"], "field_security": {"grant": null,"except": null}}]}"""; e = expectThrows( ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(exceptGrantNull), false, XContentType.JSON) @@ -118,9 +161,8 @@ public void testParseFieldPermissions() throws Exception { containsString("failed to parse indices privileges " + "for role [test]. grant must not be null.") ); - final String bothFieldsMissing = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": " - + "[\"p3\"], \"field_security\": {" - + "}}]}"; + final String bothFieldsMissing = """ + {"indices": [ {"names": "idx2", "privileges": ["p3"], "field_security": {}}]}"""; e = expectThrows( ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(bothFieldsMissing), false, XContentType.JSON) @@ -131,15 +173,26 @@ public void testParseFieldPermissions() throws Exception { ); // try with two indices and mix order a little - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " - + "\"field_security\": {" - + "\"grant\": []" - + "}}," - + "{\"names\": \"idx3\",\n" - + " \"field_security\": {\n" - + " \"grant\": [\"*\"], \n" - + " \"except\": [\"f2\"]}," - + "\"privileges\": [\"p3\"]}]}"; + q = """ + { + "indices": [ + { + "names": "idx2", + "privileges": [ "p3" ], + "field_security": { + "grant": [] + } + }, + { + "names": "idx3", + "field_security": { + "grant": [ "*" ], + "except": [ "f2" ] + }, + "privileges": [ "p3" ] + } + ] + }"""; rd = RoleDescriptor.parse("test", new BytesArray(q), false, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); @@ -149,7 +202,8 @@ public void testParseFieldPermissions() throws Exception { // test old syntax for field permissions public void testBWCFieldPermissions() throws Exception { - String q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"fields\": [\"f1\", \"f2\"]" + "}]}"; + String q = """ + {"indices": [ {"names": "idx2", "privileges": ["p3"], "fields": ["f1", "f2"]}]}"""; RoleDescriptor rd = RoleDescriptor.parse("test", new BytesArray(q), true, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] { "f1", "f2" }); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); @@ -159,16 +213,12 @@ public void testBWCFieldPermissions() throws Exception { ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(failingQuery), false, XContentType.JSON) ); - assertThat( - e.getDetailedMessage(), - containsString( - "[\"fields\": [...]] format has " - + "changed for field permissions in role [test]" - + ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" - ) - ); + assertThat(e.getDetailedMessage(), containsString(""" + ["fields": [...]] format has changed for field permissions in role [test], \ + use ["field_security": {"grant":[...],"except":[...]}] instead""")); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"fields\": []" + "}]}"; + q = """ + {"indices": [ {"names": "idx2", "privileges": ["p3"], "fields": []}]}"""; rd = RoleDescriptor.parse("test", new BytesArray(q), true, XContentType.JSON); assertArrayEquals(rd.getIndicesPrivileges()[0].getGrantedFields(), new String[] {}); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); @@ -177,16 +227,12 @@ public void testBWCFieldPermissions() throws Exception { ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(failingQuery2), false, XContentType.JSON) ); - assertThat( - e.getDetailedMessage(), - containsString( - "[\"fields\": [...]] format has " - + "changed for field permissions in role [test]" - + ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" - ) - ); + assertThat(e.getDetailedMessage(), containsString(""" + ["fields": [...]] format has changed for field permissions in role [test], \ + use ["field_security": {"grant":[...],"except":[...]}] instead""")); - q = "{\"indices\": [ {\"names\": \"idx2\", \"privileges\": [\"p3\"], " + "\"fields\": null" + "}]}"; + q = """ + {"indices": [ {"names": "idx2", "privileges": ["p3"], "fields": null}]}"""; rd = RoleDescriptor.parse("test", new BytesArray(q), true, XContentType.JSON); assertNull(rd.getIndicesPrivileges()[0].getGrantedFields()); assertNull(rd.getIndicesPrivileges()[0].getDeniedFields()); @@ -195,14 +241,9 @@ public void testBWCFieldPermissions() throws Exception { ElasticsearchParseException.class, () -> RoleDescriptor.parse("test", new BytesArray(failingQuery3), false, XContentType.JSON) ); - assertThat( - e.getDetailedMessage(), - containsString( - "[\"fields\": [...]] format has " - + "changed for field permissions in role [test]" - + ", use [\"field_security\": {\"grant\":[...],\"except\":[...]}] instead" - ) - ); + assertThat(e.getDetailedMessage(), containsString(""" + ["fields": [...]] format has changed for field permissions in role [test], \ + use ["field_security": {"grant":[...],"except":[...]}] instead""")); } public void testFieldPermissionsHashCodeThreadSafe() throws Exception { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java index bf8bcc5dcdcaf..0d9e7b5f3efe7 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/store/NativePrivilegeStoreTests.java @@ -143,8 +143,10 @@ public void testGetSinglePrivilegeByName() throws Exception { assertThat(requests.get(0), instanceOf(SearchRequest.class)); SearchRequest request = (SearchRequest) requests.get(0); final String query = Strings.toString(request.source().query()); - assertThat(query, containsString("{\"terms\":{\"application\":[\"myapp\"]")); - assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\"")); + assertThat(query, containsString(""" + {"terms":{"application":["myapp"]""")); + assertThat(query, containsString(""" + {"term":{"type":{"value":"application-privilege\"""")); final SearchHit[] hits = buildHits(sourcePrivileges); listener.get() @@ -217,14 +219,11 @@ public void testGetPrivilegesByApplicationName() throws Exception { assertThat(request.indices(), arrayContaining(RestrictedIndicesNames.SECURITY_MAIN_ALIAS)); final String query = Strings.toString(request.source().query()); - assertThat( - query, - anyOf( - containsString("{\"terms\":{\"application\":[\"myapp\",\"yourapp\"]"), - containsString("{\"terms\":{\"application\":[\"yourapp\",\"myapp\"]") - ) - ); - assertThat(query, containsString("{\"term\":{\"type\":{\"value\":\"application-privilege\"")); + assertThat(query, anyOf(containsString(""" + {"terms":{"application":["myapp","yourapp"]"""), containsString(""" + {"terms":{"application":["yourapp","myapp"]"""))); + assertThat(query, containsString(""" + {"term":{"type":{"value":"application-privilege\"""")); final SearchHit[] hits = buildHits(sourcePrivileges); listener.get() diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java index b6ce6bf510efd..868112ac59a0a 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/operator/FileOperatorUsersStoreTests.java @@ -222,20 +222,24 @@ public void testParseFileWhenFileDoesNotExist() throws Exception { } public void testParseConfig() throws IOException { - String config = "" + "operator:\n" + " - usernames: [\"operator_1\"]\n"; + String config = """ + operator: + - usernames: ["operator_1"] + """; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { final List groups = FileOperatorUsersStore.parseConfig(in).getGroups(); assertEquals(1, groups.size()); assertEquals(new FileOperatorUsersStore.Group(Set.of("operator_1")), groups.get(0)); } - config = "" - + "operator:\n" - + " - usernames: [\"operator_1\",\"operator_2\"]\n" - + " realm_name: \"file1\"\n" - + " realm_type: \"file\"\n" - + " auth_type: \"realm\"\n" - + " - usernames: [\"internal_system\"]\n"; + config = """ + operator: + - usernames: ["operator_1","operator_2"] + realm_name: "file1" + realm_type: "file" + auth_type: "realm" + - usernames: ["internal_system"] + """; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { final List groups = FileOperatorUsersStore.parseConfig(in).getGroups(); @@ -244,12 +248,13 @@ public void testParseConfig() throws IOException { assertEquals(new FileOperatorUsersStore.Group(Set.of("internal_system")), groups.get(1)); } - config = "" - + "operator:\n" - + " - realm_name: \"file1\"\n" - + " usernames: [\"internal_system\"]\n" - + " - auth_type: \"realm\"\n" - + " usernames: [\"operator_1\",\"operator_2\"]\n"; + config = """ + operator: + - realm_name: "file1" + usernames: ["internal_system"] + - auth_type: "realm" + usernames: ["operator_1","operator_2"] + """; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { final List groups = FileOperatorUsersStore.parseConfig(in).getGroups(); @@ -260,20 +265,31 @@ public void testParseConfig() throws IOException { } public void testParseInvalidConfig() throws IOException { - String config = "" + "operator:\n" + " - usernames: [\"operator_1\"]\n" + " realm_type: \"native\"\n"; + String config = """ + operator: + - usernames: ["operator_1"] + realm_type: "native" + """; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { final XContentParseException e = expectThrows(XContentParseException.class, () -> FileOperatorUsersStore.parseConfig(in)); assertThat(e.getCause().getCause().getMessage(), containsString("[realm_type] only supports [file]")); } - config = "" + "operator:\n" + " - usernames: [\"operator_1\"]\n" + " auth_type: \"token\"\n"; + config = """ + operator: + - usernames: ["operator_1"] + auth_type: "token" + """; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { final XContentParseException e = expectThrows(XContentParseException.class, () -> FileOperatorUsersStore.parseConfig(in)); assertThat(e.getCause().getCause().getMessage(), containsString("[auth_type] only supports [realm]")); } - config = "" + "operator:\n" + " auth_type: \"realm\"\n"; + config = """ + operator: + auth_type: "realm" + """; try (ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))) { final XContentParseException e = expectThrows(XContentParseException.class, () -> FileOperatorUsersStore.parseConfig(in)); assertThat(e.getCause().getMessage(), containsString("Required [usernames]")); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java index 22ccf41695c21..b4355a70d193b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/RestRequestFilterTests.java @@ -25,7 +25,8 @@ public class RestRequestFilterTests extends ESTestCase { public void testFilteringItemsInSubLevels() throws IOException { - BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); + BytesReference content = new BytesArray(""" + {"root": {"second": {"third": "password", "foo": "bar"}}}"""); RestRequestFilter filter = () -> Collections.singleton("root.second.third"); FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) .build(); @@ -46,7 +47,8 @@ public void testFilteringItemsInSubLevels() throws IOException { } public void testFilteringItemsInSubLevelsWithWildCard() throws IOException { - BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); + BytesReference content = new BytesArray(""" + {"root": {"second": {"third": "password", "foo": "bar"}}}"""); RestRequestFilter filter = () -> Collections.singleton("root.*.third"); FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) .build(); @@ -67,7 +69,8 @@ public void testFilteringItemsInSubLevelsWithWildCard() throws IOException { } public void testFilteringItemsInSubLevelsWithLeadingWildCard() throws IOException { - BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); + BytesReference content = new BytesArray(""" + {"root": {"second": {"third": "password", "foo": "bar"}}}"""); RestRequestFilter filter = () -> Collections.singleton("*.third"); FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) .build(); @@ -88,7 +91,8 @@ public void testFilteringItemsInSubLevelsWithLeadingWildCard() throws IOExceptio } public void testRemoteAddressWorks() throws IOException { - BytesReference content = new BytesArray("{\"root\": {\"second\": {\"third\": \"password\", \"foo\": \"bar\"}}}"); + BytesReference content = new BytesArray(""" + {"root": {"second": {"third": "password", "foo": "bar"}}}"""); RestRequestFilter filter = () -> Collections.singleton("*.third"); InetSocketAddress address = new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 32768); FakeRestRequest restRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(content, XContentType.JSON) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyActionTests.java index 7ffdddd56198a..88e2323b3325d 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/apikey/RestQueryApiKeyActionTests.java @@ -74,8 +74,21 @@ protected NamedXContentRegistry xContentRegistry() { } public void testQueryParsing() throws Exception { - final String query1 = "{\"query\":{\"bool\":{\"must\":[{\"terms\":{\"name\":[\"k1\",\"k2\"]}}]," - + "\"should\":[{\"prefix\":{\"metadata.environ\":\"prod\"}}]}}}"; + final String query1 = """ + { + "query": { + "bool": { + "must": [ + { + "terms": { + "name": [ "k1", "k2" ] + } + } + ], + "should": [ { "prefix": { "metadata.environ": "prod" } } ] + } + } + }"""; final FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent( new BytesArray(query1), XContentType.JSON @@ -123,9 +136,16 @@ public void doE } public void testParsingSearchParameters() throws Exception { - final String requestBody = "{\"query\":{\"match_all\":{}},\"from\":42,\"size\":20," - + "\"sort\":[\"name\",{\"creation_time\":{\"order\":\"desc\",\"format\":\"strict_date_time\"}},\"username\"]," - + "\"search_after\":[\"key-2048\",\"2021-07-01T00:00:59.000Z\"]}\n"; + final String requestBody = """ + { + "query": { + "match_all": {} + }, + "from": 42, + "size": 20, + "sort": [ "name", { "creation_time": { "order": "desc", "format": "strict_date_time" } }, "username" ], + "search_after": [ "key-2048", "2021-07-01T00:00:59.000Z" ] + }"""; final FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent( new BytesArray(requestBody), diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenActionTests.java index 8a45a1c2cc0dc..b38dd7ec982dc 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestGetTokenActionTests.java @@ -136,14 +136,13 @@ public void sendResponse(RestResponse restResponse) { } public void testParser() throws Exception { - final String request = "{" - + "\"grant_type\": \"password\"," - + "\"username\": \"user1\"," - + "\"password\": \"" - + SecuritySettingsSourceField.TEST_PASSWORD - + "\"," - + "\"scope\": \"FULL\"" - + "}"; + final String request = """ + { + "grant_type": "password", + "username": "user1", + "password": "%s", + "scope": "FULL" + }""".formatted(SecuritySettingsSourceField.TEST_PASSWORD); try ( XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) @@ -158,13 +157,12 @@ public void testParser() throws Exception { public void testParserRefreshRequest() throws Exception { final String token = randomAlphaOfLengthBetween(4, 32); - final String request = "{" - + "\"grant_type\": \"refresh_token\"," - + "\"refresh_token\": \"" - + token - + "\"," - + "\"scope\": \"FULL\"" - + "}"; + final String request = """ + { + "grant_type": "refresh_token", + "refresh_token": "%s", + "scope": "FULL" + }""".formatted(token); try ( XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java index e7f14118b39ff..ff45bd3f0b6f2 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/oauth2/RestInvalidateTokenActionTests.java @@ -18,7 +18,8 @@ public class RestInvalidateTokenActionTests extends ESTestCase { public void testParserForUserAndRealm() throws Exception { - final String request = "{" + "\"username\": \"user1\"," + "\"realm_name\": \"realm1\"" + "}"; + final String request = """ + {"username": "user1","realm_name": "realm1"}"""; try ( XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) @@ -32,7 +33,8 @@ public void testParserForUserAndRealm() throws Exception { } public void testParserForToken() throws Exception { - final String request = "{" + "\"refresh_token\": \"refresh_token_string\"" + "}"; + final String request = """ + {"refresh_token": "refresh_token_string"}"""; try ( XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) @@ -46,7 +48,8 @@ public void testParserForToken() throws Exception { } public void testParserForIncorrectInput() throws Exception { - final String request = "{" + "\"refresh_token\": \"refresh_token_string\"," + "\"token\": \"access_token_string\"" + "}"; + final String request = """ + {"refresh_token": "refresh_token_string","token": "access_token_string"}"""; try ( XContentParser parser = XContentType.JSON.xContent() .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, request) diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java index e8671c5de1ce4..6d09f2c2b50d7 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java @@ -12,6 +12,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.license.License; import org.elasticsearch.license.XPackLicenseState; import org.elasticsearch.rest.RestStatus; @@ -109,38 +110,59 @@ public void testBuildResponse() throws Exception { listener.buildResponse(response, builder); String json = Strings.toString(builder); - assertThat( - json, - equalTo( - "{" - + "\"cluster\":[\"monitor\",\"manage_ml\",\"manage_watcher\"]," - + "\"global\":[" - + "{\"application\":{\"manage\":{\"applications\":[\"app01\",\"app02\"]}}}" - + "]," - + "\"indices\":[" - + "{\"names\":[\"index-1\",\"index-2\",\"index-3-*\"]," - + "\"privileges\":[\"read\",\"write\"]," - + "\"field_security\":[" - + "{\"grant\":[\"*\"],\"except\":[\"private.*\"]}," - + "{\"grant\":[\"public.*\"]}" - + "]," - + "\"query\":[" - + "\"{ \\\"term\\\": { \\\"access\\\": \\\"public\\\" } }\"," - + "\"{ \\\"term\\\": { \\\"access\\\": \\\"standard\\\" } }\"" - + "]," - + "\"allow_restricted_indices\":false" - + "}," - + "{\"names\":[\"index-4\"],\"privileges\":[\"all\"],\"allow_restricted_indices\":true}" - + "]," - + "\"applications\":[" - + "{\"application\":\"app01\",\"privileges\":[\"read\",\"write\"],\"resources\":[\"*\"]}," - + "{\"application\":\"app01\",\"privileges\":[\"admin\"],\"resources\":[\"department/1\"]}," - + "{\"application\":\"app02\",\"privileges\":[\"all\"],\"resources\":[\"tenant/42\",\"tenant/99\"]}" - + "]," - + "\"run_as\":[\"app-user-*\",\"backup-user\"]" - + "}" - ) - ); + assertThat(json, equalTo(XContentHelper.stripWhitespace(""" + { + "cluster": [ "monitor", "manage_ml", "manage_watcher" ], + "global": [ + { + "application": { + "manage": { + "applications": [ "app01", "app02" ] + } + } + } + ], + "indices": [ + { + "names": [ "index-1", "index-2", "index-3-*" ], + "privileges": [ "read", "write" ], + "field_security": [ + { + "grant": [ "*" ], + "except": [ "private.*" ] + }, + { + "grant": [ "public.*" ] + } + ], + "query": [ "{ \\"term\\": { \\"access\\": \\"public\\" } }", "{ \\"term\\": { \\"access\\": \\"standard\\" } }" ], + "allow_restricted_indices": false + }, + { + "names": [ "index-4" ], + "privileges": [ "all" ], + "allow_restricted_indices": true + } + ], + "applications": [ + { + "application": "app01", + "privileges": [ "read", "write" ], + "resources": [ "*" ] + }, + { + "application": "app01", + "privileges": [ "admin" ], + "resources": [ "department/1" ] + }, + { + "application": "app02", + "privileges": [ "all" ], + "resources": [ "tenant/42", "tenant/99" ] + } + ], + "run_as": [ "app-user-*", "backup-user" ] + }"""))); } } diff --git a/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java b/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java index e2968d22e55ee..52a648b893a15 100644 --- a/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java +++ b/x-pack/plugin/shutdown/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/shutdown/NodeShutdownIT.java @@ -283,13 +283,14 @@ public void testStalledShardMigrationProperlyDetected() throws Exception { // Create an index, pin the allocation to the node we're about to shut down final String indexName = "test-idx"; Request createIndexRequest = new Request("PUT", indexName); - createIndexRequest.setJsonEntity( - "{\"settings\": {\"number_of_shards\": " - + numberOfShards - + ", \"number_of_replicas\": 0, \"index.routing.allocation.require._id\": \"" - + nodeIdToShutdown - + "\"}}" - ); + createIndexRequest.setJsonEntity(""" + { + "settings": { + "number_of_shards": %s, + "number_of_replicas": 0, + "index.routing.allocation.require._id": "%s" + } + }""".formatted(numberOfShards, nodeIdToShutdown)); assertOK(client().performRequest(createIndexRequest)); // Mark the node for shutdown diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java index bd4ea458d0a25..16b4735cb55c1 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeWithDocValuesIT.java @@ -63,14 +63,15 @@ public void testMappingUpdate() { ); ensureGreen(); - String update = "{\n" - + " \"properties\": {\n" - + " \"shape\": {\n" - + " \"type\": \"geo_shape\"," - + " \"strategy\": \"recursive\"" - + " }\n" - + " }\n" - + "}"; + String update = """ + { + "properties": { + "shape": { + "type": "geo_shape", + "strategy": "recursive" + } + } + }"""; if (version.before(Version.V_8_0_0)) { IllegalArgumentException e = expectThrows( diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java index 1bb457d8732a9..660fc7fd95bc1 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/LegacyGeoShapeWithDocValuesIT.java @@ -68,13 +68,14 @@ public void testMappingUpdate() { ); ensureGreen(); - String update = "{\n" - + " \"properties\": {\n" - + " \"shape\": {\n" - + " \"type\": \"geo_shape\"" - + " }\n" - + " }\n" - + "}"; + String update = """ + { + "properties": { + "shape": { + "type": "geo_shape" + } + } + }"""; IllegalArgumentException e = expectThrows( IllegalArgumentException.class, diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java index e84cf58bf897c..8b022856dbaf9 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/ShapeQueryOverShapeTests.java @@ -29,7 +29,6 @@ import java.io.IOException; import java.util.List; -import java.util.Locale; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; @@ -134,16 +133,12 @@ public void testShapeFetchingPath() throws Exception { createIndex(indexName); client().admin().indices().prepareCreate(searchIndex).setMapping("location", "type=shape").get(); - String location = "\"location\" : {\"type\":\"polygon\", \"coordinates\":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}"; + String location = """ + "location" : {"type":"polygon", "coordinates":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}"""; - client().prepareIndex(indexName) - .setId("1") - .setSource( - String.format(Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", location, location, location, location), - XContentType.JSON - ) - .setRefreshPolicy(IMMEDIATE) - .get(); + client().prepareIndex(indexName).setId("1").setSource(""" + { %s, "1" : { %s, "2" : { %s, "3" : { %s } }} } + """.formatted(location, location, location, location), XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); client().prepareIndex(searchIndex) .setId("1") .setSource( @@ -235,20 +230,13 @@ public void testIgnoreMalformed() { * Test that the indexed shape routing can be provided if it is required */ public void testIndexShapeRouting() { - String source = "{\n" - + " \"shape\" : {\n" - + " \"type\" : \"bbox\",\n" - + " \"coordinates\" : [[" - + -Float.MAX_VALUE - + "," - + Float.MAX_VALUE - + "], [" - + Float.MAX_VALUE - + ", " - + -Float.MAX_VALUE - + "]]\n" - + " }\n" - + "}"; + String source = """ + { + "shape" : { + "type" : "bbox", + "coordinates" : [[%s,%s], [%s, %s]] + } + }""".formatted(-Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE, -Float.MAX_VALUE); client().prepareIndex(INDEX).setId("0").setSource(source, XContentType.JSON).setRouting("ABC").get(); client().admin().indices().prepareRefresh(INDEX).get(); @@ -294,7 +282,8 @@ public void testContainsShapeQuery() { client().admin().indices().prepareCreate("test_contains").setMapping("location", "type=shape").execute().actionGet(); - String doc = "{\"location\" : {\"type\":\"envelope\", \"coordinates\":[ [-100.0, 100.0], [100.0, -100.0]]}}"; + String doc = """ + {"location" : {"type":"envelope", "coordinates":[ [-100.0, 100.0], [100.0, -100.0]]}}"""; client().prepareIndex("test_contains").setId("1").setSource(doc, XContentType.JSON).setRefreshPolicy(IMMEDIATE).get(); // index the mbr of the collection diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java index 7f6aef1fe30a6..a8e69bf02d64c 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/ShapeQueryBuilderTests.java @@ -153,19 +153,20 @@ public void testNoRelation() { } public void testFromJson() throws IOException { - String json = "{\n" - + " \"shape\" : {\n" - + " \"geometry\" : {\n" - + " \"shape\" : {\n" - + " \"type\" : \"envelope\",\n" - + " \"coordinates\" : [ [ 1300.0, 5300.0 ], [ 1400.0, 5200.0 ] ]\n" - + " },\n" - + " \"relation\" : \"intersects\"\n" - + " },\n" - + " \"ignore_unmapped\" : false,\n" - + " \"boost\" : 42.0\n" - + " }\n" - + "}"; + String json = """ + { + "shape" : { + "geometry" : { + "shape" : { + "type" : "envelope", + "coordinates" : [ [ 1300.0, 5300.0 ], [ 1400.0, 5200.0 ] ] + }, + "relation" : "intersects" + }, + "ignore_unmapped" : false, + "boost" : 42.0 + } + }"""; ShapeQueryBuilder parsed = (ShapeQueryBuilder) parseQuery(json); checkGeneratedJson(json.replaceAll("envelope", "Envelope"), parsed); assertEquals(json, 42.0, parsed.boost(), 0.0001); diff --git a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java index d2b63303b5f4c..b84647434b19b 100644 --- a/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java +++ b/x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/SqlQueryParameterAnalyzerTests.java @@ -26,12 +26,11 @@ public void testNoParameters() throws Exception { public void testSingleParameter() throws Exception { assertEquals(1, SqlQueryParameterAnalyzer.parametersCount("SELECT * FROM 'table' WHERE s = '?' AND b = ?")); assertEquals(1, SqlQueryParameterAnalyzer.parametersCount("SELECT * FROM 'table' WHERE b = ? AND s = '?'")); - assertEquals( - 1, - SqlQueryParameterAnalyzer.parametersCount( - "SELECT ?/10 /* multiline \n" + " * query \n" + " * more ? /* lines */ ? here \n" + " */ FROM foo" - ) - ); + assertEquals(1, SqlQueryParameterAnalyzer.parametersCount(""" + SELECT ?/10 /* multiline \s + * query\s + * more ? /* lines */ ? here\s + */ FROM foo""")); assertEquals(1, SqlQueryParameterAnalyzer.parametersCount("SELECT ?")); } @@ -40,14 +39,10 @@ public void testMultipleParameters() throws Exception { assertEquals(4, SqlQueryParameterAnalyzer.parametersCount("SELECT ?, ?, ? , ?")); assertEquals(3, SqlQueryParameterAnalyzer.parametersCount("SELECT ?, ?, '?' , ?")); assertEquals(3, SqlQueryParameterAnalyzer.parametersCount("SELECT ?, ?\n, '?' , ?")); - assertEquals( - 3, - SqlQueryParameterAnalyzer.parametersCount( - "SELECT ? - 10 -- first parameter with ????\n" - + ", ? -- second parameter with random \" and ' \n" - + ", ? -- last parameter without new line" - ) - ); + assertEquals(3, SqlQueryParameterAnalyzer.parametersCount(""" + SELECT ? - 10 -- first parameter with ???? + , ? -- second parameter with random " and '\s + , ? -- last parameter without new line""")); } public void testUnclosedJdbcEscape() { diff --git a/x-pack/plugin/sql/qa/jdbc/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/single_node/JdbcShardFailureIT.java b/x-pack/plugin/sql/qa/jdbc/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/single_node/JdbcShardFailureIT.java index 3810efd762598..7862a0307daaa 100644 --- a/x-pack/plugin/sql/qa/jdbc/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/single_node/JdbcShardFailureIT.java +++ b/x-pack/plugin/sql/qa/jdbc/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/jdbc/single_node/JdbcShardFailureIT.java @@ -22,13 +22,39 @@ public class JdbcShardFailureIT extends JdbcIntegrationTestCase { @Before public void createTestIndex() throws IOException { Request createTest1 = new Request("PUT", "/test1"); - String body1 = "{\"aliases\":{\"test\":{}}, \"mappings\": {\"properties\": {\"test_field\":{\"type\":\"integer\"}}}}"; + String body1 = """ + { + "aliases": { + "test": {} + }, + "mappings": { + "properties": { + "test_field": { + "type": "integer" + } + } + } + }"""; createTest1.setJsonEntity(body1); client().performRequest(createTest1); Request createTest2 = new Request("PUT", "/test2"); - String body2 = "{\"aliases\":{\"test\":{}}, \"mappings\": {\"properties\": {\"test_field\":{\"type\":\"integer\"}}}," - + "\"settings\": {\"index.routing.allocation.include.node\": \"nowhere\"}}"; + String body2 = """ + { + "aliases": { + "test": {} + }, + "mappings": { + "properties": { + "test_field": { + "type": "integer" + } + } + }, + "settings": { + "index.routing.allocation.include.node": "nowhere" + } + }"""; createTest2.setJsonEntity(body2); createTest2.addParameter("timeout", "100ms"); client().performRequest(createTest2); @@ -37,8 +63,10 @@ public void createTestIndex() throws IOException { request.addParameter("refresh", "true"); StringBuilder bulk = new StringBuilder(); for (int i = 0; i < 20; i++) { - bulk.append("{\"index\":{}}\n"); - bulk.append("{\"test_field\":").append(i).append("}\n"); + bulk.append(""" + {"index":{}} + {"test_field":%s} + """.formatted(i)); } request.setJsonEntity(bulk.toString()); client().performRequest(request); diff --git a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java index 5bee5fd209ce9..b026cc856ac24 100644 --- a/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java +++ b/x-pack/plugin/sql/qa/jdbc/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/PreparedStatementTestCase.java @@ -268,7 +268,8 @@ public void testOutOfRangeBigDecimal() throws SQLException { } public void testWildcardField() throws IOException, SQLException { - String mapping = "\"properties\":{\"id\":{\"type\":\"integer\"},\"text\":{\"type\":\"wildcard\"}}"; + String mapping = """ + "properties":{"id":{"type":"integer"},"text":{"type":"wildcard"}}"""; createIndex("test", Settings.EMPTY, mapping); String text = randomAlphaOfLengthBetween(1, 10); @@ -298,7 +299,8 @@ public void testWildcardField() throws IOException, SQLException { } public void testConstantKeywordField() throws IOException, SQLException { - String mapping = "\"properties\":{\"id\":{\"type\":\"integer\"},\"text\":{\"type\":\"constant_keyword\"}}"; + String mapping = """ + "properties":{"id":{"type":"integer"},"text":{"type":"constant_keyword"}}"""; createIndex("test", Settings.EMPTY, mapping); String text = randomAlphaOfLengthBetween(1, 10); @@ -467,11 +469,14 @@ private static void setupIndexForDateTimeTestsWithNanos(long randomNanos) throws } private static void setupIndexForDateTimeTests(long randomMillisOrNanos, boolean isNanos) throws IOException { - String mapping = "\"properties\":{\"id\":{\"type\":\"integer\"},"; + String mapping = """ + "properties":{"id":{"type":"integer"},"""; if (isNanos) { - mapping += "\"test_date_nanos\":{\"type\":\"date_nanos\"}}"; + mapping += """ + "test_date_nanos":{"type":"date_nanos"}}"""; } else { - mapping += "\"birth_date\":{\"type\":\"date\"}}"; + mapping += """ + "birth_date":{"type":"date"}}"""; } createIndex("emps", Settings.EMPTY, mapping); for (int i = 1; i <= 3; i++) { diff --git a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java index 434b656850809..38f31038a6497 100644 --- a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java +++ b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlCompatIT.java @@ -114,7 +114,8 @@ private void testNullsOrderWithMissingOrderSupport(RestClient client) throws IOE @SuppressWarnings("unchecked") private List runOrderByNullsLastQuery(RestClient queryClient) throws IOException { Request putIndex = new Request("PUT", "/test"); - putIndex.setJsonEntity("{\"settings\":{\"index\":{\"number_of_shards\":3}}}"); + putIndex.setJsonEntity(""" + {"settings":{"index":{"number_of_shards":3}}}"""); client().performRequest(putIndex); Request indexDocs = new Request("POST", "/test/_bulk"); diff --git a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java index 634f033fd5580..501c1bed2a446 100644 --- a/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java +++ b/x-pack/plugin/sql/qa/mixed-node/src/test/java/org/elasticsearch/xpack/sql/qa/mixed_node/SqlSearchIT.java @@ -97,14 +97,16 @@ public void testAllTypesWithRequestToOldNodes() throws Exception { // floats were indexed as Doubles and the values returned had a greater precision and more decimals builder.append(","); if (isBwcNodeBeforeFieldsApiInQL) { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + builder.append(""" + "geo_point_field":{"lat":"37.386483", "lon":"-122.083843"},"""); fieldValues.put("geo_point_field", "POINT (-122.08384302444756 37.38648299127817)"); builder.append("\"float_field\":" + randomFloat + ","); fieldValues.put("float_field", Double.valueOf(randomFloat)); builder.append("\"half_float_field\":123.456"); fieldValues.put("half_float_field", 123.45600128173828d); } else { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + builder.append(""" + "geo_point_field":{"lat":"37.386483", "lon":"-122.083843"},"""); fieldValues.put("geo_point_field", "POINT (-122.083843 37.386483)"); builder.append("\"float_field\":" + randomFloat + ","); /* @@ -133,14 +135,16 @@ public void testAllTypesWithRequestToUpgradedNodes() throws Exception { Float randomFloat = randomFloat(); builder.append(","); if (isBwcNodeBeforeFieldsApiInQL) { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + builder.append(""" + "geo_point_field":{"lat":"37.386483", "lon":"-122.083843"},"""); fieldValues.put("geo_point_field", "POINT (-122.08384302444756 37.38648299127817)"); builder.append("\"float_field\":" + randomFloat + ","); fieldValues.put("float_field", Double.valueOf(randomFloat)); builder.append("\"half_float_field\":123.456"); fieldValues.put("half_float_field", 123.45600128173828d); } else { - builder.append("\"geo_point_field\":{\"lat\":\"37.386483\", \"lon\":\"-122.083843\"},"); + builder.append(""" + "geo_point_field":{"lat":"37.386483", "lon":"-122.083843"},"""); fieldValues.put("geo_point_field", "POINT (-122.083843 37.386483)"); builder.append("\"float_field\":" + randomFloat + ","); /* @@ -214,7 +218,8 @@ private Map prepareTestData( "\"constant_keyword_field\": \"" + fieldValues.computeIfAbsent("constant_keyword_field", v -> constantKeywordValue) + "\"," ); builder.append("\"wildcard_field\": \"" + fieldValues.computeIfAbsent("wildcard_field", v -> randomAlphaOfLength(5)) + "\","); - builder.append("\"geo_point_no_dv_field\":{\"lat\":\"40.123456\", \"lon\":\"100.234567\"},"); + builder.append(""" + "geo_point_no_dv_field":{"lat":"40.123456", "lon":"100.234567"},"""); fieldValues.put("geo_point_no_dv_field", "POINT (100.234567 40.123456)"); builder.append("\"geo_shape_field\":\"POINT (-122.083843 37.386483 30)\","); fieldValues.put("geo_shape_field", "POINT (-122.083843 37.386483 30.0)"); diff --git a/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java b/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java index a7ebd86cb1cee..9b0546b5bdf20 100644 --- a/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java +++ b/x-pack/plugin/sql/qa/server/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java @@ -93,8 +93,10 @@ private void createTestData(int documents) throws UnsupportedCharsetException, I int a = 3 * i; int b = a + 1; int c = b + 1; - bulk.append("{\"index\":{\"_id\":\"" + i + "\"}}\n"); - bulk.append("{\"a\": " + a + ", \"b\": " + b + ", \"c\": " + c + "}\n"); + bulk.append(""" + {"index":{"_id":"%s"}} + {"a": %s, "b": %s, "c": %s} + """.formatted(i, a, b, c)); } request.setJsonEntity(bulk.toString()); diff --git a/x-pack/plugin/sql/qa/server/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/SqlSecurityTestCase.java b/x-pack/plugin/sql/qa/server/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/SqlSecurityTestCase.java index a760892f3f6f2..3d88bfb8a5a0e 100644 --- a/x-pack/plugin/sql/qa/server/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/SqlSecurityTestCase.java +++ b/x-pack/plugin/sql/qa/server/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/SqlSecurityTestCase.java @@ -175,14 +175,15 @@ public void oneTimeSetup() throws Exception { Request request = new Request("PUT", "/_bulk"); request.addParameter("refresh", "true"); - StringBuilder bulk = new StringBuilder(); - bulk.append("{\"index\":{\"_index\": \"test\", \"_id\":\"1\"}}\n"); - bulk.append("{\"a\": 1, \"b\": 2, \"c\": 3}\n"); - bulk.append("{\"index\":{\"_index\": \"test\", \"_id\":\"2\"}}\n"); - bulk.append("{\"a\": 4, \"b\": 5, \"c\": 6}\n"); - bulk.append("{\"index\":{\"_index\": \"bort\", \"_id\":\"1\"}}\n"); - bulk.append("{\"a\": \"test\"}\n"); - request.setJsonEntity(bulk.toString()); + String bulk = """ + {"index":{"_index": "test", "_id":"1"}} + {"a": 1, "b": 2, "c": 3} + {"index":{"_index": "test", "_id":"2"}} + {"a": 4, "b": 5, "c": 6} + {"index":{"_index": "bort", "_id":"1"}} + {"a": "test"} + """; + request.setJsonEntity(bulk); client().performRequest(request); oneTimeSetup = true; } diff --git a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcShardFailureIT.java b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcShardFailureIT.java index 8889050a51655..7934a597df42b 100644 --- a/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcShardFailureIT.java +++ b/x-pack/plugin/sql/qa/server/single-node/src/test/java/org/elasticsearch/xpack/sql/qa/single_node/JdbcShardFailureIT.java @@ -21,13 +21,28 @@ public class JdbcShardFailureIT extends JdbcIntegrationTestCase { @Before public void createTestIndex() throws IOException { Request createTest1 = new Request("PUT", "/test1"); - String body1 = "{\"aliases\":{\"test\":{}}, \"mappings\": {\"properties\": {\"test_field\":{\"type\":\"integer\"}}}}"; + String body1 = """ + {"aliases":{"test":{}}, "mappings": {"properties": {"test_field":{"type":"integer"}}}}"""; createTest1.setJsonEntity(body1); client().performRequest(createTest1); Request createTest2 = new Request("PUT", "/test2"); - String body2 = "{\"aliases\":{\"test\":{}}, \"mappings\": {\"properties\": {\"test_field\":{\"type\":\"integer\"}}}," - + "\"settings\": {\"index.routing.allocation.include.node\": \"nowhere\"}}"; + String body2 = """ + { + "aliases": { + "test": {} + }, + "mappings": { + "properties": { + "test_field": { + "type": "integer" + } + } + }, + "settings": { + "index.routing.allocation.include.node": "nowhere" + } + }"""; createTest2.setJsonEntity(body2); createTest2.addParameter("timeout", "100ms"); client().performRequest(createTest2); @@ -36,8 +51,10 @@ public void createTestIndex() throws IOException { request.addParameter("refresh", "true"); StringBuilder bulk = new StringBuilder(); for (int i = 0; i < 20; i++) { - bulk.append("{\"index\":{}}\n"); - bulk.append("{\"test_field\":" + i + "}\n"); + bulk.append(""" + {"index":{}} + {"test_field":%s} + """.formatted(i)); } request.setJsonEntity(bulk.toString()); client().performRequest(request); diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/CustomDateFormatTestCase.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/CustomDateFormatTestCase.java index 3c9b97ecffbb2..c31d8b3685562 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/CustomDateFormatTestCase.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/CustomDateFormatTestCase.java @@ -48,11 +48,10 @@ public void testCustomDateFormatsWithNowFunctions() throws IOException { for (int i = 0; i < customFormats.length; i++) { String field = "date_" + i; - docs[i] = "{\"" - + field - + "\":\"" - + DateTimeFormatter.ofPattern(customFormats[i], Locale.ROOT).format(DateUtils.nowWithMillisResolution()) - + "\"}"; + String format = DateTimeFormatter.ofPattern(customFormats[i], Locale.ROOT).format(DateUtils.nowWithMillisResolution()); + docs[i] = """ + {"%s":"%s"} + """.formatted(field, format); datesConditions.append(i > 0 ? " OR " : "").append(field + randomFrom(operators) + randomFrom(nowFunctions)); } @@ -63,7 +62,8 @@ public void testCustomDateFormatsWithNowFunctions() throws IOException { request.setEntity(new StringEntity(query(query).mode(Mode.PLAIN).timeZone(zID).toString(), ContentType.APPLICATION_JSON)); Response response = client().performRequest(request); - String expectedJsonSnippet = "{\"columns\":[{\"name\":\"c\",\"type\":\"long\"}],\"rows\":[["; + String expectedJsonSnippet = """ + {"columns":[{"name":"c","type":"long"}],"rows":[["""; try (InputStream content = response.getEntity().getContent()) { String actualJson = new BytesArray(content.readAllBytes()).utf8ToString(); // we just need to get a response that's not a date parsing error diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/FieldExtractorTestCase.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/FieldExtractorTestCase.java index 486571013d419..fe8e880d0f9b3 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/FieldExtractorTestCase.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/FieldExtractorTestCase.java @@ -32,6 +32,7 @@ import static java.util.Arrays.asList; import static java.util.Collections.singletonList; +import static org.elasticsearch.common.xcontent.XContentHelper.stripWhitespace; import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.assertResponse; import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo; import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.expectBadRequest; @@ -450,7 +451,9 @@ public void testGeoShapeField() throws IOException { actualValue = "\"foo\""; } createIndexWithFieldTypeAndProperties("geo_shape", fieldProps, explicitSourceSetting ? indexProps : null); - index("{\"geo_shape_field\":{\"type\":\"point\",\"coordinates\":" + actualValue + "}}"); + index(""" + {"geo_shape_field":{"type":"point","coordinates":%s}} + """.formatted(actualValue)); if (explicitSourceSetting == false || enableSource) { Map expected = new HashMap<>(); @@ -1073,42 +1076,45 @@ public void testByteFieldWithIntegerSubfield() throws IOException { public void testNestedFieldsHierarchyWithMultiNestedValues() throws IOException { Request request = new Request("PUT", "/test"); - request.setJsonEntity( - "{" - + " \"mappings\" : {" - + " \"properties\" : {" - + " \"h\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"i\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"j\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"f\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"o\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"b\" : {" - + " \"properties\" : {" - + " \"a\" : {" - + " \"type\" : \"keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + "}" - ); + request.setJsonEntity(""" + { + "mappings": { + "properties": { + "h": { + "type": "nested", + "properties": { + "i": { + "type": "keyword" + }, + "j": { + "type": "keyword" + }, + "f": { + "type": "nested", + "properties": { + "o": { + "type": "keyword" + }, + "b": { + "properties": { + "a": { + "type": "keyword" + } + } + } + } + } + } + } + } + } + }"""); client().performRequest(request); - index("{\"h\": [{\"i\":\"123\", \"j\":\"abc\"}, {\"i\":\"890\", \"j\":\"xyz\"}, {\"i\":\"567\", \"j\":\"klm\"}],\"test\":\"foo\"}"); + index(stripWhitespace(""" + { + "h": [ { "i": "123", "j": "abc" }, { "i": "890", "j": "xyz" }, { "i": "567", "j": "klm" } ], + "test": "foo" + }""")); Map expected = new HashMap<>(); expected.put( @@ -1125,39 +1131,41 @@ public void testNestedFieldsHierarchyWithMultiNestedValues() throws IOException public void testNestedFieldsHierarchyWithMissingValue() throws IOException { Request request = new Request("PUT", "/test"); - request.setJsonEntity( - "{" - + " \"mappings\" : {" - + " \"properties\" : {" - + " \"h\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"i\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"f\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"o\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"b\" : {" - + " \"properties\" : {" - + " \"a\" : {" - + " \"type\" : \"keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + "}" - ); + request.setJsonEntity(""" + { + "mappings": { + "properties": { + "h": { + "type": "nested", + "properties": { + "i": { + "type": "keyword" + }, + "f": { + "type": "nested", + "properties": { + "o": { + "type": "keyword" + }, + "b": { + "properties": { + "a": { + "type": "keyword" + } + } + } + } + } + } + } + } + } + }"""); client().performRequest(request); - index("{\"h\": [{\"f\":{\"b\": {\"a\": \"ABC\"}}}]}"); + index(stripWhitespace(""" + { + "h": [ { "f": { "b": { "a": "ABC" } } } ] + }""")); Map expected = new HashMap<>(); expected.put("columns", singletonList(columnInfo("plain", "h.f.o", "keyword", JDBCType.VARCHAR, Integer.MAX_VALUE))); @@ -1170,39 +1178,41 @@ public void testNestedFieldsHierarchyWithMissingValue() throws IOException { public void testNestedFieldsHierarchyExtractDeeplyNestedValue() throws IOException { Request request = new Request("PUT", "/test"); - request.setJsonEntity( - "{" - + " \"mappings\" : {" - + " \"properties\" : {" - + " \"h\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"i\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"f\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"o\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"b\" : {" - + " \"properties\" : {" - + " \"a\" : {" - + " \"type\" : \"keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + "}" - ); + request.setJsonEntity(""" + { + "mappings": { + "properties": { + "h": { + "type": "nested", + "properties": { + "i": { + "type": "keyword" + }, + "f": { + "type": "nested", + "properties": { + "o": { + "type": "keyword" + }, + "b": { + "properties": { + "a": { + "type": "keyword" + } + } + } + } + } + } + } + } + } + }"""); client().performRequest(request); - index("{\"h\": [{\"f\":{\"b\": {\"a\": \"ABC\"}}}]}"); + index(stripWhitespace(""" + { + "h": [ { "f": { "b": { "a": "ABC" } } } ] + }""")); Map expected = new HashMap<>(); expected.put("columns", singletonList(columnInfo("plain", "h.f.b.a", "keyword", JDBCType.VARCHAR, Integer.MAX_VALUE))); @@ -1212,45 +1222,58 @@ public void testNestedFieldsHierarchyExtractDeeplyNestedValue() throws IOExcepti public void testNestedFieldsHierarchyWithArrayOfValues() throws IOException { Request request = new Request("PUT", "/test"); - request.setJsonEntity( - "{" - + " \"mappings\" : {" - + " \"properties\" : {" - + " \"h\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"i\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"j\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"f\" : {" - + " \"type\" : \"nested\"," - + " \"properties\" : {" - + " \"o\" : {" - + " \"type\" : \"keyword\"" - + " }," - + " \"b\" : {" - + " \"properties\" : {" - + " \"a\" : {" - + " \"type\" : \"keyword\"" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + " }" - + "}" - ); + request.setJsonEntity(stripWhitespace(""" + { + "mappings": { + "properties": { + "h": { + "type": "nested", + "properties": { + "i": { + "type": "keyword" + }, + "j": { + "type": "keyword" + }, + "f": { + "type": "nested", + "properties": { + "o": { + "type": "keyword" + }, + "b": { + "properties": { + "a": { + "type": "keyword" + } + } + } + } + } + } + } + } + } + }""")); client().performRequest(request); - index( - "{\"h\": [{\"i\":[\"123\",\"124\",\"125\"], \"j\":\"abc\"}, {\"i\":\"890\", \"j\":\"xyz\"}, {\"i\":\"567\", \"j\":\"klm\"}]," - + "\"test\":\"foo\"}" - ); + index(stripWhitespace(""" + { + "h": [ + { + "i": [ "123", "124", "125" ], + "j": "abc" + }, + { + "i": "890", + "j": "xyz" + }, + { + "i": "567", + "j": "klm" + } + ], + "test": "foo" + }""")); Map expected = new HashMap<>(); Map actual = new HashMap<>(); diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/geo/GeoDataLoader.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/geo/GeoDataLoader.java index d8d937fb813b4..c63468520b1ca 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/geo/GeoDataLoader.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/geo/GeoDataLoader.java @@ -27,6 +27,7 @@ import java.io.InputStreamReader; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Map; import static org.elasticsearch.xpack.sql.qa.jdbc.DataLoader.createString; @@ -45,16 +46,21 @@ public static void main(String[] args) throws Exception { protected static void loadOGCDatasetIntoEs(RestClient client, String index) throws Exception { createIndex(client, index, createOGCIndexRequest()); loadData(client, index, readResource("/ogc/ogc.json")); - makeFilteredAlias(client, "lakes", index, "\"term\" : { \"ogc_type\" : \"lakes\" }"); - makeFilteredAlias(client, "road_segments", index, "\"term\" : { \"ogc_type\" : \"road_segments\" }"); - makeFilteredAlias(client, "divided_routes", index, "\"term\" : { \"ogc_type\" : \"divided_routes\" }"); - makeFilteredAlias(client, "forests", index, "\"term\" : { \"ogc_type\" : \"forests\" }"); - makeFilteredAlias(client, "bridges", index, "\"term\" : { \"ogc_type\" : \"bridges\" }"); - makeFilteredAlias(client, "streams", index, "\"term\" : { \"ogc_type\" : \"streams\" }"); - makeFilteredAlias(client, "buildings", index, "\"term\" : { \"ogc_type\" : \"buildings\" }"); - makeFilteredAlias(client, "ponds", index, "\"term\" : { \"ogc_type\" : \"ponds\" }"); - makeFilteredAlias(client, "named_places", index, "\"term\" : { \"ogc_type\" : \"named_places\" }"); - makeFilteredAlias(client, "map_neatlines", index, "\"term\" : { \"ogc_type\" : \"map_neatlines\" }"); + List aliases = """ + lakes + road_segments + divided_routes + forests + bridges + streams + buildings + ponds + named_places + map_neatlines + """.lines().toList(); + for (String alias : aliases) { + makeFilteredAlias(client, alias, index, "\"term\" : { \"ogc_type\" : \"%s\" }".formatted(alias)); + } } private static String createOGCIndexRequest() throws Exception { diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/DataLoader.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/DataLoader.java index f642a36af0752..8acbfe809e592 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/DataLoader.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/DataLoader.java @@ -79,7 +79,12 @@ private static void loadNoColsDatasetIntoEs(RestClient client, String index) thr createEmptyIndex(client, index); Request request = new Request("POST", "/" + index + "/_bulk"); request.addParameter("refresh", "true"); - request.setJsonEntity("{\"index\":{}}\n{}\n" + "{\"index\":{}}\n{}\n"); + request.setJsonEntity(""" + {"index":{}} + {} + {"index":{}} + {} + """); client.performRequest(request); } diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/BaseRestSqlTestCase.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/BaseRestSqlTestCase.java index 9e1186320d9a6..14f2d9d72f9f6 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/BaseRestSqlTestCase.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/BaseRestSqlTestCase.java @@ -179,8 +179,10 @@ protected void indexWithIndexName(String indexName, String... docs) throws IOExc request.addParameter("refresh", "true"); StringBuilder bulk = new StringBuilder(); for (String doc : docs) { - bulk.append("{\"index\":{}}\n"); - bulk.append(doc + "\n"); + bulk.append(""" + {"index":{}} + %s + """.formatted(doc)); } request.setJsonEntity(bulk.toString()); provisioningClient().performRequest(request); diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java index 1f611d7d4989d..9226b16591759 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java @@ -301,10 +301,11 @@ public void testScoreWithFieldNamedScore() throws IOException { Request request = new Request("POST", "/test/_bulk"); request.addParameter("refresh", "true"); String mode = randomMode(); - StringBuilder bulk = new StringBuilder(); - bulk.append("{\"index\":{\"_id\":\"1\"}}\n"); - bulk.append("{\"name\":\"test\", \"score\":10}\n"); - request.setJsonEntity(bulk.toString()); + String bulk = """ + {"index":{"_id":"1"}} + {"name":"test", "score":10} + """; + request.setJsonEntity(bulk); provisioningClient().performRequest(request); Map expected = new HashMap<>(); @@ -687,37 +688,41 @@ public void testPrettyPrintingEnabled() throws IOException { boolean columnar = randomBoolean(); String expected = ""; if (columnar) { - expected = "{\n" - + " \"columns\" : [\n" - + " {\n" - + " \"name\" : \"test1\",\n" - + " \"type\" : \"text\"\n" - + " }\n" - + " ],\n" - + " \"values\" : [\n" - + " [\n" - + " \"test1\",\n" - + " \"test2\"\n" - + " ]\n" - + " ]\n" - + "}\n"; + expected = """ + { + "columns" : [ + { + "name" : "test1", + "type" : "text" + } + ], + "values" : [ + [ + "test1", + "test2" + ] + ] + } + """; } else { - expected = "{\n" - + " \"columns\" : [\n" - + " {\n" - + " \"name\" : \"test1\",\n" - + " \"type\" : \"text\"\n" - + " }\n" - + " ],\n" - + " \"rows\" : [\n" - + " [\n" - + " \"test1\"\n" - + " ],\n" - + " [\n" - + " \"test2\"\n" - + " ]\n" - + " ]\n" - + "}\n"; + expected = """ + { + "columns" : [ + { + "name" : "test1", + "type" : "text" + } + ], + "rows" : [ + [ + "test1" + ], + [ + "test2" + ] + ] + } + """; } executeAndAssertPrettyPrinting(expected, "true", columnar); } @@ -726,9 +731,11 @@ public void testPrettyPrintingDisabled() throws IOException { boolean columnar = randomBoolean(); String expected = ""; if (columnar) { - expected = "{\"columns\":[{\"name\":\"test1\",\"type\":\"text\"}],\"values\":[[\"test1\",\"test2\"]]}"; + expected = """ + {"columns":[{"name":"test1","type":"text"}],"values":[["test1","test2"]]}"""; } else { - expected = "{\"columns\":[{\"name\":\"test1\",\"type\":\"text\"}],\"rows\":[[\"test1\"],[\"test2\"]]}"; + expected = """ + {"columns":[{"name":"test1","type":"text"}],"rows":[["test1"],["test2"]]}"""; } executeAndAssertPrettyPrinting(expected, randomFrom("false", null), columnar); } @@ -975,7 +982,12 @@ public void testTranslateQueryWithGroupByAndHaving() throws IOException { public void testBasicQueryText() throws IOException { index("{\"test\":\"test\"}", "{\"test\":\"test\"}"); - String expected = " test \n" + "---------------\n" + "test \n" + "test \n"; + String expected = """ + test \s + --------------- + test \s + test \s + """; Tuple response = runSqlAsText("SELECT * FROM " + indexPattern("test"), "text/plain"); assertEquals(expected, response.v1()); } @@ -997,7 +1009,12 @@ public void testDefaultQueryInCSV() throws IOException { "{\"name\":" + toJson("\"third,\"") + ", \"number\": 3 }" ); - String expected = "name,number\r\n" + "first,1\r\n" + "second\t,2\r\n" + "\"\"\"third,\"\"\",3\r\n"; + String expected = """ + name,number\r + first,1\r + second\t,2\r + ""\"third,""\",3\r + """; String query = "SELECT * FROM " + indexPattern("test") + " ORDER BY number"; Tuple response = runSqlAsText(query, "text/csv"); @@ -1014,7 +1031,11 @@ public void testQueryWithoutHeaderInCSV() throws IOException { "{\"name\":" + toJson("\"third,\"") + ", \"number\": 3 }" ); - String expected = "first,1\r\n" + "second\t,2\r\n" + "\"\"\"third,\"\"\",3\r\n"; + String expected = """ + first,1\r + second\t,2\r + ""\"third,""\",3\r + """; String query = "SELECT * FROM " + indexPattern("test") + " ORDER BY number"; Tuple response = runSqlAsText(query, "text/csv; header=absent"); @@ -1061,7 +1082,12 @@ public void testQueryInTSV() throws IOException { "{\"name\":" + toJson("\"third,\"") + ", \"number\": 3 }" ); - String expected = "name\tnumber\n" + "first\t1\n" + "second\\t\t2\n" + "\"third,\"\t3\n"; + String expected = """ + name\tnumber + first\t1 + second\\t\t2 + "third,"\t3 + """; String query = "SELECT * FROM " + indexPattern("test") + " ORDER BY number"; Tuple response = runSqlAsText(query, "text/tab-separated-values"); @@ -1129,7 +1155,9 @@ private void executeQueryWithNextPage(String format, String expectedHeader, Stri int size = 20; String[] docs = new String[size]; for (int i = 0; i < size; i++) { - docs[i] = "{\"text\":\"text" + i + "\", \"number\":" + i + "}\n"; + docs[i] = """ + {"text":"text%s", "number":%s} + """.formatted(i, i); } index(docs); @@ -1184,8 +1212,10 @@ private static void bulkLoadTestData(int count) throws IOException { request.addParameter("refresh", "true"); StringBuilder bulk = new StringBuilder(); for (int i = 0; i < count; i++) { - bulk.append("{\"index\":{\"_id\":\"" + i + "\"}}\n"); - bulk.append("{\"text\":\"text" + i + "\", \"number\":" + i + "}\n"); + bulk.append(""" + {"index":{"_id":"%s"}} + {"text":"text%s", "number":%s} + """.formatted(i, i, i)); } request.setJsonEntity(bulk.toString()); provisioningClient().performRequest(request); diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlUsageTestCase.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlUsageTestCase.java index 63fed82f70b24..660475920f2af 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlUsageTestCase.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlUsageTestCase.java @@ -301,8 +301,10 @@ private void index(List docs) throws IOException { request.addParameter("refresh", "true"); StringBuilder bulk = new StringBuilder(); for (IndexDocument doc : docs) { - bulk.append("{\"index\":{}}\n"); - bulk.append("{\"condition\":\"" + doc.condition + "\",\"name\":\"" + doc.name + "\",\"page_count\":" + doc.pageCount + "}\n"); + bulk.append(""" + {"index":{}} + {"condition":"%s","name":"%s","page_count":%s} + """.formatted(doc.condition, doc.name, doc.pageCount)); } request.setJsonEntity(bulk.toString()); client().performRequest(request); diff --git a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java index 9c1a6d73da952..5f6593c34e5f4 100644 --- a/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java +++ b/x-pack/plugin/sql/sql-action/src/test/java/org/elasticsearch/xpack/sql/action/SqlRequestParsersTests.java @@ -36,45 +36,46 @@ public void testUnknownFieldParsingErrors() throws IOException { public void testUnknownModeFieldParsingErrors() throws IOException { assertParsingErrorMessageReason( - "{\"cursor\":\"foo\",\"mode\" : \"value\"}", + """ + {"cursor":"foo","mode" : "value"}""", "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", SqlClearCursorRequest::fromXContent ); assertParsingErrorMessageReason( - "{\"cursor\":\"foo\",\"mode\" : \"value\"}", + """ + {"cursor":"foo","mode" : "value"}""", "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", SqlQueryRequest::fromXContent ); - assertParsingErrorMessageReason( - "{\"mode\" : \"value\"}", - "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", - SqlTranslateRequest::fromXContent - ); + assertParsingErrorMessageReason(""" + {"mode" : "value"}""", "No enum constant org.elasticsearch.xpack.sql.proto.Mode.VALUE", SqlTranslateRequest::fromXContent); } public void testClearCursorRequestParser() throws IOException { assertParsingErrorMessage("{\"mode\" : \"jdbc\"}", "Required [cursor]", SqlClearCursorRequest::fromXContent); - assertParsingErrorMessage( - "{\"cursor\" : \"whatever\", \"fetch_size\":123}", - "unknown field [fetch_size]", - SqlClearCursorRequest::fromXContent - ); + assertParsingErrorMessage(""" + {"cursor" : "whatever", "fetch_size":123}""", "unknown field [fetch_size]", SqlClearCursorRequest::fromXContent); Mode randomMode = randomFrom(Mode.values()); - SqlClearCursorRequest request = generateRequest( - "{\"cursor\" : \"whatever\", \"mode\" : \"" + randomMode.toString() + "\", \"client_id\" : \"bla\", \"version\": \"1.2.3\"}", - SqlClearCursorRequest::fromXContent - ); + SqlClearCursorRequest request = generateRequest(""" + { + "cursor": "whatever", + "mode": "%s", + "client_id": "bla", + "version": "1.2.3" + }""".formatted(randomMode), SqlClearCursorRequest::fromXContent); assertNull(request.clientId()); assertNull(request.version()); assertEquals(randomMode, request.mode()); assertEquals("whatever", request.getCursor()); randomMode = randomFrom(Mode.values()); - request = generateRequest( - "{\"cursor\" : \"whatever\", \"mode\" : \"" + randomMode.toString() + "\", \"client_id\" : \"bla\"}", - SqlClearCursorRequest::fromXContent - ); + request = generateRequest(""" + { + "cursor": "whatever", + "mode": "%s", + "client_id": "bla" + }""".formatted(randomMode.toString()), SqlClearCursorRequest::fromXContent); assertNull(request.clientId()); assertEquals(randomMode, request.mode()); assertEquals("whatever", request.getCursor()); @@ -84,16 +85,19 @@ public void testClearCursorRequestParser() throws IOException { assertEquals(Mode.PLAIN, request.mode()); assertEquals("whatever", request.getCursor()); - request = generateRequest( - "{\"cursor\" : \"whatever\", \"client_id\" : \"CLI\", \"version\": \"1.2.3\"}", - SqlClearCursorRequest::fromXContent - ); + request = generateRequest(""" + { + "cursor": "whatever", + "client_id": "CLI", + "version": "1.2.3" + }""", SqlClearCursorRequest::fromXContent); assertNull(request.clientId()); assertNull(request.version()); assertEquals(Mode.PLAIN, request.mode()); assertEquals("whatever", request.getCursor()); - request = generateRequest("{\"cursor\" : \"whatever\", \"client_id\" : \"cANVAs\"}", SqlClearCursorRequest::fromXContent); + request = generateRequest(""" + {"cursor" : "whatever", "client_id" : "cANVAs"}""", SqlClearCursorRequest::fromXContent); assertEquals("canvas", request.clientId()); assertEquals(Mode.PLAIN, request.mode()); assertEquals("whatever", request.getCursor()); @@ -102,26 +106,26 @@ public void testClearCursorRequestParser() throws IOException { public void testTranslateRequestParser() throws IOException { assertParsingErrorMessage("{\"qquery\" : \"select * from bla\"}", "unknown field [qquery]", SqlTranslateRequest::fromXContent); - SqlTranslateRequest request = generateRequest("{\"query\" : \"select * from foo\"}", SqlTranslateRequest::fromXContent); + SqlTranslateRequest request = generateRequest(""" + {"query" : "select * from foo"}""", SqlTranslateRequest::fromXContent); assertEquals("select * from foo", request.query()); assertEquals(Mode.PLAIN, request.mode()); Mode randomMode = randomFrom(Mode.values()); - request = generateRequest( - "{\"query\" : \"whatever\", \"client_id\" : \"foo\", \"mode\":\"" + randomMode.toString() + "\"}", - SqlTranslateRequest::fromXContent - ); + request = generateRequest(""" + { + "query": "whatever", + "client_id": "foo", + "mode": "%s" + }""".formatted(randomMode.toString()), SqlTranslateRequest::fromXContent); assertNull(request.clientId()); assertEquals(randomMode, request.mode()); } public void testQueryRequestParser() throws IOException { assertParsingErrorMessage("{\"mode\" : 123}", "mode doesn't support values of type: VALUE_NUMBER", SqlQueryRequest::fromXContent); - assertParsingErrorMessage( - "{\"cursor\" : \"whatever\", \"fetch_size\":\"abc\"}", - "failed to parse field [fetch_size]", - SqlQueryRequest::fromXContent - ); + assertParsingErrorMessage(""" + {"cursor" : "whatever", "fetch_size":"abc"}""", "failed to parse field [fetch_size]", SqlQueryRequest::fromXContent); assertParsingErrorMessage( "{\"client_id\":123}", "client_id doesn't support values of type: VALUE_NUMBER", @@ -132,7 +136,8 @@ public void testQueryRequestParser() throws IOException { "version doesn't support values of type: VALUE_NUMBER", SqlQueryRequest::fromXContent ); - assertParsingErrorMessage("{\"params\":[{\"value\":123}]}", "failed to parse field [params]", SqlQueryRequest::fromXContent); + assertParsingErrorMessage(""" + {"params":[{"value":123}]}""", "failed to parse field [params]", SqlQueryRequest::fromXContent); assertParsingErrorMessage( "{\"time_zone\":12}", "time_zone doesn't support values of type: VALUE_NUMBER", @@ -152,19 +157,18 @@ public void testQueryRequestParser() throws IOException { list.add(new SqlTypedParamValue("integer", 123, false)); } - SqlQueryRequest request = generateRequest( - "{\"cursor\" : \"whatever\", \"mode\" : \"" - + randomMode.toString() - + "\", \"client_id\" : \"bla\"," - + clientVersion - + "\"query\":\"select\"," - + "\"params\":[" - + params - + "]," - + " \"time_zone\":\"UTC\"," - + "\"request_timeout\":\"5s\",\"page_timeout\":\"10s\"}", - SqlQueryRequest::fromXContent - ); + SqlQueryRequest request = generateRequest(""" + { + "cursor": "whatever", + "mode": "%s", + "client_id": "bla", + %s + "query": "select", + "params": [ %s ], + "time_zone": "UTC", + "request_timeout": "5s", + "page_timeout": "10s" + }""".formatted(randomMode.toString(), clientVersion, params), SqlQueryRequest::fromXContent); assertNull(request.clientId()); assertEquals(randomMode, request.mode()); if (Mode.isDedicatedClient(randomMode)) { @@ -181,15 +185,28 @@ public void testQueryRequestParser() throws IOException { public void testParamsSuccessfulParsingInDriverMode() throws IOException { Mode driverMode = randomValueOtherThanMany((m) -> Mode.isDriver(m) == false, () -> randomFrom(Mode.values())); - String json = "{" - + " \"params\":[{\"type\":\"integer\",\"value\":35000}," - + " {\"type\":\"date\",\"value\":\"1960-01-01\"}," - + " {\"type\":\"boolean\",\"value\":false}," - + " {\"type\":\"keyword\",\"value\":\"foo\"}]," - + " \"mode\": \"" - + driverMode.toString() - + "\"" - + "}"; + String json = """ + { + "params": [ + { + "type": "integer", + "value": 35000 + }, + { + "type": "date", + "value": "1960-01-01" + }, + { + "type": "boolean", + "value": false + }, + { + "type": "keyword", + "value": "foo" + } + ], + "mode": "%s" + }""".formatted(driverMode); SqlQueryRequest request = generateRequest(json, SqlQueryRequest::fromXContent); List params = request.params(); assertEquals(4, params.size()); @@ -213,12 +230,11 @@ public void testParamsSuccessfulParsingInDriverMode() throws IOException { public void testParamsSuccessfulParsingInNonDriverMode() throws IOException { Mode nonDriverMode = randomValueOtherThanMany(Mode::isDriver, () -> randomFrom(Mode.values())); - String json = "{" - + " \"params\":[35000,\"1960-01-01\",false,\"foo\"]," - + " \"mode\": \"" - + nonDriverMode.toString() - + "\"" - + "}"; + String json = """ + { + "params": [ 35000, "1960-01-01", false, "foo" ], + "mode": "%s" + }""".formatted(nonDriverMode); SqlQueryRequest request = generateRequest(json, SqlQueryRequest::fromXContent); List params = request.params(); assertEquals(4, params.size()); @@ -242,18 +258,22 @@ public void testParamsSuccessfulParsingInNonDriverMode() throws IOException { public void testParamsParsingFailure_QueryRequest_NonDriver() throws IOException { Mode m = randomValueOtherThanMany(Mode::isDriver, () -> randomFrom(Mode.values())); + assertXContentParsingErrorMessage(""" + { + "params": [ { "whatever": 35000 }, "1960-01-01", false, "foo" ], + "mode": "%s" + }""".formatted(m), "[sql/query] failed to parse field [params]", SqlQueryRequest::fromXContent); + assertXContentParsingErrorMessage(""" + { + "params": [ 350.123, "1960-01-01", { "foobar": false }, "foo" ], + "mode": "}%s" + }""".formatted(m), "[sql/query] failed to parse field [params]", SqlQueryRequest::fromXContent); assertXContentParsingErrorMessage( - "{\"params\":[{\"whatever\":35000},\"1960-01-01\",false,\"foo\"],\"mode\": \"" + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlQueryRequest::fromXContent - ); - assertXContentParsingErrorMessage( - "{\"params\":[350.123,\"1960-01-01\",{\"foobar\":false},\"foo\"],\"mode\": \"}" + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlQueryRequest::fromXContent - ); - assertXContentParsingErrorMessage( - "{\"mode\": \"" + m.toString() + "\",\"params\":[350.123,\"1960-01-01\",false," + "{\"type\":\"keyword\",\"value\":\"foo\"}]}", + """ + { + "mode": "%s", + "params": [ 350.123, "1960-01-01", false, { "type": "keyword", "value": "foo" } ] + }""".formatted(m), "[params] must be an array where each entry is a single field (no objects supported)", SqlQueryRequest::fromXContent ); @@ -261,18 +281,22 @@ public void testParamsParsingFailure_QueryRequest_NonDriver() throws IOException public void testParamsParsingFailure_TranslateRequest_NonDriver() throws IOException { Mode m = randomValueOtherThanMany(Mode::isDriver, () -> randomFrom(Mode.values())); + assertXContentParsingErrorMessage(""" + { + "params": [ { "whatever": 35000 }, "1960-01-01", false, "foo" ], + "mode": "%s" + }""".formatted(m), "[sql/query] failed to parse field [params]", SqlTranslateRequest::fromXContent); + assertXContentParsingErrorMessage(""" + { + "params": [ 350.123, "1960-01-01", { "foobar": false }, "foo" ], + "mode": "}%s" + }""".formatted(m), "[sql/query] failed to parse field [params]", SqlTranslateRequest::fromXContent); assertXContentParsingErrorMessage( - "{\"params\":[{\"whatever\":35000},\"1960-01-01\",false,\"foo\"],\"mode\": \"" + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlTranslateRequest::fromXContent - ); - assertXContentParsingErrorMessage( - "{\"params\":[350.123,\"1960-01-01\",{\"foobar\":false},\"foo\"],\"mode\": \"}" + m.toString() + "\"}", - "[sql/query] failed to parse field [params]", - SqlTranslateRequest::fromXContent - ); - assertXContentParsingErrorMessage( - "{\"mode\": \"" + m.toString() + "\",\"params\":[350.123,\"1960-01-01\",false," + "{\"type\":\"keyword\",\"value\":\"foo\"}]}", + """ + { + "mode": "%s", + "params": [ 350.123, "1960-01-01", false, { "type": "keyword", "value": "foo" } ] + }""".formatted(m), "[params] must be an array where each entry is a single field (no objects supported)", SqlTranslateRequest::fromXContent ); @@ -281,29 +305,61 @@ public void testParamsParsingFailure_TranslateRequest_NonDriver() throws IOExcep public void testParamsParsingFailure_Driver() throws IOException { Mode m = randomValueOtherThanMany((t) -> Mode.isDriver(t) == false, () -> randomFrom(Mode.values())); assertXContentParsingErrorMessage( - "{\"params\":[35000,{\"value\":\"1960-01-01\",\"type\":\"date\"},{\"value\":\"foo\"," - + "\"type\":\"keyword\"}],\"mode\": \"" - + m.toString() - + "\"}", + """ + { + "params": [ + 35000, + { + "value": "1960-01-01", + "type": "date" + }, + { + "value": "foo", + "type": "keyword" + } + ], + "mode": "%s" + }""".formatted(m), "[params] must be an array where each entry is an object with a value/type pair", SqlQueryRequest::fromXContent ); assertXContentParsingErrorMessage( - "{\"params\":[{\"value\":10,\"type\":\"integer\"},{\"value\":\"1960-01-01\",\"type\":\"date\"}," - + "false,\"foo\"],\"mode\": \"" - + m.toString() - + "\"}", + """ + { + "params": [ + { + "value": 10, + "type": "integer" + }, + { + "value": "1960-01-01", + "type": "date" + }, + false, + "foo" + ], + "mode": "%s" + }""".formatted(m), "[params] must be an array where each entry is an object with a value/type pair", SqlQueryRequest::fromXContent ); - assertXContentParsingErrorMessage( - "{\"mode\": \"" - + m.toString() - + "\",\"params\":[{\"value\":10,\"type\":\"integer\"}," - + "{\"value\":\"1960-01-01\",\"type\":\"date\"},{\"foo\":\"bar\"}]}", - "[sql/query] failed to parse field [params]", - SqlQueryRequest::fromXContent - ); + assertXContentParsingErrorMessage(""" + { + "mode": "%s", + "params": [ + { + "value": 10, + "type": "integer" + }, + { + "value": "1960-01-01", + "type": "date" + }, + { + "foo": "bar" + } + ] + }""".formatted(m), "[sql/query] failed to parse field [params]", SqlQueryRequest::fromXContent); } private R generateRequest(String json, Function fromXContent) throws IOException { diff --git a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java index 1c20b36e20aa0..e005e9f668ff9 100644 --- a/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java +++ b/x-pack/plugin/sql/sql-cli/src/test/java/org/elasticsearch/xpack/sql/cli/command/ServerQueryCliCommandTests.java @@ -48,7 +48,11 @@ public void testOnePageQuery() throws Exception { when(client.basicQuery("test query", 10)).thenReturn(fakeResponse("", true, "foo")); ServerQueryCliCommand cliCommand = new ServerQueryCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "test query")); - assertEquals(" field \n---------------\nfoo \n", testTerminal.toString()); + assertEquals(""" + field \s + --------------- + foo \s + """, testTerminal.toString()); verify(client, times(1)).basicQuery(eq("test query"), eq(10)); verifyNoMoreInteractions(client); } @@ -63,10 +67,13 @@ public void testThreePageQuery() throws Exception { when(client.nextPage("my_cursor2")).thenReturn(fakeResponse("", false, "third")); ServerQueryCliCommand cliCommand = new ServerQueryCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "test query")); - assertEquals( - " field \n---------------\nfirst \nsecond \nthird \n", - testTerminal.toString() - ); + assertEquals(""" + field \s + --------------- + first \s + second \s + third \s + """, testTerminal.toString()); verify(client, times(1)).basicQuery(eq("test query"), eq(10)); verify(client, times(2)).nextPage(any()); verifyNoMoreInteractions(client); @@ -83,7 +90,13 @@ public void testTwoPageQueryWithSeparator() throws Exception { when(client.nextPage("my_cursor1")).thenReturn(fakeResponse("", false, "second")); ServerQueryCliCommand cliCommand = new ServerQueryCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "test query")); - assertEquals(" field \n---------------\nfirst \n-----\nsecond \n", testTerminal.toString()); + assertEquals(""" + field \s + --------------- + first \s + ----- + second \s + """, testTerminal.toString()); verify(client, times(1)).basicQuery(eq("test query"), eq(15)); verify(client, times(1)).nextPage(any()); verifyNoMoreInteractions(client); @@ -99,10 +112,12 @@ public void testCursorCleanupOnError() throws Exception { when(client.queryClose("my_cursor1", Mode.CLI)).thenReturn(true); ServerQueryCliCommand cliCommand = new ServerQueryCliCommand(); assertTrue(cliCommand.handle(testTerminal, cliSession, "test query")); - assertEquals( - " field \n---------------\nfirst \n" + "Bad request [test exception]\n", - testTerminal.toString() - ); + assertEquals(""" + field \s + --------------- + first \s + Bad request [test exception] + """, testTerminal.toString()); verify(client, times(1)).basicQuery(eq("test query"), eq(15)); verify(client, times(1)).nextPage(any()); verify(client, times(1)).queryClose(eq("my_cursor1"), eq(Mode.CLI)); diff --git a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java index 765e864f76fc7..e9af90c99e25e 100644 --- a/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java +++ b/x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java @@ -13,7 +13,6 @@ import java.io.InputStream; import java.nio.file.Files; import java.util.Arrays; -import java.util.Locale; import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; @@ -74,12 +73,10 @@ public void testNoError() { public void testBogusError() { IOException e = expectThrows(IOException.class, () -> parse("bogus_error.json")); - assertEquals( - "Can't parse error from Elasticsearch [Expected [error] to be an object or string but was [START_ARRAY][[]] " - + "at [line 1 col 12]. Response:\n" - + "{ \"error\": [\"bogus\"] }", - e.getMessage() - ); + assertEquals(""" + Can't parse error from Elasticsearch [Expected [error] to be an object or string but was [START_ARRAY][[]] \ + at [line 1 col 12]. Response: + { "error": ["bogus"] }""", e.getMessage()); } public void testNoStack() { @@ -103,13 +100,10 @@ public void testNoType() { public void testInvalidJson() { IOException e = expectThrows(IOException.class, () -> parse("invalid_json.txt")); - assertEquals( - "Can't parse error from Elasticsearch [Unrecognized token 'I': " - + "was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')] " - + "at [line 1 col 1]. Response:\n" - + "I'm not json at all", - e.getMessage() - ); + assertEquals(""" + Can't parse error from Elasticsearch [Unrecognized token 'I': was expecting (JSON String, Number, Array, Object or \ + token 'null', 'true' or 'false')] at [line 1 col 1]. Response: + I'm not json at all""", e.getMessage()); } public void testExceptionBuildingParser() { @@ -151,47 +145,51 @@ public void testTotalGarbage() { public void testTooBig() { StringBuilder tooBig = new StringBuilder(RemoteFailure.MAX_RAW_RESPONSE); - tooBig.append("{\n"); - tooBig.append("\"error\" : {\n"); - tooBig.append(" \"type\" : \"illegal_argument_exception\",\n"); - tooBig.append(" \"reason\" : \"something\",\n"); - tooBig.append(" \"header\" : {\n"); + tooBig.append(""" + { + "error" : { + "type" : "illegal_argument_exception", + "reason" : "something", + "header" : { + """); int i = 0; while (tooBig.length() < RemoteFailure.MAX_RAW_RESPONSE) { - tooBig.append(" \"") - .append(String.format(Locale.ROOT, "%04d", i++)) - .append("\" : \"lots and lots and lots and lots and lots of words\",\n"); + tooBig.append(""" + "%04d" : "lots and lots and lots and lots and lots of words", + """.formatted(i++)); } - tooBig.append(" \"end\" : \"lots and lots and lots and lots and lots of words\"\n"); - tooBig.append(" }\n"); - tooBig.append("}\n"); + tooBig.append(""" + "end" : "lots and lots and lots and lots and lots of words" + } + } + """); IOException e = expectThrows( IOException.class, () -> RemoteFailure.parseFromResponse(new BytesArray(tooBig.toString()).streamInput()) ); assertEquals( "Can't parse error from Elasticsearch [expected [stack_trace] cannot but didn't see it] " - + "at [line 7951 col 1]. Attempted to include response but failed because [Response too large].", + + "at [line 8463 col 1]. Attempted to include response but failed because [Response too large].", e.getMessage() ); } public void testFailureWithMetadata() throws IOException { - final StringBuilder json = new StringBuilder(); - json.append("{"); - json.append("\"error\":{"); - json.append(" \"root_cause\":[],"); - json.append(" \"type\":\"search_phase_execution_exception\","); - json.append(" \"reason\":\"all shards failed\","); - json.append(" \"phase\":\"query\","); - json.append(" \"grouped\":true,"); - json.append(" \"failed_shards\":[],"); - json.append(" \"stack_trace\":\"Failed to execute phase [query], all shards failed at...\""); - json.append(" },"); - json.append(" \"status\":503"); - json.append("}"); - - RemoteFailure failure = RemoteFailure.parseFromResponse(new BytesArray(json.toString()).streamInput()); + String json = """ + { + "error": { + "root_cause": [], + "type": "search_phase_execution_exception", + "reason": "all shards failed", + "phase": "query", + "grouped": true, + "failed_shards": [], + "stack_trace": "Failed to execute phase [query], all shards failed at..." + }, + "status": 503 + }"""; + + RemoteFailure failure = RemoteFailure.parseFromResponse(new BytesArray(json).streamInput()); assertEquals("search_phase_execution_exception", failure.type()); assertEquals("all shards failed", failure.reason()); assertThat(failure.remoteTrace(), containsString("Failed to execute phase [query], all shards failed")); @@ -203,26 +201,26 @@ public void testFailureWithMetadata() throws IOException { } public void testFailureWithMetadataAndRootCause() throws IOException { - final StringBuilder json = new StringBuilder(); - json.append("{"); - json.append("\"error\":{"); - json.append(" \"caused_by\":{"); - json.append(" \"type\":\"invalid_index_name_exception\","); - json.append(" \"reason\":\"Invalid index name [_root], must not start with '_'.\","); - json.append(" \"index_uuid\":\"_na_root\","); - json.append(" \"index\":[\"_root\",\"_foo\"],"); - json.append(" \"stack_trace\":\"[_root] InvalidIndexNameException[Invalid index name [_root], must not start with '_'.]\""); - json.append(" },"); - json.append(" \"type\":\"invalid_index_name_exception\","); - json.append(" \"reason\":\"Invalid index name [_foo], must not start with '_'.\","); - json.append(" \"index_uuid\":\"_na_foo\","); - json.append(" \"index\":[\"_foo\"],"); - json.append(" \"stack_trace\":\"[_foo] InvalidIndexNameException[Invalid index name [_foo], must not start with '_'.]\""); - json.append(" },"); - json.append(" \"status\":400"); - json.append("}"); - - RemoteFailure failure = RemoteFailure.parseFromResponse(new BytesArray(json.toString()).streamInput()); + String json = """ + { + "error": { + "caused_by": { + "type": "invalid_index_name_exception", + "reason": "Invalid index name [_root], must not start with '_'.", + "index_uuid": "_na_root", + "index": [ "_root", "_foo" ], + "stack_trace": "[_root] InvalidIndexNameException[Invalid index name [_root], must not start with '_'.]" + }, + "type": "invalid_index_name_exception", + "reason": "Invalid index name [_foo], must not start with '_'.", + "index_uuid": "_na_foo", + "index": [ "_foo" ], + "stack_trace": "[_foo] InvalidIndexNameException[Invalid index name [_foo], must not start with '_'.]" + }, + "status": 400 + }"""; + + RemoteFailure failure = RemoteFailure.parseFromResponse(new BytesArray(json).streamInput()); assertEquals("invalid_index_name_exception", failure.type()); assertEquals("Invalid index name [_foo], must not start with '_'.", failure.reason()); assertThat( diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java index f278d0d8d5373..96f98b1a80417 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/FieldAttributeTests.java @@ -286,14 +286,13 @@ public void testGroupByAmbiguity() { VerificationException.class, () -> plan("SELECT gender AS g, last_name AS g, min(salary) AS m, max(salary) as m FROM test GROUP BY g, m") ); - assertEquals( - "Found 2 problems\n" - + "line 1:91: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); " - + "matches any of [line 1:8 [g], line 1:21 [g]]\n" - + "line 1:94: Reference [m] is ambiguous (to disambiguate use quotes or qualifiers); " - + "matches any of [line 1:37 [m], line 1:55 [m]]", - ex.getMessage() - ); + String expected = """ + Found 2 problems + line 1:91: Reference [g] is ambiguous (to disambiguate use quotes or qualifiers); matches any of [line 1:8 [g], \ + line 1:21 [g]] + line 1:94: Reference [m] is ambiguous (to disambiguate use quotes or qualifiers); matches any of [line 1:37 [m], \ + line 1:55 [m]]"""; + assertEquals(expected, ex.getMessage()); } public void testFunctionOverNonExistingFieldAsArgumentAndSameAlias() throws Exception { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java index b10063a9fc4fa..e241f00e3341d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/analysis/analyzer/VerifierErrorMessagesTests.java @@ -158,32 +158,26 @@ public void testFieldAliasTypeWithoutHierarchy() { } public void testMultipleColumnsWithWildcard1() { - assertEquals( - "1:14: Unknown column [a]\n" - + "line 1:17: Unknown column [b]\n" - + "line 1:22: Unknown column [c]\n" - + "line 1:25: Unknown column [tex], did you mean [text]?", - error("SELECT bool, a, b.*, c, tex.* FROM test") - ); + assertEquals(""" + 1:14: Unknown column [a] + line 1:17: Unknown column [b] + line 1:22: Unknown column [c] + line 1:25: Unknown column [tex], did you mean [text]?""", error("SELECT bool, a, b.*, c, tex.* FROM test")); } public void testMultipleColumnsWithWildcard2() { - assertEquals( - "1:8: Unknown column [tex], did you mean [text]?\n" - + "line 1:21: Unknown column [a]\n" - + "line 1:24: Unknown column [dat], did you mean [date]?\n" - + "line 1:31: Unknown column [c]", - error("SELECT tex.*, bool, a, dat.*, c FROM test") - ); + assertEquals(""" + 1:8: Unknown column [tex], did you mean [text]? + line 1:21: Unknown column [a] + line 1:24: Unknown column [dat], did you mean [date]? + line 1:31: Unknown column [c]""", error("SELECT tex.*, bool, a, dat.*, c FROM test")); } public void testMultipleColumnsWithWildcard3() { - assertEquals( - "1:8: Unknown column [ate], did you mean [date]?\n" - + "line 1:21: Unknown column [keyw], did you mean [keyword]?\n" - + "line 1:29: Unknown column [da], did you mean [date]?", - error("SELECT ate.*, bool, keyw.*, da FROM test") - ); + assertEquals(""" + 1:8: Unknown column [ate], did you mean [date]? + line 1:21: Unknown column [keyw], did you mean [keyword]? + line 1:29: Unknown column [da], did you mean [date]?""", error("SELECT ate.*, bool, keyw.*, da FROM test")); } public void testMisspelledColumn() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java index 3ada55fa21c17..54f1d2a36426c 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/datetime/DateTimeToCharProcessorTests.java @@ -127,17 +127,18 @@ public void test() { assertEquals( String.format( Locale.ROOT, - "\n" - + "Line number: %s (in %s)\n" - + "zone: %s\n" - + "timestamp (as epoch): %s\n" - + "timestamp (java, UTC): %s\n" - + "timestamp (postgres, to_timestamp): %s\n" - + "timestamp (java with zone): %s\n" - + "format string: %s\n" - + "expected (postgres to_char result): %s\n" - + "actual (ES to_char result): %s\n" - + " FAILED (sub)pattern: %s,", + """ + + Line number: %s (in %s) + zone: %s + timestamp (as epoch): %s + timestamp (java, UTC): %s + timestamp (postgres, to_timestamp): %s + timestamp (java with zone): %s + format string: %s + expected (postgres to_char result): %s + actual (ES to_char result): %s + FAILED (sub)pattern: %s,""", lineNumber, testFile, zone, diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java index c3413662a152c..bc9402401ee1d 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryFolderTests.java @@ -304,18 +304,9 @@ public void testFoldingBooleanOrNull_HavingClause() { PhysicalPlan p = plan("SELECT keyword, max(int) FROM test GROUP BY keyword HAVING max(int) > 10 OR null"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertTrue( - ee.queryContainer() - .aggs() - .asAggBuilder() - .toString() - .replaceAll("\\s+", "") - .contains( - "\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(" - + "InternalQlScriptUtils.nullSafeCastNumeric(params.a0,params.v0),params.v1))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"INTEGER\",\"v1\":10}}," - ) - ); + assertTrue(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", "").contains(""" + "script":{"source":"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(InternalQlScriptUtils.\ + nullSafeCastNumeric(params.a0,params.v0),params.v1))","lang":"painless","params":{"v0":"INTEGER","v1":10}},""")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("test.keyword{f}#")); assertThat(ee.output().get(1).toString(), startsWith("max(int){r}")); @@ -385,15 +376,9 @@ public void testGroupKeyTypes_Boolean() { PhysicalPlan p = plan("SELECT count(*), int > 10 AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat( - ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith( - "{\"script\":{" - + "\"source\":\"InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," - + "\"value_type\":\"boolean\",\"order\":\"asc\"}}}]}}}" - ) - ); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith(""" + {"script":{"source":"InternalQlScriptUtils.gt(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)",\ + "lang":"painless","params":{"v0":"int","v1":10}},"missing_bucket":true,"value_type":"boolean","order":"asc"}}}]}}}""")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -403,15 +388,9 @@ public void testGroupKeyTypes_Integer() { PhysicalPlan p = plan("SELECT count(*), int + 10 AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat( - ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith( - "{\"script\":{" - + "\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10}},\"missing_bucket\":true," - + "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}" - ) - ); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith(""" + {"script":{"source":"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)",\ + "lang":"painless","params":{"v0":"int","v1":10}},"missing_bucket":true,"value_type":"long","order":"asc"}}}]}}}""")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -421,15 +400,9 @@ public void testGroupKeyTypes_Rational() { PhysicalPlan p = plan("SELECT count(*), sin(int) AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat( - ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith( - "{\"script\":{" - + "\"source\":\"InternalSqlScriptUtils.sin(InternalQlScriptUtils.docValue(doc,params.v0))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\"}},\"missing_bucket\":true," - + "\"value_type\":\"double\",\"order\":\"asc\"}}}]}}}" - ) - ); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith(""" + {"script":{"source":"InternalSqlScriptUtils.sin(InternalQlScriptUtils.docValue(doc,params.v0))",\ + "lang":"painless","params":{"v0":"int"}},"missing_bucket":true,"value_type":"double","order":"asc"}}}]}}}""")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -439,15 +412,9 @@ public void testGroupKeyTypes_String() { PhysicalPlan p = plan("SELECT count(*), LCASE(keyword) AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat( - ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith( - "{\"script\":{" - + "\"source\":\"InternalSqlScriptUtils.lcase(InternalQlScriptUtils.docValue(doc,params.v0))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\"}},\"missing_bucket\":true," - + "\"value_type\":\"string\",\"order\":\"asc\"}}}]}}}" - ) - ); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith(""" + {"script":{"source":"InternalSqlScriptUtils.lcase(InternalQlScriptUtils.docValue(doc,params.v0))",\ + "lang":"painless","params":{"v0":"keyword"}},"missing_bucket":true,"value_type":"string","order":"asc"}}}]}}}""")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}#")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -457,15 +424,9 @@ public void testGroupKeyTypes_IP() { PhysicalPlan p = plan("SELECT count(*), CAST(keyword AS IP) AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat( - ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith( - "{\"script\":{\"source\":\"InternalSqlScriptUtils.cast(" - + "InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"keyword\",\"v1\":\"IP\"}}," - + "\"missing_bucket\":true,\"value_type\":\"ip\",\"order\":\"asc\"}}}]}}}" - ) - ); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith(""" + {"script":{"source":"InternalSqlScriptUtils.cast(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)",\ + "lang":"painless","params":{"v0":"keyword","v1":"IP"}},"missing_bucket":true,"value_type":"ip","order":"asc"}}}]}}}""")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}#")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -475,16 +436,10 @@ public void testGroupKeyTypes_DateTime() { PhysicalPlan p = plan("SELECT count(*), date + INTERVAL '1-2' YEAR TO MONTH AS a FROM test GROUP BY a"); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec ee = (EsQueryExec) p; - assertThat( - ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), - endsWith( - "{\"script\":{" - + "\"source\":\"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," - + "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2))\",\"lang\":\"painless\",\"params\":{" - + "\"v0\":\"date\",\"v1\":\"P1Y2M\",\"v2\":\"INTERVAL_YEAR_TO_MONTH\"}},\"missing_bucket\":true," - + "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}" - ) - ); + assertThat(ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith(""" + {"script":{"source":"InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0),InternalSqlScriptUtils.\ + intervalYearMonth(params.v1,params.v2))","lang":"painless","params":{"v0":"date","v1":"P1Y2M","v2":\ + "INTERVAL_YEAR_TO_MONTH"}},"missing_bucket":true,"value_type":"long","order":"asc"}}}]}}}""")); assertEquals(2, ee.output().size()); assertThat(ee.output().get(0).toString(), startsWith("count(*){r}#")); assertThat(ee.output().get(1).toString(), startsWith("a{r}")); @@ -547,11 +502,15 @@ public void testFoldingOfPivot() { assertEquals(3, ee.output().size()); assertEquals(asList("bool", "'A'", "'B'"), Expressions.names(ee.output())); String q = ee.toString().replaceAll("\\s+", ""); - assertThat(q, containsString("\"query\":{\"terms\":{\"keyword\":[\"A\",\"B\"]")); + assertThat(q, containsString(""" + "query":{"terms":{"keyword":["A","B"]""")); String a = ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""); - assertThat(a, containsString("\"terms\":{\"field\":\"bool\"")); - assertThat(a, containsString("\"terms\":{\"field\":\"keyword\"")); - assertThat(a, containsString("{\"avg\":{\"field\":\"int\"}")); + assertThat(a, containsString(""" + "terms":{"field":"bool\"""")); + assertThat(a, containsString(""" + "terms":{"field":"bool\"""")); + assertThat(a, containsString(""" + {"avg":{"field":"int"}""")); } public void testFoldingOfPivotWithPivotedColumnFirst() { diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java index 093096e152b31..68d75634df534 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java @@ -282,13 +282,9 @@ public void testHavingWithColumnImplicitGrouping() { EsQueryExec eqe = (EsQueryExec) p; assertTrue("Should be tracking hits", eqe.queryContainer().shouldTrackHits()); assertEquals(1, eqe.output().size()); - assertThat( - eqe.queryContainer().toString().replaceAll("\\s+", ""), - containsString( - "\"script\":{\"source\":\"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":0}}" - ) - ); + assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString(""" + "script":{"source":"InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))",\ + "lang":"painless","params":{"v0":0}}""")); } public void testScriptsInsideAggregateFunctions() { @@ -301,25 +297,34 @@ public void testScriptsInsideAggregateFunctions() { aggFunction += ")"; PhysicalPlan p = optimizeAndPlan("SELECT " + aggFunction + " FROM test"); if (fd.clazz() == Count.class) { - assertESQuery( - p, - containsString( - ":{\"script\":{\"source\":\"InternalQlScriptUtils.isNotNull(InternalSqlScriptUtils.add(" - + "InternalSqlScriptUtils.abs(InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(" - + "InternalQlScriptUtils.docValue(doc,params.v0),params.v1),params.v2)),params.v3))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" - ) - ); + assertESQuery(p, containsString(""" + :{ + "script": { + "source": "InternalQlScriptUtils.isNotNull(InternalSqlScriptUtils.add(\ + InternalSqlScriptUtils.abs(InternalSqlScriptUtils.div(\ + InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(doc,params.v0),params.v1),params.v2)),params.v3))", + "lang": "painless", + "params": { + "v0": "int", + "v1": 10, + "v2": 3, + "v3": 1 + } + }""".replaceAll("\\s", ""))); } else { - assertESQuery( - p, - containsString( - ":{\"script\":{\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(" - + "InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(" - + "doc,params.v0),params.v1),params.v2)),params.v3)\",\"lang\":\"painless\",\"params\":{" - + "\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" - ) - ); + assertESQuery(p, containsString(""" + :{ + "script": { + "source": "InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(InternalSqlScriptUtils.div(\ + InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(doc,params.v0),params.v1),params.v2)),params.v3)", + "lang": "painless", + "params": { + "v0": "int", + "v1": 10, + "v2": 3, + "v3": 1 + } + }""".replaceAll("\\s", ""))); } } } @@ -765,19 +770,28 @@ public void testChronoFieldBasedDateTimeFunctionsWithMathIntervalAndGroupBy() { + randomFunction.name() + "(date + INTERVAL 1 YEAR)" ); - assertESQuery( - p, - containsString( - "{\"terms\":{\"script\":{\"source\":\"InternalSqlScriptUtils.dateTimeExtract(" - + "InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," - + "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3,params.v4)\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":\"date\",\"v1\":\"P1Y\",\"v2\":\"INTERVAL_YEAR\"," - + "\"v3\":\"Z\",\"v4\":\"" - + randomFunction.name() - + "\"}},\"missing_bucket\":true," - + "\"value_type\":\"long\",\"order\":\"asc\"}}}]}}}}" - ) - ); + assertESQuery(p, containsString(""" + { + "terms": { + "script": { + "source": "InternalSqlScriptUtils.dateTimeExtract(InternalSqlScriptUtils.add(\ + InternalQlScriptUtils.docValue(doc,params.v0),\ + InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3,params.v4)", + "lang": "painless", + "params": { + "v0": "date", + "v1": "P1Y", + "v2": "INTERVAL_YEAR", + "v3": "Z", + "v4": "%s" + } + }, + "missing_bucket": true, + "value_type": "long", + "order": "asc" + } + }}]}}}} + """.formatted(randomFunction.name()).replaceAll("\\s", ""))); } public void testDateTimeFunctionsWithMathIntervalAndGroupBy() { @@ -789,16 +803,22 @@ public void testDateTimeFunctionsWithMathIntervalAndGroupBy() { ); assertEquals(EsQueryExec.class, p.getClass()); EsQueryExec eqe = (EsQueryExec) p; - assertThat( - eqe.queryContainer().toString().replaceAll("\\s+", ""), - containsString( - "{\"terms\":{\"script\":{\"source\":\"InternalSqlScriptUtils." - + scriptMethods[pos] - + "(InternalSqlScriptUtils.add(InternalQlScriptUtils.docValue(doc,params.v0)," - + "InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3)\",\"lang\":\"painless\"," - + "\"params\":{\"v0\":\"date\",\"v1\":\"P1Y\",\"v2\":\"INTERVAL_YEAR\",\"v3\":\"Z\"}},\"missing_bucket\":true," - ) - ); + assertThat(eqe.queryContainer().toString().replaceAll("\\s+", ""), containsString(""" + { + "terms": { + "script": { + "source": "InternalSqlScriptUtils.%s(InternalSqlScriptUtils.add(\ + InternalQlScriptUtils.docValue(doc,params.v0),\ + InternalSqlScriptUtils.intervalYearMonth(params.v1,params.v2)),params.v3)", + "lang": "painless", + "params": { + "v0": "date", + "v1": "P1Y", + "v2": "INTERVAL_YEAR", + "v3": "Z" + } + }, + "missing_bucket": true,""".formatted(scriptMethods[pos]).replaceAll("\\s", ""))); } // Like/RLike/StartsWith @@ -950,7 +970,8 @@ public void testTrimWhereClause() { + trimFunctionName.toLowerCase(Locale.ROOT) + "(InternalQlScriptUtils.docValue(doc,params.v0)),params.v1))" ), - containsString("\"params\":{\"v0\":\"keyword\",\"v1\":\"foo\"}") + containsString(""" + "params":{"v0":"keyword","v1":"foo"}""") ); } @@ -980,7 +1001,8 @@ public void testTrimGroupBy() { containsString( "InternalSqlScriptUtils." + trimFunctionName.toLowerCase(Locale.ROOT) + "(InternalQlScriptUtils.docValue(doc,params.v0))" ), - containsString("\"params\":{\"v0\"=\"keyword\"}") + containsString(""" + "params":{"v0"="keyword"}""") ); } @@ -1109,37 +1131,47 @@ public void testAllCountVariantsWithHavingGenerateCorrectAggregations() { assertThat( ee.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith( - "{\"buckets_path\":{" - + "\"a0\":\"" - + cardinalityKeyword.getName() - + "\"," - + "\"a1\":\"" - + existsKeyword.getName() - + "._count\"," - + "\"a2\":\"" - + cardinalityDottedField.getName() - + "\"," - + "\"a3\":\"" - + existsDottedField.getName() - + "._count\"," - + "\"a4\":\"_count\"," - + "\"a5\":\"" - + avgInt.getName() - + "\"}," - + "\"script\":{\"source\":\"" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(" - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a1,params.v1))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a2,params.v2))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a3,params.v3))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a4,params.v4))))," - + "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a5,params.v5))))\"," - + "\"lang\":\"painless\",\"params\":{\"v0\":3,\"v1\":32,\"v2\":1,\"v3\":2,\"v4\":5,\"v5\":50000}}," - + "\"gap_policy\":\"skip\"}}}}}" + """ + { + "buckets_path": { + "a0": "%s", + "a1": "%s._count", + "a2": "%s", + "a3": "%s._count", + "a4": "_count", + "a5": "%s" + }, + "script": { + "source": "InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.and(\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a0,params.v0)),\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a1,params.v1)))),\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a2,params.v2)))),\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a3,params.v3)))),\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a4,params.v4)))),\ + InternalQlScriptUtils.nullSafeFilter(InternalQlScriptUtils.gt(params.a5,params.v5))))", + "lang": "painless", + "params": { + "v0": 3, + "v1": 32, + "v2": 1, + "v3": 2, + "v4": 5, + "v5": 50000 + } + }, + "gap_policy": "skip" + }}}}} + """.formatted( + cardinalityKeyword.getName(), + existsKeyword.getName(), + cardinalityDottedField.getName(), + existsDottedField.getName(), + avgInt.getName() + ).replaceAll("\\s", "") ) ); } @@ -1171,7 +1203,9 @@ public void testExtendedStatsAggsStddevAndVar() { assertEquals(((MetricAggRef) fe).property(), metricToAgg.get(funcName)); String aggName = eqe.queryContainer().aggs().asAggBuilder().getSubAggregations().iterator().next().getName(); - assertESQuery(p, endsWith("\"aggregations\":{\"" + aggName + "\":{\"extended_stats\":{\"field\":\"int\",\"sigma\":2.0}}}}}}")); + assertESQuery(p, endsWith(""" + "aggregations":{"%s":{"extended_stats":{"field":"int","sigma":2.0}}}}}}\ + """.formatted(aggName))); } } @@ -1181,15 +1215,10 @@ public void testScriptsInsideAggregateFunctionsExtendedStats() { if (ExtendedStatsEnclosed.class.isAssignableFrom(fd.clazz())) { String aggFunction = fd.name() + "(ABS((int * 10) / 3) + 1)"; PhysicalPlan p = optimizeAndPlan("SELECT " + aggFunction + " FROM test"); - assertESQuery( - p, - containsString( - "{\"extended_stats\":{\"script\":{\"source\":\"InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(" - + "InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(" - + "doc,params.v0),params.v1),params.v2)),params.v3)\",\"lang\":\"painless\",\"params\":{" - + "\"v0\":\"int\",\"v1\":10,\"v2\":3,\"v3\":1}}" - ) - ); + assertESQuery(p, containsString(""" + {"extended_stats":{"script":{"source":"InternalSqlScriptUtils.add(InternalSqlScriptUtils.abs(\ + InternalSqlScriptUtils.div(InternalSqlScriptUtils.mul(InternalQlScriptUtils.docValue(doc,params.v0),params.v1)\ + ,params.v2)),params.v3)","lang":"painless","params":{"v0":"int","v1":10,"v2":3,"v3":1}}""")); } } } @@ -1366,19 +1395,18 @@ public void testMultiLevelSubqueryWithoutRelation1() { } public void testMultiLevelSubqueryWithoutRelation2() { - PhysicalPlan p = optimizeAndPlan( - "SELECT i, string FROM (" - + " SELECT * FROM (" - + " SELECT int as i, str AS string FROM (" - + " SELECT * FROM (" - + " SELECT int, s AS str FROM (" - + " SELECT 1 AS int, 'foo' AS s" - + " ) AS subq1" - + " )" - + " ) AS subq2" - + " ) AS subq3" - + ")" - ); + PhysicalPlan p = optimizeAndPlan(""" + SELECT i, string FROM ( + SELECT * FROM ( + SELECT int as i, str AS string FROM ( + SELECT * FROM ( + SELECT int, s AS str FROM ( + SELECT 1 AS int, 'foo' AS s + ) AS subq1 + ) + ) AS subq2 + ) AS subq3 + )"""); assertThat(p, instanceOf(LocalExec.class)); LocalExec le = (LocalExec) p; assertThat(le.executable(), instanceOf(SingletonExecutable.class)); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java index 895a5b758ffad..57fd9b6f45c50 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/TextFormatTests.java @@ -71,12 +71,19 @@ public void testTsvFormatWithEmptyData() { public void testCsvFormatWithRegularData() { String text = CSV.format(req(), regularData()); - assertEquals("string,number\r\n" + "Along The River Bank,708\r\n" + "Mind Train,280\r\n", text); + assertEquals(""" + string,number\r + Along The River Bank,708\r + Mind Train,280\r + """, text); } public void testCsvFormatNoHeaderWithRegularData() { String text = CSV.format(reqWithParam("header", "absent"), regularData()); - assertEquals("Along The River Bank,708\r\n" + "Mind Train,280\r\n", text); + assertEquals(""" + Along The River Bank,708\r + Mind Train,280\r + """, text); } public void testCsvFormatWithCustomDelimiterRegularData() { @@ -99,25 +106,44 @@ public void testCsvFormatWithCustomDelimiterRegularData() { public void testTsvFormatWithRegularData() { String text = TSV.format(req(), regularData()); - assertEquals("string\tnumber\n" + "Along The River Bank\t708\n" + "Mind Train\t280\n", text); + assertEquals(""" + string\tnumber + Along The River Bank\t708 + Mind Train\t280 + """, text); } public void testCsvFormatWithEscapedData() { String text = CSV.format(req(), escapedData()); - assertEquals("first,\"\"\"special\"\"\"\r\n" + "normal,\"\"\"quo\"\"ted\"\",\n\"\r\n" + "commas,\"a,b,c,\n,d,e,\t\n\"\r\n", text); + assertEquals(""" + first,""\"special""\"\r + normal,""\"quo""ted"", + "\r + commas,"a,b,c, + ,d,e,\t + "\r + """, text); } public void testCsvFormatWithCustomDelimiterEscapedData() { String text = CSV.format(reqWithParam("delimiter", "\\"), escapedData()); - assertEquals( - "first\\\"\"\"special\"\"\"\r\n" + "normal\\\"\"\"quo\"\"ted\"\",\n\"\r\n" + "commas\\\"a,b,c,\n,d,e,\t\n\"\r\n", - text - ); + assertEquals(""" + first\\""\"special""\"\r + normal\\""\"quo""ted"", + "\r + commas\\"a,b,c, + ,d,e,\t + "\r + """, text); } public void testTsvFormatWithEscapedData() { String text = TSV.format(req(), escapedData()); - assertEquals("first\t\"special\"\n" + "normal\t\"quo\"ted\",\\n\n" + "commas\ta,b,c,\\n,d,e,\\t\\n\n", text); + assertEquals(""" + first\t"special" + normal\t"quo"ted",\\n + commas\ta,b,c,\\n,d,e,\\t\\n + """, text); } public void testInvalidCsvDelims() { diff --git a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/DelimitedTextStructureFinderTests.java b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/DelimitedTextStructureFinderTests.java index 01ea16eb5eef4..3530c94c8ede7 100644 --- a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/DelimitedTextStructureFinderTests.java +++ b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/DelimitedTextStructureFinderTests.java @@ -35,7 +35,11 @@ public class DelimitedTextStructureFinderTests extends TextStructureTestCase { private final TextStructureFinderFactory tsvFactory = new DelimitedTextStructureFinderFactory('\t', '"', 3, false); public void testCreateConfigsGivenCompleteCsv() throws Exception { - String sample = "time,message\n" + "2018-05-17T13:41:23,hello\n" + "2018-05-17T13:41:32,hello again\n"; + String sample = """ + time,message + 2018-05-17T13:41:23,hello + 2018-05-17T13:41:32,hello again + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -73,18 +77,23 @@ public void testCreateConfigsGivenCompleteCsv() throws Exception { } public void testCreateConfigsGivenIncompleteCsv() throws Exception { - String sample = "time,message\n" + "2018-05-17T13:41:23,hello\n" + "badrow\n" + // REALLY bad row - "2018-05-17T13:41:25,hello\n" - + "2018-05-17T13:41:26,hello\n" - + "2018-05-17T13:41:27,hello\n" - + "2018-05-17T13:41:28,hello\n" - + "2018-05-17T13:41:29,hello\n" - + "2018-05-17T13:41:30,hello\n" - + "2018-05-17T13:41:31,hello\n" - + "2018-05-17T13:41:32,hello\n" - + "2018-05-17T13:41:35\n" - + // Just missing the column - "2018-05-17T13:41:33,hello again\n"; + // REALLY bad row + // Just missing the column + String sample = """ + time,message + 2018-05-17T13:41:23,hello + badrow + 2018-05-17T13:41:25,hello + 2018-05-17T13:41:26,hello + 2018-05-17T13:41:27,hello + 2018-05-17T13:41:28,hello + 2018-05-17T13:41:29,hello + 2018-05-17T13:41:30,hello + 2018-05-17T13:41:31,hello + 2018-05-17T13:41:32,hello + 2018-05-17T13:41:35 + 2018-05-17T13:41:33,hello again + """; assertFalse(csvFactory.canCreateFromSample(explanation, sample, 0.05)); assertTrue("assertion failed. Explanation " + explanation, csvFactory.canCreateFromSample(explanation, sample, 0.10)); @@ -124,18 +133,37 @@ public void testCreateConfigsGivenIncompleteCsv() throws Exception { } public void testCreateConfigsGivenIncompleteCsvWithMultiLinedRows() throws Exception { - String sample = "time,message\n" + "2018-05-17T13:41:23,\"hello\nnew line\"\n" + "\"badrow\n\n\n\n\"\n" + // REALLY bad row - "2018-05-17T13:41:25,\"hello\nnew line\"\n" - + "2018-05-17T13:41:26,\"hello\nnew line\"\n" - + "2018-05-17T13:41:27,\"hello\nnew line\"\n" - + "2018-05-17T13:41:28,\"hello\nnew line\"\n" - + "2018-05-17T13:41:29,\"hello\nnew line\"\n" - + "2018-05-17T13:41:30,\"hello\nnew line\"\n" - + "2018-05-17T13:41:31,\"hello\nnew line\"\n" - + "2018-05-17T13:41:32,\"hello\nnew line\"\n" - + "2018-05-17T13:41:35\n" - + // Just missing the column - "2018-05-17T13:41:33,\"hello again\nnew line\"\n"; + // REALLY bad row + // Just missing the column + String sample = """ + time,message + 2018-05-17T13:41:23,"hello + new line" + "badrow + + + + " + 2018-05-17T13:41:25,"hello + new line" + 2018-05-17T13:41:26,"hello + new line" + 2018-05-17T13:41:27,"hello + new line" + 2018-05-17T13:41:28,"hello + new line" + 2018-05-17T13:41:29,"hello + new line" + 2018-05-17T13:41:30,"hello + new line" + 2018-05-17T13:41:31,"hello + new line" + 2018-05-17T13:41:32,"hello + new line" + 2018-05-17T13:41:35 + 2018-05-17T13:41:33,"hello again + new line" + """; assertFalse(csvFactory.canCreateFromSample(explanation, sample, 0.05)); assertTrue("assertion failed. Explanation " + explanation, csvFactory.canCreateFromSample(explanation, sample, 0.10)); @@ -178,7 +206,11 @@ public void testCreateConfigsGivenCompleteCsvAndColumnNamesOverride() throws Exc TextStructureOverrides overrides = TextStructureOverrides.builder().setColumnNames(Arrays.asList("my_time", "my_message")).build(); - String sample = "time,message\n" + "2018-05-17T13:41:23,hello\n" + "2018-05-17T13:41:32,hello again\n"; + String sample = """ + time,message + 2018-05-17T13:41:23,hello + 2018-05-17T13:41:32,hello again + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -221,7 +253,11 @@ public void testCreateConfigsGivenCompleteCsvAndHasHeaderRowOverride() throws Ex // detection with the wrong choice the results will be completely changed TextStructureOverrides overrides = TextStructureOverrides.builder().setHasHeaderRow(false).build(); - String sample = "time,message\n" + "2018-05-17T13:41:23,hello\n" + "2018-05-17T13:41:32,hello again\n"; + String sample = """ + time,message + 2018-05-17T13:41:23,hello + 2018-05-17T13:41:32,hello again + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -260,7 +296,12 @@ public void testCreateConfigsGivenCompleteCsvAndHasHeaderRowOverride() throws Ex public void testCreateConfigsGivenCsvWithIncompleteLastRecord() throws Exception { // note that this last record is truncated - String sample = "time,message,count\n2018-05-17T13:41:23,\"hello\nworld\",1\n2019-01-18T14:46:57,\"hello again\n"; + String sample = """ + time,message,count + 2018-05-17T13:41:23,"hello + world",1 + 2019-01-18T14:46:57,"hello again + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -298,12 +339,13 @@ public void testCreateConfigsGivenCsvWithIncompleteLastRecord() throws Exception } public void testCreateConfigsGivenCsvWithTrailingNulls() throws Exception { - String sample = "VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID," - + "store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount," - + "improvement_surcharge,total_amount,,\n" - + "2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,,\n"; + String sample = """ + VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,\ + PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount,, + 2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,, + 1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,, + 1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,, + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -327,13 +369,10 @@ public void testCreateConfigsGivenCsvWithTrailingNulls() throws Exception { } else { assertEquals(hasByteOrderMarker, structure.getHasByteOrderMarker()); } - assertEquals( - "^\"?VendorID\"?,\"?tpep_pickup_datetime\"?,\"?tpep_dropoff_datetime\"?,\"?passenger_count\"?,\"?trip_distance\"?," - + "\"?RatecodeID\"?,\"?store_and_fwd_flag\"?,\"?PULocationID\"?,\"?DOLocationID\"?,\"?payment_type\"?,\"?fare_amount\"?," - + "\"?extra\"?,\"?mta_tax\"?,\"?tip_amount\"?,\"?tolls_amount\"?," - + "\"?improvement_surcharge\"?,\"?total_amount\"?,\"?\"?,\"?\"?", - structure.getExcludeLinesPattern() - ); + assertEquals(""" + ^"?VendorID"?,"?tpep_pickup_datetime"?,"?tpep_dropoff_datetime"?,"?passenger_count"?,"?trip_distance"?,"?RatecodeID"?,\ + "?store_and_fwd_flag"?,"?PULocationID"?,"?DOLocationID"?,"?payment_type"?,"?fare_amount"?,"?extra"?,"?mta_tax"?,\ + "?tip_amount"?,"?tolls_amount"?,"?improvement_surcharge"?,"?total_amount"?,"?"?,"?"?""", structure.getExcludeLinesPattern()); assertNull(structure.getMultilineStartPattern()); assertEquals(Character.valueOf(','), structure.getDelimiter()); assertEquals(Character.valueOf('"'), structure.getQuote()); @@ -374,13 +413,13 @@ public void testCreateConfigsGivenCsvWithTrailingNullsAndOverriddenTimeField() t // Default timestamp field is the first field from the start of each row that contains a // consistent timestamp format, so if we want the second we need an override TextStructureOverrides overrides = TextStructureOverrides.builder().setTimestampField("tpep_dropoff_datetime").build(); - - String sample = "VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID," - + "store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount," - + "improvement_surcharge,total_amount,,\n" - + "2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,,\n"; + String sample = """ + VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,\ + PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount,, + 2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,, + 1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,, + 1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,, + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -404,13 +443,10 @@ public void testCreateConfigsGivenCsvWithTrailingNullsAndOverriddenTimeField() t } else { assertEquals(hasByteOrderMarker, structure.getHasByteOrderMarker()); } - assertEquals( - "^\"?VendorID\"?,\"?tpep_pickup_datetime\"?,\"?tpep_dropoff_datetime\"?,\"?passenger_count\"?,\"?trip_distance\"?," - + "\"?RatecodeID\"?,\"?store_and_fwd_flag\"?,\"?PULocationID\"?,\"?DOLocationID\"?,\"?payment_type\"?,\"?fare_amount\"?," - + "\"?extra\"?,\"?mta_tax\"?,\"?tip_amount\"?,\"?tolls_amount\"?,\"" - + "?improvement_surcharge\"?,\"?total_amount\"?,\"?\"?,\"?\"?", - structure.getExcludeLinesPattern() - ); + assertEquals(""" + ^"?VendorID"?,"?tpep_pickup_datetime"?,"?tpep_dropoff_datetime"?,"?passenger_count"?,"?trip_distance"?,"?RatecodeID"?,\ + "?store_and_fwd_flag"?,"?PULocationID"?,"?DOLocationID"?,"?payment_type"?,"?fare_amount"?,"?extra"?,"?mta_tax"?,\ + "?tip_amount"?,"?tolls_amount"?,"?improvement_surcharge"?,"?total_amount"?,"?"?,"?"?""", structure.getExcludeLinesPattern()); assertNull(structure.getMultilineStartPattern()); assertEquals(Character.valueOf(','), structure.getDelimiter()); assertEquals(Character.valueOf('"'), structure.getQuote()); @@ -447,12 +483,13 @@ public void testCreateConfigsGivenCsvWithTrailingNullsAndOverriddenTimeField() t } public void testCreateConfigsGivenCsvWithTrailingNullsExceptHeader() throws Exception { - String sample = "VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID," - + "store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount," - + "improvement_surcharge,total_amount\n" - + "2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,,\n"; + String sample = """ + VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,\ + PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount + 2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,, + 1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,, + 1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,, + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -476,12 +513,10 @@ public void testCreateConfigsGivenCsvWithTrailingNullsExceptHeader() throws Exce } else { assertEquals(hasByteOrderMarker, structure.getHasByteOrderMarker()); } - assertEquals( - "^\"?VendorID\"?,\"?tpep_pickup_datetime\"?,\"?tpep_dropoff_datetime\"?,\"?passenger_count\"?,\"?trip_distance\"?," - + "\"?RatecodeID\"?,\"?store_and_fwd_flag\"?,\"?PULocationID\"?,\"?DOLocationID\"?,\"?payment_type\"?,\"?fare_amount\"?," - + "\"?extra\"?,\"?mta_tax\"?,\"?tip_amount\"?,\"?tolls_amount\"?,\"?improvement_surcharge\"?,\"?total_amount\"?", - structure.getExcludeLinesPattern() - ); + assertEquals(""" + ^"?VendorID"?,"?tpep_pickup_datetime"?,"?tpep_dropoff_datetime"?,"?passenger_count"?,"?trip_distance"?,"?RatecodeID"?,\ + "?store_and_fwd_flag"?,"?PULocationID"?,"?DOLocationID"?,"?payment_type"?,"?fare_amount"?,"?extra"?,"?mta_tax"?,\ + "?tip_amount"?,"?tolls_amount"?,"?improvement_surcharge"?,"?total_amount"?""", structure.getExcludeLinesPattern()); assertNull(structure.getMultilineStartPattern()); assertEquals(Character.valueOf(','), structure.getDelimiter()); assertEquals(Character.valueOf('"'), structure.getQuote()); @@ -541,12 +576,13 @@ public void testCreateConfigsGivenCsvWithTrailingNullsExceptHeaderAndColumnNames ) .build(); - String sample = "VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID," - + "store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount," - + "improvement_surcharge,total_amount\n" - + "2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,,\n" - + "1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,,\n"; + String sample = """ + VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,\ + PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount + 2,2016-12-31 15:15:01,2016-12-31 15:15:09,1,.00,1,N,264,264,2,1,0,0.5,0,0,0.3,1.8,, + 1,2016-12-01 00:00:01,2016-12-01 00:10:22,1,1.60,1,N,163,143,2,9,0.5,0.5,0,0,0.3,10.3,, + 1,2016-12-01 00:00:01,2016-12-01 00:11:01,1,1.40,1,N,164,229,1,9,0.5,0.5,2.05,0,0.3,12.35,, + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -570,12 +606,10 @@ public void testCreateConfigsGivenCsvWithTrailingNullsExceptHeaderAndColumnNames } else { assertEquals(hasByteOrderMarker, structure.getHasByteOrderMarker()); } - assertEquals( - "^\"?VendorID\"?,\"?tpep_pickup_datetime\"?,\"?tpep_dropoff_datetime\"?,\"?passenger_count\"?,\"?trip_distance\"?," - + "\"?RatecodeID\"?,\"?store_and_fwd_flag\"?,\"?PULocationID\"?,\"?DOLocationID\"?,\"?payment_type\"?,\"?fare_amount\"?," - + "\"?extra\"?,\"?mta_tax\"?,\"?tip_amount\"?,\"?tolls_amount\"?,\"?improvement_surcharge\"?,\"?total_amount\"?", - structure.getExcludeLinesPattern() - ); + assertEquals(""" + ^"?VendorID"?,"?tpep_pickup_datetime"?,"?tpep_dropoff_datetime"?,"?passenger_count"?,"?trip_distance"?,"?RatecodeID"?,\ + "?store_and_fwd_flag"?,"?PULocationID"?,"?DOLocationID"?,"?payment_type"?,"?fare_amount"?,"?extra"?,"?mta_tax"?,\ + "?tip_amount"?,"?tolls_amount"?,"?improvement_surcharge"?,"?total_amount"?""", structure.getExcludeLinesPattern()); assertNull(structure.getMultilineStartPattern()); assertEquals(Character.valueOf(','), structure.getDelimiter()); assertEquals(Character.valueOf('"'), structure.getQuote()); @@ -610,9 +644,11 @@ public void testCreateConfigsGivenCsvWithTrailingNullsExceptHeaderAndColumnNames } public void testCreateConfigsGivenCsvWithTimeLastColumn() throws Exception { - String sample = "\"pos_id\",\"trip_id\",\"latitude\",\"longitude\",\"altitude\",\"timestamp\"\n" - + "\"1\",\"3\",\"4703.7815\",\"1527.4713\",\"359.9\",\"2017-01-19 16:19:04.742113\"\n" - + "\"2\",\"3\",\"4703.7815\",\"1527.4714\",\"359.9\",\"2017-01-19 16:19:05.741890\"\n"; + String sample = """ + "pos_id","trip_id","latitude","longitude","altitude","timestamp" + "1","3","4703.7815","1527.4713","359.9","2017-01-19 16:19:04.742113" + "2","3","4703.7815","1527.4714","359.9","2017-01-19 16:19:05.741890" + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -636,10 +672,8 @@ public void testCreateConfigsGivenCsvWithTimeLastColumn() throws Exception { } else { assertEquals(hasByteOrderMarker, structure.getHasByteOrderMarker()); } - assertEquals( - "^\"?pos_id\"?,\"?trip_id\"?,\"?latitude\"?,\"?longitude\"?,\"?altitude\"?,\"?timestamp\"?", - structure.getExcludeLinesPattern() - ); + assertEquals(""" + ^"?pos_id"?,"?trip_id"?,"?latitude"?,"?longitude"?,"?altitude"?,"?timestamp"?""", structure.getExcludeLinesPattern()); assertNull(structure.getMultilineStartPattern()); assertEquals(Character.valueOf(','), structure.getDelimiter()); assertEquals(Character.valueOf('"'), structure.getQuote()); @@ -653,16 +687,18 @@ public void testCreateConfigsGivenCsvWithTimeLastColumn() throws Exception { } public void testCreateConfigsGivenTsvWithSyslogLikeTimestamp() throws Exception { - String sample = "Latitude\tLongitude\tloc\tTimestamp\n" - + "25.78042\t18.441196\t\"25.7804200000,18.4411960000\"\tJun 30 2019 13:21:24\n" - + "25.743484\t18.443047\t\"25.7434840000,18.4430470000\"\tJun 30 2019 06:02:35\n" - + "25.744583\t18.442783\t\"25.7445830000,18.4427830000\"\tJun 30 2019 06:02:35\n" - + "25.754593\t18.431637\t\"25.7545930000,18.4316370000\"\tJul 1 2019 06:02:43\n" - + "25.768574\t18.433483\t\"25.7685740000,18.4334830000\"\tJul 1 2019 06:21:28\n" - + "25.757736\t18.438683\t\"25.7577360000,18.4386830000\"\tJul 1 2019 12:06:08\n" - + "25.76615\t18.436565\t\"25.7661500000,18.4365650000\"\tJul 1 2019 12:06:08\n" - + "25.76896\t18.43586\t\"25.7689600000,18.4358600000\"\tJul 1 2019 12:13:50\n" - + "25.76423\t18.43705\t\"25.7642300000,18.4370500000\"\tJul 1 2019 12:39:10\n"; + String sample = """ + Latitude\tLongitude\tloc\tTimestamp + 25.78042\t18.441196\t"25.7804200000,18.4411960000"\tJun 30 2019 13:21:24 + 25.743484\t18.443047\t"25.7434840000,18.4430470000"\tJun 30 2019 06:02:35 + 25.744583\t18.442783\t"25.7445830000,18.4427830000"\tJun 30 2019 06:02:35 + 25.754593\t18.431637\t"25.7545930000,18.4316370000"\tJul 1 2019 06:02:43 + 25.768574\t18.433483\t"25.7685740000,18.4334830000"\tJul 1 2019 06:21:28 + 25.757736\t18.438683\t"25.7577360000,18.4386830000"\tJul 1 2019 12:06:08 + 25.76615\t18.436565\t"25.7661500000,18.4365650000"\tJul 1 2019 12:06:08 + 25.76896\t18.43586\t"25.7689600000,18.4358600000"\tJul 1 2019 12:13:50 + 25.76423\t18.43705\t"25.7642300000,18.4370500000"\tJul 1 2019 12:39:10 + """; assertTrue(tsvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -686,7 +722,8 @@ public void testCreateConfigsGivenTsvWithSyslogLikeTimestamp() throws Exception } else { assertEquals(hasByteOrderMarker, structure.getHasByteOrderMarker()); } - assertEquals("^\"?Latitude\"?\\t\"?Longitude\"?\\t\"?loc\"?\\t\"?Timestamp\"?", structure.getExcludeLinesPattern()); + assertEquals(""" + ^"?Latitude"?\\t"?Longitude"?\\t"?loc"?\\t"?Timestamp"?""", structure.getExcludeLinesPattern()); assertNull(structure.getMultilineStartPattern()); assertEquals(Character.valueOf('\t'), structure.getDelimiter()); assertEquals(Character.valueOf('"'), structure.getQuote()); @@ -703,7 +740,11 @@ public void testCreateConfigsGivenTsvWithSyslogLikeTimestamp() throws Exception } public void testCreateConfigsGivenDotInFieldName() throws Exception { - String sample = "time.iso8601,message\n" + "2018-05-17T13:41:23,hello\n" + "2018-05-17T13:41:32,hello again\n"; + String sample = """ + time.iso8601,message + 2018-05-17T13:41:23,hello + 2018-05-17T13:41:32,hello again + """; assertTrue(csvFactory.canCreateFromSample(explanation, sample, 0.0)); String charset = randomFrom(POSSIBLE_CHARSETS); @@ -728,7 +769,8 @@ public void testCreateConfigsGivenDotInFieldName() throws Exception { assertEquals(hasByteOrderMarker, structure.getHasByteOrderMarker()); } // The exclude pattern needs to work on the raw text, so reflects the unmodified field names - assertEquals("^\"?time\\.iso8601\"?,\"?message\"?", structure.getExcludeLinesPattern()); + assertEquals(""" + ^"?time\\.iso8601"?,"?message"?""", structure.getExcludeLinesPattern()); assertNull(structure.getMultilineStartPattern()); assertEquals(Character.valueOf(','), structure.getDelimiter()); assertEquals(Character.valueOf('"'), structure.getQuote()); @@ -742,11 +784,13 @@ public void testCreateConfigsGivenDotInFieldName() throws Exception { } public void testFindHeaderFromSampleGivenHeaderInSample() throws IOException { - String withHeader = "time,airline,responsetime,sourcetype\n" - + "2014-06-23 00:00:00Z,AAL,132.2046,farequote\n" - + "2014-06-23 00:00:00Z,JZA,990.4628,farequote\n" - + "2014-06-23 00:00:01Z,JBU,877.5927,farequote\n" - + "2014-06-23 00:00:01Z,KLM,1355.4812,farequote\n"; + String withHeader = """ + time,airline,responsetime,sourcetype + 2014-06-23 00:00:00Z,AAL,132.2046,farequote + 2014-06-23 00:00:00Z,JZA,990.4628,farequote + 2014-06-23 00:00:01Z,JBU,877.5927,farequote + 2014-06-23 00:00:01Z,KLM,1355.4812,farequote + """; Tuple header = DelimitedTextStructureFinder.findHeaderFromSample( explanation, @@ -759,10 +803,12 @@ public void testFindHeaderFromSampleGivenHeaderInSample() throws IOException { } public void testFindHeaderFromSampleGivenHeaderNotInSample() throws IOException { - String noHeader = "2014-06-23 00:00:00Z,AAL,132.2046,farequote\n" - + "2014-06-23 00:00:00Z,JZA,990.4628,farequote\n" - + "2014-06-23 00:00:01Z,JBU,877.5927,farequote\n" - + "2014-06-23 00:00:01Z,KLM,1355.4812,farequote\n"; + String noHeader = """ + 2014-06-23 00:00:00Z,AAL,132.2046,farequote + 2014-06-23 00:00:00Z,JZA,990.4628,farequote + 2014-06-23 00:00:01Z,JBU,877.5927,farequote + 2014-06-23 00:00:01Z,KLM,1355.4812,farequote + """; Tuple header = DelimitedTextStructureFinder.findHeaderFromSample( explanation, diff --git a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/GrokPatternCreatorTests.java b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/GrokPatternCreatorTests.java index 2575e2cd1845f..a969d4c0d8237 100644 --- a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/GrokPatternCreatorTests.java +++ b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/GrokPatternCreatorTests.java @@ -526,17 +526,17 @@ public void testFindFullLineGrokPatternGivenApacheCombinedLogs() { } public void testAdjustForPunctuationGivenCommonPrefix() { - Collection snippets = Arrays.asList( - "\",\"lab6.localhost\",\"Route Domain\",\"/Common/0\",\"No-lookup\",\"192.168.33.212\",\"No-lookup\",\"192.168.33.132\"," - + "\"80\",\"46721\",\"/Common/Subnet_33\",\"TCP\",\"0\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"Staged\",\"/Common/policy1\"" - + ",\"rule1\",\"Accept\",\"\",\"\",\"\",\"0000000000000000\"", - "\",\"lab6.localhost\",\"Route Domain\",\"/Common/0\",\"No-lookup\",\"192.168.143.244\",\"No-lookup\",\"192.168.33.106\"," - + "\"55025\",\"162\",\"/Common/Subnet_33\",\"UDP\",\"0\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"Staged\",\"/Common/policy1\"" - + ",\"rule1\",\"Accept\",\"\",\"\",\"\",\"0000000000000000\"", - "\",\"lab6.localhost\",\"Route Domain\",\"/Common/0\",\"No-lookup\",\"192.168.33.3\",\"No-lookup\",\"224.0.0.102\"," - + "\"3222\",\"3222\",\"/Common/Subnet_33\",\"UDP\",\"0\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"Staged\",\"/Common/policy1\"" - + ",\"rule1\",\"Accept\",\"\",\"\",\"\",\"0000000000000000\"" - ); + Collection snippets = """ + ","lab6.localhost","Route Domain","/Common/0","No-lookup","192.168.33.212","No-lookup","192.168.33.132","80","46721",\ + "/Common/Subnet_33","TCP","0","","","","","","","","Staged","/Common/policy1","rule1","Accept","","","",\ + "0000000000000000" + ","lab6.localhost","Route Domain","/Common/0","No-lookup","192.168.143.244","No-lookup","192.168.33.106","55025","162",\ + "/Common/Subnet_33","UDP","0","","","","","","","","Staged","/Common/policy1","rule1","Accept","","","",\ + "0000000000000000" + ","lab6.localhost","Route Domain","/Common/0","No-lookup","192.168.33.3","No-lookup","224.0.0.102","3222","3222",\ + "/Common/Subnet_33","UDP","0","","","","","","","","Staged","/Common/policy1","rule1","Accept","","","",\ + "0000000000000000"\ + """.lines().toList(); GrokPatternCreator grokPatternCreator = new GrokPatternCreator( explanation, diff --git a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/LogTextStructureFinderTests.java b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/LogTextStructureFinderTests.java index ddc4ebf2c1a1f..4679f27dc9fb0 100644 --- a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/LogTextStructureFinderTests.java +++ b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/LogTextStructureFinderTests.java @@ -23,13 +23,15 @@ public class LogTextStructureFinderTests extends TextStructureTestCase { public void testCreateConfigsGivenLowLineMergeSizeLimit() { - String sample = "2019-05-16 16:56:14 line 1 abcdefghijklmnopqrstuvwxyz\n" - + "2019-05-16 16:56:14 line 2 abcdefghijklmnopqrstuvwxyz\n" - + "continuation line 2.1\n" - + "continuation line 2.2\n" - + "continuation line 2.3\n" - + "continuation line 2.4\n" - + "2019-05-16 16:56:14 line 3 abcdefghijklmnopqrstuvwxyz\n"; + String sample = """ + 2019-05-16 16:56:14 line 1 abcdefghijklmnopqrstuvwxyz + 2019-05-16 16:56:14 line 2 abcdefghijklmnopqrstuvwxyz + continuation line 2.1 + continuation line 2.2 + continuation line 2.3 + continuation line 2.4 + 2019-05-16 16:56:14 line 3 abcdefghijklmnopqrstuvwxyz + """; assertTrue(factory.canCreateFromSample(explanation, sample, 0.0)); @@ -99,10 +101,12 @@ public void testCreateConfigsGivenElasticsearchLog() throws Exception { public void testCreateConfigsGivenElasticsearchLogAndTimestampFormatOverride() throws Exception { - String sample = "12/31/2018 1:40PM INFO foo\n" - + "1/31/2019 11:40AM DEBUG bar\n" - + "2/1/2019 11:00PM INFO foo\n" - + "2/2/2019 1:23AM DEBUG bar\n"; + String sample = """ + 12/31/2018 1:40PM INFO foo + 1/31/2019 11:40AM DEBUG bar + 2/1/2019 11:00PM INFO foo + 2/2/2019 1:23AM DEBUG bar + """; TextStructureOverrides overrides = TextStructureOverrides.builder().setTimestampFormat("M/d/yyyy h:mma").build(); @@ -310,11 +314,13 @@ public void testErrorOnIncorrectMessageFormation() { // This sample causes problems because the (very weird) primary timestamp format // is not detected but a secondary format that only occurs in one line is detected - String sample = "Day 21 Month 1 Year 2019 11:04 INFO [localhost] - starting\n" - + "Day 21 Month 1 Year 2019 11:04 INFO [localhost] - startup date [Mon Jan 21 11:04:19 CET 2019]\n" - + "Day 21 Month 1 Year 2019 11:04 DEBUG [localhost] - details\n" - + "Day 21 Month 1 Year 2019 11:04 DEBUG [localhost] - more details\n" - + "Day 21 Month 1 Year 2019 11:04 WARN [localhost] - something went wrong\n"; + String sample = """ + Day 21 Month 1 Year 2019 11:04 INFO [localhost] - starting + Day 21 Month 1 Year 2019 11:04 INFO [localhost] - startup date [Mon Jan 21 11:04:19 CET 2019] + Day 21 Month 1 Year 2019 11:04 DEBUG [localhost] - details + Day 21 Month 1 Year 2019 11:04 DEBUG [localhost] - more details + Day 21 Month 1 Year 2019 11:04 WARN [localhost] - something went wrong + """; String charset = randomFrom(POSSIBLE_CHARSETS); Boolean hasByteOrderMarker = randomHasByteOrderMarker(charset); diff --git a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TextStructureTestCase.java b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TextStructureTestCase.java index 72b5bfdab7d90..1bb83f5ac8661 100644 --- a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TextStructureTestCase.java +++ b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TextStructureTestCase.java @@ -28,50 +28,56 @@ public abstract class TextStructureTestCase extends ESTestCase { .collect(Collectors.toList()) ); - protected static final String CSV_SAMPLE = "time,id,value\n" - + "2018-05-17T16:23:40,key1,42.0\n" - + "2018-05-17T16:24:11,\"key with spaces\",42.0\n"; - - protected static final String NDJSON_SAMPLE = "{\"logger\":\"controller\",\"timestamp\":1478261151445,\"level\":\"INFO\"," - + "\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 1\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n" - + "{\"logger\":\"controller\",\"timestamp\":1478261151445," - + "\"level\":\"INFO\",\"pid\":42,\"thread\":\"0x7fff7d2a8000\",\"message\":\"message 2\",\"class\":\"ml\"," - + "\"method\":\"core::SomeNoiseMaker\",\"file\":\"Noisemaker.cc\",\"line\":333}\n"; - - protected static final String PIPE_DELIMITED_SAMPLE = "2018-01-06 16:56:14.295748|INFO |VirtualServer |1 |" - + "listening on 0.0.0.0:9987, :::9987\n" - + "2018-01-06 17:19:44.465252|INFO |VirtualServer |1 |client " - + "'User1'(id:2) changed default admin channelgroup to 'Guest'(id:8)\n" - + "2018-01-06 17:21:25.764368|INFO |VirtualServer |1 |client " - + "'User1'(id:2) was added to channelgroup 'Channel Admin'(id:5) by client 'User1'(id:2) in channel 'Default Channel'(id:1)"; - - protected static final String SEMI_COLON_DELIMITED_SAMPLE = "\"pos_id\";\"trip_id\";\"latitude\";\"longitude\";\"altitude\";" - + "\"timestamp\"\n" - + "\"1\";\"3\";\"4703.7815\";\"1527.4713\";\"359.9\";\"2017-01-19 16:19:04.742113\"\n" - + "\"2\";\"3\";\"4703.7815\";\"1527.4714\";\"359.9\";\"2017-01-19 16:19:05.741890\"\n" - + "\"3\";\"3\";\"4703.7816\";\"1527.4716\";\"360.3\";\"2017-01-19 16:19:06.738842\""; - - protected static final String TEXT_SAMPLE = "[2018-05-11T17:07:29,461][INFO ][o.e.n.Node ] [node-0] initializing ...\n" - + "[2018-05-11T17:07:29,553][INFO ][o.e.e.NodeEnvironment ] [node-0] using [1] data paths, mounts [[/ (/dev/disk1)]], " - + "net usable_space [223.4gb], net total_space [464.7gb], types [hfs]\n" - + "[2018-05-11T17:07:29,553][INFO ][o.e.e.NodeEnvironment ] [node-0] heap size [3.9gb], " - + "compressed ordinary object pointers [true]\n" - + "[2018-05-11T17:07:29,556][INFO ][o.e.n.Node ] [node-0] node name [node-0], node ID [tJ9u8HcaTbWxRtnlfz1RQA]\n"; - - protected static final String TSV_SAMPLE = "time\tid\tvalue\n" - + "2018-05-17T16:23:40\tkey1\t42.0\n" - + "2018-05-17T16:24:11\t\"key with spaces\"\t42.0\n"; - - protected static final String XML_SAMPLE = "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n"; + protected static final String CSV_SAMPLE = """ + time,id,value + 2018-05-17T16:23:40,key1,42.0 + 2018-05-17T16:24:11,"key with spaces",42.0 + """; + protected static final String NDJSON_SAMPLE = """ + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 1",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + {"logger":"controller","timestamp":1478261151445,"level":"INFO","pid":42,"thread":"0x7fff7d2a8000","message":"message 2",\ + "class":"ml","method":"core::SomeNoiseMaker","file":"Noisemaker.cc","line":333} + """; + + protected static final String PIPE_DELIMITED_SAMPLE = """ + 2018-01-06 16:56:14.295748|INFO |VirtualServer |1 |listening on 0.0.0.0:9987, :::9987 + 2018-01-06 17:19:44.465252|INFO |VirtualServer |1 |client 'User1'(id:2) changed default admin channelgroup to 'Guest'(id:8) + 2018-01-06 17:21:25.764368|INFO |VirtualServer |1 |client 'User1'(id:2) was added to channelgroup 'Channel Admin'(id:5) by \ + client 'User1'(id:2) in channel 'Default Channel'(id:1)"""; + + protected static final String SEMI_COLON_DELIMITED_SAMPLE = """ + "pos_id";"trip_id";"latitude";"longitude";"altitude";"timestamp" + "1";"3";"4703.7815";"1527.4713";"359.9";"2017-01-19 16:19:04.742113" + "2";"3";"4703.7815";"1527.4714";"359.9";"2017-01-19 16:19:05.741890" + "3";"3";"4703.7816";"1527.4716";"360.3";"2017-01-19 16:19:06.738842 + """; + + protected static final String TEXT_SAMPLE = """ + [2018-05-11T17:07:29,461][INFO ][o.e.n.Node ] [node-0] initializing ... + [2018-05-11T17:07:29,553][INFO ][o.e.e.NodeEnvironment ] [node-0] using [1] data paths, mounts [[/ (/dev/disk1)]], net \ + usable_space [223.4gb], net total_space [464.7gb], types [hfs] + [2018-05-11T17:07:29,553][INFO ][o.e.e.NodeEnvironment ] [node-0] heap size [3.9gb], compressed ordinary object pointers \ + [true] + [2018-05-11T17:07:29,556][INFO ][o.e.n.Node ] [node-0] node name [node-0], node ID [tJ9u8HcaTbWxRtnlfz1RQA] + """; + + protected static final String TSV_SAMPLE = """ + time\tid\tvalue + 2018-05-17T16:23:40\tkey1\t42.0 + 2018-05-17T16:24:11\t"key with spaces"\t42.0 + """; + + protected static final String XML_SAMPLE = """ + + + + + + + + + """; // This doesn't need closing because it has an infinite timeout protected static final TimeoutChecker NOOP_TIMEOUT_CHECKER = new TimeoutChecker("unit test", null, null); diff --git a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TimestampFormatFinderTests.java b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TimestampFormatFinderTests.java index bf03d2c6e0709..3bd93302a96be 100644 --- a/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TimestampFormatFinderTests.java +++ b/x-pack/plugin/text-structure/src/test/java/org/elasticsearch/xpack/textstructure/structurefinder/TimestampFormatFinderTests.java @@ -25,90 +25,53 @@ import java.util.regex.Pattern; public class TimestampFormatFinderTests extends TextStructureTestCase { - + @SuppressWarnings("checkstyle:linelength") private static final String EXCEPTION_TRACE_SAMPLE = - "[2018-02-28T14:49:40,517][DEBUG][o.e.a.b.TransportShardBulkAction] [an_index][2] failed to execute bulk item " - + "(index) BulkShardRequest [[an_index][2]] containing [33] requests\n" - + "java.lang.IllegalArgumentException: Document contains at least one immense term in field=\"message.keyword\" (whose UTF8 " - + "encoding is longer than the max length 32766), all of which were skipped. Please correct the analyzer to not produce " - + "such terms. The prefix of the first immense term is: '[60, 83, 79, 65, 80, 45, 69, 78, 86, 58, 69, 110, 118, 101, 108, " - + "111, 112, 101, 32, 120, 109, 108, 110, 115, 58, 83, 79, 65, 80, 45]...', original message: bytes can be at most 32766 " - + "in length; got 49023\n" - + "\tat org.apache.lucene.index.DefaultIndexingChain$PerField.invert(DefaultIndexingChain.java:796) " - + "~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43]\n" - + "\tat org.apache.lucene.index.DefaultIndexingChain.processField(DefaultIndexingChain.java:430) " - + "~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43]\n" - + "\tat org.apache.lucene.index.DefaultIndexingChain.processDocument(DefaultIndexingChain.java:392) " - + "~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43]\n" - + "\tat org.apache.lucene.index.DocumentsWriterPerThread.updateDocument(DocumentsWriterPerThread.java:240) " - + "~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43]\n" - + "\tat org.apache.lucene.index.DocumentsWriter.updateDocument(DocumentsWriter.java:496) " - + "~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43]\n" - + "\tat org.apache.lucene.index.IndexWriter.updateDocument(IndexWriter.java:1729) " - + "~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43]\n" - + "\tat org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1464) " - + "~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43]\n" - + "\tat org.elasticsearch.index.engine.InternalEngine.index(InternalEngine.java:1070) ~[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.index.engine.InternalEngine.indexIntoLucene(InternalEngine.java:1012) " - + "~[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.index.engine.InternalEngine.index(InternalEngine.java:878) ~[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.index.shard.IndexShard.index(IndexShard.java:738) ~[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.index.shard.IndexShard.applyIndexOperation(IndexShard.java:707) ~[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.index.shard.IndexShard.applyIndexOperationOnPrimary(IndexShard.java:673) " - + "~[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.bulk.TransportShardBulkAction.executeIndexRequestOnPrimary(TransportShardBulkAction.java:548) " - + "~[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.bulk.TransportShardBulkAction.executeIndexRequest(TransportShardBulkAction.java:140) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.bulk.TransportShardBulkAction.executeBulkItemRequest(TransportShardBulkAction.java:236) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.bulk.TransportShardBulkAction.performOnPrimary(TransportShardBulkAction.java:123) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:110) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:72) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryShardReference.perform" - + "(TransportReplicationAction.java:1034) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryShardReference.perform" - + "(TransportReplicationAction.java:1012) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.ReplicationOperation.execute(ReplicationOperation.java:103) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$AsyncPrimaryAction.onResponse" - + "(TransportReplicationAction.java:359) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$AsyncPrimaryAction.onResponse" - + "(TransportReplicationAction.java:299) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$1.onResponse" - + "(TransportReplicationAction.java:975) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$1.onResponse" - + "(TransportReplicationAction.java:972) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.index.shard.IndexShardOperationPermits.acquire(IndexShardOperationPermits.java:238) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.index.shard.IndexShard.acquirePrimaryOperationPermit(IndexShard.java:2220) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction.acquirePrimaryShardReference" - + "(TransportReplicationAction.java:984) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction.access$500(TransportReplicationAction.java:98) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$AsyncPrimaryAction.doRun" - + "(TransportReplicationAction.java:320) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler" - + ".messageReceived(TransportReplicationAction.java:295) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler" - + ".messageReceived(TransportReplicationAction.java:282) [elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:66) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:656) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:635) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) " - + "[elasticsearch-6.2.1.jar:6.2.1]\n" - + "\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_144]\n" - + "\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_144]\n" - + "\tat java.lang.Thread.run(Thread.java:748) [?:1.8.0_144]\n"; + """ + [2018-02-28T14:49:40,517][DEBUG][o.e.a.b.TransportShardBulkAction] [an_index][2] failed to execute bulk item (index) BulkShardRequest [[an_index][2]] containing [33] requests + java.lang.IllegalArgumentException: Document contains at least one immense term in field="message.keyword" (whose UTF8 encoding is longer than the max length 32766), all of which were skipped. Please correct the analyzer to not produce such terms. The prefix of the first immense term is: '[60, 83, 79, 65, 80, 45, 69, 78, 86, 58, 69, 110, 118, 101, 108, 111, 112, 101, 32, 120, 109, 108, 110, 115, 58, 83, 79, 65, 80, 45]...', original message: bytes can be at most 32766 in length; got 49023 + at org.apache.lucene.index.DefaultIndexingChain$PerField.invert(DefaultIndexingChain.java:796) ~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43] + at org.apache.lucene.index.DefaultIndexingChain.processField(DefaultIndexingChain.java:430) ~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43] + at org.apache.lucene.index.DefaultIndexingChain.processDocument(DefaultIndexingChain.java:392) ~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43] + at org.apache.lucene.index.DocumentsWriterPerThread.updateDocument(DocumentsWriterPerThread.java:240) ~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43] + at org.apache.lucene.index.DocumentsWriter.updateDocument(DocumentsWriter.java:496) ~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43] + at org.apache.lucene.index.IndexWriter.updateDocument(IndexWriter.java:1729) ~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43] + at org.apache.lucene.index.IndexWriter.addDocument(IndexWriter.java:1464) ~[lucene-core-7.2.1.jar:7.2.1 b2b6438b37073bee1fca40374e85bf91aa457c0b - ubuntu - 2018-01-10 00:48:43] + at org.elasticsearch.index.engine.InternalEngine.index(InternalEngine.java:1070) ~[elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.index.engine.InternalEngine.indexIntoLucene(InternalEngine.java:1012) ~[elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.index.engine.InternalEngine.index(InternalEngine.java:878) ~[elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.index.shard.IndexShard.index(IndexShard.java:738) ~[elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.index.shard.IndexShard.applyIndexOperation(IndexShard.java:707) ~[elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.index.shard.IndexShard.applyIndexOperationOnPrimary(IndexShard.java:673) ~[elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.bulk.TransportShardBulkAction.executeIndexRequestOnPrimary(TransportShardBulkAction.java:548) ~[elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.bulk.TransportShardBulkAction.executeIndexRequest(TransportShardBulkAction.java:140) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.bulk.TransportShardBulkAction.executeBulkItemRequest(TransportShardBulkAction.java:236) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.bulk.TransportShardBulkAction.performOnPrimary(TransportShardBulkAction.java:123) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:110) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.bulk.TransportShardBulkAction.shardOperationOnPrimary(TransportShardBulkAction.java:72) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryShardReference.perform(TransportReplicationAction.java:1034) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryShardReference.perform(TransportReplicationAction.java:1012) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.ReplicationOperation.execute(ReplicationOperation.java:103) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$AsyncPrimaryAction.onResponse(TransportReplicationAction.java:359) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$AsyncPrimaryAction.onResponse(TransportReplicationAction.java:299) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$1.onResponse(TransportReplicationAction.java:975) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$1.onResponse(TransportReplicationAction.java:972) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.index.shard.IndexShardOperationPermits.acquire(IndexShardOperationPermits.java:238) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.index.shard.IndexShard.acquirePrimaryOperationPermit(IndexShard.java:2220) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction.acquirePrimaryShardReference(TransportReplicationAction.java:984) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction.access$500(TransportReplicationAction.java:98) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$AsyncPrimaryAction.doRun(TransportReplicationAction.java:320) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler.messageReceived(TransportReplicationAction.java:295) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.action.support.replication.TransportReplicationAction$PrimaryOperationTransportHandler.messageReceived(TransportReplicationAction.java:282) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:66) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:656) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:635) [elasticsearch-6.2.1.jar:6.2.1] + at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-6.2.1.jar:6.2.1] + at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_144] + at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_144] + at java.lang.Thread.run(Thread.java:748) [?:1.8.0_144] + """; public void testValidOverrideFormatToGrokAndRegex() { @@ -837,10 +800,8 @@ public void testFindBoundsForCandidate() { TimestampFormatFinder.findBoundsForCandidate(TimestampFormatFinder.TAI64N_CANDIDATE_FORMAT, numberPosBitSet) ); - numberPosBitSet = TimestampFormatFinder.stringToNumberPosBitSet( - "192.168.62.101 - - [29/Jun/2016:12:11:31 +0000] " - + "\"POST //apiserv:8080/engine/v2/jobs HTTP/1.1\" 201 42 \"-\" \"curl/7.46.0\" 384" - ); + numberPosBitSet = TimestampFormatFinder.stringToNumberPosBitSet(""" + 192.168.62.101 - - [29/Jun/2016:12:11:31 +0000] "POST //apiserv:8080/engine/v2/jobs HTTP/1.1" 201 42 "-" "curl/7.46.0" 384"""); assertEquals( new Tuple<>(-1, -1), TimestampFormatFinder.findBoundsForCandidate(TimestampFormatFinder.ISO8601_CANDIDATE_FORMAT, numberPosBitSet) @@ -1453,54 +1414,42 @@ public void testFindFormatGivenRealLogMessages() { } public void testSelectBestMatchGivenAllSame() { - String sample = "[2018-06-27T11:59:22,125][INFO ][o.e.n.Node ] [node-0] initializing ...\n" - + "[2018-06-27T11:59:22,201][INFO ][o.e.e.NodeEnvironment ] [node-0] using [1] data paths, mounts [[/ (/dev/disk1)]], " - + "net usable_space [216.1gb], net total_space [464.7gb], types [hfs]\n" - + "[2018-06-27T11:59:22,202][INFO ][o.e.e.NodeEnvironment ] [node-0] heap size [494.9mb], " - + "compressed ordinary object pointers [true]\n" - + "[2018-06-27T11:59:22,204][INFO ][o.e.n.Node ] [node-0] node name [node-0], node ID [Ha1gD8nNSDqjd6PIyu3DJA]\n" - + "[2018-06-27T11:59:22,204][INFO ][o.e.n.Node ] [node-0] version[6.4.0-SNAPSHOT], pid[2785], " - + "build[default/zip/3c60efa/2018-06-26T14:55:15.206676Z], OS[Mac OS X/10.12.6/x86_64], " - + "JVM[\"Oracle Corporation\"/Java HotSpot(TM) 64-Bit Server VM/10/10+46]\n" - + "[2018-06-27T11:59:22,205][INFO ][o.e.n.Node ] [node-0] JVM arguments [-Xms1g, -Xmx1g, " - + "-XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, " - + "-XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, " - + "-XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, " - + "-Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, " - + "-Djava.io.tmpdir=/var/folders/k5/5sqcdlps5sg3cvlp783gcz740000h0/T/elasticsearch.nFUyeMH1, " - + "-XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, " - + "-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m, " - + "-Djava.locale.providers=COMPAT, -Dio.netty.allocator.type=unpooled, -ea, -esa, -Xms512m, -Xmx512m, " - + "-Des.path.home=/Users/dave/elasticsearch/distribution/build/cluster/run node0/elasticsearch-6.4.0-SNAPSHOT, " - + "-Des.path.conf=/Users/dave/elasticsearch/distribution/build/cluster/run node0/elasticsearch-6.4.0-SNAPSHOT/config, " - + "-Des.distribution.flavor=default, -Des.distribution.type=zip]\n" - + "[2018-06-27T11:59:22,205][WARN ][o.e.n.Node ] [node-0] version [6.4.0-SNAPSHOT] is a pre-release version of " - + "Elasticsearch and is not suitable for production\n" - + "[2018-06-27T11:59:23,585][INFO ][o.e.p.PluginsService ] [node-0] loaded module [aggs-matrix-stats]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [analysis-common]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [ingest-common]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [lang-expression]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [lang-mustache]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [lang-painless]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [mapper-extras]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [parent-join]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [percolator]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [rank-eval]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [reindex]\n" - + "[2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [repository-url]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [transport-netty4]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-core]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-deprecation]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-graph]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-logstash]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-ml]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-monitoring]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-rollup]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-security]\n" - + "[2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-sql]\n" - + "[2018-06-27T11:59:23,588][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-upgrade]\n" - + "[2018-06-27T11:59:23,588][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-watcher]\n" - + "[2018-06-27T11:59:23,588][INFO ][o.e.p.PluginsService ] [node-0] no plugins loaded\n"; + @SuppressWarnings("checkstyle:linelength") + String sample = + """ + [2018-06-27T11:59:22,125][INFO ][o.e.n.Node ] [node-0] initializing ... + [2018-06-27T11:59:22,201][INFO ][o.e.e.NodeEnvironment ] [node-0] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [216.1gb], net total_space [464.7gb], types [hfs] + [2018-06-27T11:59:22,202][INFO ][o.e.e.NodeEnvironment ] [node-0] heap size [494.9mb], compressed ordinary object pointers [true] + [2018-06-27T11:59:22,204][INFO ][o.e.n.Node ] [node-0] node name [node-0], node ID [Ha1gD8nNSDqjd6PIyu3DJA] + [2018-06-27T11:59:22,204][INFO ][o.e.n.Node ] [node-0] version[6.4.0-SNAPSHOT], pid[2785], build[default/zip/3c60efa/2018-06-26T14:55:15.206676Z], OS[Mac OS X/10.12.6/x86_64], JVM["Oracle Corporation"/Java HotSpot(TM) 64-Bit Server VM/10/10+46] + [2018-06-27T11:59:22,205][INFO ][o.e.n.Node ] [node-0] JVM arguments [-Xms1g, -Xmx1g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.io.tmpdir=/var/folders/k5/5sqcdlps5sg3cvlp783gcz740000h0/T/elasticsearch.nFUyeMH1, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m, -Djava.locale.providers=COMPAT, -Dio.netty.allocator.type=unpooled, -ea, -esa, -Xms512m, -Xmx512m, -Des.path.home=/Users/dave/elasticsearch/distribution/build/cluster/run node0/elasticsearch-6.4.0-SNAPSHOT, -Des.path.conf=/Users/dave/elasticsearch/distribution/build/cluster/run node0/elasticsearch-6.4.0-SNAPSHOT/config, -Des.distribution.flavor=default, -Des.distribution.type=zip] + [2018-06-27T11:59:22,205][WARN ][o.e.n.Node ] [node-0] version [6.4.0-SNAPSHOT] is a pre-release version of Elasticsearch and is not suitable for production + [2018-06-27T11:59:23,585][INFO ][o.e.p.PluginsService ] [node-0] loaded module [aggs-matrix-stats] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [analysis-common] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [ingest-common] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [lang-expression] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [lang-mustache] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [lang-painless] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [mapper-extras] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [parent-join] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [percolator] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [rank-eval] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [reindex] + [2018-06-27T11:59:23,586][INFO ][o.e.p.PluginsService ] [node-0] loaded module [repository-url] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [transport-netty4] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-core] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-deprecation] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-graph] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-logstash] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-ml] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-monitoring] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-rollup] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-security] + [2018-06-27T11:59:23,587][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-sql] + [2018-06-27T11:59:23,588][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-upgrade] + [2018-06-27T11:59:23,588][INFO ][o.e.p.PluginsService ] [node-0] loaded module [x-pack-watcher] + [2018-06-27T11:59:23,588][INFO ][o.e.p.PluginsService ] [node-0] no plugins loaded + """; TimestampFormatFinder timestampFormatFinder = LogTextStructureFinder.populateTimestampFormatFinder( explanation, diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java index a83b3813f7856..3a5ea944761d2 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TestFeatureResetIT.java @@ -38,11 +38,13 @@ public class TestFeatureResetIT extends TransformIntegTestCase { @Before public void setLogging() throws IOException { Request settingsRequest = new Request("PUT", "/_cluster/settings"); - settingsRequest.setJsonEntity( - "{\"persistent\": {" - + "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"debug\"," - + "\"logger.org.elasticsearch.xpack.transform\": \"trace\"}}" - ); + settingsRequest.setJsonEntity(""" + { + "persistent": { + "logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer": "debug", + "logger.org.elasticsearch.xpack.transform": "trace" + } + }"""); client().performRequest(settingsRequest); } diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java index 6140edbb02c68..3bea7753a7622 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIT.java @@ -70,11 +70,13 @@ static String getDateStringForRow(int row) { @Before public void setClusterSettings() throws IOException { Request settingsRequest = new Request("PUT", "/_cluster/settings"); - settingsRequest.setJsonEntity( - "{\"persistent\": {" - + "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"debug\"," - + "\"logger.org.elasticsearch.xpack.transform\": \"debug\"}}" - ); + settingsRequest.setJsonEntity(""" + { + "persistent": { + "logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer": "debug", + "logger.org.elasticsearch.xpack.transform": "debug" + } + }"""); client().performRequest(settingsRequest); } @@ -408,21 +410,10 @@ private void indexMoreDocs(long timestamp, long userId, String index) throws Exc int stars = (i + 20) % 5; long business = (i + 100) % 50; - StringBuilder sourceBuilder = new StringBuilder(); - sourceBuilder.append("{\"user_id\":\"") - .append("user_") - .append(userId) - .append("\",\"count\":") - .append(i) - .append(",\"business_id\":\"") - .append("business_") - .append(business) - .append("\",\"stars\":") - .append(stars) - .append(",\"timestamp\":") - .append(timestamp) - .append("}"); - bulk.add(new IndexRequest().source(sourceBuilder.toString(), XContentType.JSON)); + String source = """ + {"user_id":"user_%s","count":%s,"business_id":"business_%s","stars":%s,"timestamp":%s} + """.formatted(userId, i, business, stars, timestamp); + bulk.add(new IndexRequest().source(source, XContentType.JSON)); } bulk.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); bulkIndexDocs(bulk); diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java index 3039691ccfd36..4b62cc6e80376 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java @@ -379,20 +379,10 @@ protected void createReviewsIndex( if (user != null) { sourceBuilder.append("\"user_id\":\"").append("user_").append(user).append("\","); } - sourceBuilder.append("\"count\":") - .append(i) - .append(",\"business_id\":\"") - .append("business_") - .append(business) - .append("\",\"stars\":") - .append(stars) - .append(",\"comment\":") - .append("\"Great stuff, deserves " + stars + " stars\"") - .append(",\"regular_object\":{\"foo\": 42}") - .append(",\"nested_object\":{\"bar\": 43}") - .append(",\"timestamp\":\"") - .append(dateString) - .append("\"}"); + sourceBuilder.append(""" + "count":%s,"business_id":"business_%s","stars":%s,"comment":"Great stuff, deserves %s stars","regular_object":\ + {"foo": 42},"nested_object":{"bar": 43},"timestamp":"%s"} + """.formatted(i, business, stars, stars, dateString)); bulk.add(new IndexRequest().source(sourceBuilder.toString(), XContentType.JSON)); if (i % 100 == 0) { diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java index a195cd8afeba4..b386fe4a59f47 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/continuous/TransformContinuousIT.java @@ -117,15 +117,16 @@ public void setClusterSettings() throws IOException { // Set logging level to trace // see: https://github.com/elastic/elasticsearch/issues/45562 Request addFailureRetrySetting = new Request("PUT", "/_cluster/settings"); - addFailureRetrySetting.setJsonEntity( - "{\"persistent\": {\"xpack.transform.num_transform_failure_retries\": \"" - + 0 - + "\"," - + "\"logger.org.elasticsearch.action.bulk\": \"info\"," - + // reduces bulk failure spam - "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"debug\"," - + "\"logger.org.elasticsearch.xpack.transform\": \"debug\"}}" - ); + // reduces bulk failure spam + addFailureRetrySetting.setJsonEntity(""" + { + "persistent": { + "xpack.transform.num_transform_failure_retries": "0", + "logger.org.elasticsearch.action.bulk": "info", + "logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer": "debug", + "logger.org.elasticsearch.xpack.transform": "debug" + } + }"""); client().performRequest(addFailureRetrySetting); } @@ -428,17 +429,13 @@ private void putIndex(String indexName, String dateType, boolean isDataStream) t logger.info("Creating source index with: {}", indexSettingsAndMappings); if (isDataStream) { Request createCompositeTemplate = new Request("PUT", "_index_template/" + indexName + "_template"); - createCompositeTemplate.setJsonEntity( - "{\n" - + " \"index_patterns\": [ \"" - + indexName - + "\" ],\n" - + " \"data_stream\": {\n" - + " },\n" - + " \"template\": \n" - + indexSettingsAndMappings - + "}" - ); + createCompositeTemplate.setJsonEntity(""" + { + "index_patterns": [ "%s" ], + "data_stream": { + }, + "template": %s + }""".formatted(indexName, indexSettingsAndMappings)); client().performRequest(createCompositeTemplate); client().performRequest(new Request("PUT", "_data_stream/" + indexName)); } else { diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java index acbd8cbd74726..b0d56f47cbec6 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformAuditorIT.java @@ -70,7 +70,8 @@ public void testAuditorWritesAudits() throws Exception { // Make sure we wrote to the audit final Request request = new Request("GET", TransformInternalIndexConstants.AUDIT_INDEX + "/_search"); - request.setJsonEntity("{\"query\":{\"term\":{\"transform_id\":\"simple_pivot_for_audit\"}}}"); + request.setJsonEntity(""" + {"query":{"term":{"transform_id":"simple_pivot_for_audit"}}}"""); assertBusy(() -> { assertTrue(indexExists(TransformInternalIndexConstants.AUDIT_INDEX)); assertTrue(aliasExists(TransformInternalIndexConstants.AUDIT_INDEX_READ_ALIAS)); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java index 69548506fd560..89042a05a7bf1 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformGetAndGetStatsIT.java @@ -407,26 +407,38 @@ public void testGetStatsWithContinuous() throws Exception { String transformSrc = "reviews_cont_pivot_test"; createReviewsIndex(transformSrc); final Request createTransformRequest = createRequestWithAuth("PUT", getTransformEndpoint() + transformId, null); - String config = "{ \"dest\": {\"index\":\"" - + transformDest - + "\"}," - + " \"source\": {\"index\":\"" - + transformSrc - + "\"}," - + " \"frequency\": \"1s\"," - + " \"sync\": {\"time\":{\"field\": \"timestamp\", \"delay\": \"1s\"}}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "dest": { + "index": "%s" + }, + "source": { + "index": "%s" + }, + "frequency": "1s", + "sync": { + "time": { + "field": "timestamp", + "delay": "1s" + } + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(transformDest, transformSrc); createTransformRequest.setJsonEntity(config); @@ -464,19 +476,11 @@ public void testGetStatsWithContinuous() throws Exception { final StringBuilder bulk = new StringBuilder(); long now = Instant.now().toEpochMilli() - 1_000; for (int i = 0; i < numDocs; i++) { - bulk.append("{\"index\":{\"_index\":\"" + transformSrc + "\"}}\n") - .append("{\"user_id\":\"") - .append("user_") - // Doing only new users so that there is a deterministic number of docs for progress - .append(randomFrom(42, 47, 113)) - .append("\",\"business_id\":\"") - .append("business_") - .append(10) - .append("\",\"stars\":") - .append(5) - .append(",\"timestamp\":") - .append(now) - .append("}\n"); + // Doing only new users so that there is a deterministic number of docs for progress + bulk.append(""" + {"index":{"_index":"%s"}} + {"user_id":"user_%s","business_id":"business_%s","stars":%s,"timestamp":%s} + """.formatted(transformSrc, randomFrom(42, 47, 113), 10, 5, now)); } bulk.append("\r\n"); final Request bulkRequest = new Request("POST", "/_bulk"); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java index 9485a6b851489..6bfdd57eba112 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java @@ -144,22 +144,31 @@ public void testSimpleBooleanPivot() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "" - + "{" - + "\"source\":{\"index\": \"boolean_value\"}," - + "\"dest\" :{\"index\": \"pivot_boolean_value\"}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"bool\": {" - + " \"terms\": {" - + " \"field\": \"bool\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"val\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "boolean_value" + }, + "dest": { + "index": "pivot_boolean_value" + }, + "pivot": { + "group_by": { + "bool": { + "terms": { + "field": "bool" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "val" + } + } + } + } + }"""; createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -202,27 +211,34 @@ public void testSimplePivotWithScript() throws Exception { ); // same pivot as testSimplePivot, but we retrieve the grouping key using a script and add prefix - String config = "{" - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"script\": {" - + " \"source\": \"'reviewer_' + doc['user_id'].value\"" - + " } } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }," - + "\"frequency\":\"1s\"" - + "}"; + String config = """ + { + "dest": { + "index": "%s" + }, + "source": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "script": { + "source": "'reviewer_' + doc['user_id'].value" + } + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + }, + "frequency": "1s" + }""".formatted(transformIndex, REVIEWS_INDEX_NAME); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -248,20 +264,18 @@ public void testPivotWithPipeline() throws Exception { String pipelineId = "my-pivot-pipeline"; int pipelineValue = 42; Request pipelineRequest = new Request("PUT", "/_ingest/pipeline/" + pipelineId); - pipelineRequest.setJsonEntity( - "{\n" - + " \"description\" : \"my pivot pipeline\",\n" - + " \"processors\" : [\n" - + " {\n" - + " \"set\" : {\n" - + " \"field\": \"pipeline_field\",\n" - + " \"value\": " - + pipelineValue - + " }\n" - + " }\n" - + " ]\n" - + "}" - ); + pipelineRequest.setJsonEntity(""" + { + "description" : "my pivot pipeline", + "processors" : [ + { + "set" : { + "field": "pipeline_field", + "value": %s + } + } + ] + }""".formatted(pipelineValue)); client().performRequest(pipelineRequest); setupDataAccessRole(DATA_ACCESS_ROLE, REVIEWS_INDEX_NAME, transformIndex); @@ -294,32 +308,40 @@ public void testBucketSelectorPivot() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"over_38\": {" - + " \"bucket_selector\" : {" - + " \"buckets_path\": {\"rating\":\"avg_rating\"}, " - + " \"script\": \"params.rating > 3.8\"" - + " }" - + " } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "over_38": { + "bucket_selector": { + "buckets_path": { + "rating": "avg_rating" + }, + "script": "params.rating > 3.8" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -346,28 +368,39 @@ public void testContinuousPivot() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + indexName - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"sync\": {\"time\": {\"field\": \"timestamp\", \"delay\": \"1s\"}}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"," - + " \"missing_bucket\": true" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "sync": { + "time": { + "field": "timestamp", + "delay": "1s" + } + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id", + "missing_bucket": true + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -392,54 +425,23 @@ public void testContinuousPivot() throws Exception { long dateStamp = Instant.now().toEpochMilli() - 1_000; for (int i = 0; i < 25; i++) { - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); int stars = (i * 32) % 5; long business = (stars * user) % 13; String location = (user + 10) + "," + (user + 15); - - bulk.append("{\"user_id\":\"") - .append("user_") - .append(user) - .append("\",\"business_id\":\"") - .append("business_") - .append(business) - .append("\",\"stars\":") - .append(stars) - .append(",\"location\":\"") - .append(location) - .append("\",\"timestamp\":") - .append(dateStamp) - .append("}\n"); - + bulk.append(""" + {"index":{"_index":"%s"}} + {"user_id":"user_%s","business_id":"business_%s","stars":%s,"location":"%s","timestamp":%s} + """.formatted(indexName, user, business, stars, location, dateStamp)); stars = 5; business = 11; - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"user_id\":\"") - .append("user_") - .append(user26) - .append("\",\"business_id\":\"") - .append("business_") - .append(business) - .append("\",\"stars\":") - .append(stars) - .append(",\"location\":\"") - .append(location) - .append("\",\"timestamp\":") - .append(dateStamp) - .append("}\n"); - - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{") - .append("\"business_id\":\"") - .append("business_") - .append(business) - .append("\",\"stars\":") - .append(stars) - .append(",\"location\":\"") - .append(location) - .append("\",\"timestamp\":") - .append(dateStamp) - .append("}\n"); + bulk.append(""" + {"index":{"_index":"%s"}} + {"user_id":"user_%s","business_id":"business_%s","stars":%s,"location":"%s","timestamp":%s} + """.formatted(indexName, user26, business, stars, location, dateStamp)); + bulk.append(""" + {"index":{"_index":"%s"}} + {"business_id":"business_%s","stars":%s,"location":"%s","timestamp":%s} + """.formatted(indexName, business, stars, location, dateStamp)); } bulk.append("\r\n"); @@ -474,26 +476,32 @@ public void testHistogramPivot() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"every_2\": {" - + " \"histogram\": {" - + " \"interval\": 2,\"field\":\"stars\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "every_2": { + "histogram": { + "interval": 2, + "field": "stars" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -519,27 +527,39 @@ public void testContinuousPivotHistogram() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + indexName - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"sync\": {\"time\": {\"field\": \"timestamp\", \"delay\": \"1s\"}}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"every_2\": {" - + " \"histogram\": {" - + " \"interval\": 2,\"field\":\"stars\"" - + " } } }," - + " \"aggregations\": {" - + " \"user_dc\": {" - + " \"cardinality\": {" - + " \"field\": \"user_id\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "sync": { + "time": { + "field": "timestamp", + "delay": "1s" + } + }, + "pivot": { + "group_by": { + "every_2": { + "histogram": { + "interval": 2, + "field": "stars" + } + } + }, + "aggregations": { + "user_dc": { + "cardinality": { + "field": "user_id" + } + } + } + } + }""".formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -569,22 +589,11 @@ public void testContinuousPivotHistogram() throws Exception { // add 5 data points with 3 new users: 27, 28, 29 for (int i = 25; i < 30; i++) { - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); String location = (i + 10) + "," + (i + 15); - - bulk.append("{\"user_id\":\"") - .append("user_") - .append(i) - .append("\",\"business_id\":\"") - .append("business_") - .append(i) - .append("\",\"stars\":") - .append(3) - .append(",\"location\":\"") - .append(location) - .append("\",\"timestamp\":") - .append(dateStamp) - .append("}\n"); + bulk.append(""" + {"index":{"_index":"%s"}} + {"user_id":"user_%s","business_id":"business_%s","stars":%s,"location":"%s","timestamp":%s} + """.formatted(indexName, i, i, 3, location, dateStamp)); } bulk.append("\r\n"); @@ -630,51 +639,61 @@ public void testBiggerPivot() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"variability_rating\": {" - + " \"median_absolute_deviation\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"sum_rating\": {" - + " \"sum\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"cardinality_business\": {" - + " \"cardinality\": {" - + " \"field\": \"business_id\"" - + " } }," - + " \"min_rating\": {" - + " \"min\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"max_rating\": {" - + " \"max\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"count\": {" - + " \"value_count\": {" - + " \"field\": \"business_id\"" - + " } }" - + " } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "variability_rating": { + "median_absolute_deviation": { + "field": "stars" + } + }, + "sum_rating": { + "sum": { + "field": "stars" + } + }, + "cardinality_business": { + "cardinality": { + "field": "business_id" + } + }, + "min_rating": { + "min": { + "field": "stars" + } + }, + "max_rating": { + "max": { + "field": "stars" + } + }, + "count": { + "value_count": { + "field": "business_id" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -727,39 +746,46 @@ public void testPivotWithTermsAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"every_2\": {" - + " \"histogram\": {" - + " \"interval\": 2,\"field\":\"stars\"" - + " } } }," - + " \"aggregations\": {" - + " \"common_users\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"," - + " \"size\": 2" - + " }," - + " \"aggs\" : {" - + " \"common_businesses\": {" - + " \"terms\": {" - + " \"field\": \"business_id\"," - + " \"size\": 2" - + " }}" - + " } " - + " }," - + " \"rare_users\": {" - + " \"rare_terms\": {" - + " \"field\": \"user_id\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "every_2": { + "histogram": { + "interval": 2, + "field": "stars" + } + } + }, + "aggregations": { + "common_users": { + "terms": { + "field": "user_id", + "size": 2 + }, + "aggs": { + "common_businesses": { + "terms": { + "field": "business_id", + "size": 2 + } + } + } + }, + "rare_users": { + "rare_terms": { + "field": "user_id" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -820,20 +846,32 @@ private void assertDateHistogramPivot(String indexName) throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" + " \"source\": {\"index\":\"" + indexName + "\"}," + " \"dest\": {\"index\":\"" + transformIndex + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"by_hr\": {" - + " \"date_histogram\": {" - + " \"fixed_interval\": \"1h\",\"field\":\"timestamp\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "by_hr": { + "date_histogram": { + "fixed_interval": "1h", + "field": "timestamp" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); @@ -857,18 +895,35 @@ public void testPreviewTransform() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" + " \"source\": {\"index\":\"" + REVIEWS_INDEX_NAME + "\"} ,"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"user.id\": {\"terms\": { \"field\": \"user_id\" }}," - + " \"by_day\": {\"date_histogram\": {\"fixed_interval\": \"1d\",\"field\":\"timestamp\"}}}," - + " \"aggregations\": {" - + " \"user.avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "pivot": { + "group_by": { + "user.id": { + "terms": { + "field": "user_id" + } + }, + "by_day": { + "date_histogram": { + "fixed_interval": "1d", + "field": "timestamp" + } + } + }, + "aggregations": { + "user.avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME); + createPreviewRequest.setJsonEntity(config); Map previewTransformResponse = entityAsMap(client().performRequest(createPreviewRequest)); @@ -891,41 +946,55 @@ public void testPreviewTransformWithPipeline() throws Exception { String pipelineId = "my-preview-pivot-pipeline"; int pipelineValue = 42; Request pipelineRequest = new Request("PUT", "/_ingest/pipeline/" + pipelineId); - pipelineRequest.setJsonEntity( - "{\n" - + " \"description\" : \"my pivot preview pipeline\",\n" - + " \"processors\" : [\n" - + " {\n" - + " \"set\" : {\n" - + " \"field\": \"pipeline_field\",\n" - + " \"value\": " - + pipelineValue - + " }\n" - + " }\n" - + " ]\n" - + "}" - ); + pipelineRequest.setJsonEntity(""" + { + "description": "my pivot preview pipeline", + "processors": [ + { + "set": { + "field": "pipeline_field", + "value": %s + } + } + ] + } + """.formatted(pipelineValue)); client().performRequest(pipelineRequest); setupDataAccessRole(DATA_ACCESS_ROLE, REVIEWS_INDEX_NAME); final Request createPreviewRequest = createRequestWithAuth("POST", getTransformEndpoint() + "_preview", null); - String config = "{ \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"} ," - + "\"dest\": {\"pipeline\": \"" - + pipelineId - + "\"}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"user.id\": {\"terms\": { \"field\": \"user_id\" }}," - + " \"by_day\": {\"date_histogram\": {\"fixed_interval\": \"1d\",\"field\":\"timestamp\"}}}," - + " \"aggregations\": {" - + " \"user.avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "pipeline": "%s" + }, + "pivot": { + "group_by": { + "user.id": { + "terms": { + "field": "user_id" + } + }, + "by_day": { + "date_histogram": { + "fixed_interval": "1d", + "field": "timestamp" + } + } + }, + "aggregations": { + "user.avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, pipelineId); createPreviewRequest.setJsonEntity(config); Map previewTransformResponse = entityAsMap(client().performRequest(createPreviewRequest)); @@ -955,30 +1024,37 @@ public void testPivotWithMaxOnDateField() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\": \"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": { \n" - + " \"group_by\": {\n" - + " \"by_day\": {\"date_histogram\": {\n" - + " \"fixed_interval\": \"1d\",\"field\":\"timestamp\"\n" - + " }}\n" - + " },\n" - + " \n" - + " \"aggs\" :{\n" - + " \"avg_rating\": {\n" - + " \"avg\": {\"field\": \"stars\"}\n" - + " },\n" - + " \"timestamp\": {\n" - + " \"max\": {\"field\": \"timestamp\"}\n" - + " }\n" - + " }}" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "by_day": { + "date_histogram": { + "fixed_interval": "1d", + "field": "timestamp" + } + } + }, + "aggs": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "timestamp": { + "max": { + "field": "timestamp" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); @@ -1009,34 +1085,39 @@ public void testPivotWithScriptedMetricAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"squared_sum\": {" - + " \"scripted_metric\": {" - + " \"init_script\": \"state.reviews_sqrd = []\"," - + " \"map_script\": \"state.reviews_sqrd.add(doc.stars.value * doc.stars.value)\"," - + " \"combine_script\": \"state.reviews_sqrd\"," - + " \"reduce_script\": \"def sum = 0.0; for(l in states){ for(a in l) { sum += a}} return sum\"" - + " } }" - + " } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "squared_sum": { + "scripted_metric": { + "init_script": "state.reviews_sqrd = []", + "map_script": "state.reviews_sqrd.add(doc.stars.value * doc.stars.value)", + "combine_script": "state.reviews_sqrd", + "reduce_script": "def sum = 0.0; for(l in states){ for(a in l) { sum += a}} return sum" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1069,32 +1150,39 @@ public void testPivotWithBucketScriptAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"avg_rating_again\": {" - + " \"bucket_script\": {" - + " \"buckets_path\": {\"param_1\": \"avg_rating\"}," - + " \"script\": \"return params.param_1\"" - + " } }" - + " } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "avg_rating_again": { + "bucket_script": { + "buckets_path": { + "param_1": "avg_rating" + }, + "script": "return params.param_1" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1133,23 +1221,37 @@ public void testPivotWithGeoBoundsAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" + " \"source\": {\"index\":\"" + indexName + "\"}," + " \"dest\": {\"index\":\"" + transformIndex + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"boundary\": {" - + " \"geo_bounds\": {\"field\": \"location\"}" - + " } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "boundary": { + "geo_bounds": { + "field": "location" + } + } + } + } + } + """.formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1188,29 +1290,36 @@ public void testPivotWithGeoCentroidAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"location\": {" - + " \"geo_centroid\": {\"field\": \"location\"}" - + " } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "location": { + "geo_centroid": { + "field": "location" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1246,29 +1355,41 @@ public void testPivotWithGeoLineAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"location\": {" - + " \"geo_line\": {\"point\": {\"field\":\"location\"}, \"sort\": {\"field\": \"timestamp\"}}" - + " } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "location": { + "geo_line": { + "point": { + "field": "location" + }, + "sort": { + "field": "timestamp" + } + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1305,30 +1426,37 @@ public void testPivotWithGeotileGroupBy() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"tile\": {" - + " \"geotile_grid\": {" - + " \"field\": \"location\"," - + " \"precision\": 12" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"boundary\": {" - + " \"geo_bounds\": {\"field\": \"location\"}" - + " } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "tile": { + "geotile_grid": { + "field": "location", + "precision": 12 + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "boundary": { + "geo_bounds": { + "field": "location" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1371,27 +1499,36 @@ public void testPivotWithWeightedAvgAgg() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"weighted_avg\": {" - + " \"value\": {\"field\": \"stars\"}," - + " \"weight\": {\"field\": \"stars\"}" - + "} } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "weighted_avg": { + "value": { + "field": "stars" + }, + "weight": { + "field": "stars" + } + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1417,27 +1554,36 @@ public void testPivotWithTopMetrics() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"top_business\": {" - + " \"top_metrics\": {" - + " \"metrics\": {\"field\": \"business_id\"}," - + " \"sort\": {\"timestamp\": \"desc\"}" - + "} } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "top_business": { + "top_metrics": { + "metrics": { + "field": "business_id" + }, + "sort": { + "timestamp": "desc" + } + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1469,31 +1615,69 @@ public void testManyBucketsWithSmallPageSize() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"user.id\": {\"terms\": { \"field\": \"user_id\" }}," - + " \"business.id\": {\"terms\": { \"field\": \"business_id\" }}," - + " \"every_star\": {\"histogram\": { \"field\": \"stars\", \"interval\": 1 }}," - + " \"every_two_star\": {\"histogram\": { \"field\": \"stars\", \"interval\": 2 }}," - + " \"by_second\": {\"date_histogram\": {\"fixed_interval\": \"1s\",\"field\":\"timestamp\"}}," - + " \"by_day\": {\"date_histogram\": {\"fixed_interval\": \"1d\",\"field\":\"timestamp\"}}," - + " \"by_minute\": {\"date_histogram\": {\"fixed_interval\": \"1m\",\"field\":\"timestamp\"}}}," - + " \"aggregations\": {" - + " \"user.avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }," - + " \"settings\": {" - + " \"max_page_search_size\": 10" - + " }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "user.id": { + "terms": { + "field": "user_id" + } + }, + "business.id": { + "terms": { + "field": "business_id" + } + }, + "every_star": { + "histogram": { + "field": "stars", + "interval": 1 + } + }, + "every_two_star": { + "histogram": { + "field": "stars", + "interval": 2 + } + }, + "by_second": { + "date_histogram": { + "fixed_interval": "1s", + "field": "timestamp" + } + }, + "by_day": { + "date_histogram": { + "fixed_interval": "1d", + "field": "timestamp" + } + }, + "by_minute": { + "date_histogram": { + "fixed_interval": "1m", + "field": "timestamp" + } + } + }, + "aggregations": { + "user.avg_rating": { + "avg": { + "field": "stars" + } + } + } + }, + "settings": { + "max_page_search_size": 10 + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -1507,11 +1691,13 @@ public void testManyBucketsWithSmallPageSize() throws Exception { public void testContinuousStopWaitForCheckpoint() throws Exception { Request updateLoggingLevels = new Request("PUT", "/_cluster/settings"); - updateLoggingLevels.setJsonEntity( - "{\"persistent\": {" - + "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"trace\"," - + "\"logger.org.elasticsearch.xpack.transform\": \"trace\"}}" - ); + updateLoggingLevels.setJsonEntity(""" + { + "persistent": { + "logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer": "trace", + "logger.org.elasticsearch.xpack.transform": "trace" + } + }"""); client().performRequest(updateLoggingLevels); String indexName = "continuous_reviews_wait_for_checkpoint"; createReviewsIndex(indexName); @@ -1523,27 +1709,38 @@ public void testContinuousStopWaitForCheckpoint() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + indexName - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"sync\": {\"time\": {\"field\": \"timestamp\", \"delay\": \"1s\"}}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "sync": { + "time": { + "field": "timestamp", + "delay": "1s" + } + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -1579,27 +1776,38 @@ public void testContinuousDateNanos() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + indexName - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"sync\": {\"time\": {\"field\": \"timestamp\", \"delay\": \"1s\"}}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"rating\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "sync": { + "time": { + "field": "timestamp", + "delay": "1s" + } + }, + "pivot": { + "group_by": { + "id": { + "terms": { + "field": "id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "rating" + } + } + } + } + }""".formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -1616,15 +1824,10 @@ public void testContinuousDateNanos() throws Exception { final StringBuilder bulk = new StringBuilder(); for (int i = 0; i < 20; i++) { - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"id\":\"") - .append("id_") - .append(i % 5) - .append("\",\"rating\":") - .append(7) - .append(",\"timestamp\":") - .append("\"" + nanoResolutionTimeStamp + "\"") - .append("}\n"); + bulk.append(""" + {"index":{"_index":"%s"}} + {"id":"id_%s","rating":%s,"timestamp":"%s"} + """.formatted(indexName, i % 5, 7, nanoResolutionTimeStamp)); } bulk.append("\r\n"); @@ -1658,32 +1861,38 @@ public void testPivotWithPercentile() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"p\": {" - + " \"percentiles\" : {" - + " \"field\": \"stars\", " - + " \"percents\": [5, 50, 90, 99.9]" - + " }" - + " } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "p": { + "percentiles": { + "field": "stars", + "percents": [ 5, 50, 90, 99.9 ] + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -1722,48 +1931,62 @@ public void testPivotWithFilter() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_WITH_SOME_DATA_ACCESS ); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"top_ratings\": {" - + " \"filter\": {" - + " \"range\": {" - + " \"stars\": {" - + " \"gte\": 4 " - + " } } } }," - + " \"top_ratings_detail\": {" - + " \"filter\": {" - + " \"range\": {" - + " \"stars\": {" - + " \"gte\": 4" - + " } } }," - + " \"aggregations\": {" - + " \"unique_count\": {" - + " \"cardinality\": {" - + " \"field\": \"business_id\"" - + " } }," - + " \"max\": {" - + " \"max\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"min\": {" - + " \"min\": {" - + " \"field\": \"stars\"" - + " } }" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "top_ratings": { + "filter": { + "range": { + "stars": { + "gte": 4 + } + } + } + }, + "top_ratings_detail": { + "filter": { + "range": { + "stars": { + "gte": 4 + } + } + }, + "aggregations": { + "unique_count": { + "cardinality": { + "field": "business_id" + } + }, + "max": { + "max": { + "field": "stars" + } + }, + "min": { + "min": { + "field": "stars" + } + } + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -1861,15 +2084,10 @@ private void createDateNanoIndex(String indexName, int numDocs) throws IOExcepti String randomNanos = "," + randomIntBetween(100000000, 999999999); final StringBuilder bulk = new StringBuilder(); for (int i = 0; i < numDocs; i++) { - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"id\":\"") - .append("id_") - .append(i % 10) - .append("\",\"rating\":") - .append(i % 7) - .append(",\"timestamp\":") - .append("\"2020-01-27T01:59:00" + randomNanos + "Z\"") - .append("}\n"); + bulk.append(""" + {"index":{"_index":"%s"}} + {"id":"id_%s","rating":%s,"timestamp":"2020-01-27T01:59:00%sZ"} + """.formatted(indexName, i % 10, i % 7, randomNanos)); if (i % 50 == 0) { bulk.append("\r\n"); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java index 0f2a1eda23310..24e5c13b48201 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestSpecialCasesIT.java @@ -52,16 +52,17 @@ public void testIndexTemplateMappingClash() throws Exception { // output field "rating.avg" in the pivot config final Request createIndexTemplateRequest = new Request("PUT", "_template/special_pivot_template"); - String template = "{" - + "\"index_patterns\" : [\"special_pivot_template*\"]," - + " \"mappings\" : {" - + " \"properties\": {" - + " \"rating\":{" - + " \"type\": \"float\"\n" - + " }" - + " }" - + " }" - + "}"; + String template = """ + { + "index_patterns": [ "special_pivot_template*" ], + "mappings": { + "properties": { + "rating": { + "type": "float" + } + } + } + }"""; createIndexTemplateRequest.setJsonEntity(template); createIndexTemplateRequest.setOptions(expectWarnings(RestPutIndexTemplateAction.DEPRECATION_WARNING)); @@ -70,27 +71,31 @@ public void testIndexTemplateMappingClash() throws Exception { final Request createTransformRequest = new Request("PUT", getTransformEndpoint() + transformId); - String config = "{" - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"rating.avg\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }" - + " } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "rating.avg": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(REVIEWS_INDEX_NAME, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -138,25 +143,29 @@ public void testSparseDataPercentiles() throws Exception { } final StringBuilder bulk = new StringBuilder(); - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-1\",\"cpu\": 22}\n"); - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-1\",\"cpu\": 55}\n"); - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-1\",\"cpu\": 23}\n"); - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-2\",\"cpu\": 0}\n"); - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-2\",\"cpu\": 99}\n"); - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-1\",\"cpu\": 28}\n"); - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-1\",\"cpu\": 77}\n"); + bulk.append(""" + {"index":{"_index":"%s"}} + {"host":"host-1","cpu": 22} + {"index":{"_index":"%s"}} + {"host":"host-1","cpu": 55} + {"index":{"_index":"%s"}} + {"host":"host-1","cpu": 23} + {"index":{"_index":"%s"}} + {"host":"host-2","cpu": 0} + {"index":{"_index":"%s"}} + {"host":"host-2","cpu": 99} + {"index":{"_index":"%s"}} + {"host":"host-1","cpu": 28} + {"index":{"_index":"%s"}} + {"host":"host-1","cpu": 77} + """.formatted(indexName, indexName, indexName, indexName, indexName, indexName, indexName)); // missing value for cpu - bulk.append("{\"index\":{\"_index\":\"" + indexName + "\"}}\n"); - bulk.append("{\"host\":\"host-3\"}\n"); - bulk.append("\r\n"); + bulk.append(""" + {"index":{"_index":"%s"}} + {"host":"host-3"} + + """.formatted(indexName)); final Request bulkRequest = new Request("POST", "/_bulk"); bulkRequest.addParameter("refresh", "true"); bulkRequest.setJsonEntity(bulk.toString()); @@ -164,21 +173,31 @@ public void testSparseDataPercentiles() throws Exception { final Request createTransformRequest = new Request("PUT", getTransformEndpoint() + transformId); - String config = "{" + " \"source\": {\"index\":\"" + indexName + "\"}," + " \"dest\": {\"index\":\"" + transformIndex + "\"},"; - - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"host\": {" - + " \"terms\": {" - + " \"field\": \"host\"" - + " } } }," - + " \"aggregations\": {" - + " \"p\": {" - + " \"percentiles\": {" - + " \"field\": \"cpu\"" - + " } }" - + " } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "pivot": { + "group_by": { + "host": { + "terms": { + "field": "host" + } + } + }, + "aggregations": { + "p": { + "percentiles": { + "field": "cpu" + } + } + } + } + }""".formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformResetIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformResetIT.java index 454e12d0e9728..6afa2455a3686 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformResetIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformResetIT.java @@ -79,25 +79,31 @@ public void testReset() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_1 ); - String config = "{ \"dest\": {\"index\":\"" - + transformDest - + "\"}," - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } }" - + " }" - + "}"; + String config = """ + { + "dest": { + "index": "%s" + }, + "source": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(transformDest, REVIEWS_INDEX_NAME); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java index 6c6cf0da696f6..0d82ff9cceaf3 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java @@ -72,7 +72,9 @@ protected void createReviewsIndex( int hour = 10; int min = 10; for (int i = 0; i < numDocs; i++) { - bulk.append("{\"create\":{\"_index\":\"" + indexName + "\"}}\n"); + bulk.append(""" + {"create":{"_index":"%s"}} + """.formatted(indexName)); long user = Math.round(Math.pow(i * 31 % 1000, distributionTable[i % distributionTable.length]) % 27); int stars = distributionTable[(i * 33) % distributionTable.length]; long business = Math.round(Math.pow(user * stars, distributionTable[i % distributionTable.length]) % 13); @@ -174,17 +176,14 @@ protected void putReviewsIndex(String indexName, String dateType, boolean isData builder.endObject(); if (isDataStream) { Request createCompositeTemplate = new Request("PUT", "_index_template/" + indexName + "_template"); - createCompositeTemplate.setJsonEntity( - "{\n" - + " \"index_patterns\": [ \"" - + indexName - + "\" ],\n" - + " \"data_stream\": {\n" - + " },\n" - + " \"template\": \n" - + Strings.toString(builder) - + "}" - ); + createCompositeTemplate.setJsonEntity(""" + { + "index_patterns": [ "%s" ], + "data_stream": { + }, + "template": + %s + }""".formatted(indexName, Strings.toString(builder))); client().performRequest(createCompositeTemplate); client().performRequest(new Request("PUT", "_data_stream/" + indexName)); } else { @@ -222,22 +221,39 @@ protected void createReviewsIndexNano() throws IOException { protected void createContinuousPivotReviewsTransform(String transformId, String transformIndex, String authHeader) throws IOException { - String config = "{ \"dest\": {\"index\":\"" + transformIndex + "\"}," + " \"source\": {\"index\":\"" + REVIEWS_INDEX_NAME + "\"}," // Set frequency high for testing - + " \"sync\": {\"time\":{\"field\": \"timestamp\", \"delay\": \"15m\"}}," - + " \"frequency\": \"1s\"," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "dest": { + "index": "%s" + }, + "source": { + "index": "%s" + }, + "sync": { + "time": { + "field": "timestamp", + "delay": "15m" + } + }, + "frequency": "1s", + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(transformIndex, REVIEWS_INDEX_NAME); createReviewsTransform(transformId, authHeader, config); } @@ -253,56 +269,71 @@ protected void createPivotReviewsTransform( String config = "{"; if (pipeline != null) { - config += " \"dest\": {\"index\":\"" + transformIndex + "\", \"pipeline\":\"" + pipeline + "\"},"; + config += """ + "dest": {"index":"%s", "pipeline":"%s"},""".formatted(transformIndex, pipeline); } else { - config += " \"dest\": {\"index\":\"" + transformIndex + "\"},"; + config += """ + "dest": {"index":"%s"},""".formatted(transformIndex); } if (query != null) { - config += " \"source\": {\"index\":\"" + sourceIndex + "\", \"query\":{" + query + "}},"; + config += """ + "source": {"index":"%s", "query":{%s}},""".formatted(sourceIndex, query); } else { - config += " \"source\": {\"index\":\"" + sourceIndex + "\"},"; + config += """ + "source": {"index":"%s"},""".formatted(sourceIndex); } - config += " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } }," - + " \"affiliate_missing\": {" - + " \"missing\": {" - + " \"field\": \"affiliate_id\"" - + " } }," - + " \"stats\": {" - + " \"stats\": {" - + " \"field\": \"stars\"" - + " } } } }," - + "\"frequency\":\"1s\"" - + "}"; + config += """ + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + }, + "affiliate_missing": { + "missing": { + "field": "affiliate_id" + } + }, + "stats": { + "stats": { + "field": "stars" + } + } + } + }, + "frequency": "1s" + }"""; createReviewsTransform(transformId, authHeader, config); } protected void createLatestReviewsTransform(String transformId, String transformIndex) throws IOException { - String config = "{" - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"latest\": {" - + " \"unique_key\": [ \"user_id\" ]," - + " \"sort\": \"@timestamp\"" - + " }," - + "\"frequency\":\"1s\"" - + "}"; + String config = """ + { + "dest": { + "index": "%s" + }, + "source": { + "index": "%s" + }, + "latest": { + "unique_key": [ + "user_id" + ], + "sort": "@timestamp" + }, + "frequency": "1s" + }""".formatted(transformIndex, REVIEWS_INDEX_NAME); createReviewsTransform(transformId, null, config); } @@ -480,15 +511,15 @@ static int getTransformCheckpoint(String transformId) throws IOException { protected void setupDataAccessRole(String role, String... indices) throws IOException { String indicesStr = Arrays.stream(indices).collect(Collectors.joining("\",\"", "\"", "\"")); Request request = new Request("PUT", "/_security/role/" + role); - request.setJsonEntity( - "{" - + " \"indices\" : [" - + " { \"names\": [" - + indicesStr - + "], \"privileges\": [\"create_index\", \"read\", \"write\", \"view_index_metadata\"] }" - + " ]" - + "}" - ); + request.setJsonEntity(""" + { + "indices": [ + { + "names": [ %s ], + "privileges": [ "create_index", "read", "write", "view_index_metadata" ] + } + ] + }""".formatted(indicesStr)); client().performRequest(request); } @@ -497,7 +528,9 @@ protected void setupUser(String user, List roles) throws IOException { String rolesStr = roles.stream().collect(Collectors.joining("\",\"", "\"", "\"")); Request request = new Request("PUT", "/_security/user/" + user); - request.setJsonEntity("{" + " \"password\" : \"" + password + "\"," + " \"roles\" : [ " + rolesStr + " ]" + "}"); + request.setJsonEntity(""" + { "password" : "%s", "roles" : [ %s ]} + """.formatted(password, rolesStr)); client().performRequest(request); } @@ -525,16 +558,11 @@ protected static String getTransformEndpoint() { private void logAudits() throws Exception { logger.info("writing audit messages to the log"); Request searchRequest = new Request("GET", TransformInternalIndexConstants.AUDIT_INDEX + "/_search?ignore_unavailable=true"); - searchRequest.setJsonEntity( - "{ \"size\": 100," - + " \"sort\": [" - + " {" - + " \"timestamp\": {" - + " \"order\": \"asc\"" - + " }" - + " }" - + " ] }" - ); + searchRequest.setJsonEntity(""" + { + "size": 100, + "sort": [ { "timestamp": { "order": "asc" } } ] + }"""); assertBusy(() -> { try { diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java index f357bd946ce6a..6b11c5757dfea 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRobustnessIT.java @@ -32,27 +32,38 @@ public void testTaskRemovalAfterInternalIndexGotDeleted() throws Exception { String transformId = "simple_continuous_pivot"; String transformIndex = "pivot_reviews_continuous"; final Request createTransformRequest = new Request("PUT", TransformField.REST_BASE_PATH_TRANSFORMS + transformId); - String config = "{" - + " \"source\": {\"index\":\"" - + indexName - + "\"}," - + " \"dest\": {\"index\":\"" - + transformIndex - + "\"}," - + " \"frequency\": \"1s\"," - + " \"sync\": {\"time\": {\"field\": \"timestamp\", \"delay\": \"1s\"}}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }" - + "}"; + String config = """ + { + "source": { + "index": "%s" + }, + "dest": { + "index": "%s" + }, + "frequency": "1s", + "sync": { + "time": { + "field": "timestamp", + "delay": "1s" + } + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(indexName, transformIndex); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java index 8da3f271aa6c1..1f52c2ff9d311 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformTaskFailedStateIT.java @@ -37,15 +37,16 @@ public void setClusterSettings() throws IOException { // Set logging level to trace // see: https://github.com/elastic/elasticsearch/issues/45562 Request addFailureRetrySetting = new Request("PUT", "/_cluster/settings"); - addFailureRetrySetting.setJsonEntity( - "{\"persistent\": {\"xpack.transform.num_transform_failure_retries\": \"" - + 0 - + "\"," - + "\"logger.org.elasticsearch.action.bulk\": \"info\"," - + // reduces bulk failure spam - "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"trace\"," - + "\"logger.org.elasticsearch.xpack.transform\": \"trace\"}}" - ); + // reduces bulk failure spam + addFailureRetrySetting.setJsonEntity(""" + { + "persistent": { + "xpack.transform.num_transform_failure_retries": "0", + "logger.org.elasticsearch.action.bulk": "info", + "logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer": "trace", + "logger.org.elasticsearch.xpack.transform": "trace" + } + }"""); client().performRequest(addFailureRetrySetting); } diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUpdateIT.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUpdateIT.java index b4b366bf58eb2..4d3ccfdbae2c1 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUpdateIT.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformUpdateIT.java @@ -22,7 +22,6 @@ import java.util.Map; import java.util.concurrent.TimeUnit; -import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; import static org.hamcrest.Matchers.equalTo; public class TransformUpdateIT extends TransformRestTestCase { @@ -92,26 +91,32 @@ public void testUpdateDeprecatedSettings() throws Exception { getTransformEndpoint() + transformId, BASIC_AUTH_VALUE_TRANSFORM_ADMIN_1 ); - String config = "{ \"dest\": {\"index\":\"" - + transformDest - + "\"}," - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } }," - + " \"max_page_search_size\": 555" - + " }" - + "}"; + String config = """ + { + "dest": { + "index": "%s" + }, + "source": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + }, + "max_page_search_size": 555 + } + }""".formatted(transformDest, REVIEWS_INDEX_NAME); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); @@ -167,25 +172,31 @@ public void testUpdateTransferRights() throws Exception { BASIC_AUTH_VALUE_TRANSFORM_ADMIN_2 ); - String config = "{ \"dest\": {\"index\":\"" - + transformDest - + "\"}," - + " \"source\": {\"index\":\"" - + REVIEWS_INDEX_NAME - + "\"}," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } }" - + " }" - + "}"; + String config = """ + { + "dest": { + "index": "%s" + }, + "source": { + "index": "%s" + }, + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + } + }""".formatted(transformDest, REVIEWS_INDEX_NAME); createTransformRequest.setJsonEntity(config); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); diff --git a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java index b9e21f4249740..b387a078794f0 100644 --- a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java +++ b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformInternalIndexIT.java @@ -65,27 +65,37 @@ public void testUpdateDeletesOldTransformConfig() throws Exception { String transformIndex = "transform-index-deletes-old"; createSourceIndex(transformIndex); String transformId = "transform-update-deletes-old-transform-config"; - String config = "{\"dest\": {\"index\":\"bar\"}," - + " \"source\": {\"index\":\"" - + transformIndex - + "\", \"query\": {\"match_all\":{}}}," - + " \"id\": \"" - + transformId - + "\"," - + " \"doc_type\": \"data_frame_transform_config\"," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }," - + "\"frequency\":\"1s\"" - + "}"; + String config = """ + { + "dest": { + "index": "bar" + }, + "source": { + "index": "%s", + "query": { + "match_all": {} + } + }, + "id": "%s", + "doc_type": "data_frame_transform_config", + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + }, + "frequency": "1s" + }""".formatted(transformIndex, transformId); IndexRequest indexRequest = new IndexRequest(OLD_INDEX).id(TransformConfig.documentId(transformId)) .source(config, XContentType.JSON) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); diff --git a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformOldTransformsIT.java b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformOldTransformsIT.java index 6db36ef958303..a8ca04448f8ef 100644 --- a/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformOldTransformsIT.java +++ b/x-pack/plugin/transform/src/internalClusterTest/java/org/elasticsearch/xpack/transform/integration/TransformOldTransformsIT.java @@ -77,30 +77,38 @@ public void testStopThrowsForDeprecatedTransformConfig() throws Exception { Version.V_7_2_0, VersionUtils.getPreviousVersion(TransformDeprecations.MIN_TRANSFORM_VERSION) ); - String config = "{\"dest\": {\"index\":\"bar\"}," - + " \"source\": {\"index\":\"" - + transformIndex - + "\", \"query\": {\"match_all\":{}}}," - + " \"id\": \"" - + transformId - + "\"," - + " \"doc_type\": \"data_frame_transform_config\"," - + " \"pivot\": {" - + " \"group_by\": {" - + " \"reviewer\": {" - + " \"terms\": {" - + " \"field\": \"user_id\"" - + " } } }," - + " \"aggregations\": {" - + " \"avg_rating\": {" - + " \"avg\": {" - + " \"field\": \"stars\"" - + " } } } }," - + "\"frequency\":\"1s\"," - + "\"version\":\"" - + transformVersion - + "\"" - + "}"; + String config = """ + { + "dest": { + "index": "bar" + }, + "source": { + "index": "%s", + "query": { + "match_all": {} + } + }, + "id": "%s", + "doc_type": "data_frame_transform_config", + "pivot": { + "group_by": { + "reviewer": { + "terms": { + "field": "user_id" + } + } + }, + "aggregations": { + "avg_rating": { + "avg": { + "field": "stars" + } + } + } + }, + "frequency": "1s", + "version": "%s" + }""".formatted(transformIndex, transformId, transformVersion); IndexRequest indexRequest = new IndexRequest(OLD_INDEX).id(TransformConfig.documentId(transformId)) .source(config, XContentType.JSON) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java index 35fcab503d397..9b3617c04fd1c 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/AggregationResultUtilsTests.java @@ -221,9 +221,9 @@ protected NamedXContentRegistry xContentRegistry() { public void testExtractCompositeAggregationResults() throws IOException { String targetField = randomAlphaOfLengthBetween(5, 10); - GroupConfig groupBy = parseGroupConfig( - "{ \"" + targetField + "\" : {" + "\"terms\" : {" + " \"field\" : \"doesn't_matter_for_this_test\"" + "} } }" - ); + GroupConfig groupBy = parseGroupConfig(""" + { "%s" : {"terms" : { "field" : "doesn't_matter_for_this_test"} } } + """.formatted(targetField)); String aggName = randomAlphaOfLengthBetween(5, 10); String aggTypedName = "avg#" + aggName; @@ -251,22 +251,19 @@ public void testExtractCompositeAggregationResultsMultipleGroups() throws IOExce String targetField = randomAlphaOfLengthBetween(5, 10); String targetField2 = randomAlphaOfLengthBetween(5, 10) + "_2"; - GroupConfig groupBy = parseGroupConfig( - "{" - + "\"" - + targetField - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }," - + "\"" - + targetField2 - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }" - + "}" - ); + GroupConfig groupBy = parseGroupConfig(""" + { + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + }, + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + } + }""".formatted(targetField, targetField2)); String aggName = randomAlphaOfLengthBetween(5, 10); String aggTypedName = "avg#" + aggName; @@ -295,9 +292,14 @@ public void testExtractCompositeAggregationResultsMultipleGroups() throws IOExce public void testExtractCompositeAggregationResultsMultiAggregations() throws IOException { String targetField = randomAlphaOfLengthBetween(5, 10); - GroupConfig groupBy = parseGroupConfig( - "{\"" + targetField + "\" : {" + "\"terms\" : {" + " \"field\" : \"doesn't_matter_for_this_test\"" + "} } }" - ); + GroupConfig groupBy = parseGroupConfig(""" + { + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + } + }""".formatted(targetField)); String aggName = randomAlphaOfLengthBetween(5, 10); String aggTypedName = "avg#" + aggName; @@ -356,22 +358,19 @@ public void testExtractCompositeAggregationResultsMultiAggregationsAndTypes() th String targetField = randomAlphaOfLengthBetween(5, 10); String targetField2 = randomAlphaOfLengthBetween(5, 10) + "_2"; - GroupConfig groupBy = parseGroupConfig( - "{" - + "\"" - + targetField - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }," - + "\"" - + targetField2 - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }" - + "}" - ); + GroupConfig groupBy = parseGroupConfig(""" + { + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + }, + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + } + }""".formatted(targetField, targetField2)); String aggName = randomAlphaOfLengthBetween(5, 10); String aggTypedName = "avg#" + aggName; @@ -450,22 +449,19 @@ public void testExtractCompositeAggregationResultsWithDynamicType() throws IOExc String targetField = randomAlphaOfLengthBetween(5, 10); String targetField2 = randomAlphaOfLengthBetween(5, 10) + "_2"; - GroupConfig groupBy = parseGroupConfig( - "{" - + "\"" - + targetField - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }," - + "\"" - + targetField2 - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }" - + "}" - ); + GroupConfig groupBy = parseGroupConfig(""" + { + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + }, + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + } + }""".formatted(targetField, targetField2)); String aggName = randomAlphaOfLengthBetween(5, 10); String aggTypedName = "scripted_metric#" + aggName; @@ -517,22 +513,19 @@ public void testExtractCompositeAggregationResultsWithPipelineAggregation() thro String targetField = randomAlphaOfLengthBetween(5, 10); String targetField2 = randomAlphaOfLengthBetween(5, 10) + "_2"; - GroupConfig groupBy = parseGroupConfig( - "{" - + "\"" - + targetField - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }," - + "\"" - + targetField2 - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }" - + "}" - ); + GroupConfig groupBy = parseGroupConfig(""" + { + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + }, + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + } + }""".formatted(targetField, targetField2)); String aggName = randomAlphaOfLengthBetween(5, 10); String aggTypedName = "avg#" + aggName; @@ -608,22 +601,19 @@ public void testExtractCompositeAggregationResultsDocIDs() throws IOException { String targetField = randomAlphaOfLengthBetween(5, 10); String targetField2 = randomAlphaOfLengthBetween(5, 10) + "_2"; - GroupConfig groupBy = parseGroupConfig( - "{" - + "\"" - + targetField - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }," - + "\"" - + targetField2 - + "\" : {" - + " \"terms\" : {" - + " \"field\" : \"doesn't_matter_for_this_test\"" - + " } }" - + "}" - ); + GroupConfig groupBy = parseGroupConfig(""" + { + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + }, + "%s": { + "terms": { + "field": "doesn't_matter_for_this_test" + } + } + }""".formatted(targetField, targetField2)); String aggName = randomAlphaOfLengthBetween(5, 10); String aggTypedName = "avg#" + aggName; diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java index 227aadc64f055..4a5f522ebde73 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/PivotTests.java @@ -189,11 +189,24 @@ public void testValidateAllUnsupportedAggregations() throws Exception { } public void testGetPerformanceCriticalFields() throws IOException { - String groupConfigJson = "{" - + "\"group-A\": { \"terms\": { \"field\": \"field-A\" } }," - + "\"group-B\": { \"terms\": { \"field\": \"field-B\" } }," - + "\"group-C\": { \"terms\": { \"field\": \"field-C\" } }" - + "}"; + String groupConfigJson = """ + { + "group-A": { + "terms": { + "field": "field-A" + } + }, + "group-B": { + "terms": { + "field": "field-B" + } + }, + "group-C": { + "terms": { + "field": "field-C" + } + } + }"""; GroupConfig groupConfig; try (XContentParser parser = createParser(JsonXContent.jsonXContent, groupConfigJson)) { groupConfig = GroupConfig.fromXContent(parser, false); @@ -275,63 +288,95 @@ private AggregationConfig getValidAggregationConfig() throws IOException { private AggregationConfig getAggregationConfig(String agg) throws IOException { if (agg.equals(AggregationType.SCRIPTED_METRIC.getName())) { - return parseAggregations( - "{\"pivot_scripted_metric\": {\n" - + "\"scripted_metric\": {\n" - + " \"init_script\" : \"state.transactions = []\",\n" - + " \"map_script\" : " - + " \"state.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)\", \n" - + " \"combine_script\" : \"double profit = 0; for (t in state.transactions) { profit += t } return profit\",\n" - + " \"reduce_script\" : \"double profit = 0; for (a in states) { profit += a } return profit\"\n" - + " }\n" - + "}}" - ); + return parseAggregations(""" + { + "pivot_scripted_metric": { + "scripted_metric": { + "init_script": "state.transactions = []", + "map_script": "state.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)", + "combine_script": "double profit = 0; for (t in state.transactions) { profit += t } return profit", + "reduce_script": "double profit = 0; for (a in states) { profit += a } return profit" + } + } + }"""); } if (agg.equals(AggregationType.BUCKET_SCRIPT.getName())) { - return parseAggregations( - "{\"pivot_bucket_script\":{" - + "\"bucket_script\":{" - + "\"buckets_path\":{\"param_1\":\"other_bucket\"}," - + "\"script\":\"return params.param_1\"}}}" - ); + return parseAggregations(""" + { + "pivot_bucket_script": { + "bucket_script": { + "buckets_path": { + "param_1": "other_bucket" + }, + "script": "return params.param_1" + } + } + }"""); } if (agg.equals(AggregationType.BUCKET_SELECTOR.getName())) { - return parseAggregations( - "{\"pivot_bucket_selector\":{" - + "\"bucket_selector\":{" - + "\"buckets_path\":{\"param_1\":\"other_bucket\"}," - + "\"script\":\"params.param_1 > 42.0\"}}}" - ); + return parseAggregations(""" + { + "pivot_bucket_selector": { + "bucket_selector": { + "buckets_path": { + "param_1": "other_bucket" + }, + "script": "params.param_1 > 42.0" + } + } + }"""); } if (agg.equals(AggregationType.WEIGHTED_AVG.getName())) { - return parseAggregations( - "{\n" - + "\"pivot_weighted_avg\": {\n" - + " \"weighted_avg\": {\n" - + " \"value\": {\"field\": \"values\"},\n" - + " \"weight\": {\"field\": \"weights\"}\n" - + " }\n" - + "}\n" - + "}" - ); + return parseAggregations(""" + { + "pivot_weighted_avg": { + "weighted_avg": { + "value": {"field": "values"}, + "weight": {"field": "weights"} + } + } + }"""); } if (agg.equals(AggregationType.FILTER.getName())) { - return parseAggregations( - "{" + "\"pivot_filter\": {" + " \"filter\": {" + " \"term\": {\"field\": \"value\"}" + " }" + "}" + "}" - ); + return parseAggregations(""" + { + "pivot_filter": { + "filter": { + "term": { + "field": "value" + } + } + } + }"""); } if (agg.equals(AggregationType.GEO_LINE.getName())) { - return parseAggregations( - "{\"pivot_geo_line\": {\"geo_line\": {\"point\": {\"field\": \"values\"}, \"sort\":{\"field\": \"timestamp\"}}}}" - ); + return parseAggregations(""" + { + "pivot_geo_line": { + "geo_line": { + "point": { + "field": "values" + }, + "sort": { + "field": "timestamp" + } + } + } + }"""); } if (agg.equals("global")) { - return parseAggregations("{\"pivot_global\": {\"global\": {}}}"); + return parseAggregations(""" + {"pivot_global": {"global": {}}}"""); } - return parseAggregations( - "{\n" + " \"pivot_" + agg + "\": {\n" + " \"" + agg + "\": {\n" + " \"field\": \"values\"\n" + " }\n" + " }" + "}" - ); + return parseAggregations(""" + { + "pivot_%s": { + "%s": { + "field": "values" + } + } + }""".formatted(agg, agg)); } private AggregationConfig parseAggregations(String json) throws IOException { diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java index 609c2cce4d758..ca952811a77bf 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/TransformAggregationsTests.java @@ -136,16 +136,13 @@ public void testAggregationsVsTransforms() { .collect(Collectors.toList()); for (String aggregationName : aggregationNames) { + String message = """ + The following aggregation is unknown to transform: [%s]. If this is a newly added aggregation, \ + please open an issue to add transform support for it. Afterwards add "%s" to the list in %s. \ + Thanks!\ + """.formatted(aggregationName, aggregationName, TransformAggregations.class.getName()); assertTrue( - "The following aggregation is unknown to transform: [" - + aggregationName - + "]. If this is a newly added aggregation, " - + "please open an issue to add transform support for it. Afterwards add \"" - + aggregationName - + "\" to the list in " - + TransformAggregations.class.getName() - + ". Thanks!", - + message, TransformAggregations.isSupportedByTransform(aggregationName) || TransformAggregations.isUnSupportedByTransform(aggregationName) ); diff --git a/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java b/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java index df35d47257a28..ee5ff87d72920 100644 --- a/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java +++ b/x-pack/plugin/vector-tile/qa/multi-cluster/src/test/java/org/elasticsearch/vectortile/VectorTileCCSIT.java @@ -38,9 +38,14 @@ private int createIndex(RestClient client, String indexName) throws IOException Response response = client.performRequest(createRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); final Request mappingRequest = new Request(HttpPut.METHOD_NAME, indexName + "/_mapping"); - mappingRequest.setJsonEntity( - "{\n" + " \"properties\": {\n" + " \"location\": {\n" + " \"type\": \"geo_shape\"\n" + " }\n" + " }\n" + "}" - ); + mappingRequest.setJsonEntity(""" + { + "properties": { + "location": { + "type": "geo_shape" + } + } + }"""); response = client.performRequest(mappingRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); diff --git a/x-pack/plugin/vector-tile/src/javaRestTest/java/org/elasticsearch/xpack/vectortile/VectorTileRestIT.java b/x-pack/plugin/vector-tile/src/javaRestTest/java/org/elasticsearch/xpack/vectortile/VectorTileRestIT.java index 243e008a7a7d7..8ef9e721cd5ca 100644 --- a/x-pack/plugin/vector-tile/src/javaRestTest/java/org/elasticsearch/xpack/vectortile/VectorTileRestIT.java +++ b/x-pack/plugin/vector-tile/src/javaRestTest/java/org/elasticsearch/xpack/vectortile/VectorTileRestIT.java @@ -69,18 +69,17 @@ private void indexPoints() throws IOException { Response response = client().performRequest(createRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); final Request mappingRequest = new Request(HttpPut.METHOD_NAME, INDEX_POINTS + "/_mapping"); - mappingRequest.setJsonEntity( - "{\n" - + " \"properties\": {\n" - + " \"location\": {\n" - + " \"type\": \"geo_point\"\n" - + " },\n" - + " \"name\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + "}" - ); + mappingRequest.setJsonEntity(""" + { + "properties": { + "location": { + "type": "geo_point" + }, + "name": { + "type": "keyword" + } + } + }"""); response = client().performRequest(mappingRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); final Rectangle r = GeoTileUtils.toBoundingBox(x, y, z); @@ -89,22 +88,10 @@ private void indexPoints() throws IOException { for (int i = 0; i < 30; i += 10) { for (int j = 0; j <= i; j++) { final Request putRequest = new Request(HttpPost.METHOD_NAME, INDEX_POINTS + "/_doc/"); - putRequest.setJsonEntity( - "{\n" - + " \"location\": \"POINT(" - + x - + " " - + y - + ")\", \"name\": \"point" - + i - + "\"" - + ", \"value1\": " - + i - + ", \"value2\": " - + (i + 1) - + "\n" - + "}" - ); + putRequest.setJsonEntity(""" + { + "location": "POINT(%s %s)", "name": "point%s", "value1": %s, "value2": %s + }""".formatted(x, y, i, i, i + 1)); response = client().performRequest(putRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_CREATED)); } @@ -125,35 +112,25 @@ private void createIndexAndPutGeometry(String indexName, Geometry geometry, Stri Response response = client().performRequest(createRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); final Request mappingRequest = new Request(HttpPut.METHOD_NAME, indexName + "/_mapping"); - mappingRequest.setJsonEntity( - "{\n" - + " \"properties\": {\n" - + " \"location\": {\n" - + " \"type\": \"geo_shape\"\n" - + " },\n" - + " \"name\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + "}" - ); + mappingRequest.setJsonEntity(""" + { + "properties": { + "location": { + "type": "geo_shape" + }, + "name": { + "type": "keyword" + } + } + }"""); response = client().performRequest(mappingRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); final Request putRequest = new Request(HttpPost.METHOD_NAME, indexName + "/_doc/" + id); - putRequest.setJsonEntity( - "{\n" - + " \"location\": \"" - + WellKnownText.toWKT(geometry) - + "\"" - + ", \"name\": \"geometry\"" - + ", \"value1\": " - + 1 - + ", \"value2\": " - + 2 - + "\n" - + "}" - ); + putRequest.setJsonEntity(""" + { + "location": "%s", "name": "geometry", "value1": %s, "value2": %s + }""".formatted(WellKnownText.toWKT(geometry), 1, 2)); response = client().performRequest(putRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_CREATED)); @@ -176,18 +153,17 @@ private void indexCollection() throws IOException { Response response = client().performRequest(createRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); final Request mappingRequest = new Request(HttpPut.METHOD_NAME, INDEX_COLLECTION + "/_mapping"); - mappingRequest.setJsonEntity( - "{\n" - + " \"properties\": {\n" - + " \"location\": {\n" - + " \"type\": \"geo_shape\"\n" - + " },\n" - + " \"name\": {\n" - + " \"type\": \"keyword\"\n" - + " }\n" - + " }\n" - + "}" - ); + mappingRequest.setJsonEntity(""" + { + "properties": { + "location": { + "type": "geo_shape" + }, + "name": { + "type": "keyword" + } + } + }"""); response = client().performRequest(mappingRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK)); @@ -208,19 +184,10 @@ private void indexCollection() throws IOException { + " " + y + "))"; - putRequest.setJsonEntity( - "{\n" - + " \"location\": \"" - + collection - + "\"" - + ", \"name\": \"collection\"" - + ", \"value1\": " - + 1 - + ", \"value2\": " - + 2 - + "\n" - + "}" - ); + putRequest.setJsonEntity(""" + { + "location": "%s", "name": "collection", "value1": %s, "value2": %s + }""".formatted(collection, 1, 2)); response = client().performRequest(putRequest); assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_CREATED)); @@ -392,17 +359,15 @@ public void testGridType() throws Exception { public void testInvalidAggName() { final Request mvtRequest = new Request(getHttpMethod(), INDEX_POINTS + "/_mvt/location/" + z + "/" + x + "/" + y); - mvtRequest.setJsonEntity( - "{\"size\" : 0," - + " \"aggs\": {\n" - + " \"_mvt_name\": {\n" - + " \"min\": {\n" - + " \"field\": \"value1\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + mvtRequest.setJsonEntity(""" + {"size" : 0, "aggs": { + "_mvt_name": { + "min": { + "field": "value1" + } + } + } + }"""); ResponseException ex = expectThrows(ResponseException.class, () -> execute(mvtRequest)); // the prefix '_mvt_' is reserved for internal aggregations assertThat(ex.getMessage(), Matchers.containsString("Invalid aggregation name [_mvt_name]")); @@ -549,31 +514,30 @@ public void testDefaultSort() throws Exception { } public void testRuntimeFieldWithSort() throws Exception { - String runtimeMapping = "\"runtime_mappings\": {\n" - + " \"width\": {\n" - + " \"script\": " - + "\"emit(doc['location'].getBoundingBox().bottomRight().getLon() - doc['location'].getBoundingBox().topLeft().getLon())\",\n" - + " \"type\": \"double\"\n" - + " }\n" - + "}\n"; + String runtimeMapping = """ + "runtime_mappings": { + "width": { + "script": "emit(doc['location'].getBoundingBox().bottomRight().getLon() - doc['location'].getBoundingBox().topLeft()\ + .getLon())", + "type": "double" + } + } + """; { // desc order, polygon should be the first hit final Request mvtRequest = new Request(getHttpMethod(), INDEX_POINTS_SHAPES + "/_mvt/location/" + z + "/" + x + "/" + y); - mvtRequest.setJsonEntity( - "{\n" - + " \"size\" : 100,\n" - + " \"grid_precision\" : 0,\n" - + runtimeMapping - + "," - + " \"sort\" : [\n" - + " {\n" - + " \"width\": {\n" - + " \"order\": \"desc\"\n" - + " }\n" - + " }\n" - + " ]" - + "}" - ); + mvtRequest.setJsonEntity(""" + { + "size" : 100, + "grid_precision" : 0, + %s, "sort" : [ + { + "width": { + "order": "desc" + } + } + ]} + """.formatted(runtimeMapping)); final VectorTile.Tile tile = execute(mvtRequest); assertThat(tile.getLayersCount(), Matchers.equalTo(2)); @@ -585,21 +549,19 @@ public void testRuntimeFieldWithSort() throws Exception { { // asc order, polygon should be the last hit final Request mvtRequest = new Request(getHttpMethod(), INDEX_POINTS_SHAPES + "/_mvt/location/" + z + "/" + x + "/" + y); - mvtRequest.setJsonEntity( - "{\n" - + " \"size\" : 100,\n" - + " \"grid_precision\" : 0,\n" - + runtimeMapping - + "," - + " \"sort\" : [\n" - + " {\n" - + " \"width\": {\n" - + " \"order\": \"asc\"\n" - + " }\n" - + " }\n" - + " ]" - + "}" - ); + mvtRequest.setJsonEntity(""" + { + "size" : 100, + "grid_precision" : 0, + %s, + "sort" : [ + { + "width": { + "order": "asc" + } + } + ]} + """.formatted(runtimeMapping)); final VectorTile.Tile tile = execute(mvtRequest); assertThat(tile.getLayersCount(), Matchers.equalTo(2)); @@ -612,17 +574,16 @@ public void testRuntimeFieldWithSort() throws Exception { public void testBasicQueryGet() throws Exception { final Request mvtRequest = new Request(getHttpMethod(), INDEX_POINTS + "/_mvt/location/" + z + "/" + x + "/" + y); - mvtRequest.setJsonEntity( - "{\n" - + " \"query\": {\n" - + " \"term\": {\n" - + " \"name\": {\n" - + " \"value\": \"point0\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + mvtRequest.setJsonEntity(""" + { + "query": { + "term": { + "name": { + "value": "point0" + } + } + } + }"""); final VectorTile.Tile tile = execute(mvtRequest); assertThat(tile.getLayersCount(), Matchers.equalTo(3)); assertLayer(tile, HITS_LAYER, 4096, 1, 2); @@ -661,17 +622,16 @@ public void testWithFields() throws Exception { public void testSingleValueAgg() throws Exception { final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y); - mvtRequest.setJsonEntity( - "{\n" - + " \"aggs\": {\n" - + " \"minVal\": {\n" - + " \"min\": {\n" - + " \"field\": \"value1\"\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + mvtRequest.setJsonEntity(""" + { + "aggs": { + "minVal": { + "min": { + "field": "value1" + } + } + } + }"""); final VectorTile.Tile tile = execute(mvtRequest); assertThat(tile.getLayersCount(), Matchers.equalTo(3)); assertLayer(tile, HITS_LAYER, 4096, 1, 2); @@ -685,18 +645,17 @@ public void testSingleValueAgg() throws Exception { public void testMultiValueAgg() throws Exception { final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y); - mvtRequest.setJsonEntity( - "{\n" - + " \"aggs\": {\n" - + " \"percentilesAgg\": {\n" - + " \"percentiles\": {\n" - + " \"field\": \"value1\",\n" - + " \"percents\": [95, 99, 99.9]\n" - + " }\n" - + " }\n" - + " }\n" - + "}" - ); + mvtRequest.setJsonEntity(""" + { + "aggs": { + "percentilesAgg": { + "percentiles": { + "field": "value1", + "percents": [95, 99, 99.9] + } + } + } + }"""); final VectorTile.Tile tile = execute(mvtRequest); assertThat(tile.getLayersCount(), Matchers.equalTo(3)); assertLayer(tile, HITS_LAYER, 4096, 1, 2); diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java index 66713cee92a29..eba35f342d21a 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/query/KnnVectorQueryBuilderTests.java @@ -111,17 +111,18 @@ public void testWrongFieldType() { @Override public void testValidOutput() { KnnVectorQueryBuilder query = new KnnVectorQueryBuilder(VECTOR_FIELD, new float[] { 1.0f, 2.0f, 3.0f }, 10); - String expected = "{\n" - + " \"knn\" : {\n" - + " \"field\" : \"vector\",\n" - + " \"vector\" : [\n" - + " 1.0,\n" - + " 2.0,\n" - + " 3.0\n" - + " ],\n" - + " \"num_candidates\" : 10\n" - + " }\n" - + "}"; + String expected = """ + { + "knn" : { + "field" : "vector", + "vector" : [ + 1.0, + 2.0, + 3.0 + ], + "num_candidates" : 10 + } + }"""; assertEquals(expected, query.toString()); } diff --git a/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java b/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java index 27914d7154eac..dc03bdd9a6e58 100644 --- a/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java +++ b/x-pack/plugin/watcher/qa/with-monitoring/src/javaRestTest/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java @@ -88,9 +88,28 @@ private String createMonitoringWatch() throws Exception { String clusterUUID = getClusterUUID(); String watchId = clusterUUID + "_kibana_version_mismatch"; Request request = new Request("PUT", "/_watcher/watch/" + watchId); - String watch = "{\"trigger\":{\"schedule\":{\"interval\":\"1000m\"}},\"input\":{\"simple\":{}}," - + "\"condition\":{\"always\":{}}," - + "\"actions\":{\"logme\":{\"logging\":{\"level\":\"info\",\"text\":\"foo\"}}}}"; + String watch = """ + { + "trigger": { + "schedule": { + "interval": "1000m" + } + }, + "input": { + "simple": {} + }, + "condition": { + "always": {} + }, + "actions": { + "logme": { + "logging": { + "level": "info", + "text": "foo" + } + } + } + }"""; request.setJsonEntity(watch); client().performRequest(request); return watchId; diff --git a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java index 6641d23036f9c..212b71a4fb765 100644 --- a/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java +++ b/x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/transform/TransformIntegrationTests.java @@ -107,22 +107,13 @@ public void testScriptTransform() throws Exception { script = mockScript("['key3' : ctx.payload.key1 + ctx.payload.key2]"); } else { logger.info("testing script transform with an indexed script"); - assertAcked( - client().admin() - .cluster() - .preparePutStoredScript() - .setId("my-script") - .setContent( - new BytesArray( - "{\"script\" : {\"lang\": \"" - + MockScriptPlugin.NAME - + "\", " - + "\"source\": \"['key3' : ctx.payload.key1 + ctx.payload.key2]\"}" - ), - XContentType.JSON - ) - .get() - ); + assertAcked(client().admin().cluster().preparePutStoredScript().setId("my-script").setContent(new BytesArray(""" + { + "script": { + "lang": "%s", + "source": "['key3' : ctx.payload.key1 + ctx.payload.key2]" + } + }""".formatted(MockScriptPlugin.NAME)), XContentType.JSON).get()); script = new Script(ScriptType.STORED, null, "my-script", Collections.emptyMap()); } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java index cbaaa65f063bf..823d0af6ecadd 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/chain/ChainInputTests.java @@ -125,10 +125,8 @@ public void testToXContent() throws Exception { XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); chainedInput.toXContent(builder, ToXContent.EMPTY_PARAMS); - assertThat( - BytesReference.bytes(builder).utf8ToString(), - is("{\"inputs\":[{\"first\":{\"simple\":{\"foo\":\"bar\"}}},{\"second\":{\"simple\":{\"spam\":\"eggs\"}}}]}") - ); + assertThat(BytesReference.bytes(builder).utf8ToString(), is(""" + {"inputs":[{"first":{"simple":{"foo":"bar"}}},{"second":{"simple":{"spam":"eggs"}}}]}""")); // parsing it back as well! Map> factories = new HashMap<>(); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachmentTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachmentTests.java index 9d4d3a91b8eeb..51db5af206165 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachmentTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/DataAttachmentTests.java @@ -24,7 +24,10 @@ public void testCreateJson() throws Exception { Attachment attachment = DataAttachment.JSON.create("data", data); InputStream input = attachment.bodyPart().getDataHandler().getInputStream(); String content = Streams.copyToString(new InputStreamReader(input, StandardCharsets.UTF_8)); - assertThat(content, is("{\n \"key\" : \"value\"\n}")); + assertThat(content, is(""" + { + "key" : "value" + }""")); } public void testCreateYaml() throws Exception { @@ -34,6 +37,9 @@ public void testCreateYaml() throws Exception { String content = Streams.copyToString(new InputStreamReader(input, StandardCharsets.UTF_8)); // the yaml factory in es always emits unix line breaks // this seems to be a bug in jackson yaml factory that doesn't default to the platform line break - assertThat(content, is("---\nkey: \"value\"\n")); + assertThat(content, is(""" + --- + key: "value" + """)); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java index a3f6f28ad43d8..73faeede2de62 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/HtmlSanitizerTests.java @@ -17,9 +17,9 @@ public class HtmlSanitizerTests extends ESTestCase { public void testDefaultOnClickDisallowed() { - String badHtml = ""; + String badHtml = """ + """; HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.EMPTY); String sanitizedHtml = sanitizer.sanitize(badHtml); assertThat(sanitizedHtml, equalTo("Click me to display Date and Time.")); @@ -40,38 +40,40 @@ public void testDefault_EmbeddedImageAllowed() { } public void testDefaultTablesAllowed() { - String html = "
    " + n.nodeName() + "%s
    " - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "" - + "
    caption
    header1header2
    Sum$180
    cost180
    "; + String html = """ + + + + + + + + + + + + + + + + + + + + + + + + +
    caption
    header1header2
    Sum$180
    cost180
    """; HtmlSanitizer sanitizer = new HtmlSanitizer(Settings.EMPTY); String sanitizedHtml = sanitizer.sanitize(html); - assertThat(sanitizedHtml, equalTo(html)); + assertThat(sanitizedHtml, equalTo(html.replaceAll("\\R", "").replaceAll("> +<", "><").replaceAll(" +", " "))); } public void testAllowStyles() { - String html = "
    "; + String html = """ +
    """; Settings settings = Settings.builder().putList("xpack.notification.email.html.sanitization.allow", "_tables", "_styles").build(); HtmlSanitizer sanitizer = new HtmlSanitizer(settings); String sanitizedHtml = sanitizer.sanitize(html); diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java index 4f1a3e861dd9c..7934670287acc 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherTemplateTests.java @@ -110,35 +110,27 @@ public void testEscaping() throws Exception { public void testSimpleParameterReplace() { { - String template = "__json__::GET _search {\"query\": " - + "{\"boosting\": {" - + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}" - + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = """ + __json__::GET _search {"query": {"boosting": {"positive": {"match": {"body": "gift"}},"negative": \ + {"term": {"body": {"value": "solr"}}}, "negative_boost": {{boost_val}} } }}"""; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); String result = textTemplateEngine.render(new TextTemplate(template), vars); - assertEquals( - "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"solr\"}}}, \"negative_boost\": 0.3 } }}", - result - ); + assertEquals(""" + GET _search {"query": {"boosting": {"positive": {"match": {"body": "gift"}},"negative": \ + {"term": {"body": {"value": "solr"}}}, "negative_boost": 0.3 } }}""", result); } { - String template = "__json__::GET _search {\"query\": " - + "{\"boosting\": {" - + "\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"{{body_val}}\"}" - + "}}, \"negative_boost\": {{boost_val}} } }}"; + String template = """ + __json__::GET _search {"query": {"boosting": {"positive": {"match": {"body": "gift"}},"negative": \ + {"term": {"body": {"value": "{{body_val}}"}}}, "negative_boost": {{boost_val}} } }}"""; Map vars = new HashMap<>(); vars.put("boost_val", "0.3"); vars.put("body_val", "\"quick brown\""); String result = textTemplateEngine.render(new TextTemplate(template), vars); - assertEquals( - "GET _search {\"query\": {\"boosting\": {\"positive\": {\"match\": {\"body\": \"gift\"}}," - + "\"negative\": {\"term\": {\"body\": {\"value\": \"\\\"quick brown\\\"\"}}}, \"negative_boost\": 0.3 } }}", - result - ); + assertEquals(""" + GET _search {"query": {"boosting": {"positive": {"match": {"body": "gift"}},"negative": \ + {"term": {"body": {"value": "\\"quick brown\\""}}}, "negative_boost": 0.3 } }}""", result); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java index d68e6e71333bf..005a089298777 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/support/search/WatcherSearchTemplateRequestTests.java @@ -21,12 +21,14 @@ public class WatcherSearchTemplateRequestTests extends ESTestCase { public void testFromXContentWithTemplateDefaultLang() throws IOException { - String source = "{\"template\":{\"id\":\"default-script\", \"params\":{\"foo\":\"bar\"}}}"; + String source = """ + {"template":{"id":"default-script", "params":{"foo":"bar"}}}"""; assertTemplate(source, "default-script", null, singletonMap("foo", "bar")); } public void testFromXContentWithTemplateCustomLang() throws IOException { - String source = "{\"template\":{\"source\":\"custom-script\", \"lang\":\"painful\",\"params\":{\"bar\":\"baz\"}}}"; + String source = """ + {"template":{"source":"custom-script", "lang":"painful","params":{"bar":"baz"}}}"""; assertTemplate(source, "custom-script", "painful", singletonMap("bar", "baz")); } diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java index feb20d50f2865..b5b2553df9761 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/FullClusterRestartIT.java @@ -213,10 +213,21 @@ public void testWatcherWithApiKey() throws Exception { if (isRunningAgainstOldCluster()) { final Request createApiKeyRequest = new Request("PUT", "/_security/api_key"); - createApiKeyRequest.setJsonEntity( - "{\"name\":\"key-1\",\"role_descriptors\":" - + "{\"r\":{\"cluster\":[\"all\"],\"indices\":[{\"names\":[\"*\"],\"privileges\":[\"all\"]}]}}}" - ); + createApiKeyRequest.setJsonEntity(""" + { + "name": "key-1", + "role_descriptors": { + "r": { + "cluster": [ "all" ], + "indices": [ + { + "names": [ "*" ], + "privileges": [ "all" ] + } + ] + } + } + }"""); final Response response = client().performRequest(createApiKeyRequest); final Map createApiKeyResponse = entityAsMap(response); @@ -349,25 +360,25 @@ public void testRollupAfterRestart() throws Exception { intervalType = "interval"; } - createRollupJobRequest.setJsonEntity( - "{" - + "\"index_pattern\":\"rollup-*\"," - + "\"rollup_index\":\"results-rollup\"," - + "\"cron\":\"*/30 * * * * ?\"," - + "\"page_size\":100," - + "\"groups\":{" - + " \"date_histogram\":{" - + " \"field\":\"timestamp\"," - + " \"" - + intervalType - + "\":\"5m\"" - + " }" - + "}," - + "\"metrics\":[" - + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" - + "]" - + "}" - ); + createRollupJobRequest.setJsonEntity(""" + { + "index_pattern": "rollup-*", + "rollup_index": "results-rollup", + "cron": "*/30 * * * * ?", + "page_size": 100, + "groups": { + "date_histogram": { + "field": "timestamp", + "%s": "5m" + } + }, + "metrics": [ + { + "field": "value", + "metrics": [ "min", "max", "sum" ] + } + ] + }""".formatted(intervalType)); Map createRollupJobResponse = entityAsMap(client().performRequest(createRollupJobRequest)); assertThat(createRollupJobResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -398,16 +409,19 @@ public void testTransformLegacyTemplateCleanup() throws Exception { // create the source index final Request createIndexRequest = new Request("PUT", "customers"); - createIndexRequest.setJsonEntity( - "{" - + "\"mappings\": {" - + " \"properties\": {" - + " \"customer_id\": { \"type\": \"keyword\" }," - + " \"price\": { \"type\": \"double\" }" - + " }" - + "}" - + "}" - ); + createIndexRequest.setJsonEntity(""" + { + "mappings": { + "properties": { + "customer_id": { + "type": "keyword" + }, + "price": { + "type": "double" + } + } + } + }"""); Map createIndexResponse = entityAsMap(client().performRequest(createIndexRequest)); assertThat(createIndexResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -418,33 +432,32 @@ public void testTransformLegacyTemplateCleanup() throws Exception { : "_data_frame/transforms/transform-full-cluster-restart-test"; final Request createTransformRequest = new Request("PUT", endpoint); - createTransformRequest.setJsonEntity( - "{" - + "\"source\":{" - + " \"index\":\"customers\"" - + "}," - + "\"description\":\"testing\"," - + "\"dest\":{" - + " \"index\":\"max_price\"" - + "}," - + "\"pivot\": {" - + " \"group_by\":{" - + " \"customer_id\":{" - + " \"terms\":{" - + " \"field\":\"customer_id\"" - + " }" - + " }" - + " }," - + " \"aggregations\":{" - + " \"max_price\":{" - + " \"max\":{" - + " \"field\":\"price\"" - + " }" - + " }" - + " }" - + "}" - + "}" - ); + createTransformRequest.setJsonEntity(""" + { + "source": { + "index": "customers" + }, + "description": "testing", + "dest": { + "index": "max_price" + }, + "pivot": { + "group_by": { + "customer_id": { + "terms": { + "field": "customer_id" + } + } + }, + "aggregations": { + "max_price": { + "max": { + "field": "price" + } + } + } + } + }"""); Map createTransformResponse = entityAsMap(client().performRequest(createTransformRequest)); assertThat(createTransformResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -584,9 +597,28 @@ private void assertWatchIndexContentsWork() throws Exception { } private void assertBasicWatchInteractions() throws Exception { - String watch = "{\"trigger\":{\"schedule\":{\"interval\":\"1s\"}},\"input\":{\"none\":{}}," - + "\"condition\":{\"always\":{}}," - + "\"actions\":{\"awesome\":{\"logging\":{\"level\":\"info\",\"text\":\"test\"}}}}"; + String watch = """ + { + "trigger": { + "schedule": { + "interval": "1s" + } + }, + "input": { + "none": {} + }, + "condition": { + "always": {} + }, + "actions": { + "awesome": { + "logging": { + "level": "info", + "text": "test" + } + } + } + }"""; Request createWatchRequest = new Request("PUT", "_watcher/watch/new_watch"); createWatchRequest.setJsonEntity(watch); Map createWatch = entityAsMap(client().performRequest(createWatchRequest)); @@ -677,41 +709,35 @@ static String toStr(Response response) throws IOException { private void createUser(final boolean oldCluster) throws Exception { final String id = oldCluster ? "preupgrade_user" : "postupgrade_user"; Request request = new Request("PUT", "/_security/user/" + id); - request.setJsonEntity( - "{\n" - + " \"password\" : \"l0ng-r4nd0m-p@ssw0rd\",\n" - + " \"roles\" : [ \"admin\", \"other_role1\" ],\n" - + " \"full_name\" : \"" - + randomAlphaOfLength(5) - + "\",\n" - + " \"email\" : \"" - + id - + "@example.com\",\n" - + " \"enabled\": true\n" - + "}" - ); + request.setJsonEntity(""" + { + "password" : "l0ng-r4nd0m-p@ssw0rd", + "roles" : [ "admin", "other_role1" ], + "full_name" : "%s", + "email" : "%s@example.com", + "enabled": true + }""".formatted(randomAlphaOfLength(5), id)); client().performRequest(request); } private void createRole(final boolean oldCluster) throws Exception { final String id = oldCluster ? "preupgrade_role" : "postupgrade_role"; Request request = new Request("PUT", "/_security/role/" + id); - request.setJsonEntity( - "{\n" - + " \"run_as\": [ \"abc\" ],\n" - + " \"cluster\": [ \"monitor\" ],\n" - + " \"indices\": [\n" - + " {\n" - + " \"names\": [ \"events-*\" ],\n" - + " \"privileges\": [ \"read\" ],\n" - + " \"field_security\" : {\n" - + " \"grant\" : [ \"category\", \"@timestamp\", \"message\" ]\n" - + " },\n" - + " \"query\": \"{\\\"match\\\": {\\\"category\\\": \\\"click\\\"}}\"\n" - + " }\n" - + " ]\n" - + "}" - ); + request.setJsonEntity(""" + { + "run_as": [ "abc" ], + "cluster": [ "monitor" ], + "indices": [ + { + "names": [ "events-*" ], + "privileges": [ "read" ], + "field_security" : { + "grant" : [ "category", "@timestamp", "message" ] + }, + "query": "{\\"match\\": {\\"category\\": \\"click\\"}}" + } + ] + }"""); client().performRequest(request); } @@ -858,10 +884,11 @@ public void testDataStreams() throws Exception { } private static void createComposableTemplate(RestClient client, String templateName, String indexPattern) throws IOException { - StringEntity templateJSON = new StringEntity( - String.format(Locale.ROOT, "{\n" + " \"index_patterns\": \"%s\",\n" + " \"data_stream\": {}\n" + "}", indexPattern), - ContentType.APPLICATION_JSON - ); + StringEntity templateJSON = new StringEntity(String.format(Locale.ROOT, """ + { + "index_patterns": "%s", + "data_stream": {} + }""", indexPattern), ContentType.APPLICATION_JSON); Request createIndexTemplateRequest = new Request("PUT", "_index_template/" + templateName); createIndexTemplateRequest.setEntity(templateJSON); client.performRequest(createIndexTemplateRequest); diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java index 84df8d10df7dd..befee7de3f757 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlConfigIndexMappingsFullClusterRestartIT.java @@ -72,19 +72,18 @@ public void testMlConfigIndexMappingsAfterMigration() throws Exception { } private void createAnomalyDetectorJob(String jobId) throws IOException { - String jobConfig = "{\n" - + " \"job_id\": \"" - + jobId - + "\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"10m\",\n" - + " \"detectors\": [{\n" - + " \"function\": \"metric\",\n" - + " \"field_name\": \"responsetime\"\n" - + " }]\n" - + " },\n" - + " \"data_description\": {}\n" - + "}"; + String jobConfig = """ + { + "job_id": "%s", + "analysis_config": { + "bucket_span": "10m", + "detectors": [{ + "function": "metric", + "field_name": "responsetime" + }] + }, + "data_description": {} + }""".formatted(jobId); Request putJobRequest = new Request("PUT", "/_ml/anomaly_detectors/" + jobId); putJobRequest.setJsonEntity(jobConfig); diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java index 3d262ed243b1e..48c2726391fda 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlHiddenIndicesFullClusterRestartIT.java @@ -161,19 +161,18 @@ private static Map contentAsMap(Response response) throws IOExce } private void createAnomalyDetectorJob(String jobId) throws IOException { - String jobConfig = "{\n" - + " \"job_id\": \"" - + jobId - + "\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"10m\",\n" - + " \"detectors\": [{\n" - + " \"function\": \"metric\",\n" - + " \"field_name\": \"responsetime\"\n" - + " }]\n" - + " },\n" - + " \"data_description\": {}\n" - + "}"; + String jobConfig = """ + { + "job_id": "%s", + "analysis_config": { + "bucket_span": "10m", + "detectors": [{ + "function": "metric", + "field_name": "responsetime" + }] + }, + "data_description": {} + }""".formatted(jobId); Request putJobRequest = new Request("PUT", "/_ml/anomaly_detectors/" + jobId); putJobRequest.setJsonEntity(jobConfig); diff --git a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java index 31585dd99de35..1a92f5c459b21 100644 --- a/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java +++ b/x-pack/qa/full-cluster-restart/src/test/java/org/elasticsearch/xpack/restart/MlMigrationFullClusterRestartIT.java @@ -62,13 +62,24 @@ public void waitForMlTemplates() throws Exception { private void createTestIndex() throws IOException { Request createTestIndex = new Request("PUT", "/airline-data"); - createTestIndex.setJsonEntity( - "{\"mappings\": { \"doc\": {\"properties\": {" - + "\"time\": {\"type\": \"date\"}," - + "\"airline\": {\"type\": \"keyword\"}," - + "\"responsetime\": {\"type\": \"float\"}" - + "}}}}" - ); + createTestIndex.setJsonEntity(""" + { + "mappings": { + "doc": { + "properties": { + "time": { + "type": "date" + }, + "airline": { + "type": "keyword" + }, + "responsetime": { + "type": "float" + } + } + } + } + }"""); client().performRequest(createTestIndex); } @@ -194,20 +205,19 @@ private void addAggregations(DatafeedConfig.Builder dfBuilder) { } private void putJob(String jobId) throws IOException { - String jobConfig = "{\n" - + " \"job_id\": \"" - + jobId - + "\",\n" - + " \"analysis_config\": {\n" - + " \"bucket_span\": \"10m\",\n" - + " \"detectors\": [{\n" - + " \"function\": \"metric\",\n" - + " \"by_field_name\": \"airline\",\n" - + " \"field_name\": \"responsetime\"\n" - + " }]\n" - + " },\n" - + " \"data_description\": {}\n" - + "}"; + String jobConfig = """ + { + "job_id": "%s", + "analysis_config": { + "bucket_span": "10m", + "detectors": [{ + "function": "metric", + "by_field_name": "airline", + "field_name": "responsetime" + }] + }, + "data_description": {}` + }""".formatted(jobId); Request putClosedJob = new Request("PUT", "/_xpack/ml/anomaly_detectors/" + jobId); putClosedJob.setJsonEntity(jobConfig); client().performRequest(putClosedJob); diff --git a/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java b/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java index 9fd2f8cf5fd3c..997c92a9444d0 100644 --- a/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java +++ b/x-pack/qa/kerberos-tests/src/test/java/org/elasticsearch/xpack/security/authc/kerberos/KerberosAuthenticationIT.java @@ -133,7 +133,9 @@ public void testGetOauth2TokenInExchangeForKerberosTickets() throws PrivilegedAc final String kerberosTicket = callbackHandler.getBase64EncodedTokenForSpnegoHeader(host); final Request request = new Request("POST", "/_security/oauth2/token"); - String json = "{" + " \"grant_type\" : \"_kerberos\", " + " \"kerberos_ticket\" : \"" + kerberosTicket + "\"" + "}"; + String json = """ + { "grant_type" : "_kerberos", "kerberos_ticket" : "%s"} + """.formatted(kerberosTicket); request.setJsonEntity(json); try (RestClient client = buildClientForUser("test_kibana_user")) { diff --git a/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java b/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java index d6883b7d59297..1368ee3a219fe 100644 --- a/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java +++ b/x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/RollupIT.java @@ -107,25 +107,30 @@ public void testBigRollup() throws Exception { // create the rollup job final Request createRollupJobRequest = new Request("PUT", "/_rollup/job/rollup-job-test"); int pageSize = randomIntBetween(2, 50); - createRollupJobRequest.setJsonEntity( - "{" - + "\"index_pattern\":\"rollup-*\"," - + "\"rollup_index\":\"results-rollup\"," - + "\"cron\":\"*/1 * * * * ?\"," // fast cron so test runs quickly - + "\"page_size\":" - + pageSize - + "," - + "\"groups\":{" - + " \"date_histogram\":{" - + " \"field\":\"timestamp\"," - + " \"fixed_interval\":\"5m\"" - + " }" - + "}," - + "\"metrics\":[" - + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" - + "]" - + "}" - ); + // fast cron so test runs quickly + createRollupJobRequest.setJsonEntity(""" + { + "index_pattern": "rollup-*", + "rollup_index": "results-rollup", + "cron": "*/1 * * * * ?", + "page_size": %s, + "groups": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "5m" + } + }, + "metrics": [ + { + "field": "value", + "metrics": [ + "min", + "max", + "sum" + ] + } + ] + }""".formatted(pageSize)); Map createRollupJobResponse = toMap(client().performRequest(createRollupJobRequest)); assertThat(createRollupJobResponse.get("acknowledged"), equalTo(Boolean.TRUE)); @@ -154,28 +159,29 @@ public void testBigRollup() throws Exception { final Request refreshRollupIndex = new Request("POST", "results-rollup/_refresh"); toMap(client().performRequest(refreshRollupIndex)); - String jsonRequestBody = "{\n" - + " \"size\": 0,\n" - + " \"query\": {\n" - + " \"match_all\": {}\n" - + " },\n" - + " \"aggs\": {\n" - + " \"date_histo\": {\n" - + " \"date_histogram\": {\n" - + " \"field\": \"timestamp\",\n" - + " \"fixed_interval\": \"60m\",\n" - + " \"format\": \"date_time\"\n" - + " },\n" - + " \"aggs\": {\n" - + " \"the_max\": {\n" - + " \"max\": {\n" - + " \"field\": \"value\"\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + " }\n" - + "}"; + String jsonRequestBody = """ + { + "size": 0, + "query": { + "match_all": {} + }, + "aggs": { + "date_histo": { + "date_histogram": { + "field": "timestamp", + "fixed_interval": "60m", + "format": "date_time" + }, + "aggs": { + "the_max": { + "max": { + "field": "value" + } + } + } + } + } + }"""; Request request = new Request("GET", "rollup-docs/_search"); request.setJsonEntity(jsonRequestBody); diff --git a/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java b/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java index bef3687d8056b..a7fb30db9ced0 100644 --- a/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java +++ b/x-pack/qa/oidc-op-tests/src/test/java/org/elasticsearch/xpack/security/authc/oidc/OpenIdConnectAuthIT.java @@ -108,45 +108,41 @@ public static void readTrustedCert() throws Exception { @BeforeClass public static void registerClients() throws Exception { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - String codeClient = "{" - + "\"grant_types\": [\"authorization_code\"]," - + "\"response_types\": [\"code\"]," - + "\"preferred_client_id\":\"https://my.elasticsearch.org/rp\"," - + "\"preferred_client_secret\":\"" - + CLIENT_SECRET - + "\"," - + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," - + "\"token_endpoint_auth_method\":\"client_secret_basic\"" - + "}"; - String implicitClient = "{" - + "\"grant_types\": [\"implicit\"]," - + "\"response_types\": [\"token id_token\"]," - + "\"preferred_client_id\":\"elasticsearch-rp\"," - + "\"preferred_client_secret\":\"" - + CLIENT_SECRET - + "\"," - + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]" - + "}"; - String postClient = "{" - + "\"grant_types\": [\"authorization_code\"]," - + "\"response_types\": [\"code\"]," - + "\"preferred_client_id\":\"elasticsearch-post\"," - + "\"preferred_client_secret\":\"" - + CLIENT_SECRET - + "\"," - + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," - + "\"token_endpoint_auth_method\":\"client_secret_post\"" - + "}"; - String jwtClient = "{" - + "\"grant_types\": [\"authorization_code\"]," - + "\"response_types\": [\"code\"]," - + "\"preferred_client_id\":\"elasticsearch-post-jwt\"," - + "\"preferred_client_secret\":\"" - + CLIENT_SECRET - + "\"," - + "\"redirect_uris\": [\"https://my.fantastic.rp/cb\"]," - + "\"token_endpoint_auth_method\":\"client_secret_jwt\"" - + "}"; + String codeClient = """ + { + "grant_types": [ "authorization_code" ], + "response_types": [ "code" ], + "preferred_client_id": "https://my.elasticsearch.org/rp", + "preferred_client_secret": "%s", + "redirect_uris": [ "https://my.fantastic.rp/cb" ], + "token_endpoint_auth_method": "client_secret_basic" + }""".formatted(CLIENT_SECRET); + String implicitClient = """ + { + "grant_types": [ "implicit" ], + "response_types": [ "token id_token" ], + "preferred_client_id": "elasticsearch-rp", + "preferred_client_secret": "%s", + "redirect_uris": [ "https://my.fantastic.rp/cb" ] + }""".formatted(CLIENT_SECRET); + String postClient = """ + { + "grant_types": [ "authorization_code" ], + "response_types": [ "code" ], + "preferred_client_id": "elasticsearch-post", + "preferred_client_secret": "%s", + "redirect_uris": [ "https://my.fantastic.rp/cb" ], + "token_endpoint_auth_method": "client_secret_post" + }""".formatted(CLIENT_SECRET); + String jwtClient = """ + { + "grant_types": [ "authorization_code" ], + "response_types": [ "code" ], + "preferred_client_id": "elasticsearch-post-jwt", + "preferred_client_secret": "%s", + "redirect_uris": [ "https://my.fantastic.rp/cb" ], + "token_endpoint_auth_method": "client_secret_jwt" + }""".formatted(CLIENT_SECRET); HttpPost httpPost = new HttpPost(REGISTRATION_URL); final BasicHttpContext context = new BasicHttpContext(); httpPost.setEntity(new StringEntity(codeClient, ContentType.APPLICATION_JSON)); @@ -205,7 +201,9 @@ private String authenticateAtOP(URI opAuthUri) throws Exception { final BasicHttpContext context = new BasicHttpContext(); // Initiate the authentication process HttpPost httpPost = new HttpPost(LOGIN_API + "initAuthRequest"); - String initJson = "{" + " \"qs\":\"" + opAuthUri.getRawQuery() + "\"" + "}"; + String initJson = """ + {"qs":"%s"} + """.formatted(opAuthUri.getRawQuery()); configureJsonRequest(httpPost, initJson); JSONObject initResponse = execute(httpClient, httpPost, context, response -> { assertHttpOk(response.getStatusLine()); @@ -215,7 +213,8 @@ private String authenticateAtOP(URI opAuthUri) throws Exception { final String sid = initResponse.getAsString("sid"); // Actually authenticate the user with ldapAuth HttpPost loginHttpPost = new HttpPost(LOGIN_API + "authenticateSubject?cacheBuster=" + randomAlphaOfLength(8)); - String loginJson = "{" + "\"username\":\"alice\"," + "\"password\":\"secret\"" + "}"; + String loginJson = """ + {"username":"alice","password":"secret"}"""; configureJsonRequest(loginHttpPost, loginJson); JSONObject loginJsonResponse = execute(httpClient, loginHttpPost, context, response -> { assertHttpOk(response.getStatusLine()); @@ -225,21 +224,20 @@ private String authenticateAtOP(URI opAuthUri) throws Exception { HttpPut consentFetchHttpPut = new HttpPut( LOGIN_API + "updateAuthRequest" + "/" + sid + "?cacheBuster=" + randomAlphaOfLength(8) ); - String consentFetchJson = "{" - + "\"sub\": \"" - + loginJsonResponse.getAsString("id") - + "\"," - + "\"acr\": \"http://loa.c2id.com/basic\"," - + "\"amr\": [\"pwd\"]," - + "\"data\": {" - + "\"email\": \"" - + loginJsonResponse.getAsString("email") - + "\"," - + "\"name\": \"" - + loginJsonResponse.getAsString("name") - + "\"" - + "}" - + "}"; + String consentFetchJson = """ + { + "sub": "%s", + "acr": "http://loa.c2id.com/basic", + "amr": [ "pwd" ], + "data": { + "email": "%s", + "name": "%s" + } + }""".formatted( + loginJsonResponse.getAsString("id"), + loginJsonResponse.getAsString("email"), + loginJsonResponse.getAsString("name") + ); configureJsonRequest(consentFetchHttpPut, consentFetchJson); JSONObject consentFetchResponse = execute(httpClient, consentFetchHttpPut, context, response -> { assertHttpOk(response.getStatusLine()); @@ -250,7 +248,8 @@ private String authenticateAtOP(URI opAuthUri) throws Exception { HttpPut consentHttpPut = new HttpPut( LOGIN_API + "updateAuthRequest" + "/" + sid + "?cacheBuster=" + randomAlphaOfLength(8) ); - String consentJson = "{" + "\"claims\":[\"name\", \"email\"]," + "\"scope\":[\"openid\"]" + "}"; + String consentJson = """ + {"claims":["name", "email"],"scope":["openid"]}"""; configureJsonRequest(consentHttpPut, consentJson); JSONObject jsonConsentResponse = execute(httpClient, consentHttpPut, context, response -> { assertHttpOk(response.getStatusLine()); @@ -497,10 +496,12 @@ private void assertHttpOk(StatusLine status) { private void setFacilitatorUser() throws Exception { try (RestClient restClient = getClient()) { Request createRoleRequest = new Request("PUT", "/_security/role/facilitator"); - createRoleRequest.setJsonEntity("{ \"cluster\" : [\"manage_oidc\", \"manage_token\"] }"); + createRoleRequest.setJsonEntity(""" + { "cluster" : ["manage_oidc", "manage_token"] }"""); restClient.performRequest(createRoleRequest); Request createUserRequest = new Request("PUT", "/_security/user/facilitator"); - createUserRequest.setJsonEntity("{ \"password\" : \"" + FACILITATOR_PASSWORD + "\", \"roles\" : [\"facilitator\"] }"); + createUserRequest.setJsonEntity(""" + { "password" : "%s", "roles" : ["facilitator"] }""".formatted(FACILITATOR_PASSWORD)); restClient.performRequest(createUserRequest); } } @@ -508,46 +509,61 @@ private void setFacilitatorUser() throws Exception { private void setRoleMappings() throws Exception { try (RestClient restClient = getClient()) { Request createRoleMappingRequest = new Request("PUT", "/_security/role_mapping/oidc_kibana"); - createRoleMappingRequest.setJsonEntity( - "{ \"roles\" : [\"kibana_admin\"]," - + "\"enabled\": true," - + "\"rules\": {" - + " \"any\" : [" - + " {\"field\": { \"realm.name\": \"" - + REALM_NAME - + "\"} }," - + " {\"field\": { \"realm.name\": \"" - + REALM_NAME_PROXY - + "\"} }," - + " {\"field\": { \"realm.name\": \"" - + REALM_NAME_CLIENT_POST_AUTH - + "\"} }," - + " {\"field\": { \"realm.name\": \"" - + REALM_NAME_CLIENT_JWT_AUTH - + "\"} }" - + " ]" - + "}" - + "}" - ); + createRoleMappingRequest.setJsonEntity(""" + { + "roles": [ "kibana_admin" ], + "enabled": true, + "rules": { + "any": [ + { + "field": { + "realm.name": "%s" + } + }, + { + "field": { + "realm.name": "%s" + } + }, + { + "field": { + "realm.name": "%s" + } + }, + { + "field": { + "realm.name": "%s" + } + } + ] + } + }""".formatted(REALM_NAME, REALM_NAME_PROXY, REALM_NAME_CLIENT_POST_AUTH, REALM_NAME_CLIENT_JWT_AUTH)); restClient.performRequest(createRoleMappingRequest); createRoleMappingRequest = new Request("PUT", "/_security/role_mapping/oidc_limited"); - createRoleMappingRequest.setJsonEntity( - "{ \"roles\" : [\"limited_user\"]," - + "\"enabled\": true," - + "\"rules\": {" - + "\"field\": { \"realm.name\": \"" - + REALM_NAME_IMPLICIT - + "\"}" - + "}" - + "}" - ); + createRoleMappingRequest.setJsonEntity(""" + { + "roles": [ "limited_user" ], + "enabled": true, + "rules": { + "field": { + "realm.name": "%s" + } + } + }""".formatted(REALM_NAME_IMPLICIT)); restClient.performRequest(createRoleMappingRequest); createRoleMappingRequest = new Request("PUT", "/_security/role_mapping/oidc_auditor"); - createRoleMappingRequest.setJsonEntity( - "{ \"roles\" : [\"auditor\"]," + "\"enabled\": true," + "\"rules\": {" + "\"field\": { \"groups\": \"audit\"}" + "}" + "}" - ); + createRoleMappingRequest.setJsonEntity(""" + { + "roles": [ "auditor" ], + "enabled": true, + "rules": { + "field": { + "groups": "audit" + } + } + }"""); restClient.performRequest(createRoleMappingRequest); } } diff --git a/x-pack/qa/repository-old-versions/src/test/java/org/elasticsearch/oldrepos/OldRepositoryAccessIT.java b/x-pack/qa/repository-old-versions/src/test/java/org/elasticsearch/oldrepos/OldRepositoryAccessIT.java index bed58999658e1..74051101bcc44 100644 --- a/x-pack/qa/repository-old-versions/src/test/java/org/elasticsearch/oldrepos/OldRepositoryAccessIT.java +++ b/x-pack/qa/repository-old-versions/src/test/java/org/elasticsearch/oldrepos/OldRepositoryAccessIT.java @@ -86,7 +86,9 @@ public void testOldRepoAccess() throws IOException { try { Request createIndex = new Request("PUT", "/test"); int numberOfShards = randomIntBetween(1, 3); - createIndex.setJsonEntity("{\"settings\":{\"number_of_shards\": " + numberOfShards + "}}"); + createIndex.setJsonEntity(""" + {"settings":{"number_of_shards": %s}} + """.formatted(numberOfShards)); oldEs.performRequest(createIndex); for (int i = 0; i < numDocs; i++) { @@ -100,7 +102,9 @@ public void testOldRepoAccess() throws IOException { // register repo on old ES and take snapshot Request createRepoRequest = new Request("PUT", "/_snapshot/testrepo"); - createRepoRequest.setJsonEntity("{\"type\":\"fs\",\"settings\":{\"location\":\"" + repoLocation + "\"}}"); + createRepoRequest.setJsonEntity(""" + {"type":"fs","settings":{"location":"%s"}} + """.formatted(repoLocation)); oldEs.performRequest(createRepoRequest); Request createSnapshotRequest = new Request("PUT", "/_snapshot/testrepo/snap1"); diff --git a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java index a0cb9ab354892..f3640c086f47b 100644 --- a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java +++ b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/AbstractMultiClusterUpgradeTestCase.java @@ -118,7 +118,12 @@ private void configureLeaderRemoteClusters() throws IOException { if (leaderRemoteClusterSeed != null) { logger.info("Configuring leader remote cluster [{}]", leaderRemoteClusterSeed); Request request = new Request("PUT", "/_cluster/settings"); - request.setJsonEntity("{\"persistent\": {\"cluster.remote.leader.seeds\": \"" + leaderRemoteClusterSeed + "\"}}"); + request.setJsonEntity(""" + { + "persistent": { + "cluster.remote.leader.seeds": "%s" + } + }""".formatted(leaderRemoteClusterSeed)); assertThat(leaderClient.performRequest(request).getStatusLine().getStatusCode(), equalTo(200)); if (followerClient != null) { assertThat(followerClient.performRequest(request).getStatusLine().getStatusCode(), equalTo(200)); @@ -133,7 +138,12 @@ private void configureFollowerRemoteClusters() throws IOException { if (followerRemoteClusterSeed != null) { logger.info("Configuring follower remote cluster [{}]", followerRemoteClusterSeed); Request request = new Request("PUT", "/_cluster/settings"); - request.setJsonEntity("{\"persistent\": {\"cluster.remote.follower.seeds\": \"" + followerRemoteClusterSeed + "\"}}"); + request.setJsonEntity(""" + { + "persistent": { + "cluster.remote.follower.seeds": "%s" + } + }""".formatted(followerRemoteClusterSeed)); assertThat(leaderClient.performRequest(request).getStatusLine().getStatusCode(), equalTo(200)); assertThat(followerClient.performRequest(request).getStatusLine().getStatusCode(), equalTo(200)); } else { diff --git a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java index df7d4b9d70323..6fcac893e070b 100644 --- a/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade-multi-cluster/src/test/java/org/elasticsearch/upgrades/CcrRollingUpgradeIT.java @@ -309,28 +309,30 @@ private static void createLeaderIndex(RestClient client, String indexName) throw private static void createIndex(RestClient client, String name, Settings settings) throws IOException { Request request = new Request("PUT", "/" + name); - request.setJsonEntity("{\n \"settings\": " + Strings.toString(settings) + "}"); + request.setJsonEntity(""" + { + "settings": %s + }""".formatted(Strings.toString(settings))); client.performRequest(request); } private static void followIndex(RestClient client, String leaderCluster, String leaderIndex, String followIndex) throws IOException { final Request request = new Request("PUT", "/" + followIndex + "/_ccr/follow?wait_for_active_shards=1"); - request.setJsonEntity( - "{\"remote_cluster\": \"" + leaderCluster + "\", \"leader_index\": \"" + leaderIndex + "\", \"read_poll_timeout\": \"10ms\"}" - ); + request.setJsonEntity(""" + {"remote_cluster": "%s", "leader_index": "%s", "read_poll_timeout": "10ms"} + """.formatted(leaderCluster, leaderIndex)); assertOK(client.performRequest(request)); } private static void putAutoFollowPattern(RestClient client, String name, String remoteCluster, String pattern) throws IOException { Request request = new Request("PUT", "/_ccr/auto_follow/" + name); - request.setJsonEntity( - "{\"leader_index_patterns\": [\"" - + pattern - + "\"], \"remote_cluster\": \"" - + remoteCluster - + "\"," - + "\"follow_index_pattern\": \"copy-{{leader_index}}\", \"read_poll_timeout\": \"10ms\"}" - ); + request.setJsonEntity(""" + { + "leader_index_patterns": [ "%s" ], + "remote_cluster": "%s", + "follow_index_pattern": "copy-{{leader_index}}", + "read_poll_timeout": "10ms" + }""".formatted(pattern, remoteCluster)); assertOK(client.performRequest(request)); } diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java index 857acd334d9a1..9177c3f8a1f94 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java @@ -26,20 +26,20 @@ public class DataStreamsUpgradeIT extends AbstractUpgradeTestCase { public void testDataStreams() throws IOException { assumeTrue("no data streams in versions before " + Version.V_7_9_0, UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_9_0)); if (CLUSTER_TYPE == ClusterType.OLD) { - String requestBody = "{\n" - + " \"index_patterns\":[\"logs-*\"],\n" - + " \"template\": {\n" - + " \"mappings\": {\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"data_stream\":{\n" - + " }\n" - + " }"; + String requestBody = """ + { + "index_patterns": [ "logs-*" ], + "template": { + "mappings": { + "properties": { + "@timestamp": { + "type": "date" + } + } + } + }, + "data_stream": {} + }"""; Request request = new Request("PUT", "/_index_template/1"); request.setJsonEntity(requestBody); useIgnoreMultipleMatchingTemplatesWarningsHandler(request); @@ -47,8 +47,10 @@ public void testDataStreams() throws IOException { StringBuilder b = new StringBuilder(); for (int i = 0; i < 1000; i++) { - b.append("{\"create\":{\"_index\":\"").append("logs-foobar").append("\"}}\n"); - b.append("{\"@timestamp\":\"2020-12-12\",\"test\":\"value").append(i).append("\"}\n"); + b.append(""" + {"create":{"_index":"logs-foobar"}} + {"@timestamp":"2020-12-12","test":"value%s"} + """.formatted(i)); } Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); @@ -109,35 +111,36 @@ public void testDataStreams() throws IOException { public void testDataStreamValidationDoesNotBreakUpgrade() throws Exception { assumeTrue("Bug started to occur from version: " + Version.V_7_10_2, UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_10_2)); if (CLUSTER_TYPE == ClusterType.OLD) { - String requestBody = "{\n" - + " \"index_patterns\":[\"logs-*\"],\n" - + " \"template\": {\n" - + " \"mappings\": {\n" - + " \"properties\": {\n" - + " \"@timestamp\": {\n" - + " \"type\": \"date\"\n" - + " }\n" - + " }\n" - + " }\n" - + " },\n" - + " \"data_stream\":{\n" - + " }\n" - + " }"; + String requestBody = """ + { + "index_patterns": [ "logs-*" ], + "template": { + "mappings": { + "properties": { + "@timestamp": { + "type": "date" + } + } + } + }, + "data_stream": {} + }"""; Request request = new Request("PUT", "/_index_template/1"); request.setJsonEntity(requestBody); useIgnoreMultipleMatchingTemplatesWarningsHandler(request); client().performRequest(request); - StringBuilder b = new StringBuilder(); - b.append("{\"create\":{\"_index\":\"").append("logs-barbaz").append("\"}}\n"); - b.append("{\"@timestamp\":\"2020-12-12\",\"test\":\"value").append(0).append("\"}\n"); - b.append("{\"create\":{\"_index\":\"").append("logs-barbaz-2021.01.13").append("\"}}\n"); - b.append("{\"@timestamp\":\"2020-12-12\",\"test\":\"value").append(0).append("\"}\n"); + String b = """ + {"create":{"_index":"logs-barbaz"}} + {"@timestamp":"2020-12-12","test":"value0"} + {"create":{"_index":"logs-barbaz-2021.01.13"}} + {"@timestamp":"2020-12-12","test":"value0"} + """; Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); bulk.addParameter("filter_path", "errors"); - bulk.setJsonEntity(b.toString()); + bulk.setJsonEntity(b); Response response = client().performRequest(bulk); assertEquals("{\"errors\":false}", EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java index a0caf168aab42..97aac449f1606 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexingIT.java @@ -50,10 +50,12 @@ public void testIndexing() throws IOException { if (CLUSTER_TYPE == ClusterType.OLD) { Request createTestIndex = new Request("PUT", "/test_index"); - createTestIndex.setJsonEntity("{\"settings\": {\"index.number_of_replicas\": 0}}"); + createTestIndex.setJsonEntity(""" + {"settings": {"index.number_of_replicas": 0}}"""); client().performRequest(createTestIndex); - String recoverQuickly = "{\"settings\": {\"index.unassigned.node_left.delayed_timeout\": \"100ms\"}}"; + String recoverQuickly = """ + {"settings": {"index.unassigned.node_left.delayed_timeout": "100ms"}}"""; Request createIndexWithReplicas = new Request("PUT", "/index_with_replicas"); createIndexWithReplicas.setJsonEntity(recoverQuickly); client().performRequest(createIndexWithReplicas); @@ -109,8 +111,10 @@ public void testIndexing() throws IOException { private void bulk(String index, String valueSuffix, int count) throws IOException { StringBuilder b = new StringBuilder(); for (int i = 0; i < count; i++) { - b.append("{\"index\": {\"_index\": \"").append(index).append("\"}}\n"); - b.append("{\"f1\": \"v").append(i).append(valueSuffix).append("\", \"f2\": ").append(i).append("}\n"); + b.append(""" + {"index": {"_index": "%s"}} + {"f1": "v%s%s", "f2": %s} + """.formatted(index, i, valueSuffix, i)); } Request bulk = new Request("POST", "/_bulk"); bulk.addParameter("refresh", "true"); @@ -123,9 +127,8 @@ static void assertCount(String index, int count) throws IOException { searchTestIndexRequest.addParameter(TOTAL_HITS_AS_INT_PARAM, "true"); searchTestIndexRequest.addParameter("filter_path", "hits.total"); Response searchTestIndexResponse = client().performRequest(searchTestIndexRequest); - assertEquals( - "{\"hits\":{\"total\":" + count + "}}", - EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8) - ); + assertEquals(""" + {"hits":{"total":%s}}\ + """.formatted(count), EntityUtils.toString(searchTestIndexResponse.getEntity(), StandardCharsets.UTF_8)); } } diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java index c686c11eff520..e7b8b7fc10b99 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlJobSnapshotUpgradeIT.java @@ -97,7 +97,8 @@ protected static void waitForPendingUpgraderTasks() throws Exception { public void testSnapshotUpgrader() throws Exception { hlrc = new HLRC(client()).machineLearning(); Request adjustLoggingLevels = new Request("PUT", "/_cluster/settings"); - adjustLoggingLevels.setJsonEntity("{\"persistent\": {" + "\"logger.org.elasticsearch.xpack.ml\": \"trace\"" + "}}"); + adjustLoggingLevels.setJsonEntity(""" + {"persistent": {"logger.org.elasticsearch.xpack.ml": "trace"}}"""); client().performRequest(adjustLoggingLevels); switch (CLUSTER_TYPE) { case OLD: diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlTrainedModelsUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlTrainedModelsUpgradeIT.java index be35dc3f7d70c..10d18d398bfd9 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlTrainedModelsUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/MlTrainedModelsUpgradeIT.java @@ -102,46 +102,40 @@ void testInfer(List modelIds) throws Exception { for (String modelId : modelIds) { Request simulate = new Request("POST", "/_ingest/pipeline/" + modelId + "/_simulate"); simulate.setJsonEntity( - "" - + "{\n" - + " \"docs\": [\n" - + " {\n" - + " \"_index\": \"index\",\n" - + " \"_id\": \"id\",\n" - + " \"_source\": " - + String.format( - Locale.ROOT, - "{\"%s\":%s,\"%s\":%f,\"%s\":%d,\"%s\":\"%s\"}", - BOOLEAN_FIELD, - BOOLEAN_FIELD_VALUES.get(0), - NUMERICAL_FIELD, - NUMERICAL_FIELD_VALUES.get(0), - DISCRETE_NUMERICAL_FIELD, - DISCRETE_NUMERICAL_FIELD_VALUES.get(0), - KEYWORD_FIELD, - KEYWORD_FIELD_VALUES.get(0) - ) - + " },\n" - + " {\n" - + " \"_index\": \"index\",\n" - + " \"_id\": \"id\",\n" - + " \"_source\": " - + String.format( - Locale.ROOT, - "{\"%s\":%s,\"%s\":%f,\"%s\":%d,\"%s\":\"%s\"}", - BOOLEAN_FIELD, - BOOLEAN_FIELD_VALUES.get(1), - NUMERICAL_FIELD, - NUMERICAL_FIELD_VALUES.get(1), - DISCRETE_NUMERICAL_FIELD, - DISCRETE_NUMERICAL_FIELD_VALUES.get(1), - KEYWORD_FIELD, - KEYWORD_FIELD_VALUES.get(1) - ) - + " }\n" - + " ]\n" - + "}" - + "" + String.format( + Locale.ROOT, + """ + { + "docs": [ + { + "_index": "index", + "_id": "id", + "_source": {"%s":%s,"%s":%f,"%s":%s,"%s":"%s"} + }, + { + "_index": "index", + "_id": "id", + "_source": {"%s":%s,"%s":%f,"%s":%s,"%s":"%s"} + } + ] + }""", + BOOLEAN_FIELD, + BOOLEAN_FIELD_VALUES.get(0), + NUMERICAL_FIELD, + NUMERICAL_FIELD_VALUES.get(0), + DISCRETE_NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD_VALUES.get(0), + KEYWORD_FIELD, + KEYWORD_FIELD_VALUES.get(0), + BOOLEAN_FIELD, + BOOLEAN_FIELD_VALUES.get(1), + NUMERICAL_FIELD, + NUMERICAL_FIELD_VALUES.get(1), + DISCRETE_NUMERICAL_FIELD, + DISCRETE_NUMERICAL_FIELD_VALUES.get(1), + KEYWORD_FIELD, + KEYWORD_FIELD_VALUES.get(1) + ) ); Response response = client().performRequest(simulate); String value = EntityUtils.toString(response.getEntity()); @@ -155,51 +149,40 @@ void testInfer(List modelIds) throws Exception { } void createAndRunRegressionJob() throws Exception { - String config = "{\n" - + " \"source\" : {\n" - + " \"index\" : [\n" - + " \"" - + INDEX_NAME - + "\"\n" - + " ]\n" - + " },\n" - + " \"dest\" : {\n" - + " \"index\" : \"regression\"\n" - + " },\n" - + " \"analysis\" : {\n" - + " \"regression\" : {\n" - + " \"dependent_variable\" : \"" - + NUMERICAL_FIELD - + "\"\n" - + " }\n" - + " },\n" - + " \"model_memory_limit\" : \"18mb\"\n" - + " }"; + String config = """ + { + "source": { + "index": [ "%s" ] + }, + "dest": { + "index": "regression" + }, + "analysis": { + "regression": { + "dependent_variable": "%s" + } + }, + "model_memory_limit": "18mb" + }""".formatted(INDEX_NAME, NUMERICAL_FIELD); putAndStartDFAAndWaitForFinish(config, "regression"); } void createAndRunClassificationJob() throws Exception { - String config = "" - + "{\n" - + " \"source\" : {\n" - + " \"index\" : [\n" - + " \"" - + INDEX_NAME - + "\"\n" - + " ]\n" - + " },\n" - + " \"dest\" : {\n" - + " \"index\" : \"classification\"\n" - + " },\n" - + " \"analysis\" : {\n" - + " \"classification\" : {\n" - + " \"dependent_variable\" : \"" - + KEYWORD_FIELD - + "\"\n" - + " }\n" - + " },\n" - + " \"model_memory_limit\" : \"18mb\"\n" - + " }"; + String config = """ + { + "source": { + "index": [ "%s" ] + }, + "dest": { + "index": "classification" + }, + "analysis": { + "classification": { + "dependent_variable": "%s" + } + }, + "model_memory_limit": "18mb" + }""".formatted(INDEX_NAME, KEYWORD_FIELD); putAndStartDFAAndWaitForFinish(config, "classification"); } @@ -218,51 +201,41 @@ void putAndStartDFAAndWaitForFinish(String config, String id) throws Exception { } void createPipeline(String id, String modelType, String modelId) throws Exception { - String body = "" - + "{\n" - + " \"processors\": [\n" - + " {\n" - + " \"inference\": {\n" - + " \"model_id\": \"" - + modelId - + "\",\n" - + " \"inference_config\": {\"" - + modelType - + "\": {}},\n" - + " \"field_map\": {}\n" - + " }\n" - + " }\n" - + " ]\n" - + "}"; + String body = """ + { + "processors": [ + { + "inference": { + "model_id": "%s", + "inference_config": { + "%s": {} + }, + "field_map": {} + } + } + ] + }""".formatted(modelId, modelType); Request putRequest = new Request("PUT", "_ingest/pipeline/" + id); putRequest.setJsonEntity(body); client().performRequest(putRequest); } void createIndex(String index) throws IOException { - String mapping = "" - + " \"properties\": {\n" - + " \"" - + BOOLEAN_FIELD - + "\": {\n" - + " \"type\": \"boolean\"\n" - + " }," - + " \"" - + NUMERICAL_FIELD - + "\": {\n" - + " \"type\": \"double\"\n" - + " }," - + " \"" - + DISCRETE_NUMERICAL_FIELD - + "\": {\n" - + " \"type\": \"integer\"\n" - + " }," - + " \"" - + KEYWORD_FIELD - + "\": {\n" - + " \"type\": \"keyword\"\n" - + " }" - + " }"; + String mapping = """ + "properties": { + "%s": { + "type": "boolean" + }, + "%s": { + "type": "double" + }, + "%s": { + "type": "integer" + }, + "%s": { + "type": "keyword" + } + }""".formatted(BOOLEAN_FIELD, NUMERICAL_FIELD, DISCRETE_NUMERICAL_FIELD, KEYWORD_FIELD); createIndex(index, Settings.EMPTY, mapping); } @@ -272,7 +245,9 @@ void indexData(String sourceIndex, int numTrainingRows) throws IOException { bulkRequests.add( String.format( Locale.ROOT, - "{\"index\":{}}\n{\"%s\":%s,\"%s\":%f,\"%s\":%d,\"%s\":\"%s\"}", + """ + {"index":{}} + {"%s":%s,"%s":%f,"%s":%s,"%s":"%s"}""", BOOLEAN_FIELD, BOOLEAN_FIELD_VALUES.get(i % BOOLEAN_FIELD_VALUES.size()), NUMERICAL_FIELD, diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java index d9d1395233416..68c4e57929852 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RollupDateHistoUpgradeIT.java @@ -59,7 +59,8 @@ public void testDateHistoIntervalUpgrade() throws Exception { OffsetDateTime timestamp = Instant.parse("2018-01-01T00:00:01.000Z").atOffset(ZoneOffset.UTC); if (CLUSTER_TYPE == ClusterType.OLD) { - String recoverQuickly = "{\"settings\": {\"index.unassigned.node_left.delayed_timeout\": \"100ms\"}}"; + String recoverQuickly = """ + {"settings": {"index.unassigned.node_left.delayed_timeout": "100ms"}}"""; Request createTargetIndex = new Request("PUT", "/target"); createTargetIndex.setJsonEntity(recoverQuickly); @@ -71,30 +72,32 @@ public void testDateHistoIntervalUpgrade() throws Exception { // create the rollup job with an old interval style final Request createRollupJobRequest = new Request("PUT", "_rollup/job/rollup-id-test"); - createRollupJobRequest.setJsonEntity( - "{" - + "\"index_pattern\":\"target\"," - + "\"rollup_index\":\"rollup\"," - + "\"cron\":\"*/1 * * * * ?\"," - + "\"page_size\":100," - + "\"groups\":{" - + " \"date_histogram\":{" - + " \"field\":\"timestamp\"," - + " \"interval\":\"5m\"" - + " }," - + "\"histogram\":{" - + " \"fields\": [\"value\"]," - + " \"interval\":1" - + " }," - + "\"terms\":{" - + " \"fields\": [\"value\"]" - + " }" - + "}," - + "\"metrics\":[" - + " {\"field\":\"value\",\"metrics\":[\"min\",\"max\",\"sum\"]}" - + "]" - + "}" - ); + createRollupJobRequest.setJsonEntity(""" + { + "index_pattern": "target", + "rollup_index": "rollup", + "cron": "*/1 * * * * ?", + "page_size": 100, + "groups": { + "date_histogram": { + "field": "timestamp", + "interval": "5m" + }, + "histogram": { + "fields": [ "value" ], + "interval": 1 + }, + "terms": { + "fields": [ "value" ] + } + }, + "metrics": [ + { + "field": "value", + "metrics": [ "min", "max", "sum" ] + } + ] + }"""); Map createRollupJobResponse = entityAsMap(client().performRequest(createRollupJobRequest)); assertThat(createRollupJobResponse.get("acknowledged"), equalTo(Boolean.TRUE)); diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java index 00acf076c7e46..7477d72adb665 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/SearchableSnapshotsRollingUpgradeIT.java @@ -394,18 +394,12 @@ private static void mountSnapshot( } else { assertThat("Parameter 'storage' was introduced in 7.12.0 with " + Storage.SHARED_CACHE, storage, equalTo(Storage.FULL_COPY)); } - request.setJsonEntity( - "{" - + " \"index\": \"" - + indexName - + "\"," - + " \"renamed_index\": \"" - + renamedIndex - + "\"," - + " \"index_settings\": " - + Strings.toString(indexSettings) - + "}" - ); + request.setJsonEntity(""" + { + "index": "%s", + "renamed_index": "%s", + "index_settings": %s + }""".formatted(indexName, renamedIndex, Strings.toString(indexSettings))); final Response response = client().performRequest(request); assertThat( "Failed to mount snapshot [" + snapshotName + "] from repository [" + repositoryName + "]: " + response, diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java index 7c2c1ac0f7dbe..c4ae1a47910e7 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TokenBackwardsCompatibilityIT.java @@ -278,19 +278,21 @@ private void assertAccessTokenDoesNotWork(String token) throws IOException { ResponseException e = expectThrows(ResponseException.class, () -> client.performRequest(request)); assertEquals(401, e.getResponse().getStatusLine().getStatusCode()); Response response = e.getResponse(); - assertEquals( - "Bearer realm=\"security\", error=\"invalid_token\", error_description=\"The access token expired\"", - response.getHeader("WWW-Authenticate") - ); + assertEquals(""" + Bearer realm="security", error="invalid_token", error_description="The access token expired"\ + """, response.getHeader("WWW-Authenticate")); } } private void assertRefreshTokenInvalidated(String refreshToken) throws IOException { for (RestClient client : twoClients) { Request refreshTokenRequest = new Request("POST", "/_security/oauth2/token"); - refreshTokenRequest.setJsonEntity( - "{\n" + " \"refresh_token\": \"" + refreshToken + "\",\n" + " \"grant_type\": \"refresh_token\"\n" + "}" - ); + refreshTokenRequest.setJsonEntity(""" + { + "refresh_token": "%s", + "grant_type": "refresh_token" + } + """.formatted(refreshToken)); ResponseException e = expectThrows(ResponseException.class, () -> client.performRequest(refreshTokenRequest)); assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); Response response = e.getResponse(); @@ -322,17 +324,12 @@ private Map getRestClientByVersion() throws IOException { private Map createTokens(RestClient client, String username, String password) throws IOException { final Request createTokenRequest = new Request("POST", "/_security/oauth2/token"); - createTokenRequest.setJsonEntity( - "{\n" - + " \"username\": \"" - + username - + "\",\n" - + " \"password\": \"" - + password - + "\",\n" - + " \"grant_type\": \"password\"\n" - + "}" - ); + createTokenRequest.setJsonEntity(""" + { + "username": "%s", + "password": "%s", + "grant_type": "password" + }""".formatted(username, password)); Response response = client().performRequest(createTokenRequest); assertOK(response); return entityAsMap(response); @@ -340,9 +337,11 @@ private Map createTokens(RestClient client, String username, Str private void storeTokens(RestClient client, int idx, String accessToken, String refreshToken) throws IOException { final Request indexRequest = new Request("PUT", "token_backwards_compatibility_it/_doc/old_cluster_token" + idx); - indexRequest.setJsonEntity( - "{\n" + " \"token\": \"" + accessToken + "\",\n" + " \"refresh_token\": \"" + refreshToken + "\"\n" + "}" - ); + indexRequest.setJsonEntity(""" + { + "token": "%s", + "refresh_token": "%s" + }""".formatted(accessToken, refreshToken)); Response indexResponse1 = client.performRequest(indexRequest); assertOK(indexResponse1); } @@ -357,9 +356,11 @@ private Map retrieveStoredTokens(RestClient client, int tokenIdx private Map refreshToken(RestClient client, String refreshToken) throws IOException { final Request refreshTokenRequest = new Request("POST", "/_security/oauth2/token"); - refreshTokenRequest.setJsonEntity( - "{\n" + " \"refresh_token\": \"" + refreshToken + "\",\n" + " \"grant_type\": \"refresh_token\"\n" + "}" - ); + refreshTokenRequest.setJsonEntity(""" + { + "refresh_token": "%s", + "grant_type": "refresh_token" + }""".formatted(refreshToken)); Response refreshResponse = client.performRequest(refreshTokenRequest); assertOK(refreshResponse); return entityAsMap(refreshResponse); diff --git a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java index 1ac52020e0501..aa042c3059657 100644 --- a/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java +++ b/x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/TransformSurvivesUpgradeIT.java @@ -87,13 +87,14 @@ protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOE */ public void testTransformRollingUpgrade() throws Exception { Request adjustLoggingLevels = new Request("PUT", "/_cluster/settings"); - adjustLoggingLevels.setJsonEntity( - "{\"persistent\": {" - + "\"logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer\": \"trace\"," - + "\"logger.org.elasticsearch.xpack.dataframe\": \"trace\"," - + "\"logger.org.elasticsearch.xpack.transform\": \"trace\"" - + "}}" - ); + adjustLoggingLevels.setJsonEntity(""" + { + "persistent": { + "logger.org.elasticsearch.xpack.core.indexing.AsyncTwoPhaseIndexer": "trace", + "logger.org.elasticsearch.xpack.dataframe": "trace", + "logger.org.elasticsearch.xpack.transform": "trace" + } + }"""); client().performRequest(adjustLoggingLevels); Request waitForYellow = new Request("GET", "/_cluster/health"); waitForYellow.addParameter("wait_for_nodes", "3"); @@ -243,28 +244,20 @@ private void awaitWrittenIndexerState(String id, Consumer> responseAss TRANSFORM_INTERNAL_INDEX_PREFIX + "*," + TRANSFORM_INTERNAL_INDEX_PREFIX_DEPRECATED + "*" + "/_search" ); - getStatsDocsRequest.setJsonEntity( - "{\n" - + " \"query\": {\n" - + " \"bool\": {\n" - + " \"filter\": \n" - + " {\"term\": {\n" - + " \"_id\": \"data_frame_transform_state_and_stats-" - + id - + "\"\n" - + " }}\n" - + " }\n" - + " },\n" - + " \"sort\": [\n" - + " {\n" - + " \"_index\": {\n" - + " \"order\": \"desc\"\n" - + " }\n" - + " }\n" - + " ],\n" - + " \"size\": 1\n" - + "}" - ); + getStatsDocsRequest.setJsonEntity(""" + { + "query": { + "bool": { + "filter": { + "term": { + "_id": "data_frame_transform_state_and_stats-%s" + } + } + } + }, + "sort": [ { "_index": { "order": "desc" } } ], + "size": 1 + }""".formatted(id)); assertBusy(() -> { // Want to make sure we get the latest docs client().performRequest(new Request("POST", TRANSFORM_INTERNAL_INDEX_PREFIX + "*/_refresh")); @@ -374,14 +367,10 @@ private void putData(String indexName, int numDocs, TimeValue fromTime, List roleMappings; diff --git a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java index 2d3a5656ea50a..c4474dab45a05 100644 --- a/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java +++ b/x-pack/qa/third-party/active-directory/src/test/java/org/elasticsearch/xpack/security/authc/ldap/MultiGroupMappingIT.java @@ -18,27 +18,30 @@ public class MultiGroupMappingIT extends AbstractAdLdapRealmTestCase { @BeforeClass public static void setRoleMappingType() { - final String extraContent = "MarvelCharacters:\n" - + " - \"CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" - + " - \"CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" - + " - \"CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" - + " - \"CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com\"\n" - + " - \"cn=SHIELD,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" - + " - \"cn=Avengers,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" - + " - \"cn=Gods,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n" - + " - \"cn=Philanthropists,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com\"\n"; + final String extraContent = """ + MarvelCharacters: + - "CN=SHIELD,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + - "CN=Avengers,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + - "CN=Gods,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + - "CN=Philanthropists,CN=Users,DC=ad,DC=test,DC=elasticsearch,DC=com" + - "cn=SHIELD,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com" + - "cn=Avengers,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com" + - "cn=Gods,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com" + - "cn=Philanthropists,ou=people,dc=oldap,dc=test,dc=elasticsearch,dc=com" + """; roleMappings = CollectionUtils.appendToCopy(roleMappings, new RoleMappingEntry(extraContent, null)); } @Override protected String configRoles() { - return super.configRoles() - + "\n" - + "MarvelCharacters:\n" - + " cluster: [ NONE ]\n" - + " indices:\n" - + " - names: 'marvel_comics'\n" - + " privileges: [ all ]\n"; + return """ + %s + MarvelCharacters: + cluster: [ NONE ] + indices: + - names: 'marvel_comics' + privileges: [ all ] + """.formatted(super.configRoles()); } public void testGroupMapping() throws IOException {