JsonConverter implements Converter { @Override public String convert(final String json, final String rootName) { String xml; try { JSONObject jsonObject = new JSONObject(json); xml = buildXMLFrom(XML.toString(jsonObject), rootName); } catch (JSONException e) { logger.debug("Couldn't convert to XML the following JSON: " + json, e); xml = ""; } return xml; }  @Override String convert(final String json, final String rootName);  }
@Test public void testGetXmlFormWithInvalidJsonShouldReturnEmptyString() { String xml = underTest.convert("content", "rootElementName"); assertEquals(xml, ""); }
@Test public void testGetXmlFormWithValidJsonShouldReturnXml() { String xml = underTest.convert("{\"field1\":\"value1\",\"field2\":\"value2\"}", "rootElementName"); String expectedResult1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rootElementName><field2>value2</field2><field1>value1</field1></rootElementName>"; String expectedResult2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rootElementName><field1>value1</field1><field2>value2</field2></rootElementName>"; assertTrue(xml.equals(expectedResult1) || xml.equals(expectedResult2), "Convert from JSON to XML failed, got answer:" + xml); }