1515import org .elasticsearch .search .SearchModule ;
1616import org .elasticsearch .test .ESTestCase ;
1717import org .elasticsearch .xcontent .ToXContent ;
18+ import org .elasticsearch .xcontent .XContentBuilder ;
19+ import org .elasticsearch .xcontent .XContentFactory ;
1820import org .elasticsearch .xcontent .XContentParser ;
1921import org .elasticsearch .xcontent .XContentType ;
2022import org .elasticsearch .xpack .application .EnterpriseSearchModuleTestUtils ;
2123import org .elasticsearch .xpack .searchbusinessrules .SpecifiedDocument ;
2224import org .junit .Before ;
23- import org .elasticsearch .xcontent .XContentBuilder ;
24- import org .elasticsearch .xcontent .XContentFactory ;
2525
2626import java .io .IOException ;
2727import java .util .Collections ;
3232import static org .elasticsearch .common .xcontent .XContentHelper .toXContent ;
3333import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertToXContentEquivalent ;
3434import static org .elasticsearch .xpack .application .rules .QueryRuleCriteriaType .EXACT ;
35+ import static org .elasticsearch .xpack .application .rules .QueryRuleCriteriaType .LTE ;
3536import static org .elasticsearch .xpack .application .rules .QueryRuleCriteriaType .PREFIX ;
3637import static org .elasticsearch .xpack .application .rules .QueryRuleCriteriaType .SUFFIX ;
37- import static org .elasticsearch .xpack .application .rules .QueryRuleCriteriaType .LTE ;
3838import static org .hamcrest .CoreMatchers .equalTo ;
3939
4040public class QueryRuleTests extends ESTestCase {
@@ -58,16 +58,16 @@ public final void testRandomSerialization() throws IOException {
5858
5959 public void testNumericValidationWithValidValues () throws IOException {
6060 String content = XContentHelper .stripWhitespace ("""
61- {
62- "rule_id": "numeric_rule",
63- "type": "pinned",
64- "criteria": [
65- { "type": "lte", "metadata": "price", "values": ["100.50", "200"] }
66- ],
67- "actions": {
68- "ids": ["id1"]
69- }
70- }""" );
61+ {
62+ "rule_id": "numeric_rule",
63+ "type": "pinned",
64+ "criteria": [
65+ { "type": "lte", "metadata": "price", "values": ["100.50", "200"] }
66+ ],
67+ "actions": {
68+ "ids": ["id1"]
69+ }
70+ }""" );
7171 QueryRule queryRule = QueryRule .fromXContentBytes (new BytesArray (content ), XContentType .JSON );
7272 boolean humanReadable = true ;
7373 BytesReference originalBytes = toShuffledXContent (queryRule , XContentType .JSON , ToXContent .EMPTY_PARAMS , humanReadable );
@@ -80,46 +80,46 @@ public void testNumericValidationWithValidValues() throws IOException {
8080
8181 public void testNumericValidationWithInvalidValues () throws IOException {
8282 String content = XContentHelper .stripWhitespace ("""
83- {
84- "rule_id": "numeric_rule",
85- "type": "pinned",
86- "criteria": [
87- { "type": "lte", "metadata": "price", "values": ["abc"] }
88- ],
89- "actions": {
90- "ids": ["id1"]
91- }
92- }""" );
83+ {
84+ "rule_id": "numeric_rule",
85+ "type": "pinned",
86+ "criteria": [
87+ { "type": "lte", "metadata": "price", "values": ["abc"] }
88+ ],
89+ "actions": {
90+ "ids": ["id1"]
91+ }
92+ }""" );
9393 expectThrows (IllegalArgumentException .class , () -> QueryRule .fromXContentBytes (new BytesArray (content ), XContentType .JSON ));
9494 }
9595
9696 public void testNumericValidationWithMixedValues () throws IOException {
9797 String content = XContentHelper .stripWhitespace ("""
98- {
99- "rule_id": "numeric_rule",
100- "type": "pinned",
101- "criteria": [
102- { "type": "lte", "metadata": "price", "values": ["100", "abc", "200"] }
103- ],
104- "actions": {
105- "ids": ["id1"]
106- }
107- }""" );
98+ {
99+ "rule_id": "numeric_rule",
100+ "type": "pinned",
101+ "criteria": [
102+ { "type": "lte", "metadata": "price", "values": ["100", "abc", "200"] }
103+ ],
104+ "actions": {
105+ "ids": ["id1"]
106+ }
107+ }""" );
108108 expectThrows (IllegalArgumentException .class , () -> QueryRule .fromXContentBytes (new BytesArray (content ), XContentType .JSON ));
109109 }
110110
111111 public void testNumericValidationWithEmptyValues () throws IOException {
112112 String content = XContentHelper .stripWhitespace ("""
113- {
114- "rule_id": "numeric_rule",
115- "type": "pinned",
116- "criteria": [
117- { "type": "lte", "metadata": "price", "values": [] }
118- ],
119- "actions": {
120- "ids": ["id1"]
121- }
122- }""" );
113+ {
114+ "rule_id": "numeric_rule",
115+ "type": "pinned",
116+ "criteria": [
117+ { "type": "lte", "metadata": "price", "values": [] }
118+ ],
119+ "actions": {
120+ "ids": ["id1"]
121+ }
122+ }""" );
123123 expectThrows (IllegalArgumentException .class , () -> QueryRule .fromXContentBytes (new BytesArray (content ), XContentType .JSON ));
124124 }
125125
@@ -420,13 +420,7 @@ public void testValidateNumericComparisonRule() {
420420 QueryRuleCriteria invalidCriteria = new QueryRuleCriteria (LTE , "price" , List .of ("not_a_number" ));
421421 IllegalArgumentException e = expectThrows (
422422 IllegalArgumentException .class ,
423- () -> new QueryRule (
424- "test_rule" ,
425- QueryRule .QueryRuleType .PINNED ,
426- List .of (invalidCriteria ),
427- Map .of ("ids" , List .of ("1" )),
428- null
429- )
423+ () -> new QueryRule ("test_rule" , QueryRule .QueryRuleType .PINNED , List .of (invalidCriteria ), Map .of ("ids" , List .of ("1" )), null )
430424 );
431425
432426 String expectedMessage = String .format (
@@ -475,20 +469,14 @@ public void testParseNumericComparisonRule() throws IOException {
475469
476470 public void testIsRuleMatchWithNumericComparison () {
477471 QueryRuleCriteria criteria = new QueryRuleCriteria (LTE , "price" , List .of ("100" ));
478- QueryRule rule = new QueryRule (
479- "test_rule" ,
480- QueryRule .QueryRuleType .PINNED ,
481- List .of (criteria ),
482- Map .of ("ids" , List .of ("1" )),
483- null
484- );
472+ QueryRule rule = new QueryRule ("test_rule" , QueryRule .QueryRuleType .PINNED , List .of (criteria ), Map .of ("ids" , List .of ("1" )), null );
485473
486474 // Test matching value
487475 assertTrue (rule .isRuleMatch (Map .of ("price" , "50" )));
488-
476+
489477 // Test non-matching value
490478 assertFalse (rule .isRuleMatch (Map .of ("price" , "150" )));
491-
479+
492480 // Test with non-numeric value
493481 assertFalse (rule .isRuleMatch (Map .of ("price" , "not_a_number" )));
494482 }
0 commit comments