Search in sources :

Example 66 with BRefValueArray

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

the class CircuitBreakerTest method testCircuitBreaker.

/**
 * Test case for a typical scenario where an upstream service may become unavailable temporarily.
 */
@Test
public void testCircuitBreaker() {
    // Expected HTTP status codes from circuit breaker responses.
    int[] expectedStatusCodes = new int[] { 200, 200, 502, 503, 503, 200, 200, 200 };
    BValue[] returnVals = BRunUtil.invoke(compileResult, "testTypicalScenario");
    Assert.assertEquals(returnVals.length, 2);
    BRefValueArray responses = (BRefValueArray) returnVals[0];
    BRefValueArray errs = (BRefValueArray) returnVals[1];
    for (int i = 0; i < responses.size(); i++) {
        long statusCode;
        // indexes are consisted with the HttpClientError Responses.
        if (i != CB_CLIENT_FIRST_ERROR_INDEX && i != CB_CLIENT_SECOND_ERROR_INDEX) {
            BStruct res = (BStruct) responses.get(i);
            statusCode = res.getIntField(0);
            Assert.assertEquals(statusCode, expectedStatusCodes[i], "Status code does not match.");
        } else {
            // the request which resulted in an error
            Assert.assertNotNull(errs.get(i));
            BStruct err = (BStruct) errs.get(i);
            statusCode = err.getIntField(0);
            // error for requests which were failed immediately.
            if (statusCode == 0) {
                String msg = err.getStringField(0);
                Assert.assertTrue(msg != null && msg.startsWith(CB_ERROR_MSG), "Invalid error message from circuit breaker.");
            } else {
                Assert.assertEquals(statusCode, 503, "Incorrect status code.");
            }
        }
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 67 with BRefValueArray

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

the class ArrayInitializerExprTest method testAnyAsArray.

@Test(description = "Test array of maps inline initializing")
public void testAnyAsArray() {
    BValue[] args = {};
    BValue[] returns = BRunUtil.invokeFunction(compileResult, "testAnyAsArray", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BRefValueArray.class);
    BRefValueArray arrayValue = (BRefValueArray) returns[0];
    Assert.assertEquals(arrayValue.size(), 3);
    Assert.assertEquals(((Long) arrayValue.get(0).value()).longValue(), 1);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 68 with BRefValueArray

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

the class ArrayInitializerExprTest method testArrayOfMapsInit.

@Test(description = "Test array of maps inline initializing")
public void testArrayOfMapsInit() {
    BValue[] args = {};
    BValue[] returns = BRunUtil.invokeFunction(compileResult, "testArrayOfMapsInit", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BRefValueArray.class);
    BRefValueArray arrayValue = (BRefValueArray) returns[0];
    Assert.assertEquals(arrayValue.size(), 3);
    BValue adrs1 = arrayValue.get(0);
    Assert.assertTrue(adrs1 instanceof BMap<?, ?>);
    BValue address = ((BMap) adrs1).get("address");
    Assert.assertTrue(address instanceof BMap<?, ?>);
    Assert.assertEquals(((BMap) address).get("city").stringValue(), "Colombo");
    BValue adrs2 = arrayValue.get(1);
    Assert.assertTrue(adrs2 instanceof BMap<?, ?>);
    address = ((BMap) adrs2).get("address");
    Assert.assertTrue(address instanceof BMap<?, ?>);
    Assert.assertEquals(((BMap) address).get("city").stringValue(), "Kandy");
    BValue adrs3 = arrayValue.get(2);
    Assert.assertTrue(adrs3 instanceof BMap<?, ?>);
    address = ((BMap) adrs3).get("address");
    Assert.assertTrue(address instanceof BMap<?, ?>);
    Assert.assertEquals(((BMap) address).get("city").stringValue(), "Galle");
}
Also used : BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) Test(org.testng.annotations.Test)

Example 69 with BRefValueArray

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

the class ArrayInitializerExprTest method testNestedArrayInit.

// @Test(description = "Test arrays initializing with different types",
// expectedExceptions = {SemanticException.class },
// expectedExceptionsMessageRegExp = "multi-type-array-initializer.bal:3: " +
// "incompatible types: 'string' cannot be assigned to 'int'")
// public void testMultiTypeMapInit() {
// BTestUtils.compile("test-src/statements/arrays/multi-type-array-initializer.bal");
// }
@Test(description = "Test nested array inline initializing")
public void testNestedArrayInit() {
    BValue[] args = {};
    BValue[] returns = BRunUtil.invokeFunction(compileResult, "testNestedArrayInit", args);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BRefValueArray.class);
    BRefValueArray arrayValue = (BRefValueArray) returns[0];
    Assert.assertEquals(arrayValue.size(), 2);
    BValue element = arrayValue.get(0);
    Assert.assertTrue(element instanceof BIntArray);
    BIntArray elementArray = (BIntArray) element;
    Assert.assertEquals(elementArray.size(), 3);
    Assert.assertEquals(elementArray.get(0), 1);
    Assert.assertEquals(elementArray.get(1), 2);
    Assert.assertEquals(elementArray.get(2), 3);
    element = arrayValue.get(1);
    Assert.assertTrue(element instanceof BIntArray);
    elementArray = (BIntArray) element;
    Assert.assertEquals(elementArray.size(), 4);
    Assert.assertEquals(elementArray.get(0), 6);
    Assert.assertEquals(elementArray.get(1), 7);
    Assert.assertEquals(elementArray.get(2), 8);
    Assert.assertEquals(elementArray.get(3), 9);
}
Also used : BValue(org.ballerinalang.model.values.BValue) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Example 70 with BRefValueArray

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

the class ArrayTest method testArrayToString.

@Test
public void testArrayToString() {
    String[] strArray = { "aaa", "bbb", "ccc" };
    BStringArray bStringArray = new BStringArray(strArray);
    Assert.assertEquals(bStringArray.stringValue(), "[\"aaa\", \"bbb\", \"ccc\"]");
    long[] longArray = { 6, 3, 8, 4 };
    BIntArray bIntArray = new BIntArray(longArray);
    Assert.assertEquals(bIntArray.stringValue(), "[6, 3, 8, 4]");
    double[] doubleArray = { 6.4, 3.7, 8.8, 7.4 };
    BFloatArray bFloatArray = new BFloatArray(doubleArray);
    Assert.assertEquals(bFloatArray.stringValue(), "[6.4, 3.7, 8.8, 7.4]");
    int[] boolArray = { 1, 1, 0 };
    BBooleanArray bBooleanArray = new BBooleanArray(boolArray);
    Assert.assertEquals(bBooleanArray.stringValue(), "[true, true, false]");
    BXMLItem[] xmlArray = { new BXMLItem("<foo/>"), new BXMLItem("<bar>hello</bar>") };
    BRefValueArray bXmlArray = new BRefValueArray(xmlArray, BTypes.typeXML);
    Assert.assertEquals(bXmlArray.stringValue(), "[<foo/>, <bar>hello</bar>]");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BFloatArray(org.ballerinalang.model.values.BFloatArray) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) Test(org.testng.annotations.Test)

Aggregations

BRefValueArray (org.ballerinalang.model.values.BRefValueArray)83 BValue (org.ballerinalang.model.values.BValue)45 Test (org.testng.annotations.Test)40 BStruct (org.ballerinalang.model.values.BStruct)37 BString (org.ballerinalang.model.values.BString)22 BInteger (org.ballerinalang.model.values.BInteger)18 BXMLSequence (org.ballerinalang.model.values.BXMLSequence)9 BeforeTest (org.testng.annotations.BeforeTest)9 BIntArray (org.ballerinalang.model.values.BIntArray)7 BMap (org.ballerinalang.model.values.BMap)7 BStringArray (org.ballerinalang.model.values.BStringArray)7 BXML (org.ballerinalang.model.values.BXML)7 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)7 HashMap (java.util.HashMap)6 BStructType (org.ballerinalang.model.types.BStructType)6 BRefType (org.ballerinalang.model.values.BRefType)5 SQLDatasource (org.ballerinalang.nativeimpl.actions.data.sql.SQLDatasource)5 BBooleanArray (org.ballerinalang.model.values.BBooleanArray)4 BFloatArray (org.ballerinalang.model.values.BFloatArray)4 IOException (java.io.IOException)3