From 5d6f879d02b43d0c80125f55255bfdf3d5fea602 Mon Sep 17 00:00:00 2001 From: Mridula Date: Wed, 12 Mar 2025 17:33:03 +0000 Subject: [PATCH] Removed logger and also fixed the nitpick comments (#124650) (cherry picked from commit 44a3ac444fed720314b179e75421b2d04e9178fc) # Conflicts: # x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/QueryRuleTests.java --- .../application/rules/QueryRuleTests.java | 78 ++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/QueryRuleTests.java b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/QueryRuleTests.java index 67e7f6ac7d9e9..632a25fa8238c 100644 --- a/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/QueryRuleTests.java +++ b/x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/rules/QueryRuleTests.java @@ -53,6 +53,78 @@ public final void testRandomSerialization() throws IOException { } } + public void testNumericValidationWithValidValues() throws IOException { + String content = XContentHelper.stripWhitespace(""" + { + "rule_id": "numeric_rule", + "type": "pinned", + "criteria": [ + { "type": "lte", "metadata": "price", "values": ["100.50", "200"] } + ], + "actions": { + "ids": ["id1"] + } + }"""); + testToXContentRules(content); + } + + public void testNumericValidationWithInvalidValues() throws IOException { + String content = XContentHelper.stripWhitespace(""" + { + "rule_id": "numeric_rule", + "type": "pinned", + "criteria": [ + { "type": "lte", "metadata": "price", "values": ["abc"] } + ], + "actions": { + "ids": ["id1"] + } + }"""); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> QueryRule.fromXContentBytes(new BytesArray(content), XContentType.JSON) + ); + assertThat(e.getMessage(), containsString("Failed to build [query_rule]")); + } + + public void testNumericValidationWithMixedValues() throws IOException { + String content = XContentHelper.stripWhitespace(""" + { + "rule_id": "numeric_rule", + "type": "pinned", + "criteria": [ + { "type": "lte", "metadata": "price", "values": ["100", "abc", "200"] } + ], + "actions": { + "ids": ["id1"] + } + }"""); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> QueryRule.fromXContentBytes(new BytesArray(content), XContentType.JSON) + ); + assertThat(e.getMessage(), containsString("Failed to build [query_rule]")); + } + + public void testNumericValidationWithEmptyValues() throws IOException { + String content = XContentHelper.stripWhitespace(""" + { + "rule_id": "numeric_rule", + "type": "pinned", + "criteria": [ + { "type": "lte", "metadata": "price", "values": [] } + ], + "actions": { + "ids": ["id1"] + } + }"""); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> QueryRule.fromXContentBytes(new BytesArray(content), XContentType.JSON) + ); + assertThat(e.getMessage(), containsString("failed to parse field [criteria]")); + } + public void testToXContent() throws IOException { String content = XContentHelper.stripWhitespace(""" { @@ -85,7 +157,11 @@ public void testToXContentEmptyCriteria() throws IOException { "criteria": [], "actions": {} }"""); - expectThrows(IllegalArgumentException.class, () -> QueryRule.fromXContentBytes(new BytesArray(content), XContentType.JSON)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> QueryRule.fromXContentBytes(new BytesArray(content), XContentType.JSON) + ); + assertThat(e.getMessage(), containsString("Failed to build [query_rule]")); } public void testToXContentValidPinnedRulesWithIds() throws IOException {