JsonMappingDataDictionary extends AbstractJsonDataDictionary { @Override protected void processMessage(Message message, TestContext context) { if (message.getPayload() == null || !StringUtils.hasText(message.getPayload(String.class))) { return; } JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE); try { Object json = parser.parse(message.getPayload(String.class)); if (json instanceof JSONObject) { traverseJsonData((JSONObject) json, "", context); } else if (json instanceof JSONArray) { JSONObject tempJson = new JSONObject(); tempJson.put("root", json); traverseJsonData(tempJson, "", context); } else { throw new CitrusRuntimeException("Unsupported json type " + json.getClass()); } message.setPayload(json.toString()); } catch (ParseException e) { log.warn("Data dictionary unable to parse JSON object", e); } }  @Override T translate(String jsonPath, T value, TestContext context);  }
@Test public void testTranslateExactMatchStrategy() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\", \"OtherNumber\": 10}}"); message.setType(MessageType.JSON.name()); Map<String, String> mappings = new HashMap<>(); mappings.put("Something.Else", "NotFound"); mappings.put("TestMessage.Text", "Hello!"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":\"No changes\",\"OtherNumber\":10}}"); }
@Test public void testTranslateStartsWithStrategy() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\"}}"); message.setType(MessageType.JSON.name()); Map<String, String> mappings = new HashMap<>(); mappings.put("TestMessage.Text", "Hello!"); mappings.put("TestMessage.Other", "Bye!"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.setPathMappingStrategy(DataDictionary.PathMappingStrategy.STARTS_WITH); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":\"Bye!\"}}"); }
@Test public void testTranslateEndsWithStrategy() { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\"}}"); Map<String, String> mappings = new HashMap<>(); mappings.put("Text", "Hello!"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.setPathMappingStrategy(DataDictionary.PathMappingStrategy.ENDS_WITH); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"OtherText\":\"Hello!\"}}"); }
@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}"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); 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!"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); 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!"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); 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 testTranslateFromMappingFile() throws Exception { Message message = new DefaultMessage("{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\"}}"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); dictionary.setMappingFile(new ClassPathResource("jsonmapping.properties", DataDictionary.class)); dictionary.afterPropertiesSet(); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello!\",\"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!"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); 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"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); 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"); JsonMappingDataDictionary dictionary = new JsonMappingDataDictionary(); dictionary.setMappings(mappings); dictionary.processMessage(message, context); Assert.assertEquals(message.getPayload(String.class), "{\"TestMessage\":{\"Text\":\"Hello World!\",\"OtherText\":\"No changes\"}}"); }