Search in sources :

Example 26 with BBlob

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

the class RequestNativeFunctionSuccessTest method testSetBinaryPayload.

@Test(description = "Test setBinaryPayload() function")
public void testSetBinaryPayload() {
    BBlob value = new BBlob("Ballerina".getBytes());
    BValue[] inputArg = { value };
    BValue[] returnVals = BRunUtil.invoke(result, "testSetBinaryPayload", 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);
    MessageDataSource messageDataSource = EntityBodyHandler.getMessageDataSource(entity);
    Assert.assertEquals(messageDataSource.getMessageAsString(), "Ballerina", "Payload is not set properly");
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) MessageDataSource(org.ballerinalang.runtime.message.MessageDataSource) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 27 with BBlob

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

the class NativeFunctionsTestCase method testPingPong.

@Test
public void testPingPong() {
    byte[] bytes = { 1, 2, 3, 4, 5 };
    // Test ping
    BValue[] inputBValuesForPing = { wsConnection, new BBlob(bytes) };
    BRunUtil.invoke(compileResult, "testPing", inputBValuesForPing);
    ByteBuffer pingBuffer = session.getBufferReceived();
    Assert.assertTrue(session.isPing());
    Assert.assertEquals(pingBuffer.array(), bytes);
    // Test pong
    BValue[] inputBValuesForPong = { wsConnection, new BBlob(bytes) };
    BRunUtil.invoke(compileResult, "testPong", inputBValuesForPong);
    ByteBuffer pongBuffer = session.getBufferReceived();
    Assert.assertTrue(session.isPong());
    Assert.assertEquals(pongBuffer.array(), bytes);
}
Also used : BValue(org.ballerinalang.model.values.BValue) ByteBuffer(java.nio.ByteBuffer) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 28 with BBlob

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

the class SecureClientSocketTest method testWriteReadContent.

@Test(dependsOnMethods = "testOpenSecureClientSocket", description = "Test content read/write")
public void testWriteReadContent() {
    final String newline = System.lineSeparator();
    String content = "Hello World" + newline;
    final byte[] contentBytes = content.getBytes();
    BValue[] args = { new BBlob(contentBytes) };
    final BValue[] writeReturns = BRunUtil.invoke(socketClient, "write", args);
    BInteger returnedSize = (BInteger) writeReturns[0];
    Assert.assertEquals(returnedSize.intValue(), content.length(), "Write content size is not match.");
    args = new BValue[] { new BInteger(content.length()) };
    final BValue[] readReturns = BRunUtil.invoke(socketClient, "read", args);
    final BBlob readContent = (BBlob) readReturns[0];
    returnedSize = (BInteger) readReturns[1];
    Assert.assertEquals(readContent.stringValue(), content, "Return content are not match with written content.");
    Assert.assertEquals(returnedSize.intValue(), content.length(), "Read size not match with the request size");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BString(org.ballerinalang.model.values.BString) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 29 with BBlob

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

the class CPU method execTypeCastOpcodes.

@SuppressWarnings("rawtypes")
private static void execTypeCastOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode, int[] operands) {
    int i;
    int j;
    // Index of the constant pool
    int cpIndex;
    BRefType bRefTypeValue;
    TypeRefCPEntry typeRefCPEntry;
    switch(opcode) {
        case InstructionCodes.I2ANY:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BInteger(sf.longRegs[i]);
            break;
        case InstructionCodes.F2ANY:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BFloat(sf.doubleRegs[i]);
            break;
        case InstructionCodes.S2ANY:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BString(sf.stringRegs[i]);
            break;
        case InstructionCodes.B2ANY:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BBoolean(sf.intRegs[i] == 1);
            break;
        case InstructionCodes.L2ANY:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BBlob(sf.byteRegs[i]);
            break;
        case InstructionCodes.ANY2I:
            i = operands[0];
            j = operands[1];
            sf.longRegs[j] = ((BInteger) sf.refRegs[i]).intValue();
            break;
        case InstructionCodes.ANY2F:
            i = operands[0];
            j = operands[1];
            sf.doubleRegs[j] = ((BFloat) sf.refRegs[i]).floatValue();
            break;
        case InstructionCodes.ANY2S:
            i = operands[0];
            j = operands[1];
            sf.stringRegs[j] = sf.refRegs[i].stringValue();
            break;
        case InstructionCodes.ANY2B:
            i = operands[0];
            j = operands[1];
            sf.intRegs[j] = ((BBoolean) sf.refRegs[i]).booleanValue() ? 1 : 0;
            break;
        case InstructionCodes.ANY2L:
            i = operands[0];
            j = operands[1];
            sf.byteRegs[j] = ((BBlob) sf.refRegs[i]).blobValue();
            break;
        case InstructionCodes.ANY2JSON:
            handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeJSON);
            break;
        case InstructionCodes.ANY2XML:
            handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeXML);
            break;
        case InstructionCodes.ANY2MAP:
            handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeMap);
            break;
        case InstructionCodes.ANY2TYPE:
            handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeDesc);
            break;
        case InstructionCodes.ANY2DT:
            handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeTable);
            break;
        case InstructionCodes.ANY2STM:
            handleAnyToRefTypeCast(ctx, sf, operands, BTypes.typeStream);
            break;
        case InstructionCodes.ANY2E:
        case InstructionCodes.ANY2T:
        case InstructionCodes.ANY2C:
        case InstructionCodes.CHECKCAST:
            i = operands[0];
            cpIndex = operands[1];
            j = operands[2];
            typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
            bRefTypeValue = sf.refRegs[i];
            if (checkCast(bRefTypeValue, typeRefCPEntry.getType())) {
                sf.refRegs[j] = sf.refRegs[i];
            } else {
                handleTypeCastError(ctx, sf, j, bRefTypeValue != null ? bRefTypeValue.getType() : BTypes.typeNull, typeRefCPEntry.getType());
            }
            break;
        case InstructionCodes.IS_ASSIGNABLE:
            i = operands[0];
            cpIndex = operands[1];
            j = operands[2];
            typeRefCPEntry = (TypeRefCPEntry) ctx.constPool[cpIndex];
            bRefTypeValue = sf.refRegs[i];
            if (isAssignable(bRefTypeValue, typeRefCPEntry.getType())) {
                sf.intRegs[j] = 1;
            } else {
                sf.intRegs[j] = 0;
            }
            break;
        case InstructionCodes.NULL2JSON:
            j = operands[1];
            sf.refRegs[j] = new BJSON("null");
            break;
        case InstructionCodes.B2JSON:
            i = operands[0];
            j = operands[1];
            sf.refRegs[j] = new BJSON(sf.intRegs[i] == 1 ? "true" : "false");
            break;
        case InstructionCodes.JSON2I:
            castJSONToInt(ctx, operands, sf);
            break;
        case InstructionCodes.JSON2F:
            castJSONToFloat(ctx, operands, sf);
            break;
        case InstructionCodes.JSON2S:
            castJSONToString(ctx, operands, sf);
            break;
        case InstructionCodes.JSON2B:
            castJSONToBoolean(ctx, operands, sf);
            break;
        case InstructionCodes.NULL2S:
            j = operands[1];
            sf.stringRegs[j] = null;
            break;
        case InstructionCodes.CHECK_CONVERSION:
            i = operands[0];
            j = operands[1];
            bRefTypeValue = sf.refRegs[i];
            sf.refRegs[j] = JSONUtils.convertUnionTypeToJSON(bRefTypeValue);
            break;
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) TypeRefCPEntry(org.ballerinalang.util.codegen.cpentries.TypeRefCPEntry) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BFloat(org.ballerinalang.model.values.BFloat) BBoolean(org.ballerinalang.model.values.BBoolean) BBlob(org.ballerinalang.model.values.BBlob) BJSON(org.ballerinalang.model.values.BJSON)

Example 30 with BBlob

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

the class Read method readResponse.

/*
     * Function which will be notified on the response obtained after the async operation.
     *
     * @param result context of the callback.
     * @return Once the callback is processed we further return back the result.
     */
private static EventResult readResponse(EventResult<Integer, EventContext> result) {
    BStruct errorStruct;
    BRefValueArray contentTuple = new BRefValueArray(readTupleType);
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    Throwable error = eventContext.getError();
    Integer numberOfBytes = result.getResponse();
    CallableUnitCallback callback = eventContext.getCallback();
    byte[] content = (byte[]) eventContext.getProperties().get(ReadBytesEvent.CONTENT_PROPERTY);
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
        context.setReturnValues(errorStruct);
    } else {
        contentTuple.add(0, new BBlob(content));
        contentTuple.add(1, new BInteger(numberOfBytes));
        context.setReturnValues(contentTuple);
    }
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BInteger(org.ballerinalang.model.values.BInteger) BStruct(org.ballerinalang.model.values.BStruct) BInteger(org.ballerinalang.model.values.BInteger) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BBlob(org.ballerinalang.model.values.BBlob) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Aggregations

BBlob (org.ballerinalang.model.values.BBlob)32 BValue (org.ballerinalang.model.values.BValue)23 Test (org.testng.annotations.Test)19 BString (org.ballerinalang.model.values.BString)16 BInteger (org.ballerinalang.model.values.BInteger)11 BBoolean (org.ballerinalang.model.values.BBoolean)6 BFloat (org.ballerinalang.model.values.BFloat)6 BStruct (org.ballerinalang.model.values.BStruct)5 BType (org.ballerinalang.model.types.BType)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)3 ByteBuffer (java.nio.ByteBuffer)2 Base64 (java.util.Base64)2 BJSON (org.ballerinalang.model.values.BJSON)2 BRefType (org.ballerinalang.model.values.BRefType)2 BlobDataSource (org.ballerinalang.runtime.message.BlobDataSource)2 MessageDataSource (org.ballerinalang.runtime.message.MessageDataSource)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CRC32 (java.util.zip.CRC32)1 Checksum (java.util.zip.Checksum)1 Context (org.ballerinalang.bre.Context)1