Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from osbornjd/jay/MarkIIINotifications
Browse files Browse the repository at this point in the history
Update Notifications
  • Loading branch information
osbornjd committed Jul 1, 2020
2 parents 03d3063 + eae67de commit d1b8b47
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 96 deletions.
Expand Up @@ -442,7 +442,13 @@ public void listAllConnections() {
*/
public boolean isConnectionOpen(String connectionName) {
Connection connection = getConnection(connectionName);
return connection.getSession().isOpen();
if(connection == null)
return false;

if(!connection.getSession().isOpen())
return false;

return true;
}

/**
Expand Down
Expand Up @@ -32,20 +32,14 @@
*/
public class EmailUpdateHandler implements ICommandUpdateHandler {

// The email address to send the message to
private String emailAddress = "";

// The text that the message should contain
private String emailText = "";

// The subject of the message
private String emailSubject = "Commands API Message";

// The host smtp server for the email address
private String emailHost = "";

// The password for the provided email to be able to send to itself
private String emailPassword = "";
// Text file credential handler for sender's email creds
TxtFileConnectionAuthorizationHandler credHandler;

/**
* Default constructor
Expand All @@ -58,32 +52,33 @@ public EmailUpdateHandler() {
*/
@Override
public void postUpdate() throws IOException {
// Create some properties and setup the default gmail
// Create some properties and setup the default
// server properties
Properties properties = System.getProperties();
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true"); // TLS
// Setup mail server
properties.setProperty("mail.smtp.host", emailHost);

properties.setProperty("mail.smtp.host", credHandler.getHostname());
// Get the default Session object.
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailAddress, emailPassword);
return new PasswordAuthentication(credHandler.getUsername(),
String.valueOf(credHandler.getPassword()));
}
});
// Set session to debug just to explicit
// Set session to debug just to be explicit
session.setDebug(true);

try {
// Create a default MimeMessage object
MimeMessage message = new MimeMessage(session);

// Set the sender and recipient of the email
message.setFrom(new InternetAddress(emailAddress));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress));
message.setFrom(new InternetAddress(credHandler.getUsername()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(credHandler.getUsername()));

// Give the email a subject and message content
message.setSubject(emailSubject);
Expand All @@ -100,7 +95,7 @@ protected PasswordAuthentication getPasswordAuthentication() {

/**
* Setter for the email subject, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailSubject}
* {@link org.eclipse.ice.commands.EmailUpdateHandler#emailSubject}
*
* @param emailSubject
*/
Expand All @@ -109,33 +104,13 @@ public void setSubject(String emailSubject) {
}

/**
* Setter for the email address, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailAddress}
*
* @param option
*/
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

/**
* Setter for the email host for email authentication, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailHost}
*
* @param emailPassword
*/
public void setSmtpHost(String emailHost) {
this.emailHost = emailHost;
}

/**
* Setter for the email password for email authentication, see
* {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailPassword}
* Setter for the text file credential handler, see
* {@link org.eclipse.ice.commands.EmailUpdateHandler#credHandler}
*
* @param emailPassword
* @param credHandler
*/
public void setPassword(String emailPassword) {
this.emailPassword = emailPassword;
public void setCredHandler(TxtFileConnectionAuthorizationHandler credHandler){
this.credHandler = credHandler;
}

/**
Expand Down
Expand Up @@ -13,10 +13,7 @@

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import org.eclipse.ice.commands.Command;
import org.eclipse.ice.commands.CommandConfiguration;
Expand Down Expand Up @@ -126,8 +123,7 @@ private CommandConfiguration setupCommandConfiguration() {
CommandConfiguration commandConfig = new CommandConfiguration();
commandConfig.setNumProcs("1");
commandConfig.setInstallDirectory("");
commandConfig
.setWorkingDirectory(System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/");
commandConfig.setWorkingDirectory(System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/");
commandConfig.setOS(System.getProperty("os.name"));
commandConfig.setExecutable("./test_code_execution.sh");
// If it is windows, configure the test to run on windows
Expand Down Expand Up @@ -155,23 +151,10 @@ private EmailUpdateHandler setupEmailUpdateHandler() {
if (System.getProperty("os.name").toLowerCase().contains("win"))
credFile = "C:\\Users\\Administrator\\email-creds.txt";

String email = "";
String password = "";
String host = "";

File file = new File(credFile);
try (Scanner scanner = new Scanner(file)) {
email = scanner.next();
password = scanner.next();
host = scanner.next();
} catch (FileNotFoundException e) {
System.out.println("Email credential file not found, can't continue with test...");
e.printStackTrace();
}
TxtFileConnectionAuthorizationHandler auth = new TxtFileConnectionAuthorizationHandler();
auth.setOption(credFile);
EmailUpdateHandler handler = new EmailUpdateHandler();
handler.setEmailAddress(email);
handler.setPassword(password);
handler.setSmtpHost(host);
handler.setCredHandler(auth);

return handler;
}
Expand Down
Expand Up @@ -11,11 +11,14 @@
*******************************************************************************/
package org.eclipse.ice.tests.commands;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import org.eclipse.ice.commands.EmailUpdateHandler;
import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler;
import org.junit.Test;

/**
Expand All @@ -33,56 +36,73 @@ public class EmailHandlerTest {
* @throws IOException
*/
@Test
public void testEmailNotificationPostUpdate() throws IOException {
public void testEmailNotificationPostUpdate() {

// Get a text file with credentials
String credFile = "/tmp/email-creds.txt";
if (System.getProperty("os.name").toLowerCase().contains("win"))
credFile = "C:\\Users\\Administrator\\email-creds.txt";

String email = "";
String password = "";
String host = "";

File file = new File(credFile);
try (Scanner scanner = new Scanner(file)) {
email = scanner.next();
password = scanner.next();
host = scanner.next();
}
TxtFileConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler();
handler.setOption(credFile);

EmailUpdateHandler updater = new EmailUpdateHandler();
// Just send an email to itself
updater.setEmailAddress(email);
updater.setPassword(password);
updater.setSmtpHost(host);
updater.setCredHandler(handler);
updater.setMessage("This is a test updater");
updater.setSubject("This is a test subject");
updater.postUpdate();

// If no exception is thrown, it completed correctly
try {
updater.postUpdate();
} catch (IOException e) {
// If exception is thrown, test failed
e.printStackTrace();
fail("testEmailNotificationPostUpdate failed");
}
}

/**
* Tests bad credential error throwing
*
* @throws IOException
*/
@Test(expected = IOException.class)
public void testEmailNotificationPostUpdateBadCreds() throws IOException {
@Test
public void testEmailNotificationPostUpdateBadCreds() {
String credFile = "/tmp/dumFile.txt";
if (System.getProperty("os.name").toLowerCase().contains("win"))
credFile = "C:\\Users\\Administrator\\dumFile.txt";

String email = "badEmail";
String password = "badPassword";
String host = "some.smtp.com";
TxtFileConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler();
// Create and write bad values to a dummy text file
FileWriter file;
try {
file = new FileWriter(credFile);
file.write("badHost\nsomeEmail\nbaddPass");
file.close();
} catch (IOException e) {
System.out.println("Couldn't create file to run test");
e.printStackTrace();
}

handler.setOption(credFile);

EmailUpdateHandler updater = new EmailUpdateHandler();
// Just send an email to itself
updater.setEmailAddress(email);
updater.setPassword(password);
updater.setSmtpHost(host);
// Setup a bad credential file
updater.setCredHandler(handler);
updater.setMessage("Bad email");
updater.setSubject("This is a bad email");
updater.postUpdate();
// Expect exception
try {
updater.postUpdate();
/// IF it worked, the test failed
fail("The test worked, and it should have caught an exception!");
} catch (IOException e) {
System.out.println("Exception correctly caught");
e.printStackTrace();
}

// Delete the dummy file we made
File fileDel = new File(credFile);
fileDel.delete();


}

}
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package org.eclipse.ice.tests.commands;

import static org.junit.Assert.fail;

import java.io.IOException;

import org.eclipse.ice.commands.CommandStatus;
Expand All @@ -33,15 +35,21 @@ public class HTTPHandlerTest {
* @throws IOException
*/
@Test
public void testHTTPNotificationPostUpdate() throws IOException {
public void testHTTPNotificationPostUpdate(){

HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler();
updater.setHTTPAddress("https://225eee153c45329a1bcedd6da637643f.m.pipedream.net");
updater.setMessage("job finished with status " + CommandStatus.INFOERROR);

updater.postUpdate();
try {
updater.postUpdate();
} catch (IOException e) {
// If an exception is thrown, test failed
e.printStackTrace();
fail("testHTTPNotificationPostUpdate threw an exception and thus failed.");
}

// test is successful if no exception is thrown

}

}

0 comments on commit d1b8b47

Please sign in to comment.