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);
}
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);
}
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);
}
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);
}
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)));
}
Aggregations