Search in sources :

Example 1 with ExpressionFunction

use of org.mule.runtime.api.el.ExpressionFunction 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);
}
Also used : ExpressionFunction(org.mule.runtime.api.el.ExpressionFunction) DataType(org.mule.runtime.api.metadata.DataType) BindingContext(org.mule.runtime.api.el.BindingContext) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 2 with ExpressionFunction

use of org.mule.runtime.api.el.ExpressionFunction 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();
}
Also used : ExpressionFunction(org.mule.runtime.api.el.ExpressionFunction) BindingContext(org.mule.runtime.api.el.BindingContext) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 3 with ExpressionFunction

use of org.mule.runtime.api.el.ExpressionFunction 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));
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) ExpressionFunction(org.mule.runtime.api.el.ExpressionFunction) BindingContext(org.mule.runtime.api.el.BindingContext) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Aggregations

BindingContext (org.mule.runtime.api.el.BindingContext)3 ExpressionFunction (org.mule.runtime.api.el.ExpressionFunction)3 TypedValue (org.mule.runtime.api.metadata.TypedValue)3 Description (io.qameta.allure.Description)2 Test (org.junit.Test)2 DataType (org.mule.runtime.api.metadata.DataType)2 FunctionParameter (org.mule.runtime.api.metadata.FunctionParameter)2