JsonPathMappingDataDictionary extends AbstractJsonDataDictionary { @Override protected void processMessage(Message message, TestContext context) { if (message.getPayload() == null || !StringUtils.hasText(message.getPayload(String.class))) { return; } JsonPathMessageProcessor delegateProcessor = new JsonPathMessageProcessor.Builder() .ignoreNotFound(true) .expressions(mappings.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, (entry) -> (Object) entry.getValue()))) .build(); delegateProcessor.processMessage(message, context); }  @Override T translate(String jsonPath, T value, TestContext context); @Override void afterPropertiesSet();  }
@Test public void testTranslateExactMatchStrategy() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\", \"OtherNumber\": 10}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.Something.Else", "NotFound"); mappings.put("$.TestMessage.Text", "Hello!"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":\"No changes\",\"OtherNumber\":10}}"); }
@Test public void testTranslateMultipleNodes() { Message message = new DefaultMessage("[" + "{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\", \"OtherNumber\": 10}}, " + "{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\", \"OtherNumber\": 10}}" + "]"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.Something.Else", "NotFound"); mappings.put("$..Text", "Hello!"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "[" + "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":\"No changes\",\"OtherNumber\":10}}," + "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":\"No changes\",\"OtherNumber\":10}}" + "]"); }
@Test public void testTranslateWithVariables() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\"}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.TestMessage.Text", "${helloText}"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); context.setVariable("helloText", "Hello!"); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":\"No changes\"}}"); }
@Test public void testTranslateWithArrays() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":[\"Hello World!\",\"Hello Galaxy!\"],\"OtherText\":\"No changes\"}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.TestMessage.Text[0]", "Hello!"); mappings.put("$.TestMessage.Text[1]", "Hello Universe!"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":[\"Hello!\",\"Hello Universe!\"],\"OtherText\":\"No changes\"}}"); }
@Test public void testTranslateWithArraysAndObjects() { Message message = new DefaultMessage("{\"TestMessage\":{\"Greetings\":[{\"Text\":\"Hello World!\"},{\"Text\":\"Hello Galaxy!\"}],\"OtherText\":\"No changes\"}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.TestMessage.Greetings[0].Text", "Hello!"); mappings.put("$.TestMessage.Greetings[1].Text", "Hello Universe!"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Greetings\":[{\"Text\":\"Hello!\"},{\"Text\":\"Hello Universe!\"}],\"OtherText\":\"No changes\"}}"); }
@Test public void testTranslateWithNullValues() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":null,\"OtherText\":null}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.TestMessage.Text", "Hello!"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":null}}"); }
@Test public void testTranslateWithNumberValues() { Message message = new DefaultMessage("{\"TestMessage\":{\"Number\":0,\"OtherNumber\":100}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.TestMessage.Number", "99"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Number\":99,\"OtherNumber\":100}}"); }
@Test public void testTranslateNoResult() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\"}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("$.Something.Else", "NotFound"); JsonPathMappingDataDictionary dictionary = new JsonPathMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\"}}"); }