use of org.mule.runtime.api.metadata.DataType 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));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class AbstractVarAssignmentDataTypePropagatorTestCase method doInnerAssignmentDataTypePropagationTest.
protected void doInnerAssignmentDataTypePropagationTest(String expression) throws Exception {
final DataType expectedDataType = DataType.builder().type(Map.class).mediaType(UNKNOWN).charset(CUSTOM_ENCODING).build();
final Map<String, String> propertyValue = new HashMap<>();
propertyValue.put(INNER_PROPERTY_NAME, TEST_MESSAGE);
PrivilegedEvent event = setVariable((PrivilegedEvent) testEvent(), propertyValue, expectedDataType);
final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(event);
CompiledExpression compiledExpression = compileMelExpression(expression, event, builder);
event = builder.build();
// Attempts to propagate a different dataType, which should be ignored
dataTypePropagator.propagate(event, builder, new TypedValue<>(propertyValue, DataType.STRING), compiledExpression);
event = builder.build();
assertThat(getVariableDataType(event), like(Map.class, UNKNOWN, CUSTOM_ENCODING));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class PropertyEnricherDataTypePropagatorTestCase method propagatesDataTypeForInlinedInvocationProperty.
@Test
public void propagatesDataTypeForInlinedInvocationProperty() 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("foo = 'unused'", new ParserContext(expressionLanguage.getParserConfiguration()));
PrivilegedEvent testEvent = this.<PrivilegedEvent.Builder>getEventBuilder().message(of(TEST_MESSAGE)).addVariable("foo", "bar").build();
final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(testEvent);
dataTypePropagator.propagate(testEvent, builder, new TypedValue<>(TEST_MESSAGE, expectedDataType), compiledExpression);
assertThat(builder.build().getVariables().get("foo").getDataType(), like(String.class, JSON, CUSTOM_ENCODING));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class PropertyExpressionDataTypeResolverTestCase method returnsInlineSessionPropertyDataType.
@Test
public void returnsInlineSessionPropertyDataType() throws Exception {
final String expression = "foo";
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) testEvent()).getSession().setProperty("foo", EXPRESSION_VALUE, expectedDataType);
assertThat(expressionDataTypeResolver.resolve((PrivilegedEvent) testEvent(), compiledExpression), like(String.class, JSON, CUSTOM_ENCODING));
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class MessageEnricherTestCase method doEnrichDataTypePropagationTest.
private void doEnrichDataTypePropagationTest(EnrichExpressionPair pair) throws Exception {
final DataType dataType = DataType.builder().type(String.class).mediaType(JSON).charset(UTF_16.name()).build();
MessageEnricher enricher = baseEnricher();
enricher.addEnrichExpressionPair(pair);
enricher.setEnrichmentMessageProcessor((InternalTestProcessor) event -> CoreEvent.builder(event).message(InternalMessage.builder(event.getMessage()).value("bar").mediaType(dataType.getMediaType()).build()).build());
CoreEvent out = process(enricher, testEvent());
assertEquals("bar", out.getVariables().get("foo").getValue());
assertThat(out.getVariables().get("foo").getDataType(), like(String.class, JSON, UTF_16));
}
Aggregations