Search in sources :

Example 1 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation in project mule by mulesoft.

the class MessageProcessingFlowTraceManagerTestCase method createMockProcessor.

public Processor createMockProcessor(String processorPath, boolean useLocationSettings) {
    ComponentLocation componentLocation = mock(ComponentLocation.class);
    when(componentLocation.getLocation()).thenReturn(processorPath);
    when(componentLocation.getFileName()).thenReturn(useLocationSettings ? of(CONFIG_FILE_NAME) : empty());
    when(componentLocation.getLineInFile()).thenReturn(useLocationSettings ? of(LINE_NUMBER) : empty());
    Component annotatedMessageProcessor = (Component) mock(Processor.class, withSettings().extraInterfaces(Component.class).defaultAnswer(RETURNS_DEEP_STUBS));
    when(annotatedMessageProcessor.getAnnotation(any())).thenReturn(null);
    when(annotatedMessageProcessor.getLocation()).thenReturn(componentLocation);
    return (Processor) annotatedMessageProcessor;
}
Also used : ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) Processor(org.mule.runtime.core.api.processor.Processor) Component(org.mule.runtime.api.component.Component)

Example 2 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation 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 3 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation in project mule by mulesoft.

the class NotificationHelperTestCase method fireNotificationUsingLocation.

@Test
public void fireNotificationUsingLocation() {
    final LocationPart flowPart = mock(LocationPart.class);
    when(flowPart.getPartPath()).thenReturn("flowName");
    final ComponentLocation location = mock(ComponentLocation.class);
    when(location.getParts()).thenReturn(Collections.singletonList(flowPart));
    when(location.getComponentIdentifier()).thenReturn(TypedComponentIdentifier.builder().type(SOURCE).identifier(buildFromStringRepresentation("http:listener")).build());
    when(messageSource.getLocation()).thenReturn(location);
    final FlowConstruct flowConstruct = mock(FlowConstruct.class, withSettings().extraInterfaces(Component.class));
    when(flowConstruct.getMuleContext()).thenReturn(muleContext);
    final int action = 100;
    helper.fireNotification(messageSource, event, location, action);
    assertConnectorMessageNotification(eventNotificationHandler, messageSource, location, action);
}
Also used : ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) LocationPart(org.mule.runtime.api.component.location.LocationPart) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Component(org.mule.runtime.api.component.Component) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 4 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method firstInterceptorMutatesEventAfter.

@Test
public void firstInterceptorMutatesEventAfter() throws Exception {
    ProcessorInterceptor interceptor1 = prepareInterceptor(new TestProcessorInterceptor("outer") {

        @Override
        public void after(ComponentLocation location, InterceptionEvent event, Optional<Throwable> thrown) {
            event.message(Message.of(TEST_PAYLOAD));
        }
    });
    ProcessorInterceptor interceptor2 = prepareInterceptor(new TestProcessorInterceptor("inner") {
    });
    startFlowWithInterceptors(interceptor1, interceptor2);
    CoreEvent result = process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    assertThat(result.getMessage().getPayload().getValue(), is(TEST_PAYLOAD));
    assertThat(result.getError().isPresent(), is(false));
    if (useMockInterceptor) {
        InOrder inOrder = inOrder(processor, interceptor1, interceptor2);
        inOrder.verify(interceptor1).before(any(), any(), any());
        inOrder.verify(interceptor2).before(any(), any(), any());
        inOrder.verify(interceptor1).around(any(), any(), any(), any());
        inOrder.verify(interceptor2).around(any(), any(), any(), any());
        inOrder.verify(processor).process(argThat(hasPayloadValue("")));
        inOrder.verify(interceptor2).after(any(), any(), eq(empty()));
        inOrder.verify(interceptor1).after(any(), any(), eq(empty()));
        assertThat(((InternalEvent) result).getInternalParameters().entrySet(), hasSize(0));
        verifyParametersResolvedAndDisposed(times(1));
    }
}
Also used : DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 5 with ComponentLocation

use of org.mule.runtime.api.component.location.ComponentLocation in project mule by mulesoft.

the class ReactiveInterceptorAdapterTestCase method interceptorMutatesEventAroundAfterFailWithCause.

@Test
public void interceptorMutatesEventAroundAfterFailWithCause() throws Exception {
    Throwable cause = new RuntimeException("");
    ProcessorInterceptor interceptor = prepareInterceptor(new ProcessorInterceptor() {

        @Override
        public CompletableFuture<InterceptionEvent> around(ComponentLocation location, Map<String, ProcessorParameterValue> parameters, InterceptionEvent event, InterceptionAction action) {
            event.message(Message.of(TEST_PAYLOAD));
            return action.fail(cause);
        }
    });
    startFlowWithInterceptors(interceptor);
    expected.expectCause(sameInstance(cause));
    try {
        process(flow, eventBuilder(muleContext).message(Message.of("")).build());
    } finally {
        if (useMockInterceptor) {
            InOrder inOrder = inOrder(processor, interceptor);
            inOrder.verify(interceptor).before(any(), mapArgWithEntry("param", ""), any());
            inOrder.verify(interceptor).around(any(), mapArgWithEntry("param", ""), any(), any());
            inOrder.verify(processor, never()).process(any());
            inOrder.verify(interceptor).after(any(), argThat(interceptionHasPayloadValue(TEST_PAYLOAD)), eq(of(cause)));
            verifyParametersResolvedAndDisposed(times(1));
        }
    }
}
Also used : DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) InOrder(org.mockito.InOrder) ProcessorInterceptor(org.mule.runtime.api.interception.ProcessorInterceptor) InterceptionEvent(org.mule.runtime.api.interception.InterceptionEvent) ProcessorParameterValue(org.mule.runtime.api.interception.ProcessorParameterValue) InterceptionAction(org.mule.runtime.api.interception.InterceptionAction) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

ComponentLocation (org.mule.runtime.api.component.location.ComponentLocation)54 Test (org.junit.Test)46 ProcessorInterceptor (org.mule.runtime.api.interception.ProcessorInterceptor)44 SmallTest (org.mule.tck.size.SmallTest)44 DefaultComponentLocation (org.mule.runtime.dsl.api.component.config.DefaultComponentLocation)43 InOrder (org.mockito.InOrder)41 InterceptionEvent (org.mule.runtime.api.interception.InterceptionEvent)41 ProcessorParameterValue (org.mule.runtime.api.interception.ProcessorParameterValue)35 CompletableFuture (java.util.concurrent.CompletableFuture)29 InterceptionAction (org.mule.runtime.api.interception.InterceptionAction)28 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)26 InternalEvent (org.mule.runtime.core.internal.message.InternalEvent)24 ExpressionRuntimeException (org.mule.runtime.core.api.expression.ExpressionRuntimeException)23 Map (java.util.Map)7 Component (org.mule.runtime.api.component.Component)7 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)7 HashMap (java.util.HashMap)6 MuleException (org.mule.runtime.api.exception.MuleException)6 ProcessorInterceptorFactory (org.mule.runtime.api.interception.ProcessorInterceptorFactory)6 Message (org.mule.runtime.api.message.Message)6