From 183c47cd9273f2bdcfa3a53fa53d90ecc4936df8 Mon Sep 17 00:00:00 2001 From: tarilabs Date: Tue, 25 Jul 2023 12:55:11 +0200 Subject: [PATCH] DROOLS-7514 strict JSON parsing with Jackson --- .../ansible/rulebook/integration/api/io/JsonMapper.java | 5 ----- .../ansible/rulebook/integration/api/ArrayAccessTest.java | 4 ++-- .../rulebook/integration/api/LogicalOperatorsTest.java | 2 +- .../rulebook/integration/api/MultipleConditionTest.java | 2 +- .../drools/ansible/rulebook/integration/api/NullTest.java | 4 ++-- .../rulebook/integration/api/SimpleLogicalOperatorsTest.java | 2 +- 6 files changed, 7 insertions(+), 12 deletions(-) diff --git a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/io/JsonMapper.java b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/io/JsonMapper.java index 8002e615..45171bf8 100644 --- a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/io/JsonMapper.java +++ b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/io/JsonMapper.java @@ -6,7 +6,6 @@ import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonNode; @@ -17,10 +16,6 @@ public class JsonMapper { private static final TypeReference>> LIST_OF_MAP_OF_STRING_AND_OBJECT = new TypeReference>>(){}; private static final TypeReference> MAP_OF_STRING_AND_OBJECT = new TypeReference>(){}; private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); - static { // most tests contains non-standard JSON; eventually check assumption non-strict json with Ansible team - OBJECT_MAPPER.enable(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES.mappedFeature()); - OBJECT_MAPPER.enable(JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature()); - } private static final JavaType JACKSON_RAW_LIST = OBJECT_MAPPER.getTypeFactory().constructRawCollectionLikeType(List.class); public static String toJson(Object object) { diff --git a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/ArrayAccessTest.java b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/ArrayAccessTest.java index 0da36213..f6b5c20c 100644 --- a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/ArrayAccessTest.java +++ b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/ArrayAccessTest.java @@ -38,10 +38,10 @@ public class ArrayAccessTest { public void testArrayAccess() { RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON1); - List matchedRules = rulesExecutor.processFacts( "{'host': 'A', 'os': {'array': ['abc']}}" ).join(); + List matchedRules = rulesExecutor.processFacts( "{\"host\": \"A\", \"os\": {\"array\": [\"abc\"]}}" ).join(); assertEquals( 0, matchedRules.size() ); - matchedRules = rulesExecutor.processFacts( "{'host': 'B', 'os': {'array': ['abc', 'windows']}}" ).join(); + matchedRules = rulesExecutor.processFacts( "{\"host\": \"B\", \"os\": {\"array\": [\"abc\", \"windows\"]}}" ).join(); assertEquals( 1, matchedRules.size() ); assertEquals( "r_0", matchedRules.get(0).getRule().getName() ); diff --git a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/LogicalOperatorsTest.java b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/LogicalOperatorsTest.java index 7c783063..90ee4694 100644 --- a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/LogicalOperatorsTest.java +++ b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/LogicalOperatorsTest.java @@ -140,7 +140,7 @@ public void testProcessRules() { assertEquals( 1, matchedRules.size() ); assertEquals( "R1", matchedRules.get(0).getRule().getName() ); - matchedRules = rulesExecutor.processFacts( "{ facts: [ { \"sensu\": { \"data\": { \"i\":3 } } }, { \"j\":3 } ] }" ).join(); + matchedRules = rulesExecutor.processFacts( "{ \"facts\": [ { \"sensu\": { \"data\": { \"i\":3 } } }, { \"j\":3 } ] }" ).join(); assertEquals( 0, matchedRules.size() ); matchedRules = rulesExecutor.processFacts( "{ \"sensu\": { \"data\": { \"i\":4 } } }" ).join(); diff --git a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/MultipleConditionTest.java b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/MultipleConditionTest.java index f2b02b72..4d3ef6ba 100644 --- a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/MultipleConditionTest.java +++ b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/MultipleConditionTest.java @@ -87,7 +87,7 @@ public class MultipleConditionTest { public void testReadJson() { RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON1); - List matchedRules = rulesExecutor.processEvents( "{ events: [ { \"i\":0 }, { \"i\":1 }, { \"i\":2 } ] }" ).join(); + List matchedRules = rulesExecutor.processEvents( "{ \"events\": [ { \"i\":0 }, { \"i\":1 }, { \"i\":2 } ] }" ).join(); assertEquals( 1, matchedRules.size() ); assertEquals( "r_0", matchedRules.get(0).getRule().getName() ); diff --git a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/NullTest.java b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/NullTest.java index 53cee171..2da33f59 100644 --- a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/NullTest.java +++ b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/NullTest.java @@ -63,7 +63,7 @@ public void test() { RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(json); - List matchedRules = rulesExecutor.processFacts( "{ \"x\":1, y:null }" ).join(); + List matchedRules = rulesExecutor.processFacts( "{ \"x\":1, \"y\":null }" ).join(); assertEquals( 1, matchedRules.size() ); matchedRules = rulesExecutor.processFacts( "{ \"x\":1 }" ).join(); @@ -164,7 +164,7 @@ public void testWithSelectAttr() { RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(json); - List matchedRules = rulesExecutor.processFacts( "{ \"x\":1, y:null }" ).join(); + List matchedRules = rulesExecutor.processFacts( "{ \"x\":1, \"y\":null }" ).join(); assertEquals( 1, matchedRules.size() ); assertEquals( "r1", matchedRules.get(0).getRule().getName() ); diff --git a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SimpleLogicalOperatorsTest.java b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SimpleLogicalOperatorsTest.java index 9722c61e..1c677fb0 100644 --- a/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SimpleLogicalOperatorsTest.java +++ b/drools-ansible-rulebook-integration-api/src/test/java/org/drools/ansible/rulebook/integration/api/SimpleLogicalOperatorsTest.java @@ -92,7 +92,7 @@ public void testProcessRules() { assertEquals( 1, matchedRules.size() ); assertEquals( "R1", matchedRules.get(0).getRule().getName() ); - matchedRules = rulesExecutor.processFacts( "{ facts: [ { \"sensu\": { \"data\": { \"i\":3 } } }, { \"j\":3 } ] }" ).join(); + matchedRules = rulesExecutor.processFacts( "{ \"facts\": [ { \"sensu\": { \"data\": { \"i\":3 } } }, { \"j\":3 } ] }" ).join(); assertEquals( 0, matchedRules.size() ); matchedRules = rulesExecutor.processFacts( "{ \"sensu\": { \"data\": { \"i\":4 } } }" ).join();