use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class DataWeaveExpressionLanguageAdaptorTestCase method payloadExpressionShouldNotBeEvaluate.
@Test
public void payloadExpressionShouldNotBeEvaluate() throws MuleException {
BindingContext bindingContext = BindingContext.builder().build();
MuleContext muleContext = mock(MuleContext.class);
DefaultExpressionLanguageFactoryService languageFactory = mock(DefaultExpressionLanguageFactoryService.class);
ExpressionLanguage expressionLanguage = spy(ExpressionLanguage.class);
when(languageFactory.create()).thenReturn(expressionLanguage);
CoreEvent event = testEvent();
new DataWeaveExpressionLanguageAdaptor(muleContext, registry, languageFactory).evaluate("#[payload]", event, bindingContext);
verify(expressionLanguage, never()).evaluate(eq("payload"), any(BindingContext.class));
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class ExtendedExpressionLanguageAdapterTestCase method globalContext.
@Test
@Description("Verifies that global binding context only work for DW.")
public void globalContext() throws Exception {
ExpressionFunction expressionFunction = new ExpressionFunction() {
private DataType dataType = STRING;
@Override
public Object call(Object[] objects, BindingContext bindingContext) {
return ((String) objects[0]).toUpperCase();
}
@Override
public Optional<DataType> returnType() {
return Optional.of(dataType);
}
@Override
public List<FunctionParameter> parameters() {
return asList(new FunctionParameter("x", dataType));
}
};
String global = "global";
String value = "var";
BindingContext context = BindingContext.builder().addBinding(global, new TypedValue(value, STRING)).addBinding("upper", new TypedValue(expressionFunction, fromFunction(expressionFunction))).build();
expressionLanguageAdapter.addGlobalBindings(context);
assertThat(expressionLanguageAdapter.evaluate(global, testEvent(), emptyBindingContext).getValue(), is(value));
assertThat(expressionLanguageAdapter.evaluate("upper('hey')", testEvent(), emptyBindingContext).getValue(), is("HEY"));
assertThat(expressionLanguageAdapter.evaluate(melify(global), testEvent(), context).getValue(), is(value));
expectedException.expect(ExpressionRuntimeException.class);
expressionLanguageAdapter.evaluate(melify(global), testEvent(), emptyBindingContext);
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class MVELExpressionLanguage method evaluate.
@Override
public TypedValue evaluate(String expression, CoreEvent event, CoreEvent.Builder eventBuilder, ComponentLocation componentLocation, BindingContext bindingContext) {
expression = removeExpressionMarker(expression);
Map<String, Object> bindingMap = bindingContext.identifiers().stream().collect(toMap(id -> id, id -> bindingContext.lookup(id).get().getValue()));
final Object value = evaluateUntyped(expression, (PrivilegedEvent) event, (PrivilegedEvent.Builder) eventBuilder, componentLocation, bindingMap);
if (value instanceof TypedValue) {
return (TypedValue) value;
} else {
final Serializable compiledExpression = expressionExecutor.getCompiledExpression(expression);
DataType dataType = event != null ? dataTypeResolver.resolve(value, (PrivilegedEvent) event, compiledExpression) : OBJECT;
return new TypedValue(value, dataType);
}
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class MuleFunctionsBindingContextProvider method getBindingContext.
@Override
public BindingContext getBindingContext() {
BindingContext.Builder builder = BindingContext.builder();
PropertyAccessFunction propertyFunction = new PropertyAccessFunction(configurationProperties);
builder.addBinding("p", new TypedValue(propertyFunction, fromFunction(propertyFunction)));
ExpressionFunction lookupFunction = new LookupFunction(componentLocator);
builder.addBinding("lookup", new TypedValue(lookupFunction, fromFunction(lookupFunction)));
ExpressionFunction causedByFunction = new CausedByFunction(errorTypeRepository);
builder.addBinding("causedBy", new TypedValue(causedByFunction, fromFunction(causedByFunction)));
return builder.build();
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class SoapOperationExecutor method evaluateHeaders.
private Object evaluateHeaders(InputStream headers) {
String hs = IOUtils.toString(headers);
BindingContext context = BindingContext.builder().addBinding("payload", new TypedValue<>(hs, DataType.XML_STRING)).build();
return expressionExecutor.evaluate("%dw 2.0 \n" + "output application/java \n" + "---\n" + "payload.headers mapObject (value, key) -> {\n" + " '$key' : write((key): value, \"application/xml\")\n" + "}", context).getValue();
}
Aggregations