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;
}
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;
}
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());
}
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()));
}
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());
}
Aggregations