Search in sources :

Example 11 with BXMLItem

use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testToJsonWithObjectArray.

@Test
public void testToJsonWithObjectArray() {
    String xmlStr = "<books><item><bookName>book1</bookName><bookId>101</bookId></item><item>" + "<bookName>book2</bookName><bookId>102</bookId></item><item><bookName>book3</bookName>" + "<bookId>103</bookId></item></books>";
    BValue[] args = { new BXMLItem(xmlStr) };
    BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
    Assert.assertTrue(returns[0] instanceof BJSON);
    Assert.assertEquals(returns[0].stringValue(), "{\"books\":{\"item\":[{\"bookName\":\"book1\",\"bookId\":\"101\"},{\"bookName\":\"book2\"," + "\"bookId\":\"102\"},{\"bookName\":\"book3\",\"bookId\":\"103\"}]}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 12 with BXMLItem

use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testToJsonXMLWithSingleElementAndAttributes.

@Test
public void testToJsonXMLWithSingleElementAndAttributes() {
    String xmlStr = "<name test=\"5\">Jack</name>";
    BValue[] args = { new BXMLItem(xmlStr) };
    BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
    Assert.assertTrue(returns[0] instanceof BJSON);
    Assert.assertEquals(returns[0].stringValue(), "{\"name\":{\"@test\":\"5\",\"#text\":\"Jack\"}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 13 with BXMLItem

use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testToJSONWithAttributeNamespacesAndPreserveNamespace.

@Test
public void testToJSONWithAttributeNamespacesAndPreserveNamespace() {
    String xmlStr = "<ns0:bookStore xmlns:ns0=\"http://sample0.com/test\" status=\"online\" ns0:id = \"10\">" + "<ns0:storeName>foo</ns0:storeName><ns0:isOpen>true</ns0:isOpen>" + "<ns2:address xmlns:ns2=\"http://sample2.com/test\" status=\"online\" ns0:id = \"10\" " + "ns2:code= \"test\">srilanka</ns2:address></ns0:bookStore>";
    BValue[] args = { new BXMLItem(xmlStr) };
    BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
    Assert.assertTrue(returns[0] instanceof BJSON);
    Assert.assertEquals(returns[0].stringValue(), "{\"ns0:bookStore\":{\"@xmlns:ns0\":" + "\"http://sample0.com/test\",\"@status\":\"online\",\"@ns0:id\":\"10\",\"ns0:storeName\":\"foo\"," + "\"ns0:isOpen\":\"true\",\"ns2:address\":{\"@xmlns:ns2\":\"http://sample2.com/test\"," + "\"@status\":\"online\",\"@ns0:id\":\"10\",\"@ns2:code\":\"test\",\"#text\":\"srilanka\"}}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 14 with BXMLItem

use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testToJsonWithSimpleXMLAndAttributes.

@Test
public void testToJsonWithSimpleXMLAndAttributes() {
    String xmlStr = "<person id = \"5\"><name>Jack</name><age>40</age></person>";
    BValue[] args = { new BXMLItem(xmlStr) };
    BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
    Assert.assertTrue(returns[0] instanceof BJSON);
    Assert.assertEquals(returns[0].stringValue(), "{\"person\":{\"@id\":\"5\",\"name\":\"Jack\",\"age\":\"40\"}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 15 with BXMLItem

use of org.ballerinalang.model.values.BXMLItem in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testToJsonWithChildElementsWithSameKey.

@Test
public void testToJsonWithChildElementsWithSameKey() {
    String xmlStr = "<books><item><item><bookName>book1</bookName><bookId>101</bookId></item></item><item><item>" + "<bookName>book2</bookName><bookId>102</bookId></item></item><item><item><bookName>book3</bookName>" + "<bookId>103</bookId></item></item></books>";
    BValue[] args = { new BXMLItem(xmlStr) };
    BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
    Assert.assertTrue(returns[0] instanceof BJSON);
    Assert.assertEquals(returns[0].stringValue(), "{\"books\":{\"item\":[{\"item\":{\"bookName\":\"book1\",\"bookId\":\"101\"}},{\"item\":" + "{\"bookName\":\"book2\",\"bookId\":\"102\"}},{\"item\":{\"bookName\":\"book3\"," + "\"bookId\":\"103\"}}]}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Aggregations

BXMLItem (org.ballerinalang.model.values.BXMLItem)56 Test (org.testng.annotations.Test)46 BValue (org.ballerinalang.model.values.BValue)42 BJSON (org.ballerinalang.model.values.BJSON)36 BString (org.ballerinalang.model.values.BString)26 OMNode (org.apache.axiom.om.OMNode)18 BXML (org.ballerinalang.model.values.BXML)17 BStruct (org.ballerinalang.model.values.BStruct)6 OMElement (org.apache.axiom.om.OMElement)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)4 ArrayList (java.util.ArrayList)3 OMText (org.apache.axiom.om.OMText)3 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)3 OMDocument (org.apache.axiom.om.OMDocument)2 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)2 BFloatArray (org.ballerinalang.model.values.BFloatArray)2 BIntArray (org.ballerinalang.model.values.BIntArray)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)2