use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class DefaultExpressionManagerTestCase method transformation.
@Test
@Description("Verifies that a simple transformation works.")
public void transformation() throws MuleException {
String expression = "payload";
TypedValue result = expressionManager.evaluate(expression, BYTE_ARRAY, builder().build(), testEvent());
assertThat(result.getValue(), is(TEST_PAYLOAD.getBytes()));
assertThat(result.getDataType(), is(BYTE_ARRAY));
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class DefaultExpressionManagerTestCase method globals.
@Test
@Description("Verifies that global bindings can be added.")
public void globals() {
DataType integerType = fromType(Integer.class);
ExpressionFunction multiply = new ExpressionFunction() {
@Override
public Object call(Object[] objects, BindingContext bindingContext) {
return ((Integer) objects[0]) * ((Integer) objects[1]);
}
@Override
public Optional<DataType> returnType() {
return of(integerType);
}
@Override
public List<FunctionParameter> parameters() {
return asList(new FunctionParameter("x", integerType), new FunctionParameter("y", integerType));
}
};
BindingContext globalContext = builder().addBinding("aNum", new TypedValue<>(4, fromType(Integer.class))).addBinding("times", new TypedValue<>(multiply, fromFunction(multiply))).build();
expressionManager.addGlobalBindings(globalContext);
TypedValue result = expressionManager.evaluate("aNum times 5");
assertThat(result.getValue(), is(20));
expressionManager.addGlobalBindings(builder().addBinding("otherNum", new TypedValue(3, integerType)).build());
result = expressionManager.evaluate("(times(7, 3) + otherNum) / aNum");
assertThat(result.getValue(), is(6));
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class DefaultExpressionManagerTestCase method simpleCustomVariable.
@Test
@Description("Verifies that custom variables are considered.")
public void simpleCustomVariable() {
Object object = new Object();
BindingContext context = builder().addBinding(MY_VAR, new TypedValue(object, OBJECT)).build();
assertThat(expressionManager.evaluate("#[myVar]", context).getValue(), equalTo(object));
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class DefaultExpressionManagerTestCase method managedCursor.
@Test
public void managedCursor() throws Exception {
final DefaultExpressionLanguageFactoryService mockFactory = mock(DefaultExpressionLanguageFactoryService.class, RETURNS_DEEP_STUBS);
final ExpressionLanguage expressionLanguage = mock(ExpressionLanguage.class, RETURNS_DEEP_STUBS);
final CursorProvider cursorProvider = mock(CursorProvider.class);
Registry registry = mock(Registry.class);
when(registry.lookupByType(DefaultExpressionLanguageFactoryService.class)).thenReturn(of(mockFactory));
when(registry.lookupByName(OBJECT_EXPRESSION_LANGUAGE)).thenReturn(of(mock(MVELExpressionLanguage.class, RETURNS_DEEP_STUBS)));
when(registry.lookupByName(COMPATIBILITY_PLUGIN_INSTALLED)).thenReturn(empty());
TypedValue value = new TypedValue(cursorProvider, BYTE_ARRAY);
when(expressionLanguage.evaluate(anyString(), any())).thenReturn(value);
when(expressionLanguage.evaluate(anyString(), any(), any())).thenReturn(value);
when(mockFactory.create()).thenReturn(expressionLanguage);
expressionManager = new DefaultExpressionManager();
((DefaultExpressionManager) expressionManager).setRegistry(registry);
((DefaultExpressionManager) expressionManager).setStreamingManager(streamingManager);
initialiseIfNeeded(expressionManager, false, muleContext);
final CoreEvent event = testEvent();
when(streamingManager.manage(cursorProvider, event)).thenReturn(cursorProvider);
expressionManager.evaluate("someExpression", event);
verify(streamingManager).manage(cursorProvider, event);
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class GlobalBindingContextProviderTestCase method function.
@Test
public void function() {
TypedValue result = muleContext.getExpressionManager().evaluate("repeat('oa', 3)");
assertThat(result.getValue(), is("oaoaoa"));
assertThat(result.getDataType(), is(assignableTo(STRING)));
}
Aggregations