Search in sources :

Example 1 with BindingContext

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));
}
Also used : MuleContext(org.mule.runtime.core.api.MuleContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultExpressionLanguageFactoryService(org.mule.runtime.api.el.DefaultExpressionLanguageFactoryService) BindingContext(org.mule.runtime.api.el.BindingContext) ExpressionLanguage(org.mule.runtime.api.el.ExpressionLanguage) Test(org.junit.Test)

Example 2 with BindingContext

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);
}
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 3 with BindingContext

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);
    }
}
Also used : ExtendedExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.ExtendedExpressionLanguageAdaptor) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) CoreMessages(org.mule.runtime.core.api.config.i18n.CoreMessages) AbstractDataTypeBuilderFactory(org.mule.runtime.api.metadata.AbstractDataTypeBuilderFactory) ExpressionCompiler(org.mule.mvel2.compiler.ExpressionCompiler) BindingContext(org.mule.runtime.api.el.BindingContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) HashMap(java.util.HashMap) DEFAULT_EXPRESSION_POSTFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_POSTFIX) DataHandler(javax.activation.DataHandler) ValidationResult.success(org.mule.runtime.api.el.ValidationResult.success) CachedMapVariableResolverFactory(org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory) ParserConfiguration(org.mule.mvel2.ParserConfiguration) Inject(javax.inject.Inject) BigDecimal(java.math.BigDecimal) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Collectors.toMap(java.util.stream.Collectors.toMap) MuleContext(org.mule.runtime.core.api.MuleContext) Map(java.util.Map) MimeType(javax.activation.MimeType) Function(org.mule.mvel2.ast.Function) Collections.singletonMap(java.util.Collections.singletonMap) BigInteger(java.math.BigInteger) IOUtils(org.mule.runtime.core.api.util.IOUtils) StringUtils.replace(org.apache.commons.lang3.StringUtils.replace) ValidationResult.failure(org.mule.runtime.api.el.ValidationResult.failure) DEFAULT_EXPRESSION_PREFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_PREFIX) Iterator(java.util.Iterator) DataType(org.mule.runtime.api.metadata.DataType) PREFIX_EXPR_SEPARATOR(org.mule.runtime.core.internal.el.DefaultExpressionManager.PREFIX_EXPR_SEPARATOR) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ValidationResult(org.mule.runtime.api.el.ValidationResult) IOException(java.io.IOException) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) MvelEnricherDataTypePropagator(org.mule.runtime.core.internal.el.mvel.datatype.MvelEnricherDataTypePropagator) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Serializable(java.io.Serializable) TypedValue(org.mule.runtime.api.metadata.TypedValue) MEL_PREFIX(org.mule.runtime.core.internal.el.DefaultExpressionManager.MEL_PREFIX) VariableResolverFactory(org.mule.mvel2.integration.VariableResolverFactory) CoreMessages.objectIsNull(org.mule.runtime.core.api.config.i18n.CoreMessages.objectIsNull) CompileException(org.mule.mvel2.CompileException) Entry(java.util.Map.Entry) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompilerTools(org.mule.mvel2.util.CompilerTools) MvelDataTypeResolver(org.mule.runtime.core.internal.el.mvel.datatype.MvelDataTypeResolver) Pattern(java.util.regex.Pattern) Serializable(java.io.Serializable) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) DataType(org.mule.runtime.api.metadata.DataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 4 with BindingContext

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

Example 5 with BindingContext

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

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