Search in sources :

Example 21 with BXMLItem

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

the class MultipartFormDataDecoderTest method testXmlBodyPartAsFileUpload.

@Test(description = "Test sending a multipart request with a json body part where the content is kept in a file")
public void testXmlBodyPartAsFileUpload() {
    String path = "/test/xmlbodypart";
    Map<String, Object> messageMap = Util.createPrerequisiteMessages(path, MULTIPART_FORM_DATA, result);
    ArrayList<BStruct> bodyParts = new ArrayList<>();
    bodyParts.add(Util.getXmlFilePart(result));
    HTTPTestRequest cMsg = Util.getCarbonMessageWithBodyParts(messageMap, Util.getArrayOfBodyParts(bodyParts));
    HTTPCarbonMessage response = Services.invokeNew(serviceResult, MOCK_ENDPOINT_NAME, cMsg);
    Assert.assertNotNull(response, "Response message not found");
    Assert.assertEquals(new BXMLItem(ResponseReader.getReturnValue(response)).getTextValue().stringValue(), "Ballerina" + " xml file part");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) ArrayList(java.util.ArrayList) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 22 with BXMLItem

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

the class Util method getXmlBodyPart.

/**
 * Get a xml body part from a given xml content.
 *
 * @param result Result of ballerina file compilation
 * @return A ballerina struct that represent a body part
 */
static BStruct getXmlBodyPart(CompileResult result) {
    BXMLItem xmlContent = new BXMLItem("<name>Ballerina</name>");
    BStruct bodyPart = getEntityStruct(result);
    EntityBodyChannel byteChannel = new EntityBodyChannel(new ByteArrayInputStream(xmlContent.getMessageAsString().getBytes(StandardCharsets.UTF_8)));
    bodyPart.addNativeData(ENTITY_BYTE_CHANNEL, new EntityWrapper(byteChannel));
    MimeUtil.setContentType(getMediaTypeStruct(result), bodyPart, APPLICATION_XML);
    return bodyPart;
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BStruct(org.ballerinalang.model.values.BStruct) EntityBodyChannel(org.ballerinalang.mime.util.EntityBodyChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityWrapper(org.ballerinalang.mime.util.EntityWrapper)

Example 23 with BXMLItem

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

the class CryptoTest method testCRC32ForXML.

@Test(description = "Testing CRC32 generation for XML")
public void testCRC32ForXML() {
    String payload = "<foo>hello</foo>";
    String expectedCRC32Hash = "748efc2";
    BValue[] returnValues = BRunUtil.invoke(compileResult, "testHashWithCRC32ForXML", new BValue[] { new BXMLItem(payload) });
    Assert.assertFalse(returnValues == null || returnValues.length == 0 || returnValues[0] == null);
    Assert.assertEquals(returnValues[0].stringValue(), expectedCRC32Hash);
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 24 with BXMLItem

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

the class RequestNativeFunctionSuccessTest method testSetXmlPayload.

@Test
public void testSetXmlPayload() {
    BXMLItem value = new BXMLItem("<name>Ballerina</name>");
    BValue[] inputArg = { value };
    BValue[] returnVals = BRunUtil.invoke(result, "testSetXmlPayload", inputArg);
    Assert.assertFalse(returnVals == null || returnVals.length == 0 || returnVals[0] == null, "Invalid Return Values.");
    Assert.assertTrue(returnVals[0] instanceof BStruct);
    BStruct entity = (BStruct) ((BStruct) returnVals[0]).getNativeData(MESSAGE_ENTITY);
    // BXMLItem xmlValue = (BXMLItem) entity.getRefField(XML_DATA_INDEX);
    BXML xmlValue = (BXML) EntityBodyHandler.getMessageDataSource(entity);
    Assert.assertEquals(xmlValue.getTextValue().stringValue(), "Ballerina", "Payload is not set properly");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BXML(org.ballerinalang.model.values.BXML) Test(org.testng.annotations.Test)

Example 25 with BXMLItem

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

the class XMLUtils method parse.

/**
 * Create a XML sequence from string inputstream.
 *
 * @param xmlStream XML imput stream
 * @return  XML Sequence
 */
@SuppressWarnings("unchecked")
public static BXML<?> parse(InputStream xmlStream) {
    BRefValueArray elementsSeq = new BRefValueArray();
    OMDocument doc;
    try {
        doc = OMXMLBuilderFactory.createOMBuilder(xmlStream).getDocument();
        Iterator<OMNode> docChildItr = doc.getChildren();
        int i = 0;
        while (docChildItr.hasNext()) {
            elementsSeq.add(i++, new BXMLItem(docChildItr.next()));
        }
    } catch (DeferredParsingException e) {
        throw new BallerinaException(e.getCause().getMessage());
    } catch (Throwable e) {
        throw new BallerinaException("failed to create xml: " + e.getMessage());
    }
    return new BXMLSequence(elementsSeq);
}
Also used : DeferredParsingException(org.apache.axiom.om.DeferredParsingException) OMNode(org.apache.axiom.om.OMNode) BXMLItem(org.ballerinalang.model.values.BXMLItem) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) OMDocument(org.apache.axiom.om.OMDocument)

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