Search in sources :

Example 26 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class MailProducerTest method testProducerBodyIsMimeMessage.

@Test
public void testProducerBodyIsMimeMessage() throws Exception {
    Mailbox.clearAll();
    getMockEndpoint("mock:result").expectedMessageCount(1);
    Address from = new InternetAddress("fromCamelTest@localhost");
    Address to = new InternetAddress("recipient2@localhost");
    Session session = Session.getDefaultInstance(System.getProperties());
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setFrom(from);
    mimeMessage.addRecipient(RecipientType.TO, to);
    mimeMessage.setSubject("This is the subject.");
    mimeMessage.setText("This is the message");
    template.sendBodyAndHeader("direct:start", mimeMessage, "To", "someone@localhost");
    assertMockEndpointsSatisfied();
    // need to check the message header
    Exchange exchange = getMockEndpoint("mock:result").getExchanges().get(0);
    assertNotNull("The message id should not be null", exchange.getIn().getHeader(MailConstants.MAIL_MESSAGE_ID));
    Mailbox box = Mailbox.get("someone@localhost");
    assertEquals(0, box.size());
    // Check if the mimeMessagea has override body and headers
    Mailbox box2 = Mailbox.get("recipient2@localhost");
    assertEquals(1, box2.size());
}
Also used : Exchange(org.apache.camel.Exchange) InternetAddress(javax.mail.internet.InternetAddress) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) Mailbox(org.jvnet.mock_javamail.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) Session(javax.mail.Session) Test(org.junit.Test)

Example 27 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class MailRouteTest method testMailSubjectWithUnicode.

@Test
public void testMailSubjectWithUnicode() throws Exception {
    Mailbox.clearAll();
    final String body = "Hello Camel Riders!";
    final String subject = "My Camel ™";
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    // now we don't use the UTF-8 encoding
    mock.expectedHeaderReceived("subject", "=?US-ASCII?Q?My_Camel_=3F?=");
    mock.expectedBodiesReceived(body);
    template.send("direct:a", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody(body);
            exchange.getIn().setHeader("subject", subject);
            exchange.setProperty(Exchange.CHARSET_NAME, "US-ASCII");
        }
    });
    mock.assertIsSatisfied();
    assertFalse("Should not have attachements", mock.getExchanges().get(0).getIn().hasAttachments());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 28 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class MimeMessageConsumeTest method testSendAndReceiveMails.

@Test
public void testSendAndReceiveMails() throws Exception {
    Mailbox.clearAll();
    MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
    resultEndpoint.expectedMinimumMessageCount(1);
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "localhost");
    Session session = Session.getInstance(properties, null);
    MimeMessage message = new MimeMessage(session);
    populateMimeMessageBody(message);
    message.setRecipients(Message.RecipientType.TO, "james3@localhost");
    Transport.send(message);
    // lets test the receive worked
    resultEndpoint.assertIsSatisfied();
    Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
    String text = exchange.getIn().getBody(String.class);
    assertEquals("mail body", body, text);
    assertNotNull("attachments got lost", exchange.getIn().getAttachments());
    for (String s : exchange.getIn().getAttachmentNames()) {
        DataHandler dh = exchange.getIn().getAttachment(s);
        Object content = dh.getContent();
        assertNotNull("Content should not be empty", content);
        assertEquals("log4j2.properties", dh.getName());
    }
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MimeMessage(javax.mail.internet.MimeMessage) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) Session(javax.mail.Session) Test(org.junit.Test)

Example 29 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class MimeMultipartAlternativeWithLongerFilenameTest method verifyTheRecivedEmail.

private void verifyTheRecivedEmail(String expectString) throws Exception {
    // need some time for the mail to arrive on the inbox (consumed and sent to the mock)
    Thread.sleep(1000);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.assertIsSatisfied();
    Exchange out = mock.assertExchangeReceived(0);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(((MailMessage) out.getIn()).getMessage().getSize());
    ((MailMessage) out.getIn()).getMessage().writeTo(baos);
    String dumpedMessage = baos.toString();
    assertTrue("There should have the " + expectString, dumpedMessage.indexOf(expectString) > 0);
    log.trace("multipart alternative: \n{}", dumpedMessage);
    // plain text
    assertEquals(alternativeBody, out.getIn().getBody(String.class));
    // attachment
    Map<String, DataHandler> attachments = out.getIn().getAttachments();
    assertNotNull("Should not have null attachments", attachments);
    assertEquals(1, attachments.size());
    assertEquals("multipart body should have 2 parts", 2, out.getIn().getBody(MimeMultipart.class).getCount());
}
Also used : Exchange(org.apache.camel.Exchange) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataHandler(javax.activation.DataHandler)

Example 30 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class MultipleDestinationConsumeTest method testSendAndReceiveMails.

@Test
public void testSendAndReceiveMails() throws Exception {
    Mailbox.clearAll();
    MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
    resultEndpoint.expectedMinimumMessageCount(1);
    MimeMessage message = new MimeMessage(mailSession);
    message.setText(body);
    message.setRecipients(Message.RecipientType.TO, new Address[] { new InternetAddress("james@localhost"), new InternetAddress("bar@localhost") });
    Transport.send(message);
    // lets test the receive worked
    resultEndpoint.assertIsSatisfied(100000);
    Exchange exchange = resultEndpoint.getReceivedExchanges().get(0);
    org.apache.camel.Message in = exchange.getIn();
    assertNotNull("Should have headers", in.getHeaders());
    MailMessage msg = (MailMessage) exchange.getIn();
    Message inMessage = msg != null ? msg.getMessage() : null;
    assertNotNull("In message has no JavaMail message!", inMessage);
    String text = in.getBody(String.class);
    assertEquals("mail body", body, text);
    // need to use iterator as some mail impl returns String[] and others a single String with comma as separator
    // so we let Camel create an iterator so we can use the same code for the test
    Object to = in.getHeader("TO");
    Iterator<String> it = CastUtils.cast(ObjectHelper.createIterator(to));
    int i = 0;
    while (it.hasNext()) {
        if (i == 0) {
            assertEquals("james@localhost", it.next().trim());
        } else {
            assertEquals("bar@localhost", it.next().trim());
        }
        i++;
    }
    Enumeration<Header> iter = CastUtils.cast(inMessage.getAllHeaders());
    while (iter.hasMoreElements()) {
        Header header = iter.nextElement();
        String[] value = message.getHeader(header.getName());
        log.debug("Header: " + header.getName() + " has value: " + ObjectHelper.asString(value));
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Exchange(org.apache.camel.Exchange) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Aggregations

Exchange (org.apache.camel.Exchange)3446 Test (org.junit.Test)1735 Processor (org.apache.camel.Processor)1405 RouteBuilder (org.apache.camel.builder.RouteBuilder)666 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)640 DefaultExchange (org.apache.camel.impl.DefaultExchange)473 Message (org.apache.camel.Message)379 Endpoint (org.apache.camel.Endpoint)235 HashMap (java.util.HashMap)190 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)155 Producer (org.apache.camel.Producer)150 File (java.io.File)120 ArrayList (java.util.ArrayList)117 CamelContext (org.apache.camel.CamelContext)117 List (java.util.List)99 Map (java.util.Map)96 ProducerTemplate (org.apache.camel.ProducerTemplate)94 IOException (java.io.IOException)92 Tx (org.nhindirect.common.tx.model.Tx)83 Predicate (org.apache.camel.Predicate)78