use of org.mule.runtime.api.metadata.DataType 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);
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class AbstractVarAssignmentDataTypePropagatorTestCase method doAssignmentDataTypePropagationTest.
protected void doAssignmentDataTypePropagationTest(String expression) throws Exception {
DataType expectedDataType = DataType.builder().type(String.class).mediaType(JSON).charset(CUSTOM_ENCODING).build();
final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(testEvent());
CompiledExpression compiledExpression = compileMelExpression(expression, (PrivilegedEvent) testEvent(), builder);
PrivilegedEvent event = builder.build();
dataTypePropagator.propagate(event, builder, new TypedValue<>(TEST_MESSAGE, expectedDataType), compiledExpression);
event = builder.build();
assertThat(getVariableDataType(event), like(String.class, JSON, CUSTOM_ENCODING));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class AbstractVarExpressionDataTypeResolverTestCase method doVarDataTypeTest.
protected void doVarDataTypeTest(String expression) throws Exception {
DataType expectedDataType = DataType.builder().type(String.class).mediaType(JSON).charset(CUSTOM_ENCODING).build();
PrivilegedEvent event = setVariable((PrivilegedEvent) testEvent(), EXPRESSION_VALUE, expectedDataType);
final ParserConfiguration parserConfiguration = MVELExpressionLanguage.createParserConfiguration(Collections.EMPTY_MAP);
final MVELExpressionLanguageContext context = createMvelExpressionLanguageContext(event, parserConfiguration);
CompiledExpression compiledExpression = (CompiledExpression) compileExpression(expression, new ParserContext(parserConfiguration));
// Expression must be executed, otherwise the variable accessor is not properly configured
MVEL.executeExpression(compiledExpression, context);
assertThat(expressionDataTypeResolver.resolve(event, compiledExpression), like(String.class, JSON, CUSTOM_ENCODING));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class PayloadEnricherDataTypePropagatorTestCase method doPayloadDataTypeTest.
private void doPayloadDataTypeTest(String expression) throws Exception {
final DataType expectedDataType = DataType.builder().type(String.class).mediaType(JSON).charset(CUSTOM_ENCODING).build();
MVELExpressionLanguage expressionLanguage = new MVELExpressionLanguage(muleContext);
final CompiledExpression compiledExpression = (CompiledExpression) compileExpression(expression, new ParserContext(expressionLanguage.getParserConfiguration()));
final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(testEvent());
dataTypePropagator.propagate((PrivilegedEvent) testEvent(), builder, new TypedValue<>(TEST_MESSAGE, expectedDataType), compiledExpression);
final CoreEvent event = builder.build();
assertThat(event.getMessage().getPayload().getDataType(), like(String.class, JSON, CUSTOM_ENCODING));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class PayloadExpressionDataTypeResolverTestCase method doPayloadDataTypeTest.
private void doPayloadDataTypeTest(String expression) throws Exception {
final DataType expectedDataType = DataType.builder().type(String.class).mediaType(JSON).charset(CUSTOM_ENCODING).build();
MVELExpressionLanguage expressionLanguage = new MVELExpressionLanguage(muleContext);
final CompiledExpression compiledExpression = (CompiledExpression) compileExpression(expression, new ParserContext(expressionLanguage.getParserConfiguration()));
PrivilegedEvent event = PrivilegedEvent.builder(testEvent()).message(InternalMessage.builder(testEvent().getMessage()).value(TEST_MESSAGE).mediaType(expectedDataType.getMediaType()).build()).build();
assertThat(dataTypeResolver.resolve(event, compiledExpression), like(String.class, JSON, CUSTOM_ENCODING));
}
Aggregations