XmlToJson { public ModelNode convert(Xml xml) { ModelNode fullConfiguration = new ModelNode(); fullConfiguration.setEmptyList(); for (Xml c : getChildren.apply(xml)) { String extensionId = getName.apply(c); String id = getAttributeValue.apply(c, "id"); ModelNode schema = knownExtensionSchemas.get(extensionId); if (schema == null) { LOG.warn("Extension '" + extensionId + "' doesn't declare a JSON schema but XML contains its configuration. Cannot convert it into" + " JSON and will ignore it!"); continue; } ModelNode config = convert(c, schema); ModelNode instanceConfig = new ModelNode(); instanceConfig.get("extension").set(extensionId); if (id != null) { instanceConfig.get("id").set(id); } instanceConfig.get("configuration").set(config); fullConfiguration.add(instanceConfig); } return fullConfiguration; } XmlToJson(Revapi revapi, Function<Xml, String> getName, Function<Xml, String> getValue,
                     BiFunction<Xml, String, String> getAttributeValue, Function<Xml, List<Xml>> getChildren);  XmlToJson(Map<String, ModelNode> knownExtensionSchemas, Function<Xml, String> getName,
              Function<Xml, String> getValue, BiFunction<Xml, String, String> getAttributeValue,
              Function<Xml, List<Xml>> getChildren); ModelNode convert(Xml xml);  }
@Test public void testBooleanConversion_true() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"boolean\"}"); Node xml = xml("<config><ext id='1'>true</ext></config>"); ModelNode extConf = converter.convert(xml).get(0); Assert.assertEquals("1", extConf.get("id").asString()); ModelNode config = extConf.get("configuration"); Assert.assertEquals(ModelType.BOOLEAN, config.getType()); Assert.assertTrue(config.asBoolean()); }
@Test public void testBooleanConversion_false() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"boolean\"}"); Node xml = xml("<config><ext>false</ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.BOOLEAN, config.getType()); Assert.assertFalse(config.asBoolean()); }
@Test(expected = IllegalArgumentException.class) public void testBooleanConversion_invalid() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"boolean\"}"); Node xml = xml("<config><ext>asdf</ext></config>"); converter.convert(xml); }
@Test public void testIntegerConversion_valid() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"integer\"}"); Node xml = xml("<config><ext>1</ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.LONG, config.getType()); Assert.assertEquals(1, config.asLong()); }
@Test(expected = IllegalArgumentException.class) public void testIntegerConversion_invalid() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"integer\"}"); Node xml = xml("<config><ext>some</ext></config>"); converter.convert(xml); }
@Test public void testNumberConversion_valid() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"number\"}"); Node xml = xml("<config><ext>1.2</ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.DOUBLE, config.getType()); Assert.assertEquals(1.2d, config.asDouble(), 0); }
@Test(expected = IllegalArgumentException.class) public void testNumberConversion_invalid() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"number\"}"); Node xml = xml("<config><ext>some</ext></config>"); converter.convert(xml); }
@Test public void testStringConversion() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"string\"}"); Node xml = xml("<config><ext>1.2</ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.STRING, config.getType()); Assert.assertEquals("1.2", config.asString()); }
@Test public void testArrayConversion() throws Exception { XmlToJson<Node> converter = converter("list", "{\"type\": \"array\", \"items\": {\"type\": \"integer\"}}"); Node xml = xml("<config><list><item>1</item>\n\n <item>2</item></list></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.LIST, config.getType()); Assert.assertEquals(2, config.asList().size()); Assert.assertEquals(1L, config.asList().get(0).asLong()); Assert.assertEquals(2L, config.asList().get(1).asLong()); }
@Test public void testArrayConversion_commentsAndWhitespaceIgnored() throws Exception { XmlToJson<Node> converter = converter("list", "{\"type\": \"array\", \"items\": {\"type\": \"integer\"}}"); Node xml = xml("<config><list>\n\n <!-- just whitespace -->\n\t </list></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.LIST, config.getType()); Assert.assertTrue(config.asList().isEmpty()); }
@Test public void testArrayConversion_invalid() throws Exception { try { XmlToJson<Node> converter = converter("list", "{\"type\": \"array\", \"items\": {\"type\": \"integer\"}}"); Node xml = xml("<config><list>text is invalid</list></config>"); converter.convert(xml).get(0).get("configuration"); Assert.fail("Invalid array conversion shouldn't have succeeded."); } catch (IllegalArgumentException e) { Assert.assertTrue(e.getMessage().contains("<list>")); } }
@Test public void testObjectConversion() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"object\", " + "\"properties\": {\"a\": {\"type\": \"integer\"}, \"b\": {\"type\": \"boolean\"}}}"); Node xml = xml("<config><ext><a>4</a><b>true</b></ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.OBJECT, config.getType()); Assert.assertEquals(4L, config.get("a").asLong()); Assert.assertEquals(true, config.get("b").asBoolean()); }
@Test public void testObjectConversion_additionalProperties() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"object\", " + "\"properties\": {\"a\": {\"type\": \"integer\"}, \"b\": {\"type\": \"boolean\"}}, " + "\"additionalProperties\": {\"type\": \"array\", \"items\": {\"type\": \"string\"}}}"); Node xml = xml("<config><ext><a>4</a><b>true</b><c/><d><x>x</x></d></ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.OBJECT, config.getType()); Assert.assertEquals(4L, config.get("a").asLong()); Assert.assertEquals(true, config.get("b").asBoolean()); Assert.assertEquals(Collections.emptyList(), config.get("c").asList()); Assert.assertEquals(1, config.get("d").asList().size()); Assert.assertEquals("x", config.get("d").get(0).asString()); }
@Test public void testEnumBasedConversion() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"enum\": [1, \"a\", true, 2.0]}"); Node xml = xml("<config><ext>1</ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.LONG, config.getType()); Assert.assertEquals(1L, config.asLong()); xml = xml("<config><ext>a</ext></config>"); config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.STRING, config.getType()); Assert.assertEquals("a", config.asString()); xml = xml("<config><ext>true</ext></config>"); config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.BOOLEAN, config.getType()); Assert.assertEquals(true, config.asBoolean()); xml = xml("<config><ext>2.0</ext></config>"); config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.DOUBLE, config.getType()); Assert.assertEquals(2.0d, config.asDouble(), 0); }
@Test(expected = IllegalArgumentException.class) public void testEnumBasedConversion_unsupported() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"enum\": [{}]}"); Node xml = xml("<config><ext>a</ext></config>"); converter.convert(xml); }
@Test public void testObjectConversion_with$refs() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"type\": \"object\", \"properties\": {\"a\": {\"$ref\": \"#/definitions/a\"}}, " + "\"definitions\": {\"a\": {\"type\": \"boolean\"}}}"); Node xml = xml("<config><ext><a>true</a></ext></config>"); ModelNode config = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(config); Assert.assertEquals(ModelType.OBJECT, config.getType()); Assert.assertEquals(true, config.get("a").asBoolean()); }
@Test public void testOneOf() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"oneOf\": [{\"type\": \"integer\"}, {\"type\": \"number\"}, {\"type\": \"boolean\"}]}"); Node xml1 = xml("<config><ext>1</ext></config>"); Node xml2 = xml("<config><ext>true</ext></config>"); Node xml3 = xml("<config><ext>asdf</ext></config>"); try { converter.convert(xml1).get(0).get("configuration"); Assert.fail("Invalid config should not have been converted."); } catch (IllegalArgumentException __) { } ModelNode c2 = converter.convert(xml2).get(0).get("configuration"); try { converter.convert(xml3).get(0).get("configuration"); Assert.fail("Invalid configuration should not have been converted."); } catch (IllegalArgumentException __) { } Assert.assertNotNull(c2); Assert.assertEquals(ModelType.BOOLEAN, c2.getType()); Assert.assertTrue(c2.asBoolean()); }
@Test public void testAnyOf() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"anyOf\": [{\"type\": \"integer\"}, {\"type\": \"number\"}, {\"type\": \"boolean\"}]}"); Node xml1 = xml("<config><ext>1</ext></config>"); Node xml2 = xml("<config><ext>true</ext></config>"); Node xml3 = xml("<config><ext>asdf</ext></config>"); ModelNode c1 = converter.convert(xml1).get(0).get("configuration"); ModelNode c2 = converter.convert(xml2).get(0).get("configuration"); try { converter.convert(xml3).get(0).get("configuration"); Assert.fail("Invalid configuration should not have been converted."); } catch (IllegalArgumentException __) { } Assert.assertNotNull(c1); Assert.assertNotNull(c2); Assert.assertEquals(ModelType.LONG, c1.getType()); Assert.assertEquals(ModelType.BOOLEAN, c2.getType()); Assert.assertEquals(1L, c1.asLong()); Assert.assertTrue(c2.asBoolean()); }
@Test public void testAllOf() throws Exception { XmlToJson<Node> converter = converter("ext", "{\"allOf\": [{\"type\": \"integer\"}, {\"type\": \"number\"}]}"); Node xml = xml("<config><ext>1</ext></config>"); ModelNode c = converter.convert(xml).get(0).get("configuration"); Assert.assertNotNull(c); Assert.assertEquals(ModelType.DOUBLE, c.getType()); Assert.assertEquals(1D, c.asDouble(), 0); }