Search in sources :

Example 16 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class MuleContextUtils method mockContextWithServices.

/**
 * Creates and configures a mock {@link MuleContext} to return testing services implementations.
 *
 * @return the created {@code muleContext}.
 */
public static MuleContextWithRegistries mockContextWithServices() {
    final MuleContextWithRegistries muleContext = mockMuleContext();
    SchedulerService schedulerService = spy(new SimpleUnitTestSupportSchedulerService());
    when(muleContext.getSchedulerService()).thenReturn(schedulerService);
    ErrorTypeRepository errorTypeRepository = mock(ErrorTypeRepository.class);
    when(muleContext.getErrorTypeRepository()).thenReturn(errorTypeRepository);
    when(errorTypeRepository.getErrorType(any(ComponentIdentifier.class))).thenReturn(of(mock(ErrorType.class)));
    final MuleRegistry registry = muleContext.getRegistry();
    NotificationListenerRegistry notificationListenerRegistry = mock(NotificationListenerRegistry.class);
    ConfigurationProperties configProps = mock(ConfigurationProperties.class);
    when(configProps.resolveBooleanProperty(any())).thenReturn(empty());
    ConfigurationComponentLocator configurationComponentLocator = mock(ConfigurationComponentLocator.class);
    when(configurationComponentLocator.find(any(Location.class))).thenReturn(empty());
    when(configurationComponentLocator.find(any(ComponentIdentifier.class))).thenReturn(emptyList());
    try {
        when(registry.lookupObject(NotificationListenerRegistry.class)).thenReturn(notificationListenerRegistry);
        Map<Class, Object> injectableObjects = new HashMap<>();
        injectableObjects.put(MuleContext.class, muleContext);
        injectableObjects.put(SchedulerService.class, schedulerService);
        injectableObjects.put(ErrorTypeRepository.class, errorTypeRepository);
        injectableObjects.put(ExtendedExpressionManager.class, muleContext.getExpressionManager());
        injectableObjects.put(StreamingManager.class, muleContext.getRegistry().lookupObject(StreamingManager.class));
        injectableObjects.put(ObjectStoreManager.class, muleContext.getRegistry().lookupObject(OBJECT_STORE_MANAGER));
        injectableObjects.put(NotificationDispatcher.class, muleContext.getRegistry().lookupObject(NotificationDispatcher.class));
        injectableObjects.put(NotificationListenerRegistry.class, notificationListenerRegistry);
        injectableObjects.put(ConfigurationComponentLocator.class, configurationComponentLocator);
        injectableObjects.put(ConfigurationProperties.class, configProps);
        // Ensure injection of consistent mock objects
        when(muleContext.getInjector()).thenReturn(new MocksInjector(injectableObjects));
    } catch (RegistrationException e1) {
        throw new MuleRuntimeException(e1);
    }
    return muleContext;
}
Also used : RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) HashMap(java.util.HashMap) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ConfigurationProperties(org.mule.runtime.api.component.ConfigurationProperties) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) StreamingManager(org.mule.runtime.core.api.streaming.StreamingManager) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) Location(org.mule.runtime.api.component.location.Location)

Example 17 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class MuleContextUtils method mockMuleContext.

public static MuleContextWithRegistries mockMuleContext() {
    final MuleContextWithRegistries muleContext = mock(DefaultMuleContext.class, withSettings().defaultAnswer(RETURNS_DEEP_STUBS).extraInterfaces(PrivilegedMuleContext.class));
    when(muleContext.getUniqueIdString()).thenReturn(UUID.getUUID());
    when(muleContext.getDefaultErrorHandler(empty())).thenReturn(new OnErrorPropagateHandler());
    StreamingManager streamingManager = mock(StreamingManager.class, RETURNS_DEEP_STUBS);
    try {
        MuleRegistry registry = mock(MuleRegistry.class);
        when(muleContext.getRegistry()).thenReturn(registry);
        ComponentInitialStateManager componentInitialStateManager = mock(ComponentInitialStateManager.class);
        when(componentInitialStateManager.mustStartMessageSource(any())).thenReturn(true);
        when(registry.lookupObject(ComponentInitialStateManager.SERVICE_ID)).thenReturn(componentInitialStateManager);
        doReturn(streamingManager).when(registry).lookupObject(StreamingManager.class);
        doReturn(mock(NotificationDispatcher.class)).when(registry).lookupObject(NotificationDispatcher.class);
        doReturn(mock(ObjectStoreManager.class, RETURNS_DEEP_STUBS)).when(registry).lookupObject(OBJECT_STORE_MANAGER);
    } catch (RegistrationException e) {
        throw new RuntimeException(e);
    }
    return muleContext;
}
Also used : ComponentInitialStateManager(org.mule.runtime.api.deployment.management.ComponentInitialStateManager) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) ObjectStoreManager(org.mule.runtime.api.store.ObjectStoreManager) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) StreamingManager(org.mule.runtime.core.api.streaming.StreamingManager) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) OnErrorPropagateHandler(org.mule.runtime.core.internal.exception.OnErrorPropagateHandler) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext)

Example 18 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class MVELExpressionLanguageTestCase method messageTakesPrecedenceOverEverything.

@Test
public void messageTakesPrecedenceOverEverything() throws Exception {
    mvel.setAliases(singletonMap("message", "'other1'"));
    PrivilegedEvent event = this.<PrivilegedEvent.Builder>getEventBuilder().message(of("")).addVariable("message", "other2").build();
    ((MuleContextWithRegistries) muleContext).getRegistry().registerObject("foo", (ExpressionLanguageExtension) context -> context.addVariable("message", "other3"));
    mvel.initialise();
    assertEquals(MessageContext.class, evaluate("message", event).getClass());
}
Also used : PropertyAccessException(org.mule.mvel2.PropertyAccessException) Arrays(java.util.Arrays) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) AbstractDataTypeBuilderFactory(org.mule.runtime.api.metadata.AbstractDataTypeBuilderFactory) URL(java.net.URL) JSON(org.mule.runtime.api.metadata.MediaType.JSON) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Random(java.util.Random) Collections.singletonList(java.util.Collections.singletonList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) EXPRESSION_WITH_DELIMITER(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageTestCase.Variant.EXPRESSION_WITH_DELIMITER) MessageContext(org.mule.runtime.core.internal.el.context.MessageContext) BigDecimal(java.math.BigDecimal) OptimizerFactory(org.mule.mvel2.optimizers.OptimizerFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) Function(org.mule.mvel2.ast.Function) Assert.fail(org.junit.Assert.fail) BigInteger(java.math.BigInteger) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) URI(java.net.URI) SAFE_REFLECTIVE(org.mule.mvel2.optimizers.OptimizerFactory.SAFE_REFLECTIVE) Parameterized(org.junit.runners.Parameterized) RegexExpressionLanguageFuntion(org.mule.runtime.core.internal.el.mvel.function.RegexExpressionLanguageFuntion) Collection(java.util.Collection) ValidationResult(org.mule.runtime.api.el.ValidationResult) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Message.of(org.mule.runtime.api.message.Message.of) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) CompileException(org.mule.mvel2.CompileException) STRING(org.mule.runtime.api.metadata.DataType.STRING) Matchers.is(org.hamcrest.Matchers.is) Mockito.withSettings(org.mockito.Mockito.withSettings) DefaultComponentLocation.fromSingleComponent(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Pattern(java.util.regex.Pattern) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) ParserContext(org.mule.mvel2.ParserContext) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) BindingContext(org.mule.runtime.api.el.BindingContext) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) HashMap(java.util.HashMap) MuleManifest(org.mule.runtime.core.api.config.MuleManifest) DataHandler(javax.activation.DataHandler) EXPRESSION_STRAIGHT_UP(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageTestCase.Variant.EXPRESSION_STRAIGHT_UP) DYNAMIC(org.mule.mvel2.optimizers.OptimizerFactory.DYNAMIC) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Calendar(java.util.Calendar) DataTypeMatcher.like(org.mule.tck.junit4.matcher.DataTypeMatcher.like) MuleException(org.mule.runtime.api.exception.MuleException) Component(org.mule.runtime.api.component.Component) MimeType(javax.activation.MimeType) Collections.singletonMap(java.util.Collections.singletonMap) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) DataType(org.mule.runtime.api.metadata.DataType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) UTF_16(java.nio.charset.StandardCharsets.UTF_16) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) TypedValue(org.mule.runtime.api.metadata.TypedValue) Rule(org.junit.Rule) FileReader(java.io.FileReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Test(org.junit.Test)

Example 19 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class AbstractFlowConstructTestCase method testRegisterUnregister.

@Test
public void testRegisterUnregister() throws MuleException, Exception {
    FlowConstruct construct = getFlowConstruct();
    ((MuleContextWithRegistries) muleContext).getRegistry().registerFlowConstruct(construct);
    assertNotNull(((MuleContextWithRegistries) muleContext).getRegistry().lookupFlowConstruct(construct.getName()));
}
Also used : MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Test(org.junit.Test)

Example 20 with MuleContextWithRegistries

use of org.mule.runtime.core.internal.context.MuleContextWithRegistries in project mule by mulesoft.

the class MVELExpressionLanguageTestCase method appTakesPrecedenceOverEverything.

@Test
public void appTakesPrecedenceOverEverything() throws Exception {
    mvel.setAliases(singletonMap("app", "'other1'"));
    PrivilegedEvent event = this.<PrivilegedEvent.Builder>getEventBuilder().message(of("")).addVariable("app", "otherb").build();
    ((MuleContextWithRegistries) muleContext).getRegistry().registerObject("foo", (ExpressionLanguageExtension) context -> context.addVariable("app", "otherc"));
    mvel.initialise();
    assertEquals(MVELArtifactContext.class, evaluate("app", event).getClass());
}
Also used : PropertyAccessException(org.mule.mvel2.PropertyAccessException) Arrays(java.util.Arrays) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) AbstractDataTypeBuilderFactory(org.mule.runtime.api.metadata.AbstractDataTypeBuilderFactory) URL(java.net.URL) JSON(org.mule.runtime.api.metadata.MediaType.JSON) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) Random(java.util.Random) Collections.singletonList(java.util.Collections.singletonList) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) EXPRESSION_WITH_DELIMITER(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageTestCase.Variant.EXPRESSION_WITH_DELIMITER) MessageContext(org.mule.runtime.core.internal.el.context.MessageContext) BigDecimal(java.math.BigDecimal) OptimizerFactory(org.mule.mvel2.optimizers.OptimizerFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) Function(org.mule.mvel2.ast.Function) Assert.fail(org.junit.Assert.fail) BigInteger(java.math.BigInteger) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) URI(java.net.URI) SAFE_REFLECTIVE(org.mule.mvel2.optimizers.OptimizerFactory.SAFE_REFLECTIVE) Parameterized(org.junit.runners.Parameterized) RegexExpressionLanguageFuntion(org.mule.runtime.core.internal.el.mvel.function.RegexExpressionLanguageFuntion) Collection(java.util.Collection) ValidationResult(org.mule.runtime.api.el.ValidationResult) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Message.of(org.mule.runtime.api.message.Message.of) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AbstractMuleContextTestCase(org.mule.tck.junit4.AbstractMuleContextTestCase) CompileException(org.mule.mvel2.CompileException) STRING(org.mule.runtime.api.metadata.DataType.STRING) Matchers.is(org.hamcrest.Matchers.is) Mockito.withSettings(org.mockito.Mockito.withSettings) DefaultComponentLocation.fromSingleComponent(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) Pattern(java.util.regex.Pattern) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) ParserContext(org.mule.mvel2.ParserContext) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) BindingContext(org.mule.runtime.api.el.BindingContext) DefaultComponentLocation(org.mule.runtime.dsl.api.component.config.DefaultComponentLocation) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) HashMap(java.util.HashMap) MuleManifest(org.mule.runtime.core.api.config.MuleManifest) DataHandler(javax.activation.DataHandler) EXPRESSION_STRAIGHT_UP(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageTestCase.Variant.EXPRESSION_STRAIGHT_UP) DYNAMIC(org.mule.mvel2.optimizers.OptimizerFactory.DYNAMIC) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) Calendar(java.util.Calendar) DataTypeMatcher.like(org.mule.tck.junit4.matcher.DataTypeMatcher.like) MuleException(org.mule.runtime.api.exception.MuleException) Component(org.mule.runtime.api.component.Component) MimeType(javax.activation.MimeType) Collections.singletonMap(java.util.Collections.singletonMap) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) DataType(org.mule.runtime.api.metadata.DataType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) UTF_16(java.nio.charset.StandardCharsets.UTF_16) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) OBJECT(org.mule.runtime.api.metadata.DataType.OBJECT) TypedValue(org.mule.runtime.api.metadata.TypedValue) Rule(org.junit.Rule) FileReader(java.io.FileReader) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Test(org.junit.Test)

Aggregations

MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)25 Test (org.junit.Test)12 MuleRegistry (org.mule.runtime.core.internal.registry.MuleRegistry)7 Before (org.junit.Before)6 NotificationDispatcher (org.mule.runtime.api.notification.NotificationDispatcher)6 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HashMap (java.util.HashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Map (java.util.Map)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 DataType (org.mule.runtime.api.metadata.DataType)4 Transformer (org.mule.runtime.core.api.transformer.Transformer)4 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 URI (java.net.URI)3