Search in sources :

Example 21 with BBlob

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

the class MimeUtilityFunctionTest method testGetAndSetBlob.

@Test(description = "Set blob data to entity and get the content back from entity as a blob")
public void testGetAndSetBlob() {
    String content = "ballerina";
    BBlob byteContent = new BBlob(content.getBytes());
    BValue[] args = { byteContent };
    BValue[] returns = BRunUtil.invoke(compileResult, "testSetAndGetBlob", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertEquals(returns[0].stringValue(), content);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 22 with BBlob

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

the class ValueTypeCastExprTest method testAnyToBlob.

@Test
public void testAnyToBlob() {
    byte[] data = "string".getBytes();
    BValue[] args = { new BBlob(data) };
    BValue[] returns = BRunUtil.invoke(result, "anyToBlob", args);
    Assert.assertTrue(returns[0] instanceof BBlob);
    Assert.assertEquals(((BBlob) returns[0]).blobValue(), data);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 23 with BBlob

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

the class ValueTypeCastExprTest method testBlobToAny.

@Test
public void testBlobToAny() {
    BValue[] args = { new BBlob("string".getBytes()) };
    BValue[] returns = BRunUtil.invoke(result, "blobToAny", args);
    Assert.assertTrue(returns[0] instanceof BBlob);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 24 with BBlob

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

the class CryptoTest method testCRC32ForBinary.

@Test(description = "Testing CRC32 generation for blobs")
public void testCRC32ForBinary() {
    String payload = "Ballerina CRC32 Hash Test for Blob";
    String expectedCRC32Hash = "f3638b7f";
    BValue[] returnValues = BRunUtil.invoke(compileResult, "testHashWithCRC32ForBinary", new BValue[] { new BBlob(payload.getBytes(StandardCharsets.UTF_8)) });
    Assert.assertFalse(returnValues == null || returnValues.length == 0 || returnValues[0] == null);
    Assert.assertEquals(returnValues[0].stringValue(), expectedCRC32Hash);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBlob(org.ballerinalang.model.values.BBlob) Test(org.testng.annotations.Test)

Example 25 with BBlob

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

the class GetCRC32 method execute.

@Override
public void execute(Context context) {
    BValue entityBody = context.getRefArgument(0);
    Checksum checksum = new CRC32();
    byte[] bytes;
    long checksumVal;
    BType argType = entityBody.getType();
    if (argType == BTypes.typeJSON || argType == BTypes.typeXML || argType == BTypes.typeString) {
        // TODO: Look at the possibility of making the encoding configurable
        bytes = entityBody.stringValue().getBytes(StandardCharsets.UTF_8);
    } else if (argType == BTypes.typeBlob) {
        bytes = ((BBlob) entityBody).blobValue();
    } else {
        throw new BallerinaException("failed to generate hash: unsupported data type: " + entityBody.getType().getName());
    }
    checksum.update(bytes, 0, bytes.length);
    checksumVal = checksum.getValue();
    context.setReturnValues(new BString(Long.toHexString(checksumVal)));
}
Also used : CRC32(java.util.zip.CRC32) BValue(org.ballerinalang.model.values.BValue) Checksum(java.util.zip.Checksum) BType(org.ballerinalang.model.types.BType) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BBlob(org.ballerinalang.model.values.BBlob)

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