Search in sources :

Example 6 with BindingContext

use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.

the class DefaultBindingContextBuilderTestCase method fromPreviousBindings.

@Test
public void fromPreviousBindings() {
    ExpressionModule module = ExpressionModule.builder(namespace).addBinding("id", typedValue).build();
    BindingContext previousContext = BindingContext.builder().addBinding(ID, typedValue).addBinding(OTHER_ID, typedValue).addModule(module).build();
    BindingContext context = BindingContext.builder(previousContext).build();
    assertThat(context.bindings(), hasSize(2));
    assertThat(context.identifiers(), hasItems(ID, OTHER_ID));
    assertThat(context.lookup(ID).get(), is(sameInstance(typedValue)));
    assertThat(context.lookup(OTHER_ID).get(), is(sameInstance(typedValue)));
    assertThat(context.modules(), hasSize(1));
    assertThat(context.modules(), hasItems(module));
}
Also used : ExpressionModule(org.mule.runtime.api.el.ExpressionModule) BindingContext(org.mule.runtime.api.el.BindingContext) Test(org.junit.Test)

Example 7 with BindingContext

use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.

the class DefaultBindingContextBuilderTestCase method addsBinding.

@Test
public void addsBinding() {
    BindingContext context = builder.addBinding(ID, typedValue).build();
    assertThat(context.bindings(), hasSize(1));
    assertThat(context.identifiers(), hasItem("id"));
    assertThat(context.lookup("id").get(), is(sameInstance(typedValue)));
}
Also used : BindingContext(org.mule.runtime.api.el.BindingContext) Test(org.junit.Test)

Example 8 with BindingContext

use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.

the class DefaultBindingContextBuilderTestCase method addsBindings.

@Test
public void addsBindings() {
    ExpressionModule module = ExpressionModule.builder(namespace).addBinding(ID, typedValue).build();
    BindingContext previousContext = BindingContext.builder().addBinding(ID, typedValue).addBinding(OTHER_ID, typedValue).addModule(module).build();
    BindingContext context = builder.addAll(previousContext).build();
    assertThat(context.bindings(), hasSize(2));
    assertThat(context.identifiers(), hasItems(ID, OTHER_ID));
    assertThat(context.lookup(ID).get(), is(sameInstance(typedValue)));
    assertThat(context.lookup(OTHER_ID).get(), is(sameInstance(typedValue)));
    assertThat(context.modules(), hasSize(1));
    assertThat(context.modules(), hasItems(module));
    Collection<Binding> moduleBindings = context.modules().iterator().next().bindings();
    assertThat(moduleBindings, hasSize(1));
    assertThat(moduleBindings.iterator().next().identifier(), is(ID));
}
Also used : Binding(org.mule.runtime.api.el.Binding) ExpressionModule(org.mule.runtime.api.el.ExpressionModule) BindingContext(org.mule.runtime.api.el.BindingContext) Test(org.junit.Test)

Example 9 with BindingContext

use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.

the class DefaultExpressionManagerMelDefaultTestCase 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));
}
Also used : BindingContext(org.mule.runtime.api.el.BindingContext) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 10 with BindingContext

use of org.mule.runtime.api.el.BindingContext 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)13 Test (org.junit.Test)9 TypedValue (org.mule.runtime.api.metadata.TypedValue)7 Description (io.qameta.allure.Description)4 ExpressionModule (org.mule.runtime.api.el.ExpressionModule)4 ExpressionFunction (org.mule.runtime.api.el.ExpressionFunction)3 DataType (org.mule.runtime.api.metadata.DataType)3 MuleContext (org.mule.runtime.core.api.MuleContext)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Pattern (java.util.regex.Pattern)1 Collectors.toMap (java.util.stream.Collectors.toMap)1