Best Citrus code snippet using com.consol.citrus.mail.server.MailServer.accept
Source:MailServerTest.java
...67 }68 return null;69 }70 }).when(endpointAdapterMock).handleMessage(any(Message.class));71 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));72 mailServer.deliver("foo@mail.com", "bar@mail.com",73 new ClassPathResource("text_mail.txt", MailServer.class).getInputStream());74 }75 @Test76 @SuppressWarnings("unchecked")77 public void testMultipartMessage() throws IOException {78 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");79 MailServer mailServer = new MailServer();80 mailServer.setEndpointAdapter(endpointAdapterMock);81 reset(endpointAdapterMock);82 doAnswer(new Answer<Message>() {83 @Override84 public Message answer(InvocationOnMock invocation) throws Throwable {85 Message message = (Message) invocation.getArguments()[0];86 Assert.assertNotNull(message.getPayload());87 Assert.assertNull(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID));88 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "foo@mail.com");89 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com");90 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "");91 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "");92 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "foo@mail.com");93 // compare the Date as a Date rather than a String, otherwsie this test fails outside of the94 // "+1" timezone95 Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));96 Date expectedDateDate = dateFormat.parse("2006-10-26T13:10:50+0200");97 Assert.assertEquals(actualDate, expectedDateDate);98 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "Multipart Testmail");99 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "multipart/mixed");100 try {101 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),102 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("multipart_mail.xml",103 MailServer.class).getInputStream()))));104 } catch (IOException e) {105 Assert.fail(e.getMessage());106 }107 return null;108 }109 }).when(endpointAdapterMock).handleMessage(any(Message.class));110 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));111 mailServer.deliver("foo@mail.com", "bar@mail.com",112 new ClassPathResource("multipart_mail.txt", MailServer.class).getInputStream());113 }114 @Test115 @SuppressWarnings("unchecked")116 public void testBinaryMessage() throws IOException {117 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");118 MailServer mailServer = new MailServer();119 mailServer.setEndpointAdapter(endpointAdapterMock);120 reset(endpointAdapterMock);121 doAnswer(new Answer<Message>() {122 @Override123 public Message answer(InvocationOnMock invocation) throws Throwable {124 Message message = (Message) invocation.getArguments()[0];125 Assert.assertNotNull(message.getPayload());126 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID), "<52A1988D.2060403@consol.de>");127 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "Foo <foo@mail.com>");128 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com");129 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "FooBar <foobar@mail.com>");130 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "");131 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "Foo <foo@mail.com>");132 // compare the Date as a Date rather than a String, otherwsie this test fails outside of the133 // "+1" timezone134 Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));135 Date expectedDateDate = dateFormat.parse("2013-12-06T10:27:41+0100");136 Assert.assertEquals(actualDate, expectedDateDate);137 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "This is brand_logo.png");138 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "multipart/mixed");139 try {140 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),141 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("binary_mail.xml",142 MailServer.class).getInputStream()))));143 } catch (IOException e) {144 Assert.fail(e.getMessage());145 }146 return null;147 }148 }).when(endpointAdapterMock).handleMessage(any(Message.class));149 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));150 mailServer.deliver("foo@mail.com", "bar@mail.com",151 new ClassPathResource("binary_mail.txt", MailServer.class).getInputStream());152 }153 @Test154 @SuppressWarnings("unchecked")155 public void testAutoAcceptDisabled() throws IOException {156 MailServer mailServer = new MailServer();157 mailServer.setEndpointAdapter(endpointAdapterMock);158 reset(endpointAdapterMock);159 doAnswer(new Answer<Message>() {160 @Override161 public Message answer(InvocationOnMock invocation) throws Throwable {162 Message message = (Message) invocation.getArguments()[0];163 Assert.assertNotNull(message.getPayload());164 try {165 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),166 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("accept-request.xml",167 MailServer.class).getInputStream()))));168 } catch (IOException e) {169 Assert.fail(e.getMessage());170 }171 return new DefaultMessage(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("accept-response.xml",172 MailServer.class).getInputStream())));173 }174 }).when(endpointAdapterMock).handleMessage(any(Message.class));175 mailServer.setAutoAccept(false);176 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));177 }178 @Test179 @SuppressWarnings("unchecked")180 public void testAutoAcceptDisabledWithTimeout() throws IOException {181 MailServer mailServer = new MailServer();182 mailServer.setEndpointAdapter(endpointAdapterMock);183 reset(endpointAdapterMock);184 when(endpointAdapterMock.handleMessage(any(Message.class))).thenReturn(null);185 mailServer.setAutoAccept(false);186 try {187 mailServer.accept("foo@mail.com", "bar@mail.com");188 Assert.fail("Missing runtime exception due to missing accept response");189 } catch (CitrusRuntimeException e) {190 Assert.assertTrue(e.getMessage().startsWith("Did not receive accept response"));191 }192 }193 @Test194 @SuppressWarnings("unchecked")195 public void testAutoAcceptDisabledWithInvalidAcceptResponse() throws IOException {196 MailServer mailServer = new MailServer();197 mailServer.setEndpointAdapter(endpointAdapterMock);198 reset(endpointAdapterMock);199 when(endpointAdapterMock.handleMessage(any(Message.class))).thenReturn(new DefaultMessage(99L));200 mailServer.setAutoAccept(false);201 try {202 mailServer.accept("foo@mail.com", "bar@mail.com");203 Assert.fail("Missing runtime exception due to invalid accept response");204 } catch (CitrusRuntimeException e) {205 Assert.assertTrue(e.getMessage().startsWith("Unable to read accept response"));206 }207 }208 @Test209 @SuppressWarnings("unchecked")210 public void testTextMessageSplitting() throws IOException {211 MailServer mailServer = new MailServer();212 mailServer.setEndpointAdapter(endpointAdapterMock);213 mailServer.setSplitMultipart(true);214 reset(endpointAdapterMock);215 doAnswer(new Answer<Message>() {216 @Override217 public Message answer(InvocationOnMock invocation) throws Throwable {218 Message message = (Message) invocation.getArguments()[0];219 Assert.assertNotNull(message.getPayload());220 Assert.assertNull(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID));221 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "foo@mail.com");222 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com,copy@mail.com");223 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "foobar@mail.com");224 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "secret@mail.com");225 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "reply@mail.com");226 Assert.assertNull(message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));227 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "Testmail");228 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "text/plain");229 try {230 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),231 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("text_mail.xml",232 MailServer.class).getInputStream()))));233 } catch (IOException e) {234 Assert.fail(e.getMessage());235 }236 return null;237 }238 }).when(endpointAdapterMock).handleMessage(any(Message.class));239 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));240 mailServer.deliver("foo@mail.com", "bar@mail.com",241 new ClassPathResource("text_mail.txt", MailServer.class).getInputStream());242 }243 @Test244 @SuppressWarnings("unchecked")245 public void testMultipartMessageSplitting() throws IOException {246 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");247 MailServer mailServer = new MailServer();248 mailServer.setEndpointAdapter(endpointAdapterMock);249 mailServer.setSplitMultipart(true);250 reset(endpointAdapterMock);251 doAnswer(new Answer<Message>() {252 @Override253 public Message answer(InvocationOnMock invocation) throws Throwable {254 Message message = (Message) invocation.getArguments()[0];255 Assert.assertNotNull(message.getPayload());256 Assert.assertNull(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID));257 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "foo@mail.com");258 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com");259 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "");260 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "");261 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "foo@mail.com");262 // compare dates as Date rather than String otherwise this test fails outside263 // of the "+1" timezone264 Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));265 Date expectedDateDate = dateFormat.parse("2006-10-26T13:10:50+0200");266 Assert.assertEquals(actualDate, expectedDateDate);267 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "Multipart Testmail");268 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "text/plain; charset=utf-8");269 try {270 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),271 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("multipart_mail_1.xml",272 MailServer.class).getInputStream()))));273 } catch (IOException e) {274 Assert.fail(e.getMessage());275 }276 return null;277 }278 }).doAnswer(new Answer<Message>() {279 @Override280 public Message answer(InvocationOnMock invocation) throws Throwable {281 Message message = (Message) invocation.getArguments()[0];282 Assert.assertNotNull(message.getPayload());283 Assert.assertNull(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID));284 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "foo@mail.com");285 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com");286 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "");287 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "");288 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "foo@mail.com");289 // compare dates as Date rather than String otherwise this test fails outside290 // of the "+1" timezone291 Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));292 Date expectedDateDate = dateFormat.parse("2006-10-26T13:10:50+0200");293 Assert.assertEquals(actualDate, expectedDateDate);294 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "Multipart Testmail");295 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "text/html; charset=utf-8");296 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FILENAME), "index.html");297 try {298 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),299 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("multipart_mail_2.xml",300 MailServer.class).getInputStream()))));301 } catch (IOException e) {302 Assert.fail(e.getMessage());303 }304 return null;305 }306 }).when(endpointAdapterMock).handleMessage(any(Message.class));307 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));308 mailServer.deliver("foo@mail.com", "bar@mail.com",309 new ClassPathResource("multipart_mail.txt", MailServer.class).getInputStream());310 }311 @Test312 @SuppressWarnings("unchecked")313 public void testBinaryMessageSplitting() throws IOException {314 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");315 MailServer mailServer = new MailServer();316 mailServer.setEndpointAdapter(endpointAdapterMock);317 mailServer.setSplitMultipart(true);318 reset(endpointAdapterMock);319 doAnswer(new Answer<Message>() {320 @Override321 public Message answer(InvocationOnMock invocation) throws Throwable {322 Message message = (Message) invocation.getArguments()[0];323 Assert.assertNotNull(message.getPayload());324 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID), "<52A1988D.2060403@consol.de>");325 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "Foo <foo@mail.com>");326 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com");327 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "FooBar <foobar@mail.com>");328 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "");329 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "Foo <foo@mail.com>");330 // compare dates as Date rather than String otherwise this test fails outside331 // of the "+1" timezone332 Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));333 Date expectedDateDate = dateFormat.parse("2013-12-06T10:27:41+0100");334 Assert.assertEquals(actualDate, expectedDateDate);335 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "This is brand_logo.png");336 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "text/plain; charset=ISO-8859-15; format=flowed");337 try {338 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),339 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("binary_mail_1.xml",340 MailServer.class).getInputStream()))));341 } catch (IOException e) {342 Assert.fail(e.getMessage());343 }344 return null;345 }346 }).doAnswer(new Answer<Message>() {347 @Override348 public Message answer(InvocationOnMock invocation) throws Throwable {349 Message message = (Message) invocation.getArguments()[0];350 Assert.assertNotNull(message.getPayload());351 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID), "<52A1988D.2060403@consol.de>");352 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "Foo <foo@mail.com>");353 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com");354 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "FooBar <foobar@mail.com>");355 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "");356 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "Foo <foo@mail.com>");357 // compare dates as Date rather than String otherwise this test fails outside358 // of the "+1" timezone359 Date actualDate = dateFormat.parse((String)message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));360 Date expectedDateDate = dateFormat.parse("2013-12-06T10:27:41+0100");361 Assert.assertEquals(actualDate, expectedDateDate);362 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "This is brand_logo.png");363 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "image/png");364 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FILENAME), "brand_logo.png");365 try {366 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),367 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("binary_mail_2.xml",368 MailServer.class).getInputStream()))));369 } catch (IOException e) {370 Assert.fail(e.getMessage());371 }372 return null;373 }374 }).when(endpointAdapterMock).handleMessage(any(Message.class));375 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));376 mailServer.deliver("foo@mail.com", "bar@mail.com",377 new ClassPathResource("binary_mail.txt", MailServer.class).getInputStream());378 }379 @Test380 @SuppressWarnings("unchecked")381 public void testSimulateError() throws IOException {382 MailServer mailServer = new MailServer();383 mailServer.setEndpointAdapter(endpointAdapterMock);384 reset(endpointAdapterMock);385 doAnswer(new Answer<Message>() {386 @Override387 public Message answer(InvocationOnMock invocation) throws Throwable {388 Message message = (Message) invocation.getArguments()[0];389 Assert.assertNotNull(message.getPayload());390 Assert.assertNull(message.getHeader(CitrusMailMessageHeaders.MAIL_MESSAGE_ID));391 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_FROM), "foo@mail.com");392 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_TO), "bar@mail.com,copy@mail.com");393 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CC), "foobar@mail.com");394 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_BCC), "secret@mail.com");395 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_REPLY_TO), "reply@mail.com");396 Assert.assertNull(message.getHeader(CitrusMailMessageHeaders.MAIL_DATE));397 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_SUBJECT), "Testmail");398 Assert.assertEquals(message.getHeader(CitrusMailMessageHeaders.MAIL_CONTENT_TYPE), "text/plain");399 try {400 Assert.assertEquals(StringUtils.trimAllWhitespace(message.getPayload(String.class)),401 StringUtils.trimAllWhitespace(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("text_mail.xml",402 MailServer.class).getInputStream()))));403 } catch (IOException e) {404 Assert.fail(e.getMessage());405 }406 return new DefaultMessage(FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource("error-response.xml",407 MailServer.class).getInputStream())));408 }409 }).when(endpointAdapterMock).handleMessage(any(Message.class));410 Assert.assertTrue(mailServer.accept("foo@mail.com", "bar@mail.com"));411 try {412 mailServer.deliver("foo@mail.com", "bar@mail.com",413 new ClassPathResource("text_mail.txt", MailServer.class).getInputStream());414 throw new CitrusRuntimeException("Missing reject exception due to simulated error");415 } catch (RejectException e) {416 Assert.assertEquals(e.getCode(), 443);417 Assert.assertEquals(e.getErrorResponse(), "443 Failed!");418 }419 }420}...
Source:SendMail_IT.java
1/*2 * Copyright (C) 2016 Red Hat, Inc.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.syndesis.test.itest.mail;17import javax.sql.DataSource;18import java.io.IOException;19import java.time.Duration;20import com.consol.citrus.annotations.CitrusResource;21import com.consol.citrus.annotations.CitrusTest;22import com.consol.citrus.dsl.endpoint.CitrusEndpoints;23import com.consol.citrus.dsl.runner.TestRunner;24import com.consol.citrus.dsl.runner.TestRunnerBeforeTestSupport;25import com.consol.citrus.http.client.HttpClient;26import com.consol.citrus.mail.message.CitrusMailMessageHeaders;27import com.consol.citrus.mail.message.MailMessage;28import com.consol.citrus.mail.server.MailServer;29import com.consol.citrus.util.FileUtils;30import io.syndesis.test.SyndesisTestEnvironment;31import io.syndesis.test.container.integration.SyndesisIntegrationRuntimeContainer;32import io.syndesis.test.itest.SyndesisIntegrationTestSupport;33import org.junit.ClassRule;34import org.junit.Test;35import org.springframework.beans.factory.annotation.Autowired;36import org.springframework.context.annotation.Bean;37import org.springframework.context.annotation.Configuration;38import org.springframework.core.io.ClassPathResource;39import org.springframework.http.HttpStatus;40import org.springframework.test.context.ContextConfiguration;41import org.springframework.util.SocketUtils;42import org.testcontainers.Testcontainers;43import org.testcontainers.containers.GenericContainer;44/**45 * @author Christoph Deppisch46 */47@ContextConfiguration(classes = SendMail_IT.EndpointConfig.class)48public class SendMail_IT extends SyndesisIntegrationTestSupport {49 private static final int MAIL_SERVER_PORT = SocketUtils.findAvailableTcpPort();50 static {51 Testcontainers.exposeHostPorts(MAIL_SERVER_PORT);52 }53 @Autowired54 private HttpClient webHookClient;55 @Autowired56 private MailServer mailServer;57 @Autowired58 private DataSource sampleDb;59 /**60 * This integration provides a webhook that expects a POST request with some contact Json object as payload. The61 * incoming contact (first_name, company, mail) triggers a send mail activity with a welcome message.62 *63 * After the mail is sent a new task entry for that contact is added to the sample database.64 */65 @ClassRule66 public static SyndesisIntegrationRuntimeContainer integrationContainer = new SyndesisIntegrationRuntimeContainer.Builder()67 .name("send-mail")68 .fromExport(SendMail_IT.class.getResource("SendMail-export"))69 .customize("$..configuredProperties.host", GenericContainer.INTERNAL_HOST_HOSTNAME)70 .customize("$..configuredProperties.port", MAIL_SERVER_PORT)71 .build()72 .withNetwork(getSyndesisDb().getNetwork())73 .withExposedPorts(SyndesisTestEnvironment.getServerPort());74 @Test75 @CitrusTest76 public void testSendMail(@CitrusResource TestRunner runner) throws IOException {77 runner.variable("first_name", "John");78 runner.variable("company", "Red Hat");79 runner.variable("email", "john@syndesis.org");80 runner.http(builder -> builder.client(webHookClient)81 .send()82 .post()83 .fork(true)84 .payload(getWebhookPayload()));85 String mailBody = FileUtils.readToString(new ClassPathResource("mail.txt", SendMail_IT.class));86 runner.receive(builder -> builder.endpoint(mailServer)87 .message(MailMessage.request()88 .from("people-team@syndesis.org")89 .to("${email}")90 .cc("")91 .bcc("")92 .subject("Welcome!")93 .body(mailBody, "text/plain; charset=UTF-8")));94 runner.send(builder -> builder.endpoint(mailServer)95 .message(MailMessage.response(250, "OK")));96 runner.http(builder -> builder.client(webHookClient)97 .receive()98 .response(HttpStatus.NO_CONTENT));99 verifyRecordsInDb(runner, 1, "New hire for ${first_name} from ${company}");100 }101 @Test102 @CitrusTest103 public void testSendMailError(@CitrusResource TestRunner runner) {104 runner.variable("first_name", "Joanne");105 runner.variable("company", "Red Hat");106 runner.variable("email", "joanne@syndesis.org");107 runner.http(builder -> builder.client(webHookClient)108 .send()109 .post()110 .fork(true)111 .payload(getWebhookPayload()));112 runner.receive(builder -> builder.endpoint(mailServer)113 .header(CitrusMailMessageHeaders.MAIL_FROM, "people-team@syndesis.org")114 .header(CitrusMailMessageHeaders.MAIL_TO, "${email}")115 .header(CitrusMailMessageHeaders.MAIL_SUBJECT, "Welcome!"));116 runner.send(builder -> builder.endpoint(mailServer)117 .message(MailMessage.response(421, "Service not available, closing transmission channel")));118 runner.http(builder -> builder.client(webHookClient)119 .receive()120 .response(HttpStatus.INTERNAL_SERVER_ERROR));121 verifyRecordsInDb(runner, 0, "New hire for ${first_name} from ${company}");122 }123 private static String getWebhookPayload() {124 return "{\"first_name\":\"${first_name}\",\"company\":\"${company}\",\"mail\":\"${email}\"}";125 }126 private void verifyRecordsInDb(TestRunner runner, int numberOfRecords, String task) {127 runner.query(builder -> builder.dataSource(sampleDb)128 .statement("select count(*) as found_records from todo where task='" + task + "'")129 .validate("found_records", String.valueOf(numberOfRecords)));130 }131 @Configuration132 public static class EndpointConfig {133 @Bean134 public HttpClient webHookClient() {135 return CitrusEndpoints.http().client()136 .requestUrl(String.format("http://localhost:%s/webhook/test-webhook", integrationContainer.getServerPort()))137 .build();138 }139 @Bean140 public MailServer mailServer() {141 return CitrusEndpoints.mail().server()142 .timeout(Duration.ofSeconds(SyndesisTestEnvironment.getDefaultTimeout()).toMillis())143 .autoStart(true)144 .autoAccept(true)145 .port(MAIL_SERVER_PORT)146 .build();147 }148 @Bean149 public TestRunnerBeforeTestSupport beforeTest(DataSource sampleDb) {150 return new TestRunnerBeforeTestSupport() {151 @Override152 public void beforeTest(TestRunner runner) {153 runner.sql(builder -> builder.dataSource(sampleDb)154 .statement("delete from todo where task like 'New hire for%'"));155 }156 };157 }158 }159}...
Source:EmployeeMailTest.java
...89 .client(serviceUri)90 .send()91 .get()92 .message()93 .accept(MediaType.APPLICATION_XML));94 citrus.run(http()95 .client(serviceUri)96 .receive()97 .response(HttpStatus.OK)98 .message()99 .body("<employees>" +100 "<employee>" +101 "<age>${employee.age}</age>" +102 "<name>${employee.name}</name>" +103 "<email>${employee.email}</email>" +104 "</employee>" +105 "</employees>"));106 }107}...
accept
Using AI Code Generation
1package com.consol.citrus.mail.server;2import org.springframework.beans.factory.annotation.Autowired;3import org.springframework.beans.factory.annotation.Qualifier;4import org.springframework.context.annotation.Bean;5import org.springframework.context.annotation.Configuration;6import org.springframework.context.annotation.Import;7import org.springframework.integration.annotation.IntegrationComponentScan;8import org.springframework.integration.annotation.ServiceActivator;9import org.springframework.integration.annotation.Transformer;10import org.springframework.integration.channel.DirectChannel;11import org.springframework.integration.config.EnableIntegration;12import org.springframework.integration.mail.transformer.MailToStringTransformer;13import org.springframework.messaging.MessageChannel;14import com.consol.citrus.dsl.builder.ReceiveMessageBuilder;15import com.consol.citrus.dsl.builder.SendMessageBuilder;16import com.consol.citrus.dsl.builder.TestBuilder;17import com.consol.citrus.dsl.runner.TestRunner;18import com.consol.citrus.dsl.runner.TestRunnerSupport;19import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;20import com.consol.citrus.mail.message.CitrusMailMessage;21@Import({MailServerConfig.class})22public class MailServerTest extends TestNGCitrusTestDesigner {23 @Qualifier("mailServer")24 private MailServer mailServer;25 @Qualifier("mailChannel")26 private MessageChannel mailChannel;27 @Qualifier("mailToStringTransformer")28 private MailToStringTransformer mailToStringTransformer;29 public MessageChannel mailChannel() {30 return new DirectChannel();31 }32 public MailToStringTransformer mailToStringTransformer() {33 return new MailToStringTransformer();34 }35 public TestRunner getTestRunner() {36 return new TestRunnerSupport() {37 public void run(TestBuilder builder) {38 builder.echo("Mail Server Test");39 }40 };41 }42 public void configure() {43 getTestRunner().run(getTestBuilder());44 echo("Mail Server Test");45 variable("mailSubject", "Mail Subject");46 variable("mailBody", "Mail Body");47 variable("mailTo", "
accept
Using AI Code Generation
1package com.consol.citrus.mail.server;2import java.io.IOException;3import java.util.ArrayList;4import java.util.List;5import org.slf4j.Logger;6import org.slf4j.LoggerFactory;7import org.springframework.mail.MailMessage;8import org.springframework.mail.MailParseException;9import com.consol.citrus.exceptions.CitrusRuntimeException;10public class MailServer {11 private static Logger log = LoggerFactory.getLogger(MailServer.class);12 private final List<MailMessage> mailMessages = new ArrayList<MailMessage>();13 private int port = 25;14 private String host = "localhost";15 private long timeout = 10000L;16 private String socketFactory;17 private int socketFactoryPort = 465;18 private String socketFactoryClass = "javax.net.ssl.SSLSocketFactory";19 private boolean socketFactoryFallback = false;20 private boolean socketFactoryClassDefault = true;21 private String auth;22 private String starttls;23 private String username;24 private String password;25 private boolean debug = false;26 private String protocol = "smtp";27 private String transportProtocol = "smtp";28 private String mailTransportClass = "com.sun.mail.smtp.SMTPTransport";29 private String mailTransportProtocol = "smtp";30 private boolean mailTransportClassDefault = true;31 private String mailStoreClass = "com.sun.mail.imap.IMAPStore";32 private boolean mailStoreClassDefault = true;33 public void accept(MailMessage mailMessage) {34 mailMessages.add(mailMessage);35 }
accept
Using AI Code Generation
1package com.consol.citrus.mail.server;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import java.util.Properties;7import javax.mail.Address;8import javax.mail.Message;9import javax.mail.MessagingException;10import javax.mail.Session;11import javax.mail.internet.InternetAddress;12import javax.mail.internet.MimeMessage;13import org.apache.commons.io.FileUtils;14import org.slf4j.Logger;15import org.slf4j.LoggerFactory;16import org.springframework.util.CollectionUtils;17import org.springframework.util.StringUtils;18public class MailServer {19 private static Logger log = LoggerFactory.getLogger(MailServer.class);20 private static final int DEFAULT_PORT = 25;21 private static final String DEFAULT_HOST = "localhost";22 private static final int DEFAULT_TIMEOUT = 5000;23 private static final int DEFAULT_POLLING_INTERVAL = 1000;24 private static final int DEFAULT_POLLING_ATTEMPTS = 10;25 private static final int DEFAULT_POLLING_ATTEMPTS_DELAY = 1000;26 private static final String DEFAULT_CHARSET = "UTF-8";27 private int port = DEFAULT_PORT;28 private String host = DEFAULT_HOST;29 private int timeout = DEFAULT_TIMEOUT;30 private int pollingInterval = DEFAULT_POLLING_INTERVAL;31 private int pollingAttempts = DEFAULT_POLLING_ATTEMPTS;32 private int pollingDelay = DEFAULT_POLLING_ATTEMPTS_DELAY;33 private String charset = DEFAULT_CHARSET;34 private Session session;35 private MailServerMessageStore messageStore;36 private List<MailServerListener> listeners = new ArrayList<MailServerListener>();37 public void start() {38 session = Session.getInstance(new Properties());39 messageStore = new MailServerMessageStore();40 log.info("Starting mail server on port: " + port);41 }
accept
Using AI Code Generation
1public class 3 {2 public static void main(String[] args) {3 MailServer mailServer = MailServer.builder()4 .port(2525)5 .user("user")6 .password("password")7 .autoStart(true)8 .build();9 mailServer.accept(".*@citrusframework.org", m -> {10 System.out.println(m.getSubject());11 System.out.println(m.getFrom());12 System.out.println(m.getTo());13 System.out.println(m.getContent());14 });15 }16}17public class 4 {18 public static void main(String[] args) {19 MailServer mailServer = MailServer.builder()20 .port(2525)21 .user("user")22 .password("password")23 .autoStart(true)24 .build();25 mailServer.accept(".*@citrusframework.org", m -> {26 System.out.println(m.getSubject());27 System.out.println(m.getFrom());28 System.out.println(m.getTo());29 System.out.println(m.getContent());30 });31 mailServer.accept(".*@citrusframework.org", m -> {32 System.out.println(m.getSubject());33 System.out.println(m.getFrom());34 System.out.println(m.getTo());35 System.out.println(m.getContent());36 });37 }38}39public class 5 {40 public static void main(String[] args) {41 MailServer mailServer = MailServer.builder()42 .port(2525)43 .user("user")44 .password("password")45 .autoStart(true)46 .build();47 mailServer.accept(".*@citrusframework.org", m -> {48 System.out.println(m.getSubject());49 System.out.println(m.getFrom());50 System.out.println(m.getTo());51 System.out.println(m.getContent());52 });53 mailServer.accept(".*@citrusframework.org", m -> {54 System.out.println(m.getSubject());55 System.out.println(m.getFrom());56 System.out.println(m.getTo());57 System.out.println(m.getContent());58 });59 mailServer.accept(".*@citrusframework.org", m -> {60 System.out.println(m.getSubject());61 System.out.println(m.getFrom());62 System.out.println(m.getTo
accept
Using AI Code Generation
1package com.consol.citrus.mail.server;2import java.util.Properties;3import javax.mail.Session;4import com.consol.citrus.mail.message.MailMessage;5import com.consol.citrus.mail.message.MailMessageConverter;6import com.consol.citrus.mail.model.MailMessageModel;7import com.consol.citrus.mail.model.MailMessageModelBuilder;8import com.consol.citrus.mail.model.MailMessageModelConverter;9public class MailServerTest {10 public static void main(String[] args) throws Exception {11 MailServer mailServer = new MailServer();12 mailServer.setPort(2525);13 mailServer.start();14 Properties props = new Properties();15 props.put("mail.smtp.host", "localhost");16 props.put("mail.smtp.port", "2525");17 Session session = Session.getInstance(props, null);18 MailMessageModel messageModel = MailMessageModelBuilder.withMailMessage()19 .from("
accept
Using AI Code Generation
1package com.consol.citrus.mail.server;2import java.io.IOException;3import java.net.Socket;4import java.util.HashMap;5import java.util.Map;6import org.slf4j.Logger;7import org.slf4j.LoggerFactory;8import com.consol.citrus.mail.message.CitrusMailMessage;9import com.consol.citrus.mail.server.handler.MailHandler;10import com.consol.citrus.mail.server.handler.MailHandlerChain;11public class MailServer {12 private static Logger log = LoggerFactory.getLogger(MailServer.class);13 private int port = 25;14 private String host = "localhost";15 private Socket socket;16 private MailHandlerChain mailHandlerChain = new MailHandlerChain();17 private Map<String, CitrusMailMessage> messageStore = new HashMap<String, CitrusMailMessage>();18 public MailServer(int port, String host) {19 this.port = port;20 this.host = host;21 }22 public MailServer(int port) {23 this.port = port;24 }25 public MailServer(String host) {26 this.host = host;27 }28 public MailServer() {29 }30 public void start() throws IOException {31 socket = new Socket(host, port);32 log.info("Mail server started on port " + port);33 }34 public void stop() {35 if (socket != null) {36 try {37 socket.close();38 } catch (IOException e) {39 log.warn("Failed to close socket", e);40 }41 }42 }43 public void accept() throws IOException {44 mailHandlerChain.handleRequest(socket.getInputStream(), socket.getOutputStream());45 }
accept
Using AI Code Generation
1package com.consol.citrus.mail;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import org.springframework.core.io.ClassPathResource;7import org.springframework.mail.MailException;8import org.springframework.mail.javamail.JavaMailSender;9import org.springframework.mail.javamail.JavaMailSenderImpl;10import org.springframework.mail.javamail.MimeMessageHelper;11import org.testng.annotations.BeforeClass;12import org.testng.annotations.Test;13import com.consol.citrus.mail.message.MailMessage;14import com.consol.citrus.mail.server.MailServer;15import com.consol.citrus.message.MessageType;16import com.consol.citrus.testng.AbstractTestNGUnitTest;17import com.consol.citrus.validation.json.JsonTextMessageValidator;18import com.consol.citrus.validation.xml.XpathMessageValidator;19import com.consol.citrus.validation.xml.XmlSchemaValidationInterceptor;20import com.consol.citrus.xml.XsdSchemaRepository;21import com.icegreen.greenmail.util.GreenMail;22import com.icegreen.greenmail.util.GreenMailUtil;23public class MailServerTest extends AbstractTestNGUnitTest {24 private MailServer mailServer;25 private MailServer mailServer2;26 private MailServer mailServer3;27 private MailServer mailServer4;28 private MailServer mailServer5;29 private MailServer mailServer6;30 private MailServer mailServer7;31 private MailServer mailServer8;32 private MailServer mailServer9;33 private MailServer mailServer10;34 private MailServer mailServer11;35 private MailServer mailServer12;36 private MailServer mailServer13;37 private MailServer mailServer14;38 private MailServer mailServer15;39 private MailServer mailServer16;40 private MailServer mailServer17;41 private MailServer mailServer18;42 private MailServer mailServer19;43 private MailServer mailServer20;44 private MailServer mailServer21;45 private MailServer mailServer22;46 private MailServer mailServer23;47 private MailServer mailServer24;48 private MailServer mailServer25;49 private MailServer mailServer26;50 private MailServer mailServer27;51 private MailServer mailServer28;52 private MailServer mailServer29;53 private MailServer mailServer30;54 private MailServer mailServer31;55 private MailServer mailServer32;56 private MailServer mailServer33;
accept
Using AI Code Generation
1package com.consol.citrus.mail.server;2import java.util.List;3import java.util.Map;4import org.apache.commons.mail.util.MimeMessageParser;5import org.apache.james.mime4j.dom.Message;6import org.slf4j.Logger;7import org.slf4j.LoggerFactory;8import org.springframework.integration.mail.MailHeaders;9import org.springframework.integration.support.MessageBuilder;10import org.springframework.mail.MailException;11import org.springframework.mail.MailParseException;12import org.springframework.mail.MailSendException;13import org.springframework.mail.SimpleMailMessage;14import org.springframework.mail.javamail.JavaMailSender;15import org.springframework.mail.javamail.MimeMessagePreparator;16import org.springframework.mail.javamail.MimeMessagePreparatorHelper;17import org.springframework.mail.javamail.MimeMessageUtils;18import org.springframework.messaging.MessageChannel;19import org.springframework.messaging.MessagingException;20import org.springframework.util.Assert;21import org.springframework.util.StringUtils;22public class MailServer {23 private static final Logger LOG = LoggerFactory.getLogger(MailServer.class);24 private MessageChannel mailChannel;25 private JavaMailSender mailSender;26 private String defaultFrom;27 private String defaultSubject;28 private String defaultText;29 private String defaultCharset = "UTF-8";30 public MailServer() {31 super();32 }33 public MailServer(MessageChannel mailChannel) {34 this.mailChannel = mailChannel;35 }36 public MailServer(JavaMailSender mailSender) {37 this.mailSender = mailSender;38 }39 public MailServer(MessageChannel mailChannel, JavaMailSender mailSender) {40 this.mailChannel = mailChannel;41 this.mailSender = mailSender;42 }43 public void send(String to) throws MailException {44 send(to, defaultSubject, defaultText);45 }
accept
Using AI Code Generation
1public class MailServerTest {2 public void test() {3 MailServer mailServer = new MailServer();4 mailServer.setPort(2525);5 mailServer.start();6 mailServer.accept(new MailServerCallback() {7 public void accept(MailServer mailServer) {8 }9 });10 mailServer.stop();11 }12}13public class MailServerTest {14 public void test() {15 MailServer mailServer = new MailServer();16 mailServer.setPort(2525);17 mailServer.start();18 mailServer.accept(new MailServerCallback() {19 public void accept(MailServer mailServer) {20 }21 });22 mailServer.stop();23 }24}25public class MailServerTest {26 public void test() {27 MailServer mailServer = new MailServer();28 mailServer.setPort(2525);29 mailServer.start();30 mailServer.accept(new MailServerCallback() {31 public void accept(MailServer mailServer) {32 }33 });34 mailServer.stop();35 }36}37public class MailServerTest {38 public void test() {39 MailServer mailServer = new MailServer();40 mailServer.setPort(2525);41 mailServer.start();42 mailServer.accept(new MailServerCallback() {43 public void accept(MailServer mailServer) {44 }45 });46 mailServer.stop();47 }48}49public class MailServerTest {50 public void test() {51 MailServer mailServer = new MailServer();52 mailServer.setPort(2525);53 mailServer.start();54 mailServer.accept(new MailServerCallback() {
accept
Using AI Code Generation
1package com.consol.citrus.mail.server;2import org.springframework.mail.javamail.JavaMailSenderImpl;3import org.springframework.mail.javamail.MimeMessageHelper;4import javax.mail.MessagingException;5import javax.mail.internet.MimeMessage;6import java.io.IOException;7public class MailServerExample {8 public static void main(String[] args) throws IOException, MessagingException {9 JavaMailSenderImpl mailSender = new JavaMailSenderImpl();10 mailSender.setHost("localhost");11 mailSender.setPort(2025);12 MimeMessage mailMessage = mailSender.createMimeMessage();13 MimeMessageHelper helper = new MimeMessageHelper(mailMessage, true);14 helper.setTo("
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!