@Override public CoreEvent process(CoreEvent event) throws MuleException { try { Message msg; Error error = null; String payload = ((InternalEvent) event).getMessageAsString(muleContext); if (payload.indexOf(rejectIfMatches) >= 0) { throw new DefaultMuleException("Saw " + rejectIfMatches); } else if (payload.toLowerCase().indexOf(rejectIfMatches) >= 0) { Exception exception = new Exception(); error = createErrorMock(exception); msg = InternalMessage.builder().nullValue().exceptionPayload(new DefaultExceptionPayload(exception)).build(); } else { msg = of("No " + rejectIfMatches); } CoreEvent muleEvent = eventBuilder(muleContext).message(msg).error(error).build(); return muleEvent; } catch (Exception e) { throw new DefaultMuleException(e); } } }
InternalMessage.builder().value(payload).inboundProperties(inboundProps).outboundProperties(outboundProps).build(); Splitter splitter = new Splitter(); Grabber grabber = new Grabber();
@Test public void createNewMessageCollectionViaMessageInterfaceCopy() { List<String> htmlStringList = new ArrayList<>(); htmlStringList.add("HTML1"); htmlStringList.add("HTML2"); htmlStringList.add("HTML3"); Message message = InternalMessage.builder().collectionValue(htmlStringList, String.class).itemMediaType(HTML) .build(); Message copy = InternalMessage.builder(message).build(); assertThat(copy.getPayload().getValue(), is(htmlStringList)); assertThat(copy.getPayload().getDataType().getType(), equalTo(ArrayList.class)); assertThat(copy.getPayload().getDataType().getMediaType(), is(ANY)); assertThat(copy.getPayload().getDataType(), instanceOf(DefaultCollectionDataType.class)); assertThat(((DefaultCollectionDataType) copy.getPayload().getDataType()).getItemDataType().getMediaType(), equalTo(HTML)); }
InternalMessage.builder().value(payload).inboundProperties(inboundProps).outboundProperties(outboundProps).build(); Splitter splitter = new Splitter(); Grabber grabber = new Grabber();
properties.put("apple", apple); InternalMessage message = InternalMessage.builder().value(TEST_MESSAGE).outboundProperties(properties).build(); assertTrue(message.getOutboundProperty("boolean", false)); assertEquals(new Integer(24), message.getOutboundProperty("number", 0));
@Override public void run() { Message okMessage = InternalMessage.builder().value("OK").build(); BaseEventContext context = mock(BaseEventContext.class); when(context.getCorrelationId()).thenReturn("1"); CoreEvent event = CoreEvent.builder(context).message(okMessage).build(); try { event = validator.process(event); } catch (ValidationException e) { if (event != null) { processedEvents.incrementAndGet(); } return; } catch (Throwable e) { e.printStackTrace(); synchronized (errorHappenedInChildThreads) { errorHappenedInChildThreads = true; } } } }
@Override public CoreEvent process(CoreEvent event) throws MuleException { try { Message msg; Error error = null; String payload = ((InternalEvent) event).getMessageAsString(muleContext); if (payload.indexOf(rejectIfMatches) >= 0) { throw new DefaultMuleException("Saw " + rejectIfMatches); } else if (payload.toLowerCase().indexOf(rejectIfMatches) >= 0) { Exception exception = new Exception(); error = createErrorMock(exception); msg = InternalMessage.builder().nullValue().exceptionPayload(new DefaultExceptionPayload(exception)).build(); } else { msg = of("No " + rejectIfMatches); } CoreEvent muleEvent = eventBuilder(muleContext).message(msg).error(error).build(); return muleEvent; } catch (Exception e) { throw new DefaultMuleException(e); } } }
@Test public void createNewMessageCollectionViaMessageInterfaceCopy() { List<String> htmlStringList = new ArrayList<>(); htmlStringList.add("HTML1"); htmlStringList.add("HTML2"); htmlStringList.add("HTML3"); Message message = InternalMessage.builder().collectionValue(htmlStringList, String.class).itemMediaType(HTML) .build(); Message copy = InternalMessage.builder(message).build(); assertThat(copy.getPayload().getValue(), is(htmlStringList)); assertThat(copy.getPayload().getDataType().getType(), equalTo(ArrayList.class)); assertThat(copy.getPayload().getDataType().getMediaType(), is(ANY)); assertThat(copy.getPayload().getDataType(), instanceOf(DefaultCollectionDataType.class)); assertThat(((DefaultCollectionDataType) copy.getPayload().getDataType()).getItemDataType().getMediaType(), equalTo(HTML)); }
@Test public void idempotentReceiver() throws Exception { final BaseEventContext contextA = mock(BaseEventContext.class); when(contextA.getCorrelationId()).thenReturn("1"); Message okMessage = InternalMessage.builder().value("OK").build(); CoreEvent event = CoreEvent.builder(contextA).message(okMessage).build(); // This one will process the event on the target endpoint CoreEvent processedEvent = idempotent.process(event); assertThat(processedEvent, sameInstance(event)); final BaseEventContext contextB = mock(BaseEventContext.class); when(contextB.getCorrelationId()).thenReturn("1"); // This will not process, because the ID is a duplicate event = CoreEvent.builder(contextB).message(okMessage).build(); expected.expect(ValidationException.class); processedEvent = idempotent.process(event); }
@Test public void createNewMessageCollectionViaMessageInterface() { List<String> htmlStringList = new ArrayList<>(); htmlStringList.add("HTML1"); htmlStringList.add("HTML2"); htmlStringList.add("HTML3"); Message message = InternalMessage.builder().collectionValue(htmlStringList, String.class).itemMediaType(HTML) .build(); assertThat(message.getPayload().getValue(), is(htmlStringList)); assertThat(message.getPayload().getDataType().getType(), equalTo(ArrayList.class)); assertThat(message.getPayload().getDataType().getMediaType(), is(ANY)); assertThat(message.getPayload().getDataType(), instanceOf(DefaultCollectionDataType.class)); assertThat(((DefaultCollectionDataType) message.getPayload().getDataType()).getItemDataType().getMediaType(), equalTo(HTML)); }
@Test public void propagateMessage() throws Exception { MessageEnricher enricher = baseEnricher(); enricher.addEnrichExpressionPair(new EnrichExpressionPair("#[mel:message.outboundProperties.myHeader]")); enricher.setEnrichmentMessageProcessor((InternalTestProcessor) event -> CoreEvent.builder(event) .message(InternalMessage.builder(event.getMessage()).value("enriched").build()).build()); CoreEvent in = eventBuilder(muleContext).message(InternalMessage.builder().value("").addOutboundProperty("foo", "bar").build()).build(); CoreEvent out = process(enricher, in); assertThat(out.getCorrelationId(), equalTo(in.getCorrelationId())); assertThat(((InternalMessage) out.getMessage()).getOutboundProperty("foo"), equalTo("bar")); assertThat(out.getMessage().getPayload().getValue(), equalTo(in.getMessage().getPayload().getValue())); }
private Message createMuleMessage() { return InternalMessage.builder().value(TEST_PAYLOAD) .addOutboundProperty("Message", "Message").build(); }
@Test public void assignValueToInboundProperty() throws Exception { event = CoreEvent.builder(context) .message(InternalMessage.builder().value("").inboundProperties(singletonMap("foo", "bar")).build()) .build(); assertUnsupportedOperation("message.inboundProperties['foo']='bar'", event); }
@Test public void inboundProperty() throws Exception { event = CoreEvent.builder(context) .message(InternalMessage.builder().value("").inboundProperties(singletonMap("foo", "bar")).build()) .build(); assertEquals("bar", evaluate("message.inboundProperties['foo']", event)); }
@Test public void setsCustomInboundPropertyDataType() throws Exception { MediaType mediaType = APPLICATION_XML_CUSTOM; InternalMessage message = InternalMessage.builder().value(TEST_PAYLOAD) .addInboundProperty(TEST_PROPERTY, TEST_PAYLOAD, mediaType).build(); assertInboundPropertyDataType(message, DataType.builder().type(String.class).mediaType(mediaType).build()); }
@Test public void setsDefaultInboundPropertyDataType() throws Exception { InternalMessage message = InternalMessage.builder().value(TEST_PAYLOAD).addInboundProperty(TEST_PROPERTY, TEST_PAYLOAD).build(); assertDefaultInboundPropertyDataType(message); }
@Test public void testStreamPayloadSerialization() throws Exception { InputStream stream = new ByteArrayInputStream(TEST_MESSAGE.getBytes()); final Message message = InternalMessage.builder().value(stream).addOutboundProperty("foo", "bar").build(); currentMuleContext.set(muleContext); InternalMessage deserializedMessage = serializationRoundtrip(message); assertEquals(byte[].class, deserializedMessage.getPayload().getDataType().getType()); byte[] payload = (byte[]) deserializedMessage.getPayload().getValue(); assertTrue(Arrays.equals(TEST_MESSAGE.getBytes(), payload)); }
/** * Creates the event builder from an event context and a flow construct * * @param eventContext Flow construct from where to create the new event */ public EventBuilder(EventContext eventContext) { this.muleEventBuilder = PrivilegedEvent.builder(eventContext); this.muleMessageBuilder = InternalMessage.builder().nullValue(); this.session = new DefaultMuleSession(); }
@Test public void setsDefaultOutboundScopePropertyDataType() throws Exception { InternalMessage message = InternalMessage.builder().value(TEST_PAYLOAD).addOutboundProperty(TEST_PROPERTY, TEST_PAYLOAD) .build(); assertDefaultOutboundPropertyDataType(message); }
@Test public void setsCustomOutboundPropertyDataType() throws Exception { MediaType mediaType = APPLICATION_XML_CUSTOM; InternalMessage message = InternalMessage.builder().value(TEST_PAYLOAD) .addOutboundProperty(TEST_PROPERTY, TEST_PAYLOAD, mediaType).build(); assertOutboundPropertyDataType(message, DataType.builder().type(String.class).mediaType(mediaType).build()); }