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");
}
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;
}
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);
}
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");
}
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);
}
Aggregations