Commits

Richard Warburton committed d6f7e37

finish off mentor notification STUFF (yeah this is a good commit message)

  • Participants
  • Parent commits e8baec3

Comments (0)

Files changed (5)

File betterrev/app/update/mentor_notification/MentorNotificationActor.java

 
 import static models.PullReviewEventType.MENTOR_NOTIFIED;
 
+import java.util.Iterator;
 import java.util.Properties;
+import java.util.Set;
 
 import javax.mail.Message;
+import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.Transport;
+import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 
+import models.Mentor;
 import models.PullReviewEvent;
 import models.PullReviewEventType;
+import play.Configuration;
+import play.Play;
 import play.api.templates.Html;
 import update.BetterrevActor;
 import views.html.emails.PullReviewCreated;
  * MentorNotificationActor
  */
 public class MentorNotificationActor extends BetterrevActor {
+    
+    private static final String FROM_EMAIL = " betterrev@googlegroups.com";
+    private static final String HTML_MIME_TYPE = "text/html";
+    
+    private Session session;
+
+    public MentorNotificationActor() {
+        session = makeSession();
+    }
 
     @Override
     public void onReceive(Object message) throws Exception {
         if (request.pullReviewEventType != PullReviewEventType.PULL_REVIEW_GENERATED) {
             return;
         }
-
-        Properties props = new Properties();
-        props.setProperty("mail.smtp.host", "localhost");
-        props.setProperty("mail.smtp.port", "3025");
-        Session session = Session.getDefaultInstance(props);
-        
-        String fromEmail = "user@bitbucket.org";
-        String toEmail = "user@localhost";
         
+                
+        sendEmailMessage(request);
+        publishResponse(request);
+    }
+
+    private void publishResponse(PullReviewEvent request) {
+        PullReviewEvent response = new PullReviewEvent(MENTOR_NOTIFIED, request.pullReview);
+        eventStream().publish(response);
+    }
+
+    private void sendEmailMessage(PullReviewEvent request) throws MessagingException, AddressException {
+        MimeMessage emailMessage = createEmail(request);
+        renderEmailContent(request, emailMessage);
+        Transport.send(emailMessage);
+    }
+
+    private MimeMessage createEmail(PullReviewEvent request) throws MessagingException, AddressException {
         MimeMessage emailMessage = new MimeMessage(session);
-        emailMessage.setFrom(new InternetAddress(fromEmail));
-        emailMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
-        
+        emailMessage.setFrom(new InternetAddress(FROM_EMAIL));
+        setRecipients(request.pullReview.mentors, emailMessage);
+        return emailMessage;
+    }
+
+    private void setRecipients(Set<Mentor> mentors, MimeMessage emailMessage) throws MessagingException, AddressException {
+        InternetAddress[] addresses = new InternetAddress[mentors.size()];
+        Iterator<Mentor> it = mentors.iterator();
+        for (int i = 0; it.hasNext(); i++) {
+            Mentor mentor = it.next();
+            addresses[i] = new InternetAddress(mentor.email);
+        }
+        emailMessage.setRecipients(Message.RecipientType.TO, addresses);
+    }
+
+    private void renderEmailContent(PullReviewEvent request, MimeMessage emailMessage) throws MessagingException {
         emailMessage.setSubject("RFR: " + request.pullReview.name);
-        
         Html body = PullReviewCreated.render(request.pullReview);
-        emailMessage.setText(body.toString());
-        
-        Transport.send(emailMessage);
-        
-        PullReviewEvent response = new PullReviewEvent(MENTOR_NOTIFIED, request.pullReview);
-        eventStream().publish(response);
+        emailMessage.setContent(body.toString(), HTML_MIME_TYPE);
+    }
+
+    private Session makeSession() {
+        Configuration conf = Play.application().configuration();
+        Properties props = new Properties();
+        loadPropertyFromConfiguration(conf, props, "mail.smtp.host");
+        loadPropertyFromConfiguration(conf, props, "mail.smtp.port");
+        return Session.getDefaultInstance(props);
+    }
+
+    private void loadPropertyFromConfiguration(Configuration from, Properties to, String propertyName) {
+        String value = from.getString(propertyName);
+        to.setProperty(propertyName, value);
     }
     
 }

File betterrev/app/views/emails/PullReviewCreated.scala.html

 @(review: PullReview)
 
+<html>
+<body>
 Hi,
 
 There is a new pull review that requires your attention. The details are as follows:
 Kind regards,
 
     @review.requester.name
+</body>
+</html>

File betterrev/conf/application.conf

 
 owner=AdoptOpenJDK
 project=better-test-repo
+
+mail.smtp.host = localhost
+mail.smtp.port = 3025
+

File betterrev/test/models/PullReviewTest.java

 package models;
 
-import org.joda.time.DateTime;
-import org.junit.Test;
-
-import java.util.Set;
-
+import static models.Mentor.MentorType.INDIVIDUAL;
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.core.IsEqual.equalTo;
 import static org.hamcrest.core.IsNot.not;
 import static org.hamcrest.core.IsNull.nullValue;
 import static org.junit.Assert.assertThat;
 
+import java.util.HashSet;
+import java.util.Set;
+
+import org.joda.time.DateTime;
+import org.junit.Test;
+
 /**
  * PullReview entity basic persistence tests.
  */
                 TEST_PULL_REVIEW_CREATED_ON, TEST_PULL_REVIEW_UPDATED_ON, TEST_BRANCH_NAME);
 
         pullReview.tags.add(TagTest.createTestInstance());
+        addPullReviewEvent(pullReview);
+        addMentor(pullReview);
+        return pullReview;
+    }
+
+    private static void addPullReviewEvent(PullReview pullReview) {
         PullReviewEvent event = new PullReviewEvent(PullReviewEventType.PULL_REVIEW_GENERATED);
         event.pullReview = pullReview;
         pullReview.pullReviewEvents.add(event);
+    }
 
-        return pullReview;
+    private static void addMentor(PullReview pullReview) {
+        pullReview.mentors = new HashSet<>();
+        Mentor testMentor = new Mentor("Helpful Openjdk Person", "helpful@example.com", INDIVIDUAL);
+        pullReview.mentors.add(testMentor);
     }
 
     public static PullReview createTestInstance(State state) {

File betterrev/test/update/mentor_notification/MentorNotifiedEventTest.java

 import update.GreenMailRule;
 import update.pullrequest.AbstractPullRequestImporterTest;
 
+import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
+import java.io.IOException;
 import java.util.List;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
         Await.result(response, Duration.Inf());
 
         List<MimeMessage> receivedMessages = mailRule.getReceivedMessages();
+        checkEmail(review, receivedMessages);
+    }
 
+    private void checkEmail(PullReview review, List<MimeMessage> receivedMessages) throws MessagingException, IOException {
         assertEquals(1, receivedMessages.size());
         MimeMessage message = receivedMessages.get(0);
-        
         assertEquals("RFR: " + review.name, message.getSubject());
         
+        checkEmailContent(review, message);
+    }
+
+    private void checkEmailContent(PullReview review, MimeMessage message) throws MessagingException, IOException {
         assertTrue(message.getContentType().contains("text"));
         String body = (String) message.getContent();
         assertTrue(body.contains(review.description));
         assertEquals(PullReviewEventType.TERMINATED, reply.pullReviewEventType);
 
         actorRule.expectNoMsgWithinTimeout(PullReviewEvent.class, 200);
-
     } 
 
     private Future<PullReviewEvent> messageNotificationActor(PullReviewEvent pullReviewEvent) {