Search in sources :

Example 6 with BString

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);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 7 with BString

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);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 8 with BString

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));
}
Also used : BString(org.ballerinalang.model.values.BString) CacheManager(javax.cache.CacheManager) Duration(javax.cache.expiry.Duration) BString(org.ballerinalang.model.values.BString) MutableConfiguration(javax.cache.configuration.MutableConfiguration)

Example 9 with BString

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));
}
Also used : BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString)

Example 10 with BString

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));
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString)

Aggregations

BString (org.ballerinalang.model.values.BString)11 BValue (org.ballerinalang.model.values.BValue)7 Test (org.testng.annotations.Test)5 BBoolean (org.ballerinalang.model.values.BBoolean)4 BJSON (org.ballerinalang.model.values.BJSON)2 CacheManager (javax.cache.CacheManager)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1 Duration (javax.cache.expiry.Duration)1 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)1 APIMThreatAnalyzer (org.wso2.carbon.apimgt.ballerina.threatprotection.analyzer.APIMThreatAnalyzer)1