use of org.apache.camel.Producer in project camel by apache.
the class MailAttachmentTest method testSendAndReceiveMailWithAttachments.
@Test
public void testSendAndReceiveMailWithAttachments() throws Exception {
// clear mailbox
Mailbox.clearAll();
// START SNIPPET: e1
// create an exchange with a normal body and attachment to be produced as email
Endpoint endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret");
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("Hello World");
DefaultAttachment att = new DefaultAttachment(new FileDataSource("src/test/data/logo.jpeg"));
att.addHeader("Content-Description", "some sample content");
in.addAttachmentObject("logo.jpeg", att);
// create a producer that can produce the exchange (= send the mail)
Producer producer = endpoint.createProducer();
// start the producer
producer.start();
// and let it go (processes the exchange by sending the email)
producer.process(exchange);
// END SNIPPET: e1
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(2000);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
Exchange out = mock.assertExchangeReceived(0);
mock.assertIsSatisfied();
// plain text
assertEquals("Hello World", out.getIn().getBody(String.class));
// attachment
Map<String, Attachment> attachments = out.getIn().getAttachmentObjects();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
Attachment attachment = out.getIn().getAttachmentObject("logo.jpeg");
DataHandler handler = attachment.getDataHandler();
assertNotNull("The logo should be there", handler);
// content type should match
boolean match1 = "image/jpeg; name=logo.jpeg".equals(handler.getContentType());
boolean match2 = "application/octet-stream; name=logo.jpeg".equals(handler.getContentType());
assertTrue("Should match 1 or 2", match1 || match2);
assertEquals("Handler name should be the file name", "logo.jpeg", handler.getName());
assertEquals("some sample content", attachment.getHeader("content-description"));
producer.stop();
}
use of org.apache.camel.Producer in project camel by apache.
the class MailSplitAttachmentsTest method testSplitAttachments.
@Test
public void testSplitAttachments() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:split");
mock.expectedMessageCount(2);
Producer producer = endpoint.createProducer();
producer.start();
producer.process(exchange);
Thread.sleep(2000);
mock.assertIsSatisfied();
Message first = mock.getReceivedExchanges().get(0).getIn();
Message second = mock.getReceivedExchanges().get(1).getIn();
assertEquals(1, first.getAttachments().size());
assertEquals(1, second.getAttachments().size());
String file1 = first.getAttachments().keySet().iterator().next();
String file2 = second.getAttachments().keySet().iterator().next();
boolean logo = file1.equals("logo.jpeg") || file2.equals("logo.jpeg");
boolean license = file1.equals("license.txt") || file2.equals("license.txt");
assertTrue("Should have logo.jpeg file attachment", logo);
assertTrue("Should have license.txt file attachment", license);
}
use of org.apache.camel.Producer in project camel by apache.
the class MailHtmlAttachmentTest method testSendAndRecieveMailWithAttachments.
@Test
public void testSendAndRecieveMailWithAttachments() throws Exception {
// clear mailbox
Mailbox.clearAll();
// START SNIPPET: e1
// create an exchange with a normal body and attachment to be produced as email
Endpoint endpoint = context.getEndpoint("smtp://james@mymailserver.com?password=secret&contentType=text/html");
// create the exchange with the mail message that is multipart with a file and a Hello World text/plain message.
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setBody("<html><body><h1>Hello</h1>World</body></html>");
in.addAttachment("logo.jpeg", new DataHandler(new FileDataSource("src/test/data/logo.jpeg")));
// create a producer that can produce the exchange (= send the mail)
Producer producer = endpoint.createProducer();
// start the producer
producer.start();
// and let it go (processes the exchange by sending the email)
producer.process(exchange);
// END SNIPPET: e1
// need some time for the mail to arrive on the inbox (consumed and sent to the mock)
Thread.sleep(2000);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
Exchange out = mock.assertExchangeReceived(0);
mock.assertIsSatisfied();
// plain text
assertEquals("<html><body><h1>Hello</h1>World</body></html>", out.getIn().getBody(String.class));
// attachment
Map<String, DataHandler> attachments = out.getIn().getAttachments();
assertNotNull("Should have attachments", attachments);
assertEquals(1, attachments.size());
DataHandler handler = out.getIn().getAttachment("logo.jpeg");
assertNotNull("The logo should be there", handler);
byte[] bytes = context.getTypeConverter().convertTo(byte[].class, handler.getInputStream());
assertNotNull("content should be there", bytes);
assertTrue("logo should be more than 1000 bytes", bytes.length > 1000);
// content type should match
boolean match1 = "image/jpeg; name=logo.jpeg".equals(handler.getContentType());
boolean match2 = "application/octet-stream; name=logo.jpeg".equals(handler.getContentType());
assertTrue("Should match 1 or 2", match1 || match2);
// save logo for visual inspection
template.sendBodyAndHeader("file://target", bytes, Exchange.FILE_NAME, "maillogo.jpg");
producer.stop();
}
use of org.apache.camel.Producer in project camel by apache.
the class MinaEncodingTest method testUDPEncodeUTF8InputIsStringNoMock.
@Test
public void testUDPEncodeUTF8InputIsStringNoMock() throws Exception {
// this unit test covers for testUDPEncodeUTF8InputIsString until the encoding is fixed
// include a UTF-8 char in the text จ is a Thai elephant
final String hello = "Hello Thai Elephant จ";
final String bye = "Hello Thai Elephant จ";
final String uri = "mina:udp://localhost:{{port}}?sync=true&encoding=UTF-8";
context.addRoutes(new RouteBuilder() {
public void configure() {
from(uri).process(new Processor() {
public void process(Exchange exchange) throws Exception {
String s = exchange.getIn().getBody(String.class);
assertEquals(hello, s);
exchange.getOut().setBody(bye);
}
});
}
});
Endpoint endpoint = context.getEndpoint(uri);
Producer producer = endpoint.createProducer();
Exchange exchange = producer.createExchange();
exchange.getIn().setBody(hello);
producer.start();
producer.process(exchange);
producer.stop();
String s = exchange.getOut().getBody(String.class);
assertEquals(bye, s);
}
use of org.apache.camel.Producer in project camel by apache.
the class MinaExchangeTimeOutTest method testUsingTimeoutParameter.
@Test
public void testUsingTimeoutParameter() throws Exception {
// use a timeout value of 2 seconds (timeout is in millis) so we should actually get a response in this test
Endpoint endpoint = context.getEndpoint("mina:tcp://localhost:{{port}}?textline=true&sync=true&timeout=2000");
Producer producer = endpoint.createProducer();
producer.start();
Exchange exchange = producer.createExchange();
exchange.getIn().setBody("Hello World");
try {
producer.process(exchange);
fail("Should have thrown an ExchangeTimedOutException wrapped in a RuntimeCamelException");
} catch (Exception e) {
assertTrue("Should have thrown an ExchangeTimedOutException", e instanceof ExchangeTimedOutException);
}
producer.stop();
}
Aggregations