Skip to content

Commit

Permalink
DROOLS-7514 strict JSON parsing with Jackson (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarilabs authored Jul 28, 2023
1 parent c447bf8 commit 18b42a0
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,10 +16,6 @@ public class JsonMapper {
private static final TypeReference<List<Map<String, Object>>> LIST_OF_MAP_OF_STRING_AND_OBJECT = new TypeReference<List<Map<String, Object>>>(){};
private static final TypeReference<Map<String, Object>> MAP_OF_STRING_AND_OBJECT = new TypeReference<Map<String, Object>>(){};
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class ArrayAccessTest {
public void testArrayAccess() {
RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON1);

List<Match> matchedRules = rulesExecutor.processFacts( "{'host': 'A', 'os': {'array': ['abc']}}" ).join();
List<Match> 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() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class MultipleConditionTest {
public void testReadJson() {
RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(JSON1);

List<Match> matchedRules = rulesExecutor.processEvents( "{ events: [ { \"i\":0 }, { \"i\":1 }, { \"i\":2 } ] }" ).join();
List<Match> matchedRules = rulesExecutor.processEvents( "{ \"events\": [ { \"i\":0 }, { \"i\":1 }, { \"i\":2 } ] }" ).join();

assertEquals( 1, matchedRules.size() );
assertEquals( "r_0", matchedRules.get(0).getRule().getName() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void test() {

RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(json);

List<Match> matchedRules = rulesExecutor.processFacts( "{ \"x\":1, y:null }" ).join();
List<Match> matchedRules = rulesExecutor.processFacts( "{ \"x\":1, \"y\":null }" ).join();
assertEquals( 1, matchedRules.size() );

matchedRules = rulesExecutor.processFacts( "{ \"x\":1 }" ).join();
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testWithSelectAttr() {

RulesExecutor rulesExecutor = RulesExecutorFactory.createFromJson(json);

List<Match> matchedRules = rulesExecutor.processFacts( "{ \"x\":1, y:null }" ).join();
List<Match> matchedRules = rulesExecutor.processFacts( "{ \"x\":1, \"y\":null }" ).join();
assertEquals( 1, matchedRules.size() );
assertEquals( "r1", matchedRules.get(0).getRule().getName() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 18b42a0

Please sign in to comment.