Search in sources :

Example 66 with TypedValue

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));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 67 with TypedValue

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));
}
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)

Example 68 with TypedValue

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));
}
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 69 with TypedValue

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);
}
Also used : CursorProvider(org.mule.runtime.api.streaming.CursorProvider) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DefaultExpressionLanguageFactoryService(org.mule.runtime.api.el.DefaultExpressionLanguageFactoryService) Registry(org.mule.runtime.api.artifact.Registry) MVELExpressionLanguage(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage) ExpressionLanguage(org.mule.runtime.api.el.ExpressionLanguage) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 70 with TypedValue

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

Aggregations

TypedValue (org.mule.runtime.api.metadata.TypedValue)97 Test (org.junit.Test)74 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)47 DataType (org.mule.runtime.api.metadata.DataType)17 Message (org.mule.runtime.api.message.Message)16 Description (io.qameta.allure.Description)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 List (java.util.List)11 SmallTest (org.mule.tck.size.SmallTest)10 BindingContext (org.mule.runtime.api.el.BindingContext)9 Map (java.util.Map)8 Optional (java.util.Optional)8 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)5 ArrayList (java.util.ArrayList)4 Matchers.anyString (org.mockito.Matchers.anyString)4 MuleException (org.mule.runtime.api.exception.MuleException)4 Error (org.mule.runtime.api.message.Error)4 ErrorType (org.mule.runtime.api.message.ErrorType)4