Search in sources :

Example 1 with StringDataSource

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");
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BXML(org.ballerinalang.model.values.BXML) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with StringDataSource

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;
}
Also used : BlobDataSource(org.ballerinalang.runtime.message.BlobDataSource) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BString(org.ballerinalang.model.values.BString) BXML(org.ballerinalang.model.values.BXML) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) BJSON(org.ballerinalang.model.values.BJSON) BBlob(org.ballerinalang.model.values.BBlob)

Example 3 with StringDataSource

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");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) BValue(org.ballerinalang.model.values.BValue) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) Test(org.testng.annotations.Test)

Example 4 with StringDataSource

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");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) Test(org.testng.annotations.Test)

Example 5 with StringDataSource

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");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) StringDataSource(org.ballerinalang.runtime.message.StringDataSource) Test(org.testng.annotations.Test)

Aggregations

StringDataSource (org.ballerinalang.runtime.message.StringDataSource)8 BString (org.ballerinalang.model.values.BString)3 BStruct (org.ballerinalang.model.values.BStruct)3 Test (org.testng.annotations.Test)3 BJSON (org.ballerinalang.model.values.BJSON)2 BXML (org.ballerinalang.model.values.BXML)2 BlobDataSource (org.ballerinalang.runtime.message.BlobDataSource)2 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)2 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)2 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)1 BBlob (org.ballerinalang.model.values.BBlob)1 BValue (org.ballerinalang.model.values.BValue)1 BallerinaIOException (org.ballerinalang.nativeimpl.io.BallerinaIOException)1 TempFileIOChannel (org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel)1 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)1 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)1