@Test public void fullErrorBinding() throws Exception { String description = "An error occurred"; String detailedDescription = "A division by zero has collapsed our systems."; String exceptionMessage = "dividend cannot be zero"; String errorId = "WEAVE_TEST"; ErrorType errorType = mock(ErrorType.class); when(errorType.getIdentifier()).thenReturn(errorId); Error error = ErrorBuilder.builder() .description(description) .detailedDescription(detailedDescription) .exception(new IllegalArgumentException(exceptionMessage)) .errorType(errorType) .build(); Optional opt = Optional.of(error); CoreEvent event = getEventWithError(opt); doReturn(testEvent().getMessage()).when(event).getMessage(); String expression = "'$(error.description) $(error.detailedDescription) $(error.cause.message) $(error.errorType.identifier)'"; TypedValue result = expressionLanguage.evaluate(expression, event, BindingContext.builder().build()); assertThat(result.getValue(), is(format("%s %s %s %s", description, detailedDescription, exceptionMessage, errorId))); }
@Test public void addsBindings() { ExpressionModule module = ExpressionModule.builder(namespace).addBinding(ID, typedValue).build(); BindingContext previousContext = BindingContext.builder() .addBinding(ID, typedValue) .addBinding(OTHER_ID, typedValue) .addModule(module) .build(); BindingContext context = builder.addAll(previousContext).build(); assertThat(context.bindings(), hasSize(2)); assertThat(context.identifiers(), hasItems(ID, OTHER_ID)); assertThat(context.lookup(ID).get(), is(sameInstance(typedValue))); assertThat(context.lookup(OTHER_ID).get(), is(sameInstance(typedValue))); assertThat(context.modules(), hasSize(1)); assertThat(context.modules(), hasItems(module)); Collection<Binding> moduleBindings = context.modules().iterator().next().bindings(); assertThat(moduleBindings, hasSize(1)); assertThat(moduleBindings.iterator().next().identifier(), is(ID)); }
String value = "var"; BindingContext context = BindingContext.builder() .addBinding(global, new TypedValue(value, STRING)) .addBinding("upper", new TypedValue(expressionFunction, fromFunction(expressionFunction))) .build(); expressionLanguageAdapter.addGlobalBindings(context);
@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)); }
protected <T> T resolveExpression(String expr, Object body, MultiMap<String, String> headers, MultiMap<String, String> queryParams, MediaType responseContentType) { if (expr == null) { return null; } else if (!expressionEvaluator.isExpression(expr)) { return (T) expr; } else { Map<Object, Object> attributes = new HashMap<>(2); attributes.put("headers", headers.toImmutableMultiMap()); attributes.put("queryParams", queryParams.toImmutableMultiMap()); BindingContext resultContext = BindingContext.builder() .addBinding("payload", new TypedValue(body, DataType.builder().fromObject(body) .mediaType(responseContentType).build())) .addBinding("attributes", new TypedValue(attributes, DataType.fromType(Map.class))) .addBinding("dataType", new TypedValue(DataType.builder().fromObject(body).mediaType(responseContentType) .build(), DataType.fromType(DataType.class))) .build(); return (T) expressionEvaluator.evaluate(expr, DataType.STRING, resultContext).getValue(); } }
@Test public void addsBindings() { ExpressionModule module = ExpressionModule.builder(namespace).addBinding(ID, typedValue).build(); BindingContext previousContext = BindingContext.builder() .addBinding(ID, typedValue) .addBinding(OTHER_ID, typedValue) .addModule(module) .build(); BindingContext context = builder.addAll(previousContext).build(); assertThat(context.bindings(), hasSize(2)); assertThat(context.identifiers(), hasItems(ID, OTHER_ID)); assertThat(context.lookup(ID).get(), is(sameInstance(typedValue))); assertThat(context.lookup(OTHER_ID).get(), is(sameInstance(typedValue))); assertThat(context.modules(), hasSize(1)); assertThat(context.modules(), hasItems(module)); Collection<Binding> moduleBindings = context.modules().iterator().next().bindings(); assertThat(moduleBindings, hasSize(1)); assertThat(moduleBindings.iterator().next().identifier(), is(ID)); }
protected <T> T resolveExpression(String expr, Object body, MultiMap<String, String> headers, MediaType responseContentType) { if (expr == null) { return null; } else if (!expressionEvaluator.isExpression(expr)) { return (T) expr; } else { BindingContext resultContext = BindingContext.builder() .addBinding("payload", new TypedValue(body, DataType.builder().fromObject(body) .mediaType(responseContentType).build())) .addBinding("attributes", new TypedValue(singletonMap("headers", headers.toImmutableMultiMap()), DataType.fromType(Map.class))) .addBinding("dataType", new TypedValue(DataType.builder().fromObject(body).mediaType(responseContentType) .build(), DataType.fromType(DataType.class))) .build(); return (T) expressionEvaluator.evaluate(expr, STRING, resultContext).getValue(); } }
@Test public void fromPreviousBindings() { ExpressionModule module = ExpressionModule.builder(namespace).addBinding("id", typedValue).build(); BindingContext previousContext = BindingContext.builder() .addBinding(ID, typedValue) .addBinding(OTHER_ID, typedValue) .addModule(module) .build(); BindingContext context = BindingContext.builder(previousContext).build(); assertThat(context.bindings(), hasSize(2)); assertThat(context.identifiers(), hasItems(ID, OTHER_ID)); assertThat(context.lookup(ID).get(), is(sameInstance(typedValue))); assertThat(context.lookup(OTHER_ID).get(), is(sameInstance(typedValue))); assertThat(context.modules(), hasSize(1)); assertThat(context.modules(), hasItems(module)); }
private BindingContext.Builder bindingContextBuilderFor(ComponentLocation componentLocation, CoreEvent event, BindingContext context) { Builder contextBuilder; if (event != null) { contextBuilder = addEventBuindingsToBuilder(event, context); } else { contextBuilder = BindingContext.builder(context); } if (componentLocation != null) { contextBuilder.addBinding(FLOW, () -> new TypedValue<>(new FlowVariablesAccessor(componentLocation.getRootContainerName()), fromType(FlowVariablesAccessor.class))); } return contextBuilder; }
@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)); }
@Test public void itemSequenceInfoBinding() throws Exception { CoreEvent event = spy(testEvent()); when(event.getItemSequenceInfo()).thenReturn(of(ItemSequenceInfo.of(43, 100))); TypedValue result = expressionLanguage.evaluate(ITEM_SEQUENCE_INFO + ".position", event, BindingContext.builder().build()); assertThat(result.getValue(), is(43)); result = expressionLanguage.evaluate(ITEM_SEQUENCE_INFO + ".sequenceSize", event, BindingContext.builder().build()); assertThat(result.getValue(), is(100)); }
@Test public void messageBinding() throws Exception { CoreEvent event = testEvent(); TypedValue result = expressionLanguage.evaluate(MESSAGE, event, BindingContext.builder().build()); assertThat(result.getValue(), is(instanceOf(Message.class))); assertEquals(((Message) result.getValue()).getPayload(), event.getMessage().getPayload()); }
@Test public void withPrefixExpression() throws Exception { TypedValue result = expressionLanguage.evaluate("#[dw:\"hey\"]", testEvent(), BindingContext.builder().build()); assertThat(result.getValue(), is("hey")); assertThat(result.getDataType(), is(assignableTo(STRING))); }
@Test public void shouldBeAddedToBindingContext() { BindingContext.Builder bindingContextBuilder = BindingContext.builder(); ExpressionModule.Builder builder = ExpressionModule.builder(namespace); builder.addBinding("id", typedValue); ExpressionModule module = builder.build(); bindingContextBuilder.addModule(module); BindingContext bindingContext = bindingContextBuilder.build(); assertThat(bindingContext.modules(), hasSize(1)); assertThat(bindingContext.modules(), hasItem(module)); }
@Test public void accessRegistryBean() throws MuleException { CoreEvent event = testEvent(); when(registry.lookupByName("myBean")).thenReturn(of(new MyBean("DataWeave"))); TypedValue evaluate = expressionLanguage.evaluate("app.registry.myBean.name", event, BindingContext.builder().build()); assertThat(evaluate.getValue(), is("DataWeave")); }
@Test public void authenticationBindingWhenNullSecurityContext() throws Exception { CoreEvent event = spy(testEvent()); TypedValue result = expressionLanguage.evaluate(AUTHENTICATION, event, BindingContext.builder().build()); assertThat(result.getValue(), is(nullValue())); }
@Test public void messageErrorBinding() throws Exception { Error error = mock(Error.class); when(error.getErrorMessage()).thenReturn(Message.of(new Integer[] {1, 3, 6})); Optional opt = Optional.of(error); CoreEvent event = getEventWithError(opt); doReturn(testEvent().getMessage()).when(event).getMessage(); String expression = "error.errorMessage.payload reduce ($$ + $)"; TypedValue result = expressionLanguage.evaluate(expression, event, BindingContext.builder().build()); assertThat(result.getValue(), is(10)); }
@Test public void errorBinding() throws Exception { Error error = mock(Error.class); Optional opt = Optional.of(error); CoreEvent event = getEventWithError(opt); doReturn(testEvent().getMessage()).when(event).getMessage(); TypedValue result = expressionLanguage.evaluate(ERROR, event, BindingContext.builder().build()); assertThat(result.getValue(), is(sameInstance(error))); }
@SuppressWarnings("deprecation") protected Object evaluate(String expression, CoreEvent event, CoreEvent.Builder eventBuilder) { return expressionLanguage.evaluate(expression, event, eventBuilder, ((Component) flowConstruct).getLocation(), BindingContext.builder().build()) .getValue(); }
@Override public Iterator<TypedValue<?>> split(String expression, CoreEvent event, ComponentLocation componentLocation, BindingContext bindingContext) throws ExpressionRuntimeException { BindingContext.Builder contextBuilder = bindingContextBuilderFor(componentLocation, event, bindingContext); return sanitizeAndEvaluate(expression, exp -> expressionExecutor.split(exp, contextBuilder.build())); }