JsonPathMessageProcessor extends AbstractMessageProcessor { @Override public void processMessage(Message message, TestContext context) { if (message.getPayload() == null || !StringUtils.hasText(message.getPayload(String.class))) { return; } String jsonPathExpression; try { JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE); Object jsonData = parser.parse(message.getPayload(String.class)); DocumentContext documentContext = JsonPath.parse(jsonData); for (Map.Entry<String, Object> entry : jsonPathExpressions.entrySet()) { jsonPathExpression = entry.getKey(); String valueExpression = context.replaceDynamicContentInString(entry.getValue().toString()); Object value; if (valueExpression.equals("true")) { value = true; } else if (valueExpression.equals("false")) { value = false; } else { try { value = NumberUtils.parseNumber(valueExpression, Integer.class); } catch (IllegalArgumentException e) { value = valueExpression; } } try { documentContext.set(jsonPathExpression, value); } catch (PathNotFoundException e) { if (!ignoreNotFound) { throw new UnknownElementException(String.format("Could not find element for expression: %s", jsonPathExpression), e); } } if (log.isDebugEnabled()) { log.debug("Element " + jsonPathExpression + " was set to value: " + valueExpression); } } message.setPayload(jsonData.toString()); } catch (ParseException e) { throw new CitrusRuntimeException("Failed to parse JSON text", e); } } JsonPathMessageProcessor(); private  JsonPathMessageProcessor(Builder builder); @Override void processMessage(Message message, TestContext context); @Override boolean supportsMessageType(String messageType); Map<String, Object> getJsonPathExpressions(); boolean isIgnoreNotFound();  }
@Test public void testConstructWithJsonPath() { Message message = new DefaultMessage("{ \"TestMessage\": { \"Text\": \"Hello World!\" }}"); Map<String, Object> jsonPathExpressions = new HashMap<>(); jsonPathExpressions.put("$.TestMessage.Text", "Hello!"); JsonPathMessageProcessor processor = new JsonPathMessageProcessor.Builder() .expressions(jsonPathExpressions) .build(); processor.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\"}}"); }
@Test public void testConstructWithJsonPathMultipleValues() { Message message = new DefaultMessage("{ \"TestMessage\": { \"Text\": \"Hello World!\", \"Id\": 1234567}}"); Map<String, Object> jsonPathExpressions = new HashMap<>(); jsonPathExpressions.put("$.TestMessage.Text", "Hello!"); jsonPathExpressions.put("$.TestMessage.Id", "9999999"); JsonPathMessageProcessor processor = new JsonPathMessageProcessor.Builder() .expressions(jsonPathExpressions) .build(); processor.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"Id\":9999999}}"); }
@Test public void testConstructWithJsonPathWithArrays() { Message message = new DefaultMessage("{ \"TestMessage\": [{ \"Text\": \"Hello World!\" }, { \"Text\": \"Another Hello World!\" }]}"); Map<String, Object> jsonPathExpressions = new HashMap<>(); jsonPathExpressions.put("$..Text", "Hello!"); JsonPathMessageProcessor processor = new JsonPathMessageProcessor.Builder() .expressions(jsonPathExpressions) .build(); processor.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":[{\"Text\":\"Hello!\"},{\"Text\":\"Hello!\"}]}"); }
@Test public void testConstructWithJsonPathNoResult() { Message message = new DefaultMessage("{ \"TestMessage\": { \"Text\": \"Hello World!\" }}"); Map<String, Object> jsonPathExpressions = new HashMap<>(); jsonPathExpressions.put("$.TestMessage.Unknown", "Hello!"); JsonPathMessageProcessor processor = new JsonPathMessageProcessor.Builder() .expressions(jsonPathExpressions) .build(); processor.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello World!\"}}"); }