diff --git a/connectors/citrus-docker/pom.xml b/connectors/citrus-docker/pom.xml index f27e4a5d29..f3fecff658 100644 --- a/connectors/citrus-docker/pom.xml +++ b/connectors/citrus-docker/pom.xml @@ -85,10 +85,6 @@ jakarta.xml.bind-api - - jakarta.activation - jakarta.activation-api - jakarta.annotation jakarta.annotation-api diff --git a/endpoints/citrus-mail/pom.xml b/endpoints/citrus-mail/pom.xml index 835f008f3f..23184f7050 100644 --- a/endpoints/citrus-mail/pom.xml +++ b/endpoints/citrus-mail/pom.xml @@ -66,26 +66,13 @@ jackson-databind - - jakarta.xml.bind - jakarta.xml.bind-api - - - org.glassfish.jaxb - jaxb-runtime - - - - com.sun.mail - jakarta.mail - com.icegreen greenmail - com.sun.activation - jakarta.activation + org.eclipse.angus + jakarta.mail diff --git a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailClient.java b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailClient.java index d08f4c44c8..864dce0c76 100644 --- a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailClient.java +++ b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailClient.java @@ -16,10 +16,9 @@ package org.citrusframework.mail.client; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.util.Properties; - +import jakarta.mail.Authenticator; +import jakarta.mail.MessagingException; +import jakarta.mail.PasswordAuthentication; import org.citrusframework.common.InitializingPhase; import org.citrusframework.context.TestContext; import org.citrusframework.endpoint.AbstractEndpoint; @@ -28,20 +27,24 @@ import org.citrusframework.message.RawMessage; import org.citrusframework.messaging.Consumer; import org.citrusframework.messaging.Producer; -import jakarta.mail.MessagingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.mail.javamail.MimeMailMessage; import org.springframework.util.StringUtils; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + /** * @author Christoph Deppisch * @since 1.4 */ public class MailClient extends AbstractEndpoint implements Producer, InitializingPhase { - /** Logger */ + private static Logger log = LoggerFactory.getLogger(MailClient.class); + private MailSender mailSender = new MailSender(); + /** * Default constructor initializing endpoint configuration. */ @@ -51,7 +54,6 @@ public MailClient() { /** * Default constructor using endpoint configuration. - * @param endpointConfiguration */ public MailClient(MailEndpointConfiguration endpointConfiguration) { super(endpointConfiguration); @@ -69,7 +71,12 @@ public void send(Message message, TestContext context) { } MimeMailMessage mimeMessage = getEndpointConfiguration().getMessageConverter().convertOutbound(message, getEndpointConfiguration(), context); - getEndpointConfiguration().getJavaMailSender().send(mimeMessage.getMimeMessage()); + + try { + mailSender.send(mimeMessage.getMimeMessage()); + } catch (MessagingException e) { + throw new CitrusRuntimeException("Failed to send mail message!", e); + } ByteArrayOutputStream bos = new ByteArrayOutputStream(); Message mailMessage; @@ -103,8 +110,6 @@ public Producer createProducer() { /** * Creates a message consumer for this endpoint. Consumer receives * messages on this endpoint. - * - * @return */ @Override public Consumer createConsumer() { @@ -113,18 +118,31 @@ public Consumer createConsumer() { @Override public void initialize() { - if (StringUtils.hasText(getEndpointConfiguration().getJavaMailSender().getUsername()) || - StringUtils.hasText(getEndpointConfiguration().getJavaMailSender().getPassword())) { + if (!StringUtils.hasText(getEndpointConfiguration().getProtocol())) { + throw new CitrusRuntimeException("A mailing protocol must be configured!"); + } - Properties javaMailProperties = getEndpointConfiguration().getJavaMailSender().getJavaMailProperties(); + if (StringUtils.hasText(getEndpointConfiguration().getUsername()) || + StringUtils.hasText(getEndpointConfiguration().getPassword())) { - javaMailProperties.setProperty("mail.smtp.auth", "true"); - getEndpointConfiguration().getJavaMailSender().setJavaMailProperties(javaMailProperties); + getEndpointConfiguration().getJavaMailProperties().setProperty("mail." + getEndpointConfiguration().getProtocol() + ".auth", "true"); + getEndpointConfiguration().setAuthenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(getEndpointConfiguration().getUsername(), getEndpointConfiguration().getPassword()); + } + }); } - if (!StringUtils.hasText(getEndpointConfiguration().getJavaMailSender().getProtocol())) { - getEndpointConfiguration().getJavaMailSender().setProtocol("smtp"); + if (StringUtils.hasText(getEndpointConfiguration().getHost())) { + getEndpointConfiguration().getJavaMailProperties().setProperty("mail." + getEndpointConfiguration().getProtocol() + ".host", getEndpointConfiguration().getHost()); + } + if (getEndpointConfiguration().getPort() > 0) { + getEndpointConfiguration().getJavaMailProperties().setProperty("mail." + getEndpointConfiguration().getProtocol() + ".port", String.valueOf(getEndpointConfiguration().getPort())); } } + void setMailSender(MailSender mailSenderMock) { + this.mailSender = mailSenderMock; + } } diff --git a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailEndpointConfiguration.java b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailEndpointConfiguration.java index 311ad185ad..2128232154 100644 --- a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailEndpointConfiguration.java +++ b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailEndpointConfiguration.java @@ -16,13 +16,14 @@ package org.citrusframework.mail.client; -import java.util.Properties; - +import jakarta.mail.Authenticator; import org.citrusframework.endpoint.AbstractEndpointConfiguration; import org.citrusframework.mail.message.MailMessageConverter; import org.citrusframework.mail.model.MailMarshaller; import org.springframework.mail.javamail.JavaMailSenderImpl; +import java.util.Properties; + /** * @author Christoph Deppisch * @since 1.4 @@ -41,14 +42,14 @@ public class MailEndpointConfiguration extends AbstractEndpointConfiguration { /** Password */ private String password; + /** An optional username + password authenticator **/ + private Authenticator authenticator; + /** Protocol */ private String protocol = JavaMailSenderImpl.DEFAULT_PROTOCOL; /** Java mail properties */ - private Properties javaMailProperties; - - /** Mail sender implementation */ - private JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); + private Properties javaMailProperties = new Properties(); /** Mail message marshaller converts from XML to mail message object */ private MailMarshaller marshaller = new MailMarshaller(); @@ -56,24 +57,7 @@ public class MailEndpointConfiguration extends AbstractEndpointConfiguration { /** Mail message converter */ private MailMessageConverter messageConverter = new MailMessageConverter(); - /** - * Gets the mail protocol. - * @return the mail protocol. - */ - public String getProtocol() { - return protocol; - } - - /** - * Set the mail protocol. Default is "smtp". - * @param protocol - */ - public void setProtocol(String protocol) { - this.protocol = protocol; - javaMailSender.setProtocol(protocol); - } - - /** + /** * Gets the mail host. * @return the mail host. */ @@ -83,11 +67,11 @@ public String getHost() { /** * Set the mail server host, typically an SMTP host. - * @param host + * @param host the mail server host. */ public void setHost(String host) { this.host = host; - javaMailSender.setHost(host); + getJavaMailProperties().setProperty("mail."+ getProtocol()+".host", host); } /** @@ -101,11 +85,11 @@ public int getPort() { /** * Set the mail server port. * Default is the Java mail port for SMTP (25). - * @param port + * @param port the mail server port. */ public void setPort(int port) { this.port = port; - javaMailSender.setPort(port); + getJavaMailProperties().put("mail."+getProtocol()+".port", "25"); } /** @@ -117,19 +101,18 @@ public String getUsername() { } /** - * Set the username for accessing the mail host. Underlying mail seesion + * Set the username for accessing the mail host. The underlying mail session * has to be configured with the property "mail.smtp.auth" set to * true. - * @param username + * @param username the username for accessing the mail host. */ public void setUsername(String username) { this.username = username; - javaMailSender.setUsername(username); } /** * Gets the mail password. - * @return the mail ppassword. + * @return the mail password. */ public String getPassword() { return password; @@ -139,11 +122,42 @@ public String getPassword() { * Set the password for accessing the mail host. Underlying mail seesion * has to be configured with the property "mail.smtp.auth" set to * true. - * @param password + * @param password the password for accessing the mail host. */ public void setPassword(String password) { this.password = password; - javaMailSender.setPassword(password); + } + + /** + * Gets the authenticator. + * @return The authenticator. + */ + public Authenticator getAuthenticator() { + return authenticator; + } + + /** + * Sets the authenticator. + * @param authenticator the authenticator. + */ + public void setAuthenticator(Authenticator authenticator) { + this.authenticator = authenticator; + } + + /** + * Gets the mail protocol. + * @return the mail protocol. + */ + public String getProtocol() { + return protocol; + } + + /** + * Set the mailing protocol. Default is "smtp". + * @param protocol the mailing protocol. + */ + public void setProtocol(String protocol) { + this.protocol = protocol; } /** @@ -157,16 +171,15 @@ public Properties getJavaMailProperties() { /** * Set JavaMail properties for the mail session such as "mail.smtp.auth" * when using username and password. New session is created when properties are set. - * @param javaMailProperties + * @param javaMailProperties all mailing properties. */ public void setJavaMailProperties(Properties javaMailProperties) { this.javaMailProperties = javaMailProperties; - javaMailSender.setJavaMailProperties(javaMailProperties); } /** * Gets the mail message marshaller implementation. - * @return + * @return the mail message marshaller. */ public MailMarshaller getMarshaller() { return marshaller; @@ -174,31 +187,15 @@ public MailMarshaller getMarshaller() { /** * Sets the mail message marshaller implementation. - * @param marshaller + * @param marshaller the mail message marshaller. */ public void setMarshaller(MailMarshaller marshaller) { this.marshaller = marshaller; } - /** - * Gets the Java mail sender implementation. - * @return - */ - public JavaMailSenderImpl getJavaMailSender() { - return javaMailSender; - } - - /** - * Sets the Java mail sender implementation. - * @param javaMailSender - */ - public void setJavaMailSender(JavaMailSenderImpl javaMailSender) { - this.javaMailSender = javaMailSender; - } - /** * Gets the mail message converter. - * @return + * @return the mail message converter. */ public MailMessageConverter getMessageConverter() { return messageConverter; @@ -206,7 +203,7 @@ public MailMessageConverter getMessageConverter() { /** * Sets the mail message converter. - * @param messageConverter + * @param messageConverter the mail message converter. */ public void setMessageConverter(MailMessageConverter messageConverter) { this.messageConverter = messageConverter; diff --git a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailSender.java b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailSender.java new file mode 100644 index 0000000000..673606e6d4 --- /dev/null +++ b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/client/MailSender.java @@ -0,0 +1,15 @@ +package org.citrusframework.mail.client; + +import jakarta.mail.MessagingException; +import jakarta.mail.Transport; +import jakarta.mail.internet.MimeMessage; + +/** + * This class exists mainly for testing purposes (mocking). + */ +public class MailSender { + + public void send(MimeMessage mimeMessage) throws MessagingException { + Transport.send(mimeMessage); + } +} diff --git a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/message/MailMessageConverter.java b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/message/MailMessageConverter.java index 2cdd71b05e..452b17d876 100644 --- a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/message/MailMessageConverter.java +++ b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/message/MailMessageConverter.java @@ -18,6 +18,7 @@ import jakarta.mail.MessagingException; import jakarta.mail.Multipart; +import jakarta.mail.Session; import jakarta.mail.internet.MimeMessage; import jakarta.mail.internet.MimePart; import javax.xml.transform.Source; @@ -70,7 +71,10 @@ public MimeMailMessage convertOutbound(Message message, MailEndpointConfiguratio MailRequest mailMessage = getMailRequest(message, endpointConfiguration); try { - MimeMessage mimeMessage = endpointConfiguration.getJavaMailSender().createMimeMessage(); + Session session = Session.getInstance(endpointConfiguration.getJavaMailProperties(), endpointConfiguration.getAuthenticator()); + session.setDebug(log.isDebugEnabled()); + + MimeMessage mimeMessage = new MimeMessage(session); MimeMailMessage mimeMailMessage = new MimeMailMessage(new MimeMessageHelper(mimeMessage, mailMessage.getBody().hasAttachments(), parseCharsetFromContentType(mailMessage.getBody().getContentType()))); diff --git a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/server/MailServer.java b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/server/MailServer.java index 33544e8e15..8b4be5607a 100644 --- a/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/server/MailServer.java +++ b/endpoints/citrus-mail/src/main/java/org/citrusframework/mail/server/MailServer.java @@ -16,13 +16,13 @@ package org.citrusframework.mail.server; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Stack; -import java.util.stream.Collectors; -import javax.xml.transform.Source; - +import com.icegreen.greenmail.mail.MailAddress; +import com.icegreen.greenmail.user.GreenMailUser; +import com.icegreen.greenmail.util.GreenMail; +import com.icegreen.greenmail.util.ServerSetup; +import jakarta.mail.AuthenticationFailedException; +import jakarta.mail.Session; +import jakarta.mail.internet.MimeMessage; import org.citrusframework.exceptions.CitrusRuntimeException; import org.citrusframework.mail.client.MailEndpointConfiguration; import org.citrusframework.mail.message.CitrusMailMessageHeaders; @@ -36,23 +36,23 @@ import org.citrusframework.mail.model.MailResponse; import org.citrusframework.message.Message; import org.citrusframework.server.AbstractServer; -import com.icegreen.greenmail.mail.MailAddress; -import com.icegreen.greenmail.user.GreenMailUser; -import com.icegreen.greenmail.util.GreenMail; -import com.icegreen.greenmail.util.ServerSetup; -import jakarta.mail.AuthenticationFailedException; -import jakarta.mail.Session; -import jakarta.mail.internet.MimeMessage; import org.springframework.mail.javamail.MimeMailMessage; +import javax.xml.transform.Source; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Stack; +import java.util.stream.Collectors; + /** * Mail server implementation starts new SMTP server instance and listens for incoming mail messages. Incoming mail messages * are converted to XML representation and forwarded to some message endpoint adapter (e.g. forwarding mail content to * a message channel). - * - * By default incoming messages are accepted automatically. When auto accept is disabled the endpoint adapter is invoked with + *

+ * By default, incoming messages are accepted automatically. When auto accept is disabled the endpoint adapter is invoked with * accept request and test case has to decide accept outcome in response. - * + *

* In case of incoming multipart mail messages the server is able to split the body parts into separate XML messages * handled by the endpoint adapter. * @@ -105,6 +105,7 @@ protected void startup() { deliver(msg.getMessage()); return user; }); + smtpServer.start(); } @@ -140,8 +141,8 @@ public boolean accept(String from, List recipients) { return acceptResponse.isAccept(); } - public void deliver(MimeMessage msg) { - MimeMailMessage mimeMailMessage = new MimeMailMessage(msg); + public void deliver(MimeMessage mimeMessage) { + MimeMailMessage mimeMailMessage = new MimeMailMessage(mimeMessage); MailMessage request = messageConverter.convertInbound(mimeMailMessage, getEndpointConfiguration(), null); Message response = invokeEndpointAdapter(request); @@ -161,7 +162,6 @@ public void deliver(MimeMessage msg) { /** * Invokes the endpoint adapter with constructed mail message and headers. - * @param mail */ protected Message invokeEndpointAdapter(MailMessage mail) { if (splitMultipart) { @@ -174,10 +174,7 @@ protected Message invokeEndpointAdapter(MailMessage mail) { /** * Split mail message into several messages. Each body and each attachment results in separate message * invoked on endpoint adapter. Mail message response if any should be sent only once within test case. - * However latest mail response sent by test case is returned, others are ignored. - * - * @param bodyPart - * @param messageHeaders + * However, latest mail response sent by test case is returned, others are ignored. */ private Message split(BodyPart bodyPart, Map messageHeaders) { MailMessage mailRequest = createMailMessage(messageHeaders, bodyPart.getContent(), bodyPart.getContentType()); @@ -209,10 +206,6 @@ private void fillStack(Message message, Stack responseStack) { /** * Creates a new mail message model object from message headers. - * @param messageHeaders - * @param body - * @param contentType - * @return */ protected MailMessage createMailMessage(Map messageHeaders, String body, String contentType) { return MailMessage.request(messageHeaders) @@ -236,19 +229,18 @@ public MailEndpointConfiguration getEndpointConfiguration() { } /** - * Return new mail session if not already created before. - * @return + * Return a new mail session if not already created before. */ public synchronized Session getSession() { - if (this.mailSession == null) { - this.mailSession = Session.getInstance(this.javaMailProperties); + if (mailSession == null) { + mailSession = Session.getInstance(javaMailProperties); } - return this.mailSession; + + return mailSession; } /** * Is auto accept enabled. - * @return */ public boolean isAutoAccept() { return autoAccept; @@ -256,7 +248,6 @@ public boolean isAutoAccept() { /** * Enable/disable auto accept feature. - * @param autoAccept */ public void setAutoAccept(boolean autoAccept) { this.autoAccept = autoAccept; @@ -264,7 +255,6 @@ public void setAutoAccept(boolean autoAccept) { /** * Gets the mail message marshaller. - * @return */ public MailMarshaller getMarshaller() { return marshaller; @@ -272,7 +262,6 @@ public MailMarshaller getMarshaller() { /** * Sets the mail message marshaller. - * @param marshaller */ public void setMarshaller(MailMarshaller marshaller) { this.marshaller = marshaller; @@ -280,7 +269,6 @@ public void setMarshaller(MailMarshaller marshaller) { /** * Gets the Java mail properties. - * @return */ public Properties getJavaMailProperties() { return javaMailProperties; @@ -288,7 +276,6 @@ public Properties getJavaMailProperties() { /** * Sets the Java mail properties. - * @param javaMailProperties */ public void setJavaMailProperties(Properties javaMailProperties) { this.javaMailProperties = javaMailProperties; @@ -296,7 +283,6 @@ public void setJavaMailProperties(Properties javaMailProperties) { /** * Gets the server port. - * @return */ public int getPort() { return port; @@ -304,7 +290,6 @@ public int getPort() { /** * Sets the server port. - * @param port */ public void setPort(int port) { this.port = port; @@ -312,7 +297,6 @@ public void setPort(int port) { /** * Gets the smtp server instance. - * @return */ public GreenMail getSmtpServer() { return smtpServer; @@ -320,7 +304,6 @@ public GreenMail getSmtpServer() { /** * Sets the smtp server instance. - * @param smtpServer */ public void setSmtpServer(GreenMail smtpServer) { this.smtpServer = smtpServer; @@ -328,7 +311,6 @@ public void setSmtpServer(GreenMail smtpServer) { /** * Gets the split multipart message. - * @return */ public boolean isSplitMultipart() { return splitMultipart; @@ -336,7 +318,6 @@ public boolean isSplitMultipart() { /** * Sets the split multipart message. - * @param splitMultipart */ public void setSplitMultipart(boolean splitMultipart) { this.splitMultipart = splitMultipart; @@ -344,7 +325,6 @@ public void setSplitMultipart(boolean splitMultipart) { /** * Gets the message converter. - * @return */ public MailMessageConverter getMessageConverter() { return messageConverter; @@ -352,7 +332,6 @@ public MailMessageConverter getMessageConverter() { /** * Sets the message converter. - * @param messageConverter */ public void setMessageConverter(MailMessageConverter messageConverter) { this.messageConverter = messageConverter; diff --git a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/client/MailClientTest.java b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/client/MailClientTest.java index 31b6591564..a03254a4f9 100644 --- a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/client/MailClientTest.java +++ b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/client/MailClientTest.java @@ -16,108 +16,113 @@ package org.citrusframework.mail.client; +import jakarta.mail.Address; +import jakarta.mail.Message; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; +import jakarta.mail.internet.MimeMultipart; import org.citrusframework.mail.model.MailMarshaller; import org.citrusframework.mail.model.MailRequest; import org.citrusframework.mail.server.MailServer; import org.citrusframework.message.DefaultMessage; import org.citrusframework.testng.AbstractTestNGUnitTest; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.springframework.core.io.ClassPathResource; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.util.StringUtils; import org.testng.Assert; -import org.testng.annotations.BeforeClass; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import jakarta.mail.*; -import jakarta.mail.internet.*; import javax.xml.transform.stream.StreamSource; -import java.util.Properties; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; /** * @author Christoph Deppisch * @since 1.4 */ -public class MailClientTest extends AbstractTestNGUnitTest { +class MailClientTest extends AbstractTestNGUnitTest { - private JavaMailSenderImpl javaMailSender = Mockito.mock(JavaMailSenderImpl.class); + private AutoCloseable mockitoContext; - private MailClient mailClient = new MailClient(); + @Mock + private MailSender mailSenderMock; - @BeforeClass - public void setup() { - mailClient.getEndpointConfiguration().setJavaMailSender(javaMailSender); - } + private MailClient fixture; - @Test - public void testSendMailMessageObject() throws Exception { - MailRequest mailRequest = (MailRequest) new MailMarshaller().unmarshal(new StreamSource( - new ClassPathResource("text_mail.xml", MailServer.class).getInputStream())); - - reset(javaMailSender); - when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage(Session.getDefaultInstance(new Properties()))); - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0]; - Assert.assertEquals(getAddresses(mimeMessage.getFrom()), "foo@mail.com"); - Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.TO)), "bar@mail.com,copy@mail.com"); - Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.CC)), "foobar@mail.com"); - Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.BCC)), "secret@mail.com"); - Assert.assertEquals(getAddresses(mimeMessage.getReplyTo()), "foo@mail.com"); - Assert.assertNotNull(mimeMessage.getSentDate()); - Assert.assertEquals(mimeMessage.getSubject(), "Testmail"); - Assert.assertEquals(mimeMessage.getContentType(), "text/plain"); - - Assert.assertEquals(mimeMessage.getContent().toString(), "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."); - return null; - } - }).when(javaMailSender).send(any(MimeMessage.class)); - - mailClient.send(new DefaultMessage(mailRequest), context); + @BeforeMethod + public void beforeMethodSetup() { + mockitoContext = MockitoAnnotations.openMocks(this); + fixture = new MailClient(); + fixture.setMailSender(mailSenderMock); } @Test - public void testSendMultipartMailMessageObject() throws Exception { - MailRequest mailRequest = (MailRequest) new MailMarshaller().unmarshal(new StreamSource( - new ClassPathResource("multipart_mail.xml", MailServer.class).getInputStream())); - - reset(javaMailSender); - when(javaMailSender.createMimeMessage()).thenReturn(new MimeMessage(Session.getDefaultInstance(new Properties()))); - doAnswer(new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0]; - Assert.assertEquals(getAddresses(mimeMessage.getFrom()), "foo@mail.com"); - Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.TO)), "bar@mail.com"); - Assert.assertEquals(getAddresses(mimeMessage.getReplyTo()), "foo@mail.com"); - Assert.assertNotNull(mimeMessage.getSentDate()); - Assert.assertEquals(mimeMessage.getSubject(), "Multipart Testmail"); - Assert.assertEquals(mimeMessage.getContentType(), "text/plain"); - - Assert.assertEquals(mimeMessage.getContent().getClass(), MimeMultipart.class); - - MimeMultipart multipart = (MimeMultipart) mimeMessage.getContent(); - - Assert.assertEquals(multipart.getCount(), 2L); - Assert.assertTrue(multipart.getContentType().startsWith("multipart/mixed")); - Assert.assertTrue(((MimeMultipart) multipart.getBodyPart(0).getContent()).getContentType().startsWith("multipart/related")); - Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getCount(), 1L); - Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getBodyPart(0).getContent().toString(), "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."); - Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getBodyPart(0).getContentType(), "text/plain"); - Assert.assertEquals(StringUtils.trimAllWhitespace(multipart.getBodyPart(1).getContent().toString()), "

HTMLAttachment

"); - Assert.assertEquals(multipart.getBodyPart(1).getFileName(), "index.html"); - Assert.assertEquals(multipart.getBodyPart(1).getDisposition(), "attachment"); - return null; - } - }).when(javaMailSender).send(any(MimeMessage.class)); + void testSendMailMessageObject() throws Exception { + MailRequest mailRequest = (MailRequest) new MailMarshaller().unmarshal( + new StreamSource( + new ClassPathResource("text_mail.xml", MailServer.class).getInputStream() + ) + ); + + doAnswer(invocation -> { + MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0]; + Assert.assertEquals(getAddresses(mimeMessage.getFrom()), "foo@mail.com"); + Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.TO)), "bar@mail.com,copy@mail.com"); + Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.CC)), "foobar@mail.com"); + Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.BCC)), "secret@mail.com"); + Assert.assertEquals(getAddresses(mimeMessage.getReplyTo()), "foo@mail.com"); + Assert.assertNotNull(mimeMessage.getSentDate()); + Assert.assertEquals(mimeMessage.getSubject(), "Testmail"); + Assert.assertEquals(mimeMessage.getContentType(), "text/plain"); + + Assert.assertEquals(mimeMessage.getContent().toString(), "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."); + return null; + }).when(mailSenderMock).send(any(MimeMessage.class)); + + fixture.send(new DefaultMessage(mailRequest), context); + } - mailClient.send(new DefaultMessage(mailRequest), context); + @Test + void testSendMultipartMailMessageObject() throws Exception { + MailRequest mailRequest = (MailRequest) new MailMarshaller().unmarshal( + new StreamSource( + new ClassPathResource("multipart_mail.xml", MailServer.class).getInputStream() + ) + ); + + doAnswer(invocation -> { + MimeMessage mimeMessage = (MimeMessage) invocation.getArguments()[0]; + Assert.assertEquals(getAddresses(mimeMessage.getFrom()), "foo@mail.com"); + Assert.assertEquals(getAddresses(mimeMessage.getRecipients(Message.RecipientType.TO)), "bar@mail.com"); + Assert.assertEquals(getAddresses(mimeMessage.getReplyTo()), "foo@mail.com"); + Assert.assertNotNull(mimeMessage.getSentDate()); + Assert.assertEquals(mimeMessage.getSubject(), "Multipart Testmail"); + Assert.assertEquals(mimeMessage.getContentType(), "text/plain"); + + Assert.assertEquals(mimeMessage.getContent().getClass(), MimeMultipart.class); + + MimeMultipart multipart = (MimeMultipart) mimeMessage.getContent(); + + Assert.assertEquals(multipart.getCount(), 2L); + Assert.assertTrue(multipart.getContentType().startsWith("multipart/mixed")); + Assert.assertTrue(((MimeMultipart) multipart.getBodyPart(0).getContent()).getContentType().startsWith("multipart/related")); + Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getCount(), 1L); + Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getBodyPart(0).getContent().toString(), "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua."); + Assert.assertEquals(((MimeMultipart) multipart.getBodyPart(0).getContent()).getBodyPart(0).getContentType(), "text/plain"); + Assert.assertEquals(StringUtils.trimAllWhitespace(multipart.getBodyPart(1).getContent().toString()), "

HTMLAttachment

"); + Assert.assertEquals(multipart.getBodyPart(1).getFileName(), "index.html"); + Assert.assertEquals(multipart.getBodyPart(1).getDisposition(), "attachment"); + return null; + }).when(mailSenderMock).send(any(MimeMessage.class)); + + fixture.send(new DefaultMessage(mailRequest), context); } @@ -128,7 +133,7 @@ private String getAddresses(Address[] addressList) { InternetAddress address = (InternetAddress) addressList[i]; if (i > 0) { - builder.append("," + address.getAddress()); + builder.append(",").append(address.getAddress()); } else { builder.append(address.getAddress()); } @@ -136,4 +141,9 @@ private String getAddresses(Address[] addressList) { return builder.toString(); } + + @AfterMethod + void afterMethodTeardown() throws Exception { + mockitoContext.close(); + } } diff --git a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/annotation/MailClientConfigParserTest.java b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/annotation/MailClientConfigParserTest.java index 7188e4831b..7fc81983ca 100644 --- a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/annotation/MailClientConfigParserTest.java +++ b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/annotation/MailClientConfigParserTest.java @@ -97,27 +97,30 @@ public void testMailClientParser() { // 1st mail mailClient Assert.assertEquals(mailClient1.getName(), "mailClient1"); - Assert.assertEquals(mailClient1.getEndpointConfiguration().getJavaMailSender().getHost(), "localhost"); - Assert.assertEquals(mailClient1.getEndpointConfiguration().getJavaMailSender().getPort(), 25000); + Assert.assertEquals(mailClient1.getEndpointConfiguration().getHost(), "localhost"); + Assert.assertEquals(mailClient1.getEndpointConfiguration().getPort(), 25000); + Assert.assertEquals(mailClient1.getEndpointConfiguration().getJavaMailProperties().getProperty("mail.smtp.host"), "localhost"); + Assert.assertEquals(mailClient1.getEndpointConfiguration().getJavaMailProperties().getProperty("mail.smtp.port"), "25000"); Assert.assertNull(mailClient1.getActor()); // 2nd mail mailClient Assert.assertEquals(mailClient2.getName(), "mailClient2"); - Assert.assertEquals(mailClient2.getEndpointConfiguration().getJavaMailSender().getHost(), "localhost"); - Assert.assertEquals(mailClient2.getEndpointConfiguration().getJavaMailSender().getPort(), 25000); - Assert.assertEquals(mailClient2.getEndpointConfiguration().getJavaMailSender().getUsername(), "mailus"); - Assert.assertEquals(mailClient2.getEndpointConfiguration().getJavaMailSender().getPassword(), "secret"); - Assert.assertEquals(mailClient2.getEndpointConfiguration().getJavaMailSender().getJavaMailProperties().get("mail.smtp.auth"), "true"); + Assert.assertEquals(mailClient2.getEndpointConfiguration().getHost(), "localhost"); + Assert.assertEquals(mailClient2.getEndpointConfiguration().getPort(), 25000); + Assert.assertEquals(mailClient2.getEndpointConfiguration().getUsername(), "mailus"); + Assert.assertEquals(mailClient2.getEndpointConfiguration().getPassword(), "secret"); + Assert.assertNotNull(mailClient2.getEndpointConfiguration().getAuthenticator()); + Assert.assertEquals(mailClient2.getEndpointConfiguration().getJavaMailProperties().get("mail.smtp.auth"), "true"); Assert.assertNull(mailClient2.getActor()); // 3rd mail mailClient Assert.assertEquals(mailClient3.getName(), "mailClient3"); - Assert.assertEquals(mailClient3.getEndpointConfiguration().getJavaMailSender().getHost(), "localhost"); - Assert.assertEquals(mailClient3.getEndpointConfiguration().getJavaMailSender().getPort(), 25000); + Assert.assertEquals(mailClient3.getEndpointConfiguration().getHost(), "localhost"); + Assert.assertEquals(mailClient3.getEndpointConfiguration().getPort(), 25000); Assert.assertNotNull(mailClient3.getActor()); Assert.assertEquals(mailClient3.getEndpointConfiguration().getMessageConverter(), messageConverter); Assert.assertEquals(mailClient3.getEndpointConfiguration().getMarshaller(), marshaller); - Assert.assertEquals(mailClient3.getEndpointConfiguration().getJavaMailSender().getJavaMailProperties(), mailProperties); + Assert.assertEquals(mailClient3.getEndpointConfiguration().getJavaMailProperties(), mailProperties); } @Test diff --git a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/xml/MailClientParserTest.java b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/xml/MailClientParserTest.java index 3d6d8a2c9e..4f078c3b26 100644 --- a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/xml/MailClientParserTest.java +++ b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/config/xml/MailClientParserTest.java @@ -40,28 +40,30 @@ public void testMailClientParser() { // 1st mail sender MailClient sender = senders.get("mailClient1"); Assert.assertEquals(sender.getName(), "mailClient1"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getHost(), "localhost"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getPort(), 25000); + Assert.assertEquals(sender.getEndpointConfiguration().getHost(), "localhost"); + Assert.assertEquals(sender.getEndpointConfiguration().getPort(), 25000); + Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailProperties().getProperty("mail.smtp.host"), "localhost"); + Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailProperties().getProperty("mail.smtp.port"), "25000"); Assert.assertNull(sender.getActor()); // 2nd mail sender sender = senders.get("mailClient2"); Assert.assertEquals(sender.getName(), "mailClient2"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getHost(), "localhost"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getPort(), 25000); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getUsername(), "mailus"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getPassword(), "secret"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getJavaMailProperties().get("mail.smtp.auth"), "true"); + Assert.assertEquals(sender.getEndpointConfiguration().getHost(), "localhost"); + Assert.assertEquals(sender.getEndpointConfiguration().getPort(), 25000); + Assert.assertEquals(sender.getEndpointConfiguration().getUsername(), "mailus"); + Assert.assertEquals(sender.getEndpointConfiguration().getPassword(), "secret"); + Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailProperties().get("mail.smtp.auth"), "true"); Assert.assertNull(sender.getActor()); // 3rd mail sender sender = senders.get("mailClient3"); Assert.assertEquals(sender.getName(), "mailClient3"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getHost(), "localhost"); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getPort(), 25000); + Assert.assertEquals(sender.getEndpointConfiguration().getHost(), "localhost"); + Assert.assertEquals(sender.getEndpointConfiguration().getPort(), 25000); Assert.assertNotNull(sender.getActor()); Assert.assertEquals(sender.getEndpointConfiguration().getMessageConverter(), beanDefinitionContext.getBean("messageConverter", MessageConverter.class)); Assert.assertEquals(sender.getEndpointConfiguration().getMarshaller(), beanDefinitionContext.getBean("marshaller", MailMarshaller.class)); - Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailSender().getJavaMailProperties().get("mail.transport.protocol"), "smtp"); + Assert.assertEquals(sender.getEndpointConfiguration().getJavaMailProperties().get("mail.transport.protocol"), "smtp"); } } diff --git a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/server/MailServerTest.java b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/server/MailServerTest.java index 5256e1e02d..082ab25708 100644 --- a/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/server/MailServerTest.java +++ b/endpoints/citrus-mail/src/test/java/org/citrusframework/mail/server/MailServerTest.java @@ -25,12 +25,14 @@ import org.citrusframework.message.DefaultMessage; import org.citrusframework.message.Message; import org.citrusframework.util.TestUtils; -import org.mockito.Mockito; -import org.mockito.stubbing.Answer; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.springframework.core.io.ClassPathResource; import org.springframework.util.FileCopyUtils; import org.springframework.util.StringUtils; import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.io.IOException; @@ -41,24 +43,31 @@ import static org.mockito.Mockito.any; import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.reset; import static org.mockito.Mockito.when; /** * @author Christoph Deppisch */ -public class MailServerTest { +class MailServerTest { - private final EndpointAdapter endpointAdapterMock = Mockito.mock(EndpointAdapter.class); + private AutoCloseable mockitoContext; - @Test - public void testTextMessage() throws IOException, MessagingException { - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); + @Mock + private EndpointAdapter endpointAdapterMock; + + private MailServer fixture; - reset(endpointAdapterMock); + @BeforeMethod + void beforeMethodSetup() { + mockitoContext = MockitoAnnotations.openMocks(this); + + fixture = new MailServer(); + fixture.setEndpointAdapter(endpointAdapterMock); + } - doAnswer((Answer) invocation -> { + @Test + void testTextMessage() throws IOException, MessagingException { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -73,30 +82,36 @@ public void testTextMessage() throws IOException, MessagingException { Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "text/plain"); try { - Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)), - StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("text_mail.xml", - MailServer.class).getInputStream())))); + Assert.assertEquals( + StringUtils.trimAllWhitespace(message.getPayload(String.class)), + StringUtils.trimAllWhitespace( + FileCopyUtils.copyToString( + new InputStreamReader( + new ClassPathResource("text_mail.xml", MailServer.class).getInputStream() + ) + ) + ) + ); } catch (IOException e) { Assert.fail(e.getMessage()); } return null; - }).when(endpointAdapterMock).handleMessage(any(Message.class)); + }) + .when(endpointAdapterMock).handleMessage(any(Message.class)); + + MimeMessage message = new MimeMessage(fixture.getSession(), new ClassPathResource("text_mail.txt", MailServer.class).getInputStream()); + fixture.deliver(message); - MimeMessage message = new MimeMessage(mailServer.getSession(), new ClassPathResource("text_mail.txt", MailServer.class).getInputStream()); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); - mailServer.deliver(message); + // Because of autoAccept = true + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); } @Test - public void testMultipartMessage() throws IOException, MessagingException { + void testMultipartMessage() throws IOException, MessagingException { final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - reset(endpointAdapterMock); - - doAnswer((Answer) invocation -> { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -107,8 +122,7 @@ public void testMultipartMessage() throws IOException, MessagingException { Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), ""); Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "foo@mail.com"); - // compare the Date as a Date rather than a String, otherwise this test fails outside of the - // "+1" timezone + // compare the Date as a Date rather than a String, otherwise this test fails outside the "+1" timezone Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE)); Date expectedDateDate = dateFormat.parse("2006-10-26T13:10:50+0200"); Assert.assertEquals(actualDate, expectedDateDate); @@ -122,7 +136,13 @@ public void testMultipartMessage() throws IOException, MessagingException { StringUtils.trimAllWhitespace(message.getPayload(String.class)) ), TestUtils.normalizeLineEndings( - StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("multipart_mail.xml", MailServer.class).getInputStream()))) + StringUtils.trimAllWhitespace( + FileCopyUtils.copyToString( + new InputStreamReader( + new ClassPathResource("multipart_mail.xml", MailServer.class).getInputStream() + ) + ) + ) ) ); } catch (IOException e) { @@ -132,21 +152,18 @@ public void testMultipartMessage() throws IOException, MessagingException { return null; }).when(endpointAdapterMock).handleMessage(any(Message.class)); - MimeMessage message = new MimeMessage(mailServer.getSession(), new ClassPathResource("multipart_mail.txt", MailServer.class).getInputStream()); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); - mailServer.deliver(message); + MimeMessage message = new MimeMessage(fixture.getSession(), new ClassPathResource("multipart_mail.txt", MailServer.class).getInputStream()); + fixture.deliver(message); + + // Because of autoAccept = true + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); } @Test - public void testBinaryMessage() throws IOException, MessagingException { + void testBinaryMessage() throws IOException, MessagingException { final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - - reset(endpointAdapterMock); - - doAnswer((Answer) invocation -> { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -157,8 +174,7 @@ public void testBinaryMessage() throws IOException, MessagingException { Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), ""); Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "Foo "); - // compare the Date as a Date rather than a String, otherwsie this test fails outside of the - // "+1" timezone + // compare the Date as a Date rather than a String, otherwsie this test fails outside the "+1" timezone Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE)); Date expectedDateDate = dateFormat.parse("2013-12-06T10:27:41+0100"); Assert.assertEquals(actualDate, expectedDateDate); @@ -167,10 +183,9 @@ public void testBinaryMessage() throws IOException, MessagingException { Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "multipart/mixed"); try { - Assert.assertEquals( - StringUtils.trimAllWhitespace(message.getPayload(String.class)), - StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("binary_mail.xml",MailServer.class).getInputStream()))) - ); + Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)), + StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("binary_mail.xml", + MailServer.class).getInputStream())))); } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -178,49 +193,54 @@ public void testBinaryMessage() throws IOException, MessagingException { return null; }).when(endpointAdapterMock).handleMessage(any(Message.class)); - MimeMessage message = new MimeMessage(mailServer.getSession(), new ClassPathResource("binary_mail.txt", MailServer.class).getInputStream()); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); - mailServer.deliver(message); + MimeMessage message = new MimeMessage(fixture.getSession(), new ClassPathResource("binary_mail.txt", MailServer.class).getInputStream()); + fixture.deliver(message); + + // Because of autoAccept = true + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); } @Test - public void testAutoAcceptDisabled() { - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - - reset(endpointAdapterMock); - - doAnswer((Answer) invocation -> { + void testAutoAcceptDisabled() { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); try { - Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)), - StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("accept-request.xml", - MailServer.class).getInputStream())))); + Assert.assertEquals( + StringUtils.trimAllWhitespace(message.getPayload(String.class)), + StringUtils.trimAllWhitespace( + FileCopyUtils.copyToString( + new InputStreamReader( + new ClassPathResource("accept-request.xml", MailServer.class).getInputStream()) + ) + ) + ); } catch (IOException e) { Assert.fail(e.getMessage()); } - return new DefaultMessage(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("accept-response.xml", - MailServer.class).getInputStream()))); + return new DefaultMessage( + FileCopyUtils.copyToString( + new InputStreamReader( + new ClassPathResource("accept-response.xml",MailServer.class).getInputStream() + ) + ) + ); }).when(endpointAdapterMock).handleMessage(any(Message.class)); - mailServer.setAutoAccept(false); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); + fixture.setAutoAccept(false); + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); } @Test - public void testAutoAcceptDisabledWithTimeout() { - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - - reset(endpointAdapterMock); + void testAutoAcceptDisabledWithTimeout() { when(endpointAdapterMock.handleMessage(any(Message.class))).thenReturn(null); - mailServer.setAutoAccept(false); + fixture.setAutoAccept(false); + try { - mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com"))); + fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com"))); Assert.fail("Missing runtime exception due to missing accept response"); } catch (CitrusRuntimeException e) { Assert.assertTrue(e.getMessage().startsWith("Did not receive accept response")); @@ -228,15 +248,12 @@ public void testAutoAcceptDisabledWithTimeout() { } @Test - public void testAutoAcceptDisabledWithInvalidAcceptResponse() { - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - - reset(endpointAdapterMock); + void testAutoAcceptDisabledWithInvalidAcceptResponse() { when(endpointAdapterMock.handleMessage(any(Message.class))).thenReturn(new DefaultMessage(99L)); - mailServer.setAutoAccept(false); + fixture.setAutoAccept(false); + try { - mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com"))); + fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com"))); Assert.fail("Missing runtime exception due to invalid accept response"); } catch (CitrusRuntimeException e) { Assert.assertTrue(e.getMessage().startsWith("Unable to read accept response")); @@ -244,14 +261,10 @@ public void testAutoAcceptDisabledWithInvalidAcceptResponse() { } @Test - public void testTextMessageSplitting() throws IOException, MessagingException { - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - mailServer.setSplitMultipart(true); + void testTextMessageSplitting() throws IOException, MessagingException { + fixture.setSplitMultipart(true); - reset(endpointAdapterMock); - - doAnswer((Answer) invocation -> { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -266,9 +279,15 @@ public void testTextMessageSplitting() throws IOException, MessagingException { Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "text/plain"); try { - Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)), - StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("text_mail.xml", - MailServer.class).getInputStream())))); + Assert.assertEquals( + StringUtils.trimAllWhitespace(message.getPayload(String.class)), + StringUtils.trimAllWhitespace( + FileCopyUtils.copyToString( + new InputStreamReader( + new ClassPathResource("text_mail.xml", MailServer.class).getInputStream()) + ) + ) + ); } catch (IOException e) { Assert.fail(e.getMessage()); } @@ -276,21 +295,20 @@ public void testTextMessageSplitting() throws IOException, MessagingException { return null; }).when(endpointAdapterMock).handleMessage(any(Message.class)); - MimeMessage message = new MimeMessage(mailServer.getSession(), new ClassPathResource("text_mail.txt", MailServer.class).getInputStream()); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); - mailServer.deliver(message); + MimeMessage message = new MimeMessage(fixture.getSession(), new ClassPathResource("text_mail.txt", MailServer.class).getInputStream()); + fixture.deliver(message); + + // Because of autoAccept = true + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); } @Test - public void testMultipartMessageSplitting() throws IOException, MessagingException { + void testMultipartMessageSplitting() throws IOException, MessagingException { final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - mailServer.setSplitMultipart(true); - reset(endpointAdapterMock); + fixture.setSplitMultipart(true); - doAnswer((Answer) invocation -> { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -301,8 +319,7 @@ public void testMultipartMessageSplitting() throws IOException, MessagingExcepti Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), ""); Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "foo@mail.com"); - // compare dates as Date rather than String otherwise this test fails outside - // of the "+1" timezone + // compare dates as Date rather than String otherwise this test fails outside the "+1" timezone Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE)); Date expectedDateDate = dateFormat.parse("2006-10-26T13:10:50+0200"); Assert.assertEquals(actualDate, expectedDateDate); @@ -319,7 +336,7 @@ public void testMultipartMessageSplitting() throws IOException, MessagingExcepti } return null; - }).doAnswer((Answer) invocation -> { + }).doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -330,8 +347,7 @@ public void testMultipartMessageSplitting() throws IOException, MessagingExcepti Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), ""); Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "foo@mail.com"); - // compare dates as Date rather than String otherwise this test fails outside - // of the "+1" timezone + // compare dates as Date rather than String otherwise this test fails outside the "+1" timezone Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE)); Date expectedDateDate = dateFormat.parse("2006-10-26T13:10:50+0200"); Assert.assertEquals(actualDate, expectedDateDate); @@ -358,21 +374,20 @@ public void testMultipartMessageSplitting() throws IOException, MessagingExcepti return null; }).when(endpointAdapterMock).handleMessage(any(Message.class)); - MimeMessage message = new MimeMessage(mailServer.getSession(), new ClassPathResource("multipart_mail.txt", MailServer.class).getInputStream()); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); - mailServer.deliver(message); + MimeMessage message = new MimeMessage(fixture.getSession(), new ClassPathResource("multipart_mail.txt", MailServer.class).getInputStream()); + fixture.deliver(message); + + // Because of autoAccept = true + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); } @Test - public void testBinaryMessageSplitting() throws IOException, MessagingException { + void testBinaryMessageSplitting() throws IOException, MessagingException { final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - mailServer.setSplitMultipart(true); - reset(endpointAdapterMock); + fixture.setSplitMultipart(true); - doAnswer((Answer) invocation -> { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -383,9 +398,7 @@ public void testBinaryMessageSplitting() throws IOException, MessagingException Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), ""); Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "Foo "); - - // compare dates as Date rather than String otherwise this test fails outside - // of the "+1" timezone + // compare dates as Date rather than String otherwise this test fails outside the "+1" timezone Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE)); Date expectedDateDate = dateFormat.parse("2013-12-06T10:27:41+0100"); Assert.assertEquals(actualDate, expectedDateDate); @@ -402,7 +415,7 @@ public void testBinaryMessageSplitting() throws IOException, MessagingException } return null; - }).doAnswer((Answer) invocation -> { + }).doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -413,8 +426,7 @@ public void testBinaryMessageSplitting() throws IOException, MessagingException Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), ""); Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "Foo "); - // compare dates as Date rather than String otherwise this test fails outside - // of the "+1" timezone + // compare dates as Date rather than String otherwise this test fails outside the "+1" timezone Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE)); Date expectedDateDate = dateFormat.parse("2013-12-06T10:27:41+0100"); Assert.assertEquals(actualDate, expectedDateDate); @@ -434,19 +446,16 @@ public void testBinaryMessageSplitting() throws IOException, MessagingException return null; }).when(endpointAdapterMock).handleMessage(any(Message.class)); - MimeMessage message = new MimeMessage(mailServer.getSession(), new ClassPathResource("binary_mail.txt", MailServer.class).getInputStream()); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); - mailServer.deliver(message); + MimeMessage message = new MimeMessage(fixture.getSession(), new ClassPathResource("binary_mail.txt", MailServer.class).getInputStream()); + fixture.deliver(message); + + // Because of autoAccept = true + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); } @Test - public void testSimulateError() throws IOException, MessagingException { - MailServer mailServer = new MailServer(); - mailServer.setEndpointAdapter(endpointAdapterMock); - - reset(endpointAdapterMock); - - doAnswer((Answer) invocation -> { + void testSimulateError() throws IOException, MessagingException { + doAnswer(invocation -> { Message message = (Message) invocation.getArguments()[0]; Assert.assertNotNull(message.getPayload()); @@ -472,14 +481,19 @@ public void testSimulateError() throws IOException, MessagingException { MailServer.class).getInputStream()))); }).when(endpointAdapterMock).handleMessage(any(Message.class)); - MimeMessage message = new MimeMessage(mailServer.getSession(), new ClassPathResource("text_mail.txt", MailServer.class).getInputStream()); - Assert.assertTrue(mailServer.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); + MimeMessage message = new MimeMessage(fixture.getSession(), new ClassPathResource("text_mail.txt", MailServer.class).getInputStream()); + Assert.assertTrue(fixture.accept("foo@mail.com", Collections.singletonList(new MailAddress("bar@mail.com")))); try { - mailServer.deliver(message); + fixture.deliver(message); throw new CitrusRuntimeException("Missing reject exception due to simulated error"); } catch (CitrusRuntimeException e) { Assert.assertEquals(e.getMessage(), "443 Failed!"); } } + + @AfterMethod + void afterMethodTeardown() throws Exception { + mockitoContext.close(); + } } diff --git a/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerAcceptIT.xml b/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerAcceptIT.xml index 08672b03a7..08dd7c5f55 100644 --- a/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerAcceptIT.xml +++ b/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerAcceptIT.xml @@ -137,7 +137,7 @@ - + diff --git a/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerErrorIT.xml b/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerErrorIT.xml index 7a44b08897..77e8a70287 100644 --- a/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerErrorIT.xml +++ b/endpoints/citrus-mail/src/test/resources/org/citrusframework/mail/integration/MailServerErrorIT.xml @@ -30,7 +30,7 @@ - + diff --git a/endpoints/citrus-ws/pom.xml b/endpoints/citrus-ws/pom.xml index 9f2fdd2f05..0781cb91b5 100644 --- a/endpoints/citrus-ws/pom.xml +++ b/endpoints/citrus-ws/pom.xml @@ -101,10 +101,6 @@ com.sun.xml.messaging.saaj saaj-impl - - com.sun.activation - jakarta.activation - jakarta.xml.bind diff --git a/pom.xml b/pom.xml index d5b389b6ec..82ed82fff0 100644 --- a/pom.xml +++ b/pom.xml @@ -168,6 +168,7 @@ 3.1.0 2.30.0 + 2.0.2 1.10.13 4.0.0-RC1 1.7.0.Final @@ -188,16 +189,15 @@ 1.2.0 3.0.18 32.1.1-jre + 2.0.0 2.2 4.10.0 5.2.1 2.7.2 2.15.2 - 2.1.2 - 2.0.1 - 2.1.1 + 2.1.1 3.1.0 - 2.0.1 + 2.0.2 6.0.0 3.0.2 2.1.1 @@ -234,9 +234,8 @@ 6.1.2 3.0.0 2.10.0 - 2.0.0 1.6.9 - 1.0.64 + 2.1.16 7.8.0 4.4.4 1.6.3 @@ -267,16 +266,6 @@ ${jakarta.jms-api.version} - - jakarta.activation - jakarta.activation-api - ${jakarta.activation.api.version} - - - com.sun.activation - jakarta.activation - ${jakarta.activation.version} - com.sun.xml.messaging.saaj saaj-impl @@ -588,16 +577,16 @@ jetty-webapp ${jetty.version} + + jakarta.annotation + jakarta.annotation-api + ${jakarta.annotation-api.version} + jakarta.servlet jakarta.servlet-api ${jakarta.servlet-api.version} - - jakarta.annotation - jakarta.annotation-api - ${jakarta.annotation.version} - jakarta.websocket jakarta.websocket-api @@ -645,16 +634,16 @@ - - com.sun.mail - jakarta.mail - ${jakarta.mail.version} - com.icegreen greenmail ${greenmail.version} + + org.eclipse.angus + jakarta.mail + ${jakarta.mail.version} + @@ -1348,11 +1337,6 @@ jaxb-xjc ${jaxb.version} - - jakarta.activation - jakarta.activation-api - ${jakarta.activation.api.version} - jakarta.xml.bind jakarta.xml.bind-api diff --git a/tools/pom.xml b/tools/pom.xml index ec69d16713..c45d3e07ba 100644 --- a/tools/pom.xml +++ b/tools/pom.xml @@ -24,7 +24,7 @@ - io.swagger + io.swagger.parser.v3 swagger-parser ${swagger.parser.version} diff --git a/tools/test-generator/pom.xml b/tools/test-generator/pom.xml index 26f60ee4e8..f5091f5cdc 100644 --- a/tools/test-generator/pom.xml +++ b/tools/test-generator/pom.xml @@ -63,7 +63,7 @@ - io.swagger + io.swagger.parser.v3 swagger-parser