Search in sources :

Example 21 with TypedValue

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));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) MessageProcessors.newChain(org.mule.runtime.core.privileged.processor.MessageProcessors.newChain) Schedulers.fromExecutorService(reactor.core.scheduler.Schedulers.fromExecutorService) Apple(org.mule.tck.testmodels.fruit.Apple) IntStream.range(java.util.stream.IntStream.range) Message(org.mule.runtime.api.message.Message) MAX_VALUE(java.lang.Integer.MAX_VALUE) TimeoutException(java.util.concurrent.TimeoutException) Matchers.hasItems(org.hamcrest.Matchers.hasItems) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) Scheduler(org.mule.runtime.api.scheduler.Scheduler) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Thread.sleep(java.lang.Thread.sleep) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Flux.fromIterable(reactor.core.publisher.Flux.fromIterable) ForkJoinStrategy(org.mule.runtime.core.internal.routing.ForkJoinStrategy) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) Message.of(org.mule.runtime.api.message.Message.of) ProcessingStrategy(org.mule.runtime.core.api.processor.strategy.ProcessingStrategy) MessageProcessorChain(org.mule.runtime.core.privileged.processor.chain.MessageProcessorChain) Matchers.any(org.mockito.Matchers.any) List(java.util.List) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) Mockito.atMost(org.mockito.Mockito.atMost) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) RoutingPair.of(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair.of) Mockito.mock(org.mockito.Mockito.mock) CompositeRoutingException(org.mule.runtime.core.privileged.routing.CompositeRoutingException) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) InternalProcessor(org.mule.runtime.core.privileged.processor.InternalProcessor) Callable(java.util.concurrent.Callable) Mockito.spy(org.mockito.Mockito.spy) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) TIMEOUT(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.TIMEOUT) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Feature(io.qameta.allure.Feature) MuleException(org.mule.runtime.api.exception.MuleException) RoutingResult(org.mule.runtime.core.privileged.routing.RoutingResult) Mono.from(reactor.core.publisher.Mono.from) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ExpectedException(org.junit.rules.ExpectedException) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Before(org.junit.Before) CheckedConsumer(org.mule.runtime.core.api.util.func.CheckedConsumer) DataType(org.mule.runtime.api.metadata.DataType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Mockito.times(org.mockito.Mockito.times) FORK_JOIN_STRATEGIES(org.mule.test.allure.AllureConstants.ForkJoinStrategiesFeature.FORK_JOIN_STRATEGIES) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) TypedValue(org.mule.runtime.api.metadata.TypedValue) Collectors.toList(java.util.stream.Collectors.toList) Mockito.never(org.mockito.Mockito.never) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) Rule(org.junit.Rule) Exceptions.rxExceptionToMuleException(org.mule.runtime.core.api.rx.Exceptions.rxExceptionToMuleException) Description(io.qameta.allure.Description) Apple(org.mule.tck.testmodels.fruit.Apple) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RoutingPair(org.mule.runtime.core.internal.routing.ForkJoinStrategy.RoutingPair) TypedValue(org.mule.runtime.api.metadata.TypedValue) Description(io.qameta.allure.Description) Test(org.junit.Test)

Example 22 with TypedValue

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);
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) WeaveDefaultExpressionLanguageFactoryService(org.mule.weave.v2.el.WeaveDefaultExpressionLanguageFactoryService) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) ExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.ExpressionLanguageAdaptor) DataWeaveExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Registry(org.mule.runtime.api.artifact.Registry) DataWeaveExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 23 with TypedValue

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);
}
Also used : BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) WeaveDefaultExpressionLanguageFactoryService(org.mule.weave.v2.el.WeaveDefaultExpressionLanguageFactoryService) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) ExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.ExpressionLanguageAdaptor) DataWeaveExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Registry(org.mule.runtime.api.artifact.Registry) DataWeaveExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.dataweave.DataWeaveExpressionLanguageAdaptor) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 24 with TypedValue

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);
    }
}
Also used : ExtendedExpressionLanguageAdaptor(org.mule.runtime.core.internal.el.ExtendedExpressionLanguageAdaptor) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) CoreMessages(org.mule.runtime.core.api.config.i18n.CoreMessages) AbstractDataTypeBuilderFactory(org.mule.runtime.api.metadata.AbstractDataTypeBuilderFactory) ExpressionCompiler(org.mule.mvel2.compiler.ExpressionCompiler) BindingContext(org.mule.runtime.api.el.BindingContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) HashMap(java.util.HashMap) DEFAULT_EXPRESSION_POSTFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_POSTFIX) DataHandler(javax.activation.DataHandler) ValidationResult.success(org.mule.runtime.api.el.ValidationResult.success) CachedMapVariableResolverFactory(org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory) ParserConfiguration(org.mule.mvel2.ParserConfiguration) Inject(javax.inject.Inject) BigDecimal(java.math.BigDecimal) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Collectors.toMap(java.util.stream.Collectors.toMap) MuleContext(org.mule.runtime.core.api.MuleContext) Map(java.util.Map) MimeType(javax.activation.MimeType) Function(org.mule.mvel2.ast.Function) Collections.singletonMap(java.util.Collections.singletonMap) BigInteger(java.math.BigInteger) IOUtils(org.mule.runtime.core.api.util.IOUtils) StringUtils.replace(org.apache.commons.lang3.StringUtils.replace) ValidationResult.failure(org.mule.runtime.api.el.ValidationResult.failure) DEFAULT_EXPRESSION_PREFIX(org.mule.runtime.core.api.el.ExpressionManager.DEFAULT_EXPRESSION_PREFIX) Iterator(java.util.Iterator) DataType(org.mule.runtime.api.metadata.DataType) PREFIX_EXPR_SEPARATOR(org.mule.runtime.core.internal.el.DefaultExpressionManager.PREFIX_EXPR_SEPARATOR) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ValidationResult(org.mule.runtime.api.el.ValidationResult) IOException(java.io.IOException) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) MvelEnricherDataTypePropagator(org.mule.runtime.core.internal.el.mvel.datatype.MvelEnricherDataTypePropagator) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Serializable(java.io.Serializable) TypedValue(org.mule.runtime.api.metadata.TypedValue) MEL_PREFIX(org.mule.runtime.core.internal.el.DefaultExpressionManager.MEL_PREFIX) VariableResolverFactory(org.mule.mvel2.integration.VariableResolverFactory) CoreMessages.objectIsNull(org.mule.runtime.core.api.config.i18n.CoreMessages.objectIsNull) CompileException(org.mule.mvel2.CompileException) Entry(java.util.Map.Entry) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompilerTools(org.mule.mvel2.util.CompilerTools) MvelDataTypeResolver(org.mule.runtime.core.internal.el.mvel.datatype.MvelDataTypeResolver) Pattern(java.util.regex.Pattern) Serializable(java.io.Serializable) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) DataType(org.mule.runtime.api.metadata.DataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 25 with TypedValue

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();
}
Also used : DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue)

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