Skip to content

Commit

Permalink
Merge pull request #29 from ArcBees/rl_replyTo_mail
Browse files Browse the repository at this point in the history
Added reply-to address for appengine mail #26
  • Loading branch information
Elrhino committed Jun 25, 2015
2 parents 957dc09 + 6331821 commit 33e9521
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down Expand Up @@ -28,4 +28,6 @@ public interface Email extends Serializable {
String getSubject();

String getBody();

String getReplyToAddress();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down Expand Up @@ -34,6 +34,8 @@ public MailBuilderFromAddress fromAddress(String from) {
public static class MailBuilderFromAddress {
private final String fromAddress;
private final MailBuilderTo mailBuilderTo;

private String replyToAddress;
private String subject;
private String body;
private String fromPersonal;
Expand All @@ -44,6 +46,7 @@ private MailBuilderFromAddress(String fromAddress,
this.subject = "";
this.fromAddress = fromAddress;
this.fromPersonal = DEFAULT_PERSONAL;
this.replyToAddress = "";
this.mailBuilderTo = mailBuilderTo;
}

Expand All @@ -65,8 +68,14 @@ public MailBuilderFromAddress fromPersonal(String fromPersonal) {
return this;
}

public MailBuilderFromAddress replyToAddress(String replyToAddress) {
this.replyToAddress = replyToAddress;

return this;
}

public Email build() {
return new EmailImpl(mailBuilderTo.to, fromAddress, fromPersonal, subject, body);
return new EmailImpl(this);
}
}

Expand All @@ -76,13 +85,15 @@ private static class EmailImpl implements Email {
private final String fromAddress;
private final String fromPersonal;
private final String body;

private EmailImpl(String to, String fromAddress, String fromPersonal, String subject, String body) {
this.to = to;
this.fromAddress = fromAddress;
this.subject = subject;
this.fromPersonal = fromPersonal;
this.body = body;
private final String replyToAddress;

private EmailImpl(MailBuilderFromAddress builder) {
this.to = builder.mailBuilderTo.to;
this.fromAddress = builder.fromAddress;
this.subject = builder.subject;
this.fromPersonal = builder.fromPersonal;
this.body = builder.body;
this.replyToAddress = builder.replyToAddress;
}

@Override
Expand All @@ -109,6 +120,11 @@ public String getSubject() {
public String getBody() {
return body;
}

@Override
public String getReplyToAddress() {
return replyToAddress;
}
}

public static MailBuilderTo to(String to) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand All @@ -21,13 +21,15 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.google.appengine.api.taskqueue.DeferredTask;
import com.google.common.base.Strings;

public class SendEmailTask implements DeferredTask {
private static final String CONTENT_TYPE = "text/html";
Expand Down Expand Up @@ -58,6 +60,12 @@ public void run() {
message.setSubject(email.getSubject());
message.setContent(email.getBody(), CONTENT_TYPE);

if (Strings.isNullOrEmpty(email.getReplyToAddress())) {
message.setReplyTo(new Address[] {
new InternetAddress(email.getReplyToAddress())
});
}

transport.send(message);
} catch (MessagingException e) {
logger.log(Level.WARNING, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2013 ArcBees Inc.
*
* 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 com.arcbees.appengine.mail;

import com.google.appengine.api.taskqueue.DeferredTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2013 ArcBees Inc.
*
* 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 com.arcbees.appengine.mail;

import com.google.appengine.api.taskqueue.DeferredTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2013 ArcBees Inc.
*
* 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 com.arcbees.appengine.mail;

import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2013 ArcBees Inc.
*
* 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 com.arcbees.appengine.mail;

import javax.mail.Message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
Expand All @@ -21,71 +21,116 @@
import static org.junit.Assert.assertEquals;

public class MailBuilderFromTest {
public static final String TO = "to";
public static final String FROM_ADDRESS = "fromAddress";
public static final String FROM_PERSONAL = "fromPersonal";
public static final String BODY = "body";
public static final String SUBJECT = "subject";
public static final String REPLY_TO_ADDRESS = "replyToAddress";

@Test
public void build_allFieldsFilled_emailIsWellBuilt() {
//given
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to("to").fromAddress("fromAddress").fromPersonal
("fromPersonal").body("body").subject("subject");
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to(TO)
.fromAddress(FROM_ADDRESS)
.fromPersonal(FROM_PERSONAL)
.body(BODY)
.subject(SUBJECT)
.replyToAddress(REPLY_TO_ADDRESS);

//when
Email email = mailBuilderFromAddress.build();

//then
assertEquals("to", email.getTo());
assertEquals("fromAddress", email.getFromAddress());
assertEquals("fromPersonal", email.getFromPersonal());
assertEquals("body", email.getBody());
assertEquals("subject", email.getSubject());
assertEquals(TO, email.getTo());
assertEquals(FROM_ADDRESS, email.getFromAddress());
assertEquals(FROM_PERSONAL, email.getFromPersonal());
assertEquals(BODY, email.getBody());
assertEquals(SUBJECT, email.getSubject());
assertEquals(REPLY_TO_ADDRESS, email.getReplyToAddress());
}

@Test
public void build_missingBody_emailIsWellBuilt() {
//given
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to("to").fromAddress("fromAddress").fromPersonal
("fromPersonal").subject("subject");
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to(TO)
.fromAddress(FROM_ADDRESS)
.fromPersonal(FROM_PERSONAL)
.subject(SUBJECT)
.replyToAddress(REPLY_TO_ADDRESS);

//when
Email email = mailBuilderFromAddress.build();

//then
assertEquals("to", email.getTo());
assertEquals("fromAddress", email.getFromAddress());
assertEquals("fromPersonal", email.getFromPersonal());
assertEquals(TO, email.getTo());
assertEquals(FROM_ADDRESS, email.getFromAddress());
assertEquals(FROM_PERSONAL, email.getFromPersonal());
assertEquals("", email.getBody());
assertEquals("subject", email.getSubject());
assertEquals(SUBJECT, email.getSubject());
assertEquals(REPLY_TO_ADDRESS, email.getReplyToAddress());
}

@Test
public void build_missingSubject_emailIsWellBuilt() {
//given
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to("to").fromAddress("fromAddress").fromPersonal
("fromPersonal").body("body");
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to(TO)
.fromAddress(FROM_ADDRESS)
.fromPersonal(FROM_PERSONAL)
.body(BODY)
.replyToAddress(REPLY_TO_ADDRESS);

//when
Email email = mailBuilderFromAddress.build();

//then
assertEquals("to", email.getTo());
assertEquals("fromAddress", email.getFromAddress());
assertEquals("fromPersonal", email.getFromPersonal());
assertEquals("body", email.getBody());
assertEquals(TO, email.getTo());
assertEquals(FROM_ADDRESS, email.getFromAddress());
assertEquals(FROM_PERSONAL, email.getFromPersonal());
assertEquals(BODY, email.getBody());
assertEquals("", email.getSubject());
assertEquals(REPLY_TO_ADDRESS, email.getReplyToAddress());
}

@Test
public void build_missingPersonal_emailIsWellBuilt() {
//given
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to("to").fromAddress("fromAddress").body
("body").subject("subject");
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to(TO)
.fromAddress(FROM_ADDRESS)
.body(BODY)
.subject(SUBJECT)
.replyToAddress(REPLY_TO_ADDRESS);

//when
Email email = mailBuilderFromAddress.build();

//then
assertEquals("to", email.getTo());
assertEquals("fromAddress", email.getFromAddress());
assertEquals(TO, email.getTo());
assertEquals(FROM_ADDRESS, email.getFromAddress());
assertEquals(EmailBuilder.DEFAULT_PERSONAL, email.getFromPersonal());
assertEquals("body", email.getBody());
assertEquals("subject", email.getSubject());
assertEquals(BODY, email.getBody());
assertEquals(SUBJECT, email.getSubject());
assertEquals(REPLY_TO_ADDRESS, email.getReplyToAddress());
}

@Test
public void build_missingReplyToAddress_emailIsWellBuilt() {
//given
EmailBuilder.MailBuilderFromAddress mailBuilderFromAddress = EmailBuilder.to(TO)
.fromAddress(FROM_ADDRESS)
.fromPersonal(FROM_PERSONAL)
.body(BODY)
.subject(SUBJECT);

//when
Email email = mailBuilderFromAddress.build();

//then
assertEquals(TO, email.getTo());
assertEquals(FROM_ADDRESS, email.getFromAddress());
assertEquals(FROM_PERSONAL, email.getFromPersonal());
assertEquals(BODY, email.getBody());
assertEquals(SUBJECT, email.getSubject());
assertEquals("", email.getReplyToAddress());
}
}
Loading

0 comments on commit 33e9521

Please sign in to comment.