Search in sources :

Example 21 with InternalMessage

use of org.mule.runtime.core.internal.message.InternalMessage in project mule by mulesoft.

the class MessageContextTestCase method payloadAsDataType.

@Test
public void payloadAsDataType() throws Exception {
    InternalMessage transformedMessage = mock(InternalMessage.class, RETURNS_DEEP_STUBS);
    TransformationService transformationService = mock(TransformationService.class);
    when(transformedMessage.getPayload()).thenReturn(new TypedValue<Object>(TEST_PAYLOAD, STRING));
    muleContext.setTransformationService(transformationService);
    when(transformationService.transform(event.getMessage(), DataType.STRING)).thenReturn(transformedMessage);
    Object result = evaluate("message.payloadAs(" + DataType.class.getName() + ".STRING)", event);
    assertSame(TEST_PAYLOAD, result);
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) DataType(org.mule.runtime.api.metadata.DataType) TransformationService(org.mule.runtime.api.transformation.TransformationService) Test(org.junit.Test)

Example 22 with InternalMessage

use of org.mule.runtime.core.internal.message.InternalMessage in project mule by mulesoft.

the class SplitterTestCase method assertSplitParts.

private void assertSplitParts(int count, boolean counted, Map<String, Serializable> inboundProps, Map<String, Serializable> outboundProps, Map<String, Object> invocationProps, List<CoreEvent> splits, Set<Object> actualSequences) {
    for (CoreEvent event : splits) {
        Message msg = event.getMessage();
        assertTrue(msg.getPayload().getValue() instanceof String);
        if (counted) {
            assertThat(event.getGroupCorrelation().get().getGroupSize().getAsInt(), is(count));
        } else {
            assertThat(event.getGroupCorrelation().get().getGroupSize().isPresent(), is(false));
        }
        actualSequences.add(event.getGroupCorrelation().get().getSequence());
        String str = (String) msg.getPayload().getValue();
        assertTrue(TEST_LIST_MULTIPLE.contains(str));
        for (String key : inboundProps.keySet()) {
            assertEquals(((InternalMessage) msg).getInboundProperty(key), inboundProps.get(key));
        }
        for (String key : outboundProps.keySet()) {
            assertEquals(((InternalMessage) msg).getOutboundProperty(key), outboundProps.get(key));
        }
        for (String key : invocationProps.keySet()) {
            assertEquals(event.getVariables().get(key).getValue(), invocationProps.get(key));
        }
    }
}
Also used : Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent)

Example 23 with InternalMessage

use of org.mule.runtime.core.internal.message.InternalMessage in project mule by mulesoft.

the class CustomJavaSerializationProtocolTestCase method serializeWithoutDefaultConstructorFromArtifactClassLoader.

@Test
public final void serializeWithoutDefaultConstructorFromArtifactClassLoader() throws Exception {
    final File compiledClasses = new File(temporaryFolder.getRoot(), "compiledClasses");
    compiledClasses.mkdirs();
    final File sourceFile = new File(getClass().getResource("/org/foo/SerializableClass.java").toURI());
    CompilerUtils.SingleClassCompiler compiler = new CompilerUtils.SingleClassCompiler();
    compiler.compile(sourceFile);
    final URL[] urls = new URL[] { compiler.getTargetFolder().toURL() };
    final ClassLoaderLookupPolicy lookupPolicy = mock(ClassLoaderLookupPolicy.class);
    when(lookupPolicy.getClassLookupStrategy(any())).thenReturn(PARENT_FIRST);
    final MuleArtifactClassLoader artifactClassLoader = new MuleArtifactClassLoader(ARTIFACT_ID, new ArtifactDescriptor(ARTIFACT_NAME), urls, getClass().getClassLoader(), lookupPolicy);
    when(classLoaderRepository.getId(artifactClassLoader)).thenReturn(Optional.of(ARTIFACT_ID));
    when(classLoaderRepository.find(ARTIFACT_ID)).thenReturn(Optional.of(artifactClassLoader));
    final Class<?> echoTestClass = artifactClassLoader.loadClass(SERIALIZABLE_CLASS);
    final Object payload = echoTestClass.newInstance();
    setObjectName(payload);
    CoreEvent event = eventBuilder(muleContext).message(of(payload)).build();
    byte[] bytes = serializationProtocol.serialize(event.getMessage());
    InternalMessage message = serializationProtocol.deserialize(bytes);
    Object deserialized = message.getPayload().getValue();
    assertThat(deserialized.getClass().getName(), equalTo(SERIALIZABLE_CLASS));
    assertThat(deserialized.getClass().getClassLoader(), equalTo(artifactClassLoader));
    assertThat(deserialized, equalTo(payload));
    assertThat(getObjectName(deserialized), equalTo(INSTANCE_NAME));
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) ClassLoaderLookupPolicy(org.mule.runtime.module.artifact.api.classloader.ClassLoaderLookupPolicy) URL(java.net.URL) ArtifactDescriptor(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptor) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) CompilerUtils(org.mule.tck.util.CompilerUtils) MuleArtifactClassLoader(org.mule.runtime.module.artifact.api.classloader.MuleArtifactClassLoader) File(java.io.File) Test(org.junit.Test)

Example 24 with InternalMessage

use of org.mule.runtime.core.internal.message.InternalMessage in project mule by mulesoft.

the class LoggerMessageProcessorTestCase method buildMuleEvent.

private CoreEvent buildMuleEvent() {
    CoreEvent event = mock(CoreEvent.class);
    InternalMessage message = mock(InternalMessage.class);
    when(message.toString()).thenReturn("text to log");
    when(event.getMessage()).thenReturn(message);
    return event;
}
Also used : InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent)

Example 25 with InternalMessage

use of org.mule.runtime.core.internal.message.InternalMessage in project mule by mulesoft.

the class SimpleCollectionAggregatorTestCase method testAggregateMultipleEvents.

@Test
public void testAggregateMultipleEvents() throws Exception {
    Flow flow = createAndRegisterFlow(muleContext, APPLE_FLOW, componentLocator);
    assertNotNull(flow);
    SimpleCollectionAggregator router = new SimpleCollectionAggregator();
    SensingNullMessageProcessor sensingMessageProcessor = getSensingNullMessageProcessor();
    router.setListener(sensingMessageProcessor);
    router.setAnnotations(getAppleFlowComponentLocationAnnotations());
    initialiseIfNeeded(router, true, muleContext);
    EventContext executionContext = create(flow, TEST_CONNECTOR_LOCATION, "foo");
    Message message1 = Message.of("test event A");
    Message message2 = Message.of("test event B");
    Message message3 = Message.of("test event C");
    CoreEvent event1 = InternalEvent.builder(executionContext).message(message1).groupCorrelation(Optional.of(GroupCorrelation.of(0, 3))).build();
    CoreEvent event2 = InternalEvent.builder(executionContext).message(message2).build();
    CoreEvent event3 = InternalEvent.builder(executionContext).message(message3).build();
    assertNull(router.process(event1));
    assertNull(router.process(event2));
    CoreEvent resultEvent = router.process(event3);
    assertNotNull(sensingMessageProcessor.event);
    assertThat(resultEvent, equalTo(sensingMessageProcessor.event));
    Message nextMessage = sensingMessageProcessor.event.getMessage();
    assertNotNull(nextMessage);
    assertTrue(nextMessage.getPayload().getValue() instanceof List<?>);
    List<InternalMessage> list = (List<InternalMessage>) nextMessage.getPayload().getValue();
    assertEquals(3, list.size());
    String[] results = new String[3];
    list.stream().map(msg -> msg.getPayload().getValue()).collect(toList()).toArray(results);
    // Need to sort result because of MULE-5998
    Arrays.sort(results);
    assertEquals("test event A", results[0]);
    assertEquals("test event B", results[1]);
    assertEquals("test event C", results[2]);
}
Also used : EventContext(org.mule.runtime.api.event.EventContext) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) Message(org.mule.runtime.api.message.Message) InternalMessage(org.mule.runtime.core.internal.message.InternalMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) SensingNullMessageProcessor(org.mule.tck.SensingNullMessageProcessor) MuleTestUtils.createAndRegisterFlow(org.mule.tck.MuleTestUtils.createAndRegisterFlow) Flow(org.mule.runtime.core.api.construct.Flow) Test(org.junit.Test)

Aggregations

InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)27 Test (org.junit.Test)22 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)20 Message (org.mule.runtime.api.message.Message)12 DataType (org.mule.runtime.api.metadata.DataType)12 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)7 UTF_16 (java.nio.charset.StandardCharsets.UTF_16)6 ArrayList (java.util.ArrayList)6 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)6 CoreMatchers.hasItem (org.hamcrest.CoreMatchers.hasItem)6 CoreMatchers.not (org.hamcrest.CoreMatchers.not)6 CoreMatchers.sameInstance (org.hamcrest.CoreMatchers.sameInstance)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertNull (org.junit.Assert.assertNull)6 Assert.assertThat (org.junit.Assert.assertThat)6 Rule (org.junit.Rule)6 ExpectedException (org.junit.rules.ExpectedException)6 ExpectedException.none (org.junit.rules.ExpectedException.none)6 Message.of (org.mule.runtime.api.message.Message.of)6 JSON (org.mule.runtime.api.metadata.MediaType.JSON)6