JsonExpressionEvaluator { public static String evaluate(String jsonText, JsonEvaluationSpecification specification) throws JsonMappingException { JsonNode rootNode; try { ObjectMapper mapper = new ObjectMapper(); rootNode = mapper.readTree(new StringReader(jsonText)); } catch (Exception e) { log.error("Exception while parsing Json text", e); throw new JsonMappingException("Exception while parsing Json payload"); } JsonNode evaluatedNode = rootNode.at(specification.getExp()); String caseKey = evaluatedNode.asText(); switch (specification.getOperator()) { case equals: String value = specification.getCases().get(caseKey); return (value != null ? value : specification.getCases().getDefault()); case range: double caseNumber = 0.000; try { caseNumber = Double.parseDouble(caseKey); } catch (NumberFormatException nfe) { log.error(caseKey + " into range expression cannot be parsed as number. Considering default case."); return specification.getCases().getDefault(); } return foundRangeMatchingCase(caseNumber, specification.getCases()); case regexp: for (String choiceKey : specification.getCases().keySet()) { if (!"default".equals(choiceKey)) { if (Pattern.matches(choiceKey, caseKey)) { return specification.getCases().get(choiceKey); } } } break; case size: if (evaluatedNode.isArray()) { int size = evaluatedNode.size(); return foundRangeMatchingCase(size, specification.getCases()); } break; case presence: if (caseKey != null && caseKey.length() > 0) { if (specification.getCases().containsKey("found")) { return specification.getCases().get("found"); } } else { if (specification.getCases().containsKey("missing")) { return specification.getCases().get("missing"); } } break; } return specification.getCases().getDefault(); }  static String evaluate(String jsonText, JsonEvaluationSpecification specification);  }
@Test public void testPresenceOperatorDispatcher() throws Exception { DispatchCases cases = new DispatchCases(); Map<String, String> dispatchCases = new HashMap<>(); dispatchCases.put("found", "Extra"); dispatchCases.put("missing", "Normal"); dispatchCases.put("default", "Basic"); cases.putAll(dispatchCases); JsonEvaluationSpecification specifications = new JsonEvaluationSpecification(); specifications.setExp("/extra"); specifications.setOperator(EvaluationOperator.presence); specifications.setCases(cases); String result = JsonExpressionEvaluator.evaluate(BELGIUM_BEER, specifications); assertEquals("Normal", result); result = JsonExpressionEvaluator.evaluate(GERMAN_BEER, specifications); assertEquals("Extra", result); result = JsonExpressionEvaluator.evaluate(ENGLISH_BEER, specifications); assertEquals("Normal", result); }
@Test public void testEqualsOperatorDispatcher() throws Exception { DispatchCases cases = new DispatchCases(); Map<String, String> dispatchCases = new HashMap<>(); dispatchCases.put("Belgium", "OK"); dispatchCases.put("Germany", "KO"); dispatchCases.put("default", "Why not?"); cases.putAll(dispatchCases); JsonEvaluationSpecification specifications = new JsonEvaluationSpecification(); specifications.setExp("/country"); specifications.setOperator(EvaluationOperator.equals); specifications.setCases(cases); String result = JsonExpressionEvaluator.evaluate(BELGIUM_BEER, specifications); assertEquals("OK", result); result = JsonExpressionEvaluator.evaluate(GERMAN_BEER, specifications); assertEquals("KO", result); result = JsonExpressionEvaluator.evaluate(ENGLISH_BEER, specifications); assertEquals("Why not?", result); }
@Test public void testRangeOperatorDispatcher() throws Exception { DispatchCases cases = new DispatchCases(); Map<String, String> dispatchCases = new HashMap<>(); dispatchCases.put("[4.2;5.0]", "Acceptable"); dispatchCases.put("[0;4[", "Too low"); dispatchCases.put("default", "Why not?"); cases.putAll(dispatchCases); JsonEvaluationSpecification specifications = new JsonEvaluationSpecification(); specifications.setExp("/rating"); specifications.setOperator(EvaluationOperator.range); specifications.setCases(cases); String result = JsonExpressionEvaluator.evaluate(BELGIUM_BEER, specifications); assertEquals("Acceptable", result); result = JsonExpressionEvaluator.evaluate(GERMAN_BEER, specifications); assertEquals("Too low", result); result = JsonExpressionEvaluator.evaluate(ENGLISH_BEER, specifications); assertEquals("Why not?", result); }
@Test public void testRegexpOperatorDispatcher() throws Exception { DispatchCases cases = new DispatchCases(); Map<String, String> dispatchCases = new HashMap<>(); dispatchCases.put(".*[Aa][Ll][Ee].*", "OK"); dispatchCases.put("default", "Bad"); cases.putAll(dispatchCases); JsonEvaluationSpecification specifications = new JsonEvaluationSpecification(); specifications.setExp("/type"); specifications.setOperator(EvaluationOperator.regexp); specifications.setCases(cases); String result = JsonExpressionEvaluator.evaluate(BELGIUM_BEER, specifications); assertEquals("OK", result); result = JsonExpressionEvaluator.evaluate(GERMAN_BEER, specifications); assertEquals("Bad", result); result = JsonExpressionEvaluator.evaluate(ENGLISH_BEER, specifications); assertEquals("Bad", result); }
@Test public void testSizeOperatorDispatcher() throws Exception { DispatchCases cases = new DispatchCases(); Map<String, String> dispatchCases = new HashMap<>(); dispatchCases.put("[0;2]", "Standard"); dispatchCases.put("[3;5]", "Lot of"); dispatchCases.put("default", "Huge"); cases.putAll(dispatchCases); JsonEvaluationSpecification specifications = new JsonEvaluationSpecification(); specifications.setExp("/cars"); specifications.setOperator(EvaluationOperator.size); specifications.setCases(cases); String result = JsonExpressionEvaluator.evaluate(LAURENT_CARS, specifications); assertEquals("Standard", result); result = JsonExpressionEvaluator.evaluate(LOT_OF_CARS, specifications); assertEquals("Lot of", result); result = JsonExpressionEvaluator.evaluate(HUGE_LIST_OF_CARS, specifications); assertEquals("Huge", result); }