use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class AbstractForkJoinStrategyTestCase method flowVarsMerged.
@Test
@Description("After successful completion of all routes the variables from each route are merged into the result.")
public void flowVarsMerged() throws Throwable {
final String beforeVarName = "before";
final String beforeVarValue = "beforeValue";
final String beforeVar2Name = "before2";
final String beforeVar2Value = "before2Value";
final String beforeVar2NewValue = "before2NewValue";
final String fooVarName = "foo";
final String fooVarValue = "fooValue1";
final String fooVar2Name = "foo2";
final String fooVar2Value1 = "foo2Value1";
final String fooVar2Value2 = "foo2Value2";
final String fooVar3Name = "foo3";
final String fooVar3Value1 = "foo3Value1";
final Apple fooVar3Value2 = new Apple();
CoreEvent original = builder(this.<CoreEvent>newEvent()).addVariable(beforeVarName, beforeVarValue).addVariable(beforeVar2Name, beforeVar2Value).build();
RoutingPair pair1 = of(original, createChain(event -> builder(event).addVariable(beforeVar2Name, beforeVar2NewValue).addVariable(fooVarName, fooVarValue).addVariable(fooVar2Name, fooVar2Value1).addVariable(fooVar3Name, fooVar3Value1).build()));
RoutingPair pair2 = of(original, createChain(event -> builder(event).addVariable(fooVar2Name, fooVar2Value2).addVariable(fooVar3Name, fooVar3Value2).build()));
CoreEvent result = invokeStrategyBlocking(strategy, original, asList(pair1, pair2));
assertThat(result.getVariables().keySet(), hasSize(5));
assertThat(result.getVariables().keySet(), hasItems(beforeVarName, beforeVar2Name, fooVarName, fooVarName, fooVar2Name, fooVar3Name));
assertThat(result.getVariables().get(beforeVarName).getValue(), equalTo(beforeVarValue));
assertThat(result.getVariables().get(beforeVar2Name).getValue(), equalTo(beforeVar2NewValue));
assertThat(result.getVariables().get(fooVarName).getValue(), equalTo(fooVarValue));
TypedValue fooVar2 = result.getVariables().get(fooVar2Name);
assertThat(fooVar2.getDataType(), equalTo(DataType.builder().collectionType(List.class).itemType(String.class).build()));
assertThat(((List<String>) fooVar2.getValue()), hasItems(fooVar2Value1, fooVar2Value2));
TypedValue fooVar3 = result.getVariables().get(fooVar3Name);
assertThat(fooVar3.getDataType(), equalTo(DataType.builder().collectionType(List.class).itemType(Object.class).build()));
assertThat(((List<Object>) fooVar3.getValue()), hasItems(fooVar3Value1, fooVar3Value2));
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class IdempotentMessageValidatorTestCase method testIdCheckWithHash.
@Test
public void testIdCheckWithHash() throws Exception {
String dwHashExpression = "%dw 2.0\n" + "output text/plain\n" + "import dw::Crypto\n" + "---\n" + "Crypto::hashWith(payload,'SHA-256')";
String payload = "payload to be hashed";
final BaseEventContext context = mock(BaseEventContext.class);
when(context.getCorrelationId()).thenReturn("1");
Message message = of(payload);
CoreEvent event = CoreEvent.builder(context).message(message).build();
// Set DW expression to hash value
idempotent.setIdExpression(dwHashExpression);
// Evaluate DW expression outside MessageValidator
ExpressionLanguageAdaptor expressionLanguageAdaptor = new DataWeaveExpressionLanguageAdaptor(muleContext, mock(Registry.class), new WeaveDefaultExpressionLanguageFactoryService());
TypedValue hashedValue = expressionLanguageAdaptor.evaluate(dwHashExpression, event, NULL_BINDING_CONTEXT);
// This one will process the event on the target endpoint
CoreEvent processedEvent = idempotent.process(event);
assertNotNull(processedEvent);
assertEquals(idempotent.getObjectStore().retrieve(IOUtils.toString((ByteArrayBasedCursorStreamProvider) hashedValue.getValue())), "1");
// This will not process, because the message is a duplicate
message = of(payload);
event = CoreEvent.builder(context).message(message).build();
expected.expect(ValidationException.class);
processedEvent = idempotent.process(event);
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class IdempotentMessageValidatorTestCase method differentIdsShouldBeStored.
@Test
public void differentIdsShouldBeStored() throws Exception {
String dwHashExpression = "%dw 2.0\n" + "output text/plain\n" + "import dw::Crypto\n" + "---\n" + "Crypto::SHA1(payload)";
String payload = "payload to be hashed";
String otherPayload = "this is another payload to be hashed";
final BaseEventContext context = mock(BaseEventContext.class);
when(context.getCorrelationId()).thenReturn("1");
Message message = of(payload);
CoreEvent event = CoreEvent.builder(context).message(message).build();
// Set DW expression to hash value
idempotent.setIdExpression(dwHashExpression);
// Evaluate DW expression outside MessageValidator
ExpressionLanguageAdaptor expressionLanguageAdaptor = new DataWeaveExpressionLanguageAdaptor(muleContext, mock(Registry.class), new WeaveDefaultExpressionLanguageFactoryService());
TypedValue hashedValue = expressionLanguageAdaptor.evaluate(dwHashExpression, event, NULL_BINDING_CONTEXT);
// This one will process the event on the target endpoint
CoreEvent processedEvent = idempotent.process(event);
assertNotNull(processedEvent);
assertEquals(idempotent.getObjectStore().retrieve(IOUtils.toString((ByteArrayBasedCursorStreamProvider) hashedValue.getValue())), "1");
// This will process, because the message is a new one
Message otherMessage = of(otherPayload);
event = CoreEvent.builder(context).message(otherMessage).build();
processedEvent = idempotent.process(event);
assertNotNull(processedEvent);
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class MVELExpressionLanguage method evaluate.
@Override
public TypedValue evaluate(String expression, CoreEvent event, CoreEvent.Builder eventBuilder, ComponentLocation componentLocation, BindingContext bindingContext) {
expression = removeExpressionMarker(expression);
Map<String, Object> bindingMap = bindingContext.identifiers().stream().collect(toMap(id -> id, id -> bindingContext.lookup(id).get().getValue()));
final Object value = evaluateUntyped(expression, (PrivilegedEvent) event, (PrivilegedEvent.Builder) eventBuilder, componentLocation, bindingMap);
if (value instanceof TypedValue) {
return (TypedValue) value;
} else {
final Serializable compiledExpression = expressionExecutor.getCompiledExpression(expression);
DataType dataType = event != null ? dataTypeResolver.resolve(value, (PrivilegedEvent) event, compiledExpression) : OBJECT;
return new TypedValue(value, dataType);
}
}
use of org.mule.runtime.api.metadata.TypedValue in project mule by mulesoft.
the class SetPayloadMessageProcessor method process.
@Override
public CoreEvent process(CoreEvent event) throws MuleException {
final Message.Builder builder = Message.builder(event.getMessage());
final CoreEvent.Builder eventBuilder = CoreEvent.builder(event);
if (dataType == null) {
final TypedValue typedValue = resolveTypedValue(event);
builder.value(typedValue.getValue()).mediaType(typedValue.getDataType().getMediaType());
} else {
Object value = resolveValue(event);
final DataTypeParamsBuilder dataTypeBuilder = DataType.builder(dataType).type(value == null ? Object.class : value.getClass());
builder.value(value).mediaType(dataTypeBuilder.build().getMediaType());
}
return eventBuilder.message(builder.build()).build();
}
Aggregations