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