Search in sources :

Example 56 with BRefValueArray

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

the class GetValues method execute.

public void execute(Context ctx) {
    BMap<String, BValue> map = (BMap<String, BValue>) ctx.getRefArgument(0);
    Set<String> keySet = map.keySet();
    BRefValueArray bRefValueArray = new BRefValueArray(BTypes.typeAny);
    int i = 0;
    for (String key : keySet) {
        BValue value = map.get(key);
        bRefValueArray.add(i, ((BRefType) value));
        i++;
    }
    ctx.setReturnValues(bRefValueArray);
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Example 57 with BRefValueArray

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

the class StreamLiteralTest method testStreamPublishingAndSubscriptionForMultipleEvents.

@Test(description = "Test receipt of multiple events with correct subscription and publishing")
public void testStreamPublishingAndSubscriptionForMultipleEvents() {
    BValue[] returns = BRunUtil.invoke(result, "testStreamPublishingAndSubscriptionForMultipleEvents");
    BRefValueArray publishedEmployeeEvents = (BRefValueArray) returns[0];
    BRefValueArray receivedEmployeeEvents = (BRefValueArray) returns[1];
    Assert.assertNotNull(publishedEmployeeEvents);
    Assert.assertNotNull(receivedEmployeeEvents);
    Assert.assertEquals(publishedEmployeeEvents.size(), receivedEmployeeEvents.size(), "Number of Employee " + "Events received does not match the number published");
    for (int i = 0; i < publishedEmployeeEvents.size(); i++) {
        BStruct publishedEmployeeEvent = (BStruct) publishedEmployeeEvents.get(i);
        BStruct receivedEmployeeEvent = (BStruct) receivedEmployeeEvents.get(i);
        Assert.assertTrue(Objects.equals(publishedEmployeeEvent.getType().getName(), receivedEmployeeEvent.getType().getName()));
        Assert.assertEquals(publishedEmployeeEvent.getIntField(0), receivedEmployeeEvent.getIntField(0), "Struct field \"id\" of received event does not match that of published event");
        Assert.assertEquals(publishedEmployeeEvent.getStringField(0), receivedEmployeeEvent.getStringField(0), "Struct field \"name\" of received event does not match that of published event");
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 58 with BRefValueArray

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

the class VectorTest method testRemove.

@Test(description = "Test case for testing removal of elements from a vector.")
public void testRemove() {
    long[] expectedVals = new long[] { 20, 30, 50, 60, 70, 80, 90 };
    long[] removedVals = new long[] { 10, 40, 100 };
    long vectorSize = 10;
    BValue[] returns = BRunUtil.invoke(compileResult, "testRemove", new BValue[] { new BInteger(vectorSize) });
    Assert.assertNotNull(returns);
    BStruct vector = (BStruct) returns[0];
    BRefValueArray vectorEntries = (BRefValueArray) vector.getRefField(0);
    long finalVectorSize = vector.getIntField(0);
    Assert.assertEquals(finalVectorSize, expectedVals.length);
    for (int i = 0; i < removedVals.length; i++) {
        Assert.assertEquals(((BInteger) returns[i + 1]).intValue(), removedVals[i]);
    }
    for (int i = 0; i < finalVectorSize; i++) {
        Assert.assertEquals(vectorEntries.get(i).value(), expectedVals[i]);
    }
    for (int i = (int) finalVectorSize; i < vectorEntries.size(); i++) {
        Assert.assertNull(vectorEntries.get(i));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 59 with BRefValueArray

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

the class Util method prepareRequestWithMultiparts.

/**
 * Prepare carbon request message with multiparts.
 *
 * @param outboundRequest Represent outbound carbon request
 * @param requestStruct   Ballerina request struct which contains multipart data
 */
private static void prepareRequestWithMultiparts(HTTPCarbonMessage outboundRequest, BStruct requestStruct) {
    BStruct entityStruct = requestStruct.getNativeData(MESSAGE_ENTITY) != null ? (BStruct) requestStruct.getNativeData(MESSAGE_ENTITY) : null;
    if (entityStruct != null) {
        BRefValueArray bodyParts = entityStruct.getNativeData(BODY_PARTS) != null ? (BRefValueArray) entityStruct.getNativeData(BODY_PARTS) : null;
        if (bodyParts != null) {
            HttpDataFactory dataFactory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
            setDataFactory(dataFactory);
            try {
                HttpPostRequestEncoder nettyEncoder = new HttpPostRequestEncoder(dataFactory, outboundRequest.getNettyHttpRequest(), true);
                for (int i = 0; i < bodyParts.size(); i++) {
                    BStruct bodyPart = (BStruct) bodyParts.get(i);
                    encodeBodyPart(nettyEncoder, outboundRequest.getNettyHttpRequest(), bodyPart);
                }
                nettyEncoder.finalizeRequest();
                requestStruct.addNativeData(MULTIPART_ENCODER, nettyEncoder);
            } catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
                log.error("Error occurred while creating netty request encoder for multipart data binding", e.getMessage());
            }
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) HttpDataFactory(io.netty.handler.codec.http.multipart.HttpDataFactory) DefaultHttpDataFactory(io.netty.handler.codec.http.multipart.DefaultHttpDataFactory)

Example 60 with BRefValueArray

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

the class SystemTest method testFormatBooleanTrue.

@Test
public void testFormatBooleanTrue() {
    BRefValueArray fArgs = new BRefValueArray();
    fArgs.add(0, new BBoolean(true));
    BValue[] args = { new BString("%b"), fArgs };
    BValue[] returns = BRunUtil.invoke(compileResult, "testSprintf", args);
    Assert.assertEquals(returns[0].stringValue(), "true");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Aggregations

BRefValueArray (org.ballerinalang.model.values.BRefValueArray)83 BValue (org.ballerinalang.model.values.BValue)45 Test (org.testng.annotations.Test)40 BStruct (org.ballerinalang.model.values.BStruct)37 BString (org.ballerinalang.model.values.BString)22 BInteger (org.ballerinalang.model.values.BInteger)18 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)9 BeforeTest (org.testng.annotations.BeforeTest)9 BIntArray (org.ballerinalang.model.values.BIntArray)7 BMap (org.ballerinalang.model.values.BMap)7 BStringArray (org.ballerinalang.model.values.BStringArray)7 BXML (org.ballerinalang.model.values.BXML)7 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 BStructType (org.ballerinalang.model.types.BStructType)6 BRefType (org.ballerinalang.model.values.BRefType)5 SQLDatasource (org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)5 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)4 BFloatArray (org.ballerinalang.model.values.BFloatArray)4 IOException (java.io.IOException)3