Search in sources :

Example 11 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class DataWeaveExpressionLanguageAdaptorTestCase method accessRegistryAnnotatedBean.

@Test
public void accessRegistryAnnotatedBean() throws MuleException {
    CoreEvent event = testEvent();
    when(registry.lookupByName("myBean")).thenReturn(of(new MyAnnotatedBean("DataWeave")));
    TypedValue evaluate = expressionLanguage.evaluate("app.registry.myBean", DataType.fromType(MyBean.class), event, fromSingleComponent("flow"), BindingContext.builder().build(), false);
    assertThat(evaluate.getValue(), is(instanceOf(MyAnnotatedBean.class)));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 12 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class DataWeaveExpressionLanguageAdaptorTestCase method stringExpression.

@Test
public void stringExpression() throws Exception {
    TypedValue result = expressionLanguage.evaluate("\"hey\"", testEvent(), BindingContext.builder().build());
    assertThat(result.getValue(), is("hey"));
    assertThat(result.getDataType(), is(assignableTo(STRING)));
}
Also used : TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 13 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class DataWeaveExpressionLanguageAdaptorTestCase method attributesBinding.

@Test
public void attributesBinding() throws Exception {
    CoreEvent event = getEventWithError(empty());
    SomeAttributes attributes = new SomeAttributes();
    InternalMessage message = (InternalMessage) Message.builder().nullValue().attributesValue(attributes).build();
    when(event.getMessage()).thenReturn(message);
    TypedValue result = expressionLanguage.evaluate(ATTRIBUTES, event, BindingContext.builder().build());
    assertThat(result.getValue(), is(equalTo(attributes)));
    assertThat(result.getDataType().getType(), is((equalTo(SomeAttributes.class))));
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 14 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.

the class ExtendedExpressionLanguageAdapterTestCase method enrichTypedValueCompatibility.

@Test
@Description("Verifies that enrichment using a TypedValue only works for MVEL.")
public void enrichTypedValueCompatibility() throws MuleException {
    CoreEvent event = testEvent();
    CoreEvent.Builder builder = builder(event);
    TypedValue myPayload = new TypedValue("myPayload", STRING);
    String expression = "payload";
    expressionLanguageAdapter.enrich(melify(expression), event, builder, TEST_CONNECTOR_LOCATION, myPayload);
    CoreEvent enrichedEvent = builder.build();
    assertThat(enrichedEvent.getMessage().getPayload().getValue(), is(myPayload.getValue()));
    assertThat(enrichedEvent.getMessage().getPayload().getDataType(), is(myPayload.getDataType()));
    expectedException.expect(UnsupportedOperationException.class);
    expressionLanguageAdapter.enrich(expression, event, builder, TEST_CONNECTOR_LOCATION, myPayload);
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 15 with TypedValue

use of org.mule.runtime.api.metadata.TypedValue 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)

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