INTEXT-136: Make based on Spring Cloud AWS
JIRA: https://jira.spring.io/browse/INTEXT-136 * Add `spring-cloud-aws` dependencies * Remove all SES stuff, since Spring Cloud AWS provides implementations for `org.springframework.mail.MailSender`
This commit is contained in:
@@ -1,220 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.aws.ses;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.integration.mail.MailHeaders.BCC;
|
||||
import static org.springframework.integration.mail.MailHeaders.CC;
|
||||
import static org.springframework.integration.mail.MailHeaders.FROM;
|
||||
import static org.springframework.integration.mail.MailHeaders.SUBJECT;
|
||||
import static org.springframework.integration.mail.MailHeaders.TO;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
|
||||
|
||||
/**
|
||||
* The test class for {@link AmazonSESMessageHandler}
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Rob Harrop
|
||||
*
|
||||
* @since 0.5
|
||||
*
|
||||
*/
|
||||
public class AmazonSESMessageHandlerTests {
|
||||
|
||||
private static final List<SimpleMailMessage> messages = new ArrayList<SimpleMailMessage>();
|
||||
private static final List<MimeMessage> mimeMessages = new ArrayList<MimeMessage>();
|
||||
private static AmazonSESMessageHandler handler;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupAmazonSESMailSender() {
|
||||
JavaMailSender sender = mock(JavaMailSender.class);
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
messages.add((SimpleMailMessage)args[0]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(SimpleMailMessage.class));
|
||||
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
messages.addAll(Arrays.asList((SimpleMailMessage[])args));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(SimpleMailMessage[].class));
|
||||
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
mimeMessages.addAll(Arrays.asList((MimeMessage[])args));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(MimeMessage[].class));
|
||||
|
||||
doAnswer(new Answer<Void>() {
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
Object[] args = invocation.getArguments();
|
||||
if(args != null) {
|
||||
mimeMessages.add((MimeMessage)args[0]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}).when(sender).send(any(MimeMessage.class));
|
||||
handler = new AmazonSESMessageHandler(sender);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
messages.clear();
|
||||
mimeMessages.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
*Test case for sending a message with subject line of unexpected type
|
||||
*{@link AmazonSESSimpleMailMessage}
|
||||
*
|
||||
*/
|
||||
@Test(expected=MessageHandlingException.class)
|
||||
public void withIncorrectSubjectClass() {
|
||||
Message<?> testMessage = MessageBuilder
|
||||
.withPayload("Test")
|
||||
.setHeader(SUBJECT, Arrays.asList("Some Header"))
|
||||
.build();
|
||||
handler.handleMessage(testMessage);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test case where the message text used of incorrect type
|
||||
*
|
||||
*/
|
||||
@Test(expected=MessageHandlingException.class)
|
||||
public void withIncorrectMessageTextClass() {
|
||||
Message<?> testMessage = MessageBuilder
|
||||
.withPayload(Arrays.asList("Content"))
|
||||
.build();
|
||||
handler.handleMessage(testMessage);
|
||||
}
|
||||
|
||||
//Test case to check if from id exists or not cant work now as MailSendingMessageHandler
|
||||
//doesn't check if from is present or not
|
||||
|
||||
/**
|
||||
* With all the details
|
||||
*/
|
||||
@Test
|
||||
public void withAllTheDetails() {
|
||||
clear();
|
||||
Message<?> testMessage = MessageBuilder
|
||||
.withPayload("Test Content")
|
||||
.setHeader(TO, "to@to.com")
|
||||
.setHeader(BCC, "bcc@bcc.com")
|
||||
.setHeader(CC, "cc@cc.com")
|
||||
.setHeader(SUBJECT, "Test Subject")
|
||||
.setHeader(FROM, "from@from.com")
|
||||
.build();
|
||||
handler.handleMessage(testMessage);
|
||||
SimpleMailMessage message = messages.get(0);
|
||||
String[] to = message.getTo();
|
||||
Assert.assertNotNull(to);
|
||||
Assert.assertEquals(1,to.length);
|
||||
Assert.assertEquals("to@to.com", to[0]);
|
||||
|
||||
String[] cc = message.getCc();
|
||||
Assert.assertNotNull(cc);
|
||||
Assert.assertEquals(1,cc.length);
|
||||
Assert.assertEquals("cc@cc.com", cc[0]);
|
||||
|
||||
String[] bcc = message.getBcc();
|
||||
Assert.assertNotNull(bcc);
|
||||
Assert.assertEquals(1,bcc.length);
|
||||
Assert.assertEquals("bcc@bcc.com", bcc[0]);
|
||||
|
||||
Assert.assertEquals("from@from.com",message.getFrom());
|
||||
Assert.assertEquals("Test Subject",message.getSubject());
|
||||
Assert.assertEquals("Test Content", message.getText());
|
||||
}
|
||||
|
||||
//Might need to uncomment this test later when we start allowing null in TO email id of
|
||||
//spring-int-mail
|
||||
// /**
|
||||
// *
|
||||
// */
|
||||
// @Test
|
||||
// public void withAllMultipleToBccAndCC() {
|
||||
// clear();
|
||||
// Message<?> testMessage = MessageBuilder
|
||||
// .withPayload("Test Content")
|
||||
// .setHeader(TO,
|
||||
// Arrays.asList("to1@to.com","to2@to.com"))
|
||||
// .setHeader(BCC,
|
||||
// Arrays.asList("bcc1@bcc.com","bcc2@bcc.com"))
|
||||
// .setHeader(CC,
|
||||
// Arrays.asList("cc1@cc.com","cc2@cc.com"))
|
||||
// .setHeader(SUBJECT, "Test Subject")
|
||||
// .setHeader(FROM, "from@from.com")
|
||||
// .build();
|
||||
// handler.handleMessage(testMessage);
|
||||
// SimpleMailMessage message = messages.get(0);
|
||||
// String[] to = message.getTo();
|
||||
// Assert.assertNotNull(to);
|
||||
// Assert.assertEquals(2,to.length);
|
||||
// Assert.assertEquals("to1@to.com", to[0]);
|
||||
// Assert.assertEquals("to2@to.com", to[1]);
|
||||
//
|
||||
// String[] cc = message.getCc();
|
||||
// Assert.assertNotNull(cc);
|
||||
// Assert.assertEquals(2,cc.length);
|
||||
// Assert.assertEquals("cc1@cc.com", cc[0]);
|
||||
// Assert.assertEquals("cc2@cc.com", cc[1]);
|
||||
//
|
||||
// String[] bcc = message.getBcc();
|
||||
// Assert.assertEquals(2,bcc.length);
|
||||
// Assert.assertEquals("bcc1@bcc.com", bcc[0]);
|
||||
// Assert.assertEquals("bcc2@bcc.com", bcc[1]);
|
||||
//
|
||||
// Assert.assertEquals("from@from.com",message.getFrom());
|
||||
//
|
||||
// Assert.assertEquals("Test Subject",message.getSubject());
|
||||
// Assert.assertEquals("Test Content", message.getText());
|
||||
// }
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.aws.ses.config.xml;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
|
||||
import com.amazonaws.services.simpleemail.AWSJavaMailTransport;
|
||||
|
||||
/**
|
||||
* The test class for the AmazonSESOutboundAdapterParser
|
||||
*
|
||||
* @author Amol Nayak
|
||||
* @author Rob Harrop
|
||||
*
|
||||
* @since 0.5
|
||||
*
|
||||
*/
|
||||
public class AmazonSESOutboundAdapterParserTests {
|
||||
|
||||
|
||||
/**
|
||||
* Tests the creation of context with a valid definition by specifying the credentials
|
||||
* in a properties file
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void propFileValidOutboundAdapter() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:ses-propfile-valid-test.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("validDefinition",EventDrivenConsumer.class);
|
||||
assertValidDefinition(consumer);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the creation of context with a valid definition by specifying the credentials
|
||||
* as individual attributes of the xml
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void propsValidOutboundAdapter() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:ses-props-valid-test.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("validDefinition",EventDrivenConsumer.class);
|
||||
assertValidDefinition(consumer);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the creation of context with a valid definition by specifying the credentials
|
||||
* as individual attributes of the xml
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void refValidOutboundAdapter() {
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:ses-cred-ref-valid-test.xml");
|
||||
EventDrivenConsumer consumer = ctx.getBean("validDefinition",EventDrivenConsumer.class);
|
||||
assertValidDefinition(consumer);
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
private void assertValidDefinition(EventDrivenConsumer consumer) {
|
||||
Assert.assertNotNull("Expected a non null EventDrivenConsumer", consumer);
|
||||
MessageHandler handler = TestUtils.getPropertyValue(consumer, "handler", MessageHandler.class);
|
||||
Assert.assertNotNull("Expected a non null messagehandler", handler);
|
||||
|
||||
Properties properties = TestUtils.getPropertyValue(handler, "mailSender.javaMailSender.javaMailProperties", Properties.class);
|
||||
Assert.assertNotNull("Expected a non null instance of credentials", properties);
|
||||
Assert.assertEquals("dummy", properties.getProperty(AWSJavaMailTransport.AWS_ACCESS_KEY_PROPERTY));
|
||||
Assert.assertEquals("dummy", properties.getProperty(AWSJavaMailTransport.AWS_SECRET_KEY_PROPERTY));
|
||||
}
|
||||
|
||||
/**
|
||||
* A Test case that tests by specifying both the aws credentials properties file
|
||||
* and the individual properties to set the credentials
|
||||
*/
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void invalidDefinitionWithBothPropsAndPropFile() {
|
||||
new ClassPathXmlApplicationContext("classpath:ses-both-awscred-props-property.xml");
|
||||
}
|
||||
}
|
||||
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.aws.ses.core;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.aws.core.PropertiesAWSCredentials;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* The test class for the {@link DefaultAmazonSESMailSender} class.
|
||||
*
|
||||
* NOTE: You will have to modify the to and from email's yourselves
|
||||
* as you can send to verified emails only as part of the free tier
|
||||
* in which you may be running the test.
|
||||
* To run this test you need to have your AWSAccess key and Secret key in the
|
||||
* file awscredentials.properties in the classpath. This file is not present in the
|
||||
* repository and you need to add one yourselves to src/test/resources folder and have
|
||||
* two properties accessKey and secretKey in it containing the access and the secret key
|
||||
*
|
||||
* @author Amol Nayak
|
||||
*
|
||||
* @since 0.5
|
||||
*
|
||||
*/
|
||||
@Ignore
|
||||
public class DefaultAmazonSESMailSenderAWSTests {
|
||||
|
||||
private static final String TO_EMAIL_ID = "amolnayak311@gmail.com";
|
||||
private static DefaultAmazonSESMailSender sender;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static final void setupSender() throws Exception {
|
||||
PropertiesAWSCredentials credentials =
|
||||
new PropertiesAWSCredentials("classpath:awscredentials.properties");
|
||||
credentials.afterPropertiesSet();
|
||||
sender = new DefaultAmazonSESMailSender(credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a mail using {@link AmazonSESSimpleMailMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendSimpleMailMessage() {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(TO_EMAIL_ID);
|
||||
message.setText("Some Test body content");
|
||||
message.setSubject("Test subject message");
|
||||
message.setTo(new String[]{TO_EMAIL_ID});
|
||||
sender.send(message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a mail using {@link AmazonSESSimpleMailMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendSimpleMailMessageArray() {
|
||||
SimpleMailMessage[] messages = new SimpleMailMessage[2];
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setFrom(TO_EMAIL_ID);
|
||||
message.setText("Some Test body content one");
|
||||
message.setSubject("Test subject message one");
|
||||
message.setTo(new String[]{TO_EMAIL_ID});
|
||||
messages[0] = message;
|
||||
message = new SimpleMailMessage();
|
||||
message.setFrom(TO_EMAIL_ID);
|
||||
message.setText("Some Test body content two");
|
||||
message.setSubject("Test subject message two");
|
||||
message.setTo(new String[]{TO_EMAIL_ID});
|
||||
messages[1] = message;
|
||||
sender.send(messages);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The test case for sending a {@link MimeMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendMimeMessage() throws Exception {
|
||||
MimeMessageHelper helper = new MimeMessageHelper(sender.createMimeMessage());
|
||||
helper.setText("Some HTML Text", true);
|
||||
helper.setTo(TO_EMAIL_ID);
|
||||
helper.setFrom(TO_EMAIL_ID);
|
||||
helper.setSubject("Some HTML Message's Subject Line");
|
||||
MimeMessage message = helper.getMimeMessage();
|
||||
sender.send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* The test case for sending a {@link MimeMessage}
|
||||
*/
|
||||
@Test
|
||||
public void sendMimeMessageArray() throws Exception {
|
||||
MimeMessage[] messages = new MimeMessage[2];
|
||||
MimeMessageHelper helper = new MimeMessageHelper(sender.createMimeMessage());
|
||||
helper.setText("Some HTML Text One", true);
|
||||
helper.setTo(TO_EMAIL_ID);
|
||||
helper.setFrom(TO_EMAIL_ID);
|
||||
helper.setSubject("Some HTML Message's Subject Line One");
|
||||
MimeMessage message = helper.getMimeMessage();
|
||||
messages[0] = message;
|
||||
helper = new MimeMessageHelper(sender.createMimeMessage());
|
||||
helper.setText("Some HTML Text Two", true);
|
||||
helper.setTo(TO_EMAIL_ID);
|
||||
helper.setFrom(TO_EMAIL_ID);
|
||||
helper.setSubject("Some HTML Message's Subject Line Two");
|
||||
message = helper.getMimeMessage();
|
||||
messages[1] = message;
|
||||
sender.send(messages);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user