JsonPathPayloadMessageSelector extends AbstractMessageSelector { @Override public boolean accept(Message<?> message) { String payload = getPayloadAsString(message); if (StringUtils.hasText(payload) && !payload.trim().startsWith("{") && !payload.trim().startsWith("[")) { return false; } try { return evaluate(JsonPathUtils.evaluateAsString(payload, selectKey)); } catch (CitrusRuntimeException e) { return false; } } JsonPathPayloadMessageSelector(String expression, String control, TestContext context); @Override boolean accept(Message<?> message); static final String SELECTOR_PREFIX; }
@Test public void testJsonPathEvaluation() { JsonPathPayloadMessageSelector messageSelector = new JsonPathPayloadMessageSelector("jsonPath:$.foo.text", "foobar", context); Assert.assertTrue(messageSelector.accept(MessageBuilder.withPayload("{ \"foo\": { \"text\": \"foobar\" } }").build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("{ \"foo\": { \"text\": \"barfoo\" } }").build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("{ \"bar\": { \"text\": \"foobar\" } }").build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("This is plain text!").build())); }
@Test public void testJsonPathEvaluationValidationMatcher() { JsonPathPayloadMessageSelector messageSelector = new JsonPathPayloadMessageSelector("jsonPath:$.foo.text", "@startsWith(foo)@", context); Assert.assertTrue(messageSelector.accept(MessageBuilder.withPayload("{ \"foo\": { \"text\": \"foobar\" } }").build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("{ \"foo\": { \"text\": \"barfoo\" } }").build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("{ \"bar\": { \"text\": \"foobar\" } }").build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload("This is plain text!").build())); }
@Test public void testJsonPathEvaluationWithMessageObjectPayload() { JsonPathPayloadMessageSelector messageSelector = new JsonPathPayloadMessageSelector("jsonPath:$.foo.text", "foobar", context); Assert.assertTrue(messageSelector.accept(MessageBuilder.withPayload(new DefaultMessage("{ \"foo\": { \"text\": \"foobar\" } }")).build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload(new DefaultMessage("{ \"foo\": { \"text\": \"barfoo\" } }")).build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload(new DefaultMessage("{ \"bar\": { \"text\": \"foobar\" } }")).build())); Assert.assertFalse(messageSelector.accept(MessageBuilder.withPayload(new DefaultMessage("This is plain text!")).build())); }