use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.
the class CacheTest method testCacheOperations.
@Test
public void testCacheOperations() {
// Create arguments to initiate cache
BValue[] args = { new BString("cacheName"), new BString("15"), new BString("cacheKey"), new BString("cacheValue") };
// Test ballerina cache create, put and get for BString
BValue[] returns = BLangFunctions.invokeNew(bLangProgram, "testCache", args);
// Assert if cache entry is BValue
Assert.assertTrue(returns[0] instanceof BValue);
final String expected = "cacheValue";
// Assert if return entry matched with exact entry we put there
Assert.assertEquals(returns[0].stringValue(), expected);
// Test ballerina Boolean values.
BValue[] argsBoolean = { new BString("cacheName"), new BString("15"), new BString("cacheKey"), new BBoolean(false) };
BValue[] returnsBoolean = BLangFunctions.invokeNew(bLangProgram, "testCache", argsBoolean);
Assert.assertTrue(returnsBoolean[0] instanceof BBoolean);
// Test ballerina JSON values.
BValue[] argsJSON = { new BString("cacheName"), new BString("15"), new BString("cacheKey"), new BJSON("{}") };
BValue[] returnsJSON = BLangFunctions.invokeNew(bLangProgram, "testCache", argsJSON);
Assert.assertTrue(returnsJSON[0] instanceof BJSON);
}
use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.
the class MapPutAndGetTestCase method testMapOperations.
@Test
public void testMapOperations() {
// Create arguments to initiate map put
BValue[] args = { new BString("testMapEntry"), new BString("testMapValue") };
// Test ballerina map put and get for BString
BValue[] returns = BLangFunctions.invokeNew(bLangProgram, "testMapPutAndGet", args);
// Assert if map entry is BValue
Assert.assertTrue(returns[0] instanceof BValue);
final String expected = "testMapValue";
// Assert if return entry matched with exact entry we put there
Assert.assertEquals(returns[0].stringValue(), expected);
// Test ballerina Boolean values.
BValue[] argsBoolean = { new BString("testMapEntry"), new BBoolean(false) };
BValue[] returnsBoolean = BLangFunctions.invokeNew(bLangProgram, "testMapPutAndGet", argsBoolean);
Assert.assertTrue(returnsBoolean[0] instanceof BBoolean);
// Test ballerina JSON values.
BValue[] argsJSON = { new BString("testMapEntry"), new BJSON("{}") };
BValue[] returnsJSON = BLangFunctions.invokeNew(bLangProgram, "testMapPutAndGet", argsJSON);
Assert.assertTrue(returnsJSON[0] instanceof BJSON);
}
use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.
the class CreateCache method execute.
@Override
public BValue[] execute(Context context) {
String cacheName = getStringArgument(context, 0);
String cacheTimeoutString = getStringArgument(context, 1);
// Default cache timeout is 15 minutes
int cacheTimeout = 15;
if (cacheTimeoutString != null && cacheTimeoutString.length() > 0) {
cacheTimeout = ((cacheTimeout = Integer.parseInt(cacheTimeoutString)) > 0 ? cacheTimeout : 15);
}
CacheManager cacheManager = CacheManagerHolder.getInstance().getCacheManager();
if ((cacheManager).getCache(cacheName) == null) {
Duration cacheExpiry = new Duration(TimeUnit.MINUTES, cacheTimeout);
MutableConfiguration<String, String> config = new MutableConfiguration<>();
config.setStoreByValue(true).setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(cacheExpiry)).setStatisticsEnabled(false).setStoreByValue(false);
cacheManager.createCache(cacheName, config);
}
return getBValues(new BString(cacheName));
}
use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.
the class RemoveCacheEntry method execute.
@Override
public BValue[] execute(Context context) {
String cacheName = getStringArgument(context, 0);
String cacheKey = getStringArgument(context, 1);
CacheManagerHolder.getInstance().getCacheManager().getCache(cacheName).remove(cacheKey);
return getBValues(new BString(cacheName));
}
use of org.ballerinalang.model.values.BString in project carbon-apimgt by wso2.
the class PutMapEntry method execute.
@Override
public BValue[] execute(Context context) {
String key = getStringArgument(context, 0);
BValue value = getRefArgument(context, 0);
MapManagerHolder.getInstance().getMapManager().put(key, value);
return getBValues(new BString(key));
}
Aggregations