use of org.ballerinalang.runtime.message.StringDataSource in project ballerina by ballerina-lang.
the class MultipartEncoderTest method validateBodyPartContent.
/**
* Validate that the decoded body part content matches with the encoded content.
*
* @param mimeParts List of decoded body parts
* @param bodyPart Ballerina body part
* @throws IOException When an exception occurs during binary data decoding
*/
private void validateBodyPartContent(List<MIMEPart> mimeParts, BStruct bodyPart) throws IOException {
EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(0));
BJSON jsonData = EntityBodyHandler.constructJsonDataSource(bodyPart);
Assert.assertNotNull(jsonData);
Assert.assertEquals(jsonData.getMessageAsString(), "{\"" + "bodyPart" + "\":\"" + "jsonPart" + "\"}");
EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(1));
BXML xmlData = EntityBodyHandler.constructXmlDataSource(bodyPart);
Assert.assertNotNull(xmlData);
Assert.assertEquals(xmlData.getMessageAsString(), "<name>Ballerina xml file part</name>");
EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(2));
StringDataSource textData = EntityBodyHandler.constructStringDataSource(bodyPart);
Assert.assertNotNull(textData);
Assert.assertEquals(textData.getMessageAsString(), "Ballerina text body part");
EntityBodyHandler.populateBodyContent(bodyPart, mimeParts.get(3));
BlobDataSource blobDataSource = EntityBodyHandler.constructBlobDataSource(bodyPart);
Assert.assertNotNull(blobDataSource);
Assert.assertEquals(blobDataSource.getMessageAsString(), "Ballerina binary file part");
}
use of org.ballerinalang.runtime.message.StringDataSource in project ballerina by ballerina-lang.
the class HttpDispatcher method populateAndGetEntityBody.
private static BValue populateAndGetEntityBody(HttpResource httpResource, BStruct inRequest, BStruct inRequestEntity, BType entityBodyType) throws IOException {
HttpUtil.populateEntityBody(null, inRequest, inRequestEntity, true);
switch(entityBodyType.getTag()) {
case TypeTags.STRING_TAG:
StringDataSource stringDataSource = EntityBodyHandler.constructStringDataSource(inRequestEntity);
EntityBodyHandler.addMessageDataSource(inRequestEntity, stringDataSource);
return stringDataSource != null ? new BString(stringDataSource.getMessageAsString()) : null;
case TypeTags.JSON_TAG:
BJSON bjson = EntityBodyHandler.constructJsonDataSource(inRequestEntity);
EntityBodyHandler.addMessageDataSource(inRequestEntity, bjson);
return bjson;
case TypeTags.XML_TAG:
BXML bxml = EntityBodyHandler.constructXmlDataSource(inRequestEntity);
EntityBodyHandler.addMessageDataSource(inRequestEntity, bxml);
return bxml;
case TypeTags.BLOB_TAG:
BlobDataSource blobDataSource = EntityBodyHandler.constructBlobDataSource(inRequestEntity);
EntityBodyHandler.addMessageDataSource(inRequestEntity, blobDataSource);
return new BBlob(blobDataSource != null ? blobDataSource.getValue() : new byte[0]);
case TypeTags.STRUCT_TAG:
bjson = EntityBodyHandler.constructJsonDataSource(inRequestEntity);
EntityBodyHandler.addMessageDataSource(inRequestEntity, bjson);
try {
return JSONUtils.convertJSONToStruct(bjson, (BStructType) entityBodyType);
} catch (NullPointerException ex) {
throw new BallerinaConnectorException("cannot convert payload to struct type: " + entityBodyType.getName());
}
}
return null;
}
use of org.ballerinalang.runtime.message.StringDataSource in project ballerina by ballerina-lang.
the class RequestNativeFunctionSuccessTest method testSetStringPayload.
@Test
public void testSetStringPayload() {
BString value = new BString("Ballerina");
BValue[] inputArg = { value };
BValue[] returnVals = BRunUtil.invoke(result, "testSetStringPayload", 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);
StringDataSource stringValue = (StringDataSource) EntityBodyHandler.getMessageDataSource(entity);
Assert.assertEquals(stringValue.getMessageAsString(), "Ballerina", "Payload is not set properly");
}
use of org.ballerinalang.runtime.message.StringDataSource in project ballerina by ballerina-lang.
the class ServiceTest method testConstantValueAsAnnAttributeVal.
@Test(description = "Test using constant as annotation attribute value")
public void testConstantValueAsAnnAttributeVal() {
HTTPTestRequest requestMsg = MessageUtils.generateHTTPMessage("/echo/constantPath", "GET");
HTTPCarbonMessage responseMsg = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, requestMsg);
Assert.assertNotNull(responseMsg);
String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(responseMsg).getInputStream());
StringDataSource stringDataSource = new StringDataSource(responseMsgPayload);
Assert.assertNotNull(stringDataSource);
Assert.assertEquals(stringDataSource.getValue(), "constant path test");
}
use of org.ballerinalang.runtime.message.StringDataSource in project ballerina by ballerina-lang.
the class ServiceTest method testGetServiceLevelString.
@Test(description = "Test accessing service level variable in resource")
public void testGetServiceLevelString() {
HTTPTestRequest requestMsg = MessageUtils.generateHTTPMessage("/echo/getServiceLevelString", "GET");
HTTPCarbonMessage responseMsg = Services.invokeNew(compileResult, TEST_ENDPOINT_NAME, requestMsg);
Assert.assertNotNull(responseMsg);
String responseMsgPayload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(responseMsg).getInputStream());
StringDataSource stringDataSource = new StringDataSource(responseMsgPayload);
Assert.assertNotNull(stringDataSource);
Assert.assertEquals(stringDataSource.getValue(), "sample value");
}
Aggregations