How to use BodyPart class of com.consol.citrus.mail.model package

Best Citrus code snippet using com.consol.citrus.mail.model.BodyPart

Source:MailServer.java Github

copy

Full Screen

...28import com.consol.citrus.mail.message.MailMessage;29import com.consol.citrus.mail.message.MailMessageConverter;30import com.consol.citrus.mail.model.AcceptResponse;31import com.consol.citrus.mail.model.AttachmentPart;32import com.consol.citrus.mail.model.BodyPart;33import com.consol.citrus.mail.model.MailMarshaller;34import com.consol.citrus.mail.model.MailRequest;35import com.consol.citrus.mail.model.MailResponse;36import com.consol.citrus.message.Message;37import com.consol.citrus.server.AbstractServer;38import org.springframework.mail.javamail.MimeMailMessage;39import org.subethamail.smtp.RejectException;40import org.subethamail.smtp.helper.SimpleMessageListener;41import org.subethamail.smtp.helper.SimpleMessageListenerAdapter;42import org.subethamail.smtp.server.SMTPServer;43/**44 * Mail server implementation starts new SMTP server instance and listens for incoming mail messages. Incoming mail messages45 * are converted to XML representation and forwarded to some message endpoint adapter (e.g. forwarding mail content to46 * a message channel).47 *48 * By default incoming messages are accepted automatically. When auto accept is disabled the endpoint adapter is invoked with49 * accept request and test case has to decide accept outcome in response.50 *51 * In case of incoming multipart mail messages the server is able to split the body parts into separate XML messages52 * handled by the endpoint adapter.53 *54 * @author Christoph Deppisch55 * @since 1.456 */57public class MailServer extends AbstractServer implements SimpleMessageListener {58 /** Server port */59 private int port = 25;60 /** XML message mapper */61 private MailMarshaller marshaller = new MailMarshaller();62 /** Mail message converter */63 private MailMessageConverter messageConverter = new MailMessageConverter();64 /** Java mail session */65 private Session mailSession;66 /** Java mail properties */67 private Properties javaMailProperties = new Properties();68 /** Should accept automatically or handled via test case */69 private boolean autoAccept = true;70 /** Should split multipart messages for each mime part */71 private boolean splitMultipart = false;72 /** Smtp server instance */73 private SMTPServer smtpServer;74 @Override75 protected void startup() {76 smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(this));77 smtpServer.setSoftwareName(getName());78 smtpServer.setPort(port);79 smtpServer.start();80 }81 @Override82 protected void shutdown() {83 smtpServer.stop();84 }85 @Override86 public boolean accept(String from, String recipient) {87 if (autoAccept) {88 return true;89 }90 Message response = getEndpointAdapter().handleMessage(91 MailMessage.accept(from, recipient)92 .marshaller(marshaller));93 if (response == null || response.getPayload() == null) {94 throw new CitrusRuntimeException("Did not receive accept response. Missing accept response because autoAccept is disabled.");95 }96 AcceptResponse acceptResponse = null;97 if (response.getPayload() instanceof AcceptResponse) {98 acceptResponse = (AcceptResponse) response.getPayload();99 } else if (response.getPayload() instanceof String) {100 acceptResponse = (AcceptResponse) marshaller.unmarshal(response.getPayload(Source.class));101 }102 if (acceptResponse == null) {103 throw new CitrusRuntimeException("Unable to read accept response from payload: " + response);104 }105 return acceptResponse.isAccept();106 }107 @Override108 public void deliver(String from, String recipient, InputStream data) {109 try {110 MimeMailMessage mimeMailMessage = new MimeMailMessage(new MimeMessage(getSession(), data));111 MailMessage request = messageConverter.convertInbound(mimeMailMessage, getEndpointConfiguration(), null);112 Message response = invokeEndpointAdapter(request);113 if (response != null && response.getPayload() != null) {114 MailResponse mailResponse = null;115 if (response.getPayload() instanceof MailResponse) {116 mailResponse = (MailResponse) response.getPayload();117 } else if (response.getPayload() instanceof String) {118 mailResponse = (MailResponse) marshaller.unmarshal(response.getPayload(Source.class));119 }120 if (mailResponse != null && mailResponse.getCode() != MailResponse.OK_CODE) {121 throw new RejectException(mailResponse.getCode(), mailResponse.getMessage());122 }123 }124 } catch (MessagingException e) {125 throw new CitrusRuntimeException(e);126 }127 }128 /**129 * Invokes the endpoint adapter with constructed mail message and headers.130 * @param mail131 */132 protected Message invokeEndpointAdapter(MailMessage mail) {133 if (splitMultipart) {134 return split(mail.getPayload(MailRequest.class).getBody(), mail.getHeaders());135 } else {136 return getEndpointAdapter().handleMessage(mail);137 }138 }139 /**140 * Split mail message into several messages. Each body and each attachment results in separate message141 * invoked on endpoint adapter. Mail message response if any should be sent only once within test case.142 * However latest mail response sent by test case is returned, others are ignored.143 *144 * @param bodyPart145 * @param messageHeaders146 */147 private Message split(BodyPart bodyPart, Map<String, Object> messageHeaders) {148 MailMessage mailRequest = createMailMessage(messageHeaders, bodyPart.getContent(), bodyPart.getContentType());149 Stack<Message> responseStack = new Stack<>();150 if (bodyPart instanceof AttachmentPart) {151 fillStack(getEndpointAdapter().handleMessage(mailRequest152 .setHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE, bodyPart.getContentType())153 .setHeader(CitrusMailMessageHeaders.MAIL_FILENAME, ((AttachmentPart) bodyPart).getFileName())), responseStack);154 } else {155 fillStack(getEndpointAdapter().handleMessage(mailRequest156 .setHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE, bodyPart.getContentType())), responseStack);157 }158 if (bodyPart.hasAttachments()) {159 for (AttachmentPart attachmentPart : bodyPart.getAttachments().getAttachments()) {160 fillStack(split(attachmentPart, messageHeaders), responseStack);161 }...

Full Screen

Full Screen

Source:MailSteps.java Github

copy

Full Screen

...20import com.consol.citrus.annotations.CitrusAnnotations;21import com.consol.citrus.annotations.CitrusFramework;22import com.consol.citrus.annotations.CitrusResource;23import com.consol.citrus.context.TestContext;24import com.consol.citrus.mail.model.BodyPart;25import com.consol.citrus.mail.model.MailMarshaller;26import com.consol.citrus.mail.model.MailRequest;27import com.consol.citrus.mail.server.MailServer;28import com.consol.citrus.mail.server.MailServerBuilder;29import com.consol.citrus.message.MessageType;30import com.consol.citrus.message.builder.ObjectMappingPayloadBuilder;31import io.cucumber.java.Before;32import io.cucumber.java.Scenario;33import io.cucumber.java.en.Given;34import io.cucumber.java.en.Then;35import org.citrusframework.yaks.kubernetes.KubernetesSteps;36import static com.consol.citrus.actions.ReceiveMessageAction.Builder.receive;37public class MailSteps {38 @CitrusResource39 private TestCaseRunner runner;40 @CitrusResource41 private TestContext context;42 @CitrusFramework43 private Citrus citrus;44 private MailServer mailServer;45 private int port = 22222;46 private KubernetesSteps kubernetesSteps;47 @Before48 public void before(Scenario scenario) {49 kubernetesSteps = new KubernetesSteps();50 CitrusAnnotations.injectAll(kubernetesSteps, citrus, context);51 CitrusAnnotations.injectTestRunner(kubernetesSteps, runner);52 kubernetesSteps.before(scenario);53 }54 @Given("^mail server port (\\d++)$")55 public void createMailServer(int port) {56 this.port = port;57 }58 @Given("^start mail server$")59 public void createMailServer() {60 MailMarshaller marshaller = new MailMarshaller();61 marshaller.setType(MessageType.JSON.name());62 mailServer = new MailServerBuilder()63 .port(port)64 .marshaller(marshaller)65 .autoStart(true)66 .build();67 citrus.getCitrusContext().getReferenceResolver().bind("mail-server", mailServer);68 kubernetesSteps.createService("mail-server", 25, port);69 mailServer.initialize();70 }71 @Then("verify mail received")72 public void receiveMail() {73 MailRequest request = new MailRequest();74 request.setFrom("${from}");75 request.setTo("${to}");76 request.setCc("");77 request.setBcc("");78 request.setSubject("${subject}");79 request.setBody(new BodyPart("${message}", "text/plain"));80 runner.run(receive(mailServer)81 .message()82 .body(new ObjectMappingPayloadBuilder(request, mailServer.getMarshaller())));83 }84}...

Full Screen

Full Screen

BodyPart

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.mail.message.MailMessage;2import com.consol.citrus.mail.model.*;3import org.springframework.mail.javamail.MimeMessageHelper;4import org.springframework.mail.javamail.MimeMessagePreparator;5import org.springframework.mail.javamail.MimeMailMessage;6public class MailMessageBuilder {7 public static void main(String[] args) {8 MimeMessagePreparator preparator = new MimeMessagePreparator() {9 public void prepare(MimeMessage mimeMessage) throws Exception {10 MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");11 message.setSubject("Test Mail");12 message.setFrom("

Full Screen

Full Screen

BodyPart

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.message;2import com.consol.citrus.mail.model.BodyPart;3import com.consol.citrus.mail.model.MailMessage;4import com.consol.citrus.mail.model.MimeMultipart;5import com.consol.citrus.message.Message;6import com.consol.citrus.message.MessageHeaders;7import com.consol.citrus.message.MessageType;8import com.consol.citrus.message.builder.DefaultMessageBuilder;9import com.consol.citrus.message.builder.ObjectMappingPayloadBuilder;10import com.consol.citrus.message.builder.PayloadTemplateMessageBuilder;11import com.consol.citrus.message.builder.PayloadTemplateProcessor;12import com.consol.citrus.message.builder.ScriptTemplateMessageBuilder;13import com.consol.citrus.message.builder.ScriptTemplateProcessor;14import com.consol.citrus.message.builder.StaticMessageContentBuilder;15import com.consol.citrus.message.builder.TemplateMessageBuilder;16import com.consol.citrus.message.builder.TemplateProcessor;17import com.consol.citrus.message.builder.XMLPayloadTemplateMessageBuilder;18import com.consol.citrus.message.builder.XMLPayloadTemplateProcessor;19import com.consol.citrus.message.builder.XQueryPayloadTemplateMessageBuilder;20import com.consol.citrus.message.builder.XQueryPayloadTemplateProcessor;21import com.consol.citrus.message.builder.XpathPayloadTemplateMessageBuilder;22import com.consol.citrus.message.builder.XpathPayloadTemplateProcessor;23import com.consol.citrus.message.builder.XslPayloadTemplateMessageBuilder;24import com.consol.citrus.message.builder.XslPayloadTemplateProcessor;25import com.consol.citrus.message.selector.MessageSelector;26import com.consol.citrus.message.selector.MessageSelectorBuilder;27import com.consol.citrus.messaging.Producer;28import com.consol.citrus.messaging.SelectiveConsumer;29import com.consol.citrus.messaging.SelectiveProducer;30import com.consol.citrus.validation.builder.*;31import com.consol.citrus.validation.xml.XmlMessageValidationContext;32import com.consol.citrus.xml.XpathUtils;33import com.consol.citrus.xml.XsdSchemaRepository;34import com.consol.citrus.xml.namespace.NamespaceContextBuilder;35import org.springframework.core.io.Resource;36import org.springframework.util.CollectionUtils;37import org.springframework.util.StringUtils;38import org.springframework.xml.transform.StringSource;39import org.springframework.xml.xpath.Jaxp13XPathTemplate;40import org.springframework.xml.xpath.XPathTemplate;41import org.w3c.dom

Full Screen

Full Screen

BodyPart

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.mail.model.BodyPart;2import com.consol.citrus.mail.model.Attachment;3import com.consol.citrus.mail.model.TextBody;4import com.consol.citrus.mail.model.HtmlBody;5import com.consol.citrus.mail.model.InlineImage;6import com.co

Full Screen

Full Screen

BodyPart

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.model;2import java.io.IOException;3import java.io.InputStream;4import java.util.List;5import javax.mail.MessagingException;6import javax.mail.internet.MimeBodyPart;7import org.apache.commons.io.IOUtils;8public class BodyPart {9 private MimeBodyPart mimeBodyPart;10 public BodyPart(MimeBodyPart mimeBodyPart) {11 this.mimeBodyPart = mimeBodyPart;12 }13 public String getContent() throws IOException, MessagingException {14 return IOUtils.toString(mimeBodyPart.getInputStream());15 }16 public String getContentType() throws MessagingException {17 return mimeBodyPart.getContentType();18 }19 public String getFileName() throws MessagingException {20 return mimeBodyPart.getFileName();21 }22 public List<BodyPart> getBodyParts() throws MessagingException, IOException {23 List<BodyPart> bodyParts = Lists.newArrayList();24 if (mimeBodyPart.isMimeType("multipart/*")) {25 Multipart multipart = (Multipart) mimeBodyPart.getContent();26 for (int i = 0; i < multipart.getCount(); i++) {27 bodyParts.add(new BodyPart((MimeBodyPart) multipart.getBodyPart(i)));28 }29 }30 return bodyParts;31 }32 public InputStream getInputStream() throws IOException, MessagingException {33 return mimeBodyPart.getInputStream();34 }35 public String getDisposition() throws MessagingException {36 return mimeBodyPart.getDisposition();37 }38 public String getHeader(String name) throws MessagingException {39 return mimeBodyPart.getHeader(name, null);40 }41 public String[] getHeaderNames() throws MessagingException {42 return mimeBodyPart.getHeaderNames();43 }44 public String[] getHeaderValues(String name) throws MessagingException {45 return mimeBodyPart.getHeader(name);46 }47 public String getEncoding() throws MessagingException {48 return mimeBodyPart.getEncoding();49 }50 public String getDescription() throws MessagingException {51 return mimeBodyPart.getDescription();52 }53 public String getContentId() throws MessagingException {54 return mimeBodyPart.getContentID();55 }56 public String getContentMD5() throws MessagingException {57 return mimeBodyPart.getContentMD5();58 }59 public String getLanguage() throws MessagingException {60 return mimeBodyPart.getLanguage();61 }62 public int getSize() throws MessagingException {63 return mimeBodyPart.getSize();64 }65 public void setContent(Object o, String s) throws MessagingException {

Full Screen

Full Screen

BodyPart

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail;2import com.consol.citrus.mail.model.BodyPart;3import com.consol.citrus.mail.model.MailMessage;4import com.consol.citrus.mail.server.MailServer;5import com.consol.citrus.mail.server.MailServerBuilder;6import org.springframework.core.io.ClassPathResource;7import java.io.IOException;8import java.util.ArrayList;9import java.util.List;10public class MailTest {11 public static void main(String[] args) throws IOException {12 .port(2500)13 .autoStart(true)14 .build();15 MailMessage mailMessage = new MailMessage();16 mailMessage.setFrom("

Full Screen

Full Screen

BodyPart

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.mail.model;2import java.io.*;3import java.util.*;4import javax.mail.*;5import javax.mail.internet.*;6{7public static void main(String[] args) throws Exception8{9BodyPart bodyPart = new BodyPart();10bodyPart.setContent("This is the body of the message");11MimeMultipart mimeMultipart = new MimeMultipart();12mimeMultipart.addBodyPart(bodyPart);13MimeMessage mimeMessage = new MimeMessage((Session)null);14mimeMessage.setContent(mimeMultipart);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Citrus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful