From 53c98ee0f3fdae7e6b70cbb11b2567c2ccdb3709 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Wed, 1 Jul 2020 08:07:25 -0400 Subject: [PATCH 1/4] Change tests to catch exception and fail Signed-off-by: Joe Osborn --- .../ice/tests/commands/EmailHandlerTest.java | 18 ++++++++++++++---- .../ice/tests/commands/HTTPHandlerTest.java | 14 +++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java index 61eb56e14..75245ccf0 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java @@ -11,7 +11,10 @@ *******************************************************************************/ package org.eclipse.ice.tests.commands; +import static org.junit.Assert.fail; + import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; @@ -33,7 +36,7 @@ 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"; @@ -49,6 +52,9 @@ public void testEmailNotificationPostUpdate() throws IOException { email = scanner.next(); password = scanner.next(); host = scanner.next(); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + System.out.println("Email credential file not found in testEmailNotificationPostUpdate"); } EmailUpdateHandler updater = new EmailUpdateHandler(); @@ -58,9 +64,13 @@ public void testEmailNotificationPostUpdate() throws IOException { updater.setSmtpHost(host); 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"); + } } /** diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java index 5d8f012fa..24ad346c9 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java @@ -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; @@ -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 + } } From bb553cbc0a2313681c005d667a500b417668ce7c Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Wed, 1 Jul 2020 12:33:16 -0400 Subject: [PATCH 2/4] Fix EmailHandler to use ConnectionAuthorization Signed-off-by: Joe Osborn --- .../ice/commands/EmailUpdateHandler.java | 57 +++++----------- .../ice/tests/commands/EmailHandlerTest.java | 66 ++++++++++--------- 2 files changed, 51 insertions(+), 72 deletions(-) diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java index d3d45d884..14f090e6f 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java @@ -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 @@ -58,23 +52,24 @@ 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 { @@ -82,8 +77,8 @@ protected PasswordAuthentication getPasswordAuthentication() { 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); @@ -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 */ @@ -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; } /** diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java index 75245ccf0..c5c94947a 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java @@ -13,12 +13,11 @@ import static org.junit.Assert.fail; -import java.io.File; -import java.io.FileNotFoundException; +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; /** @@ -43,25 +42,12 @@ public void testEmailNotificationPostUpdate() { 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 e1) { - e1.printStackTrace(); - System.out.println("Email credential file not found in testEmailNotificationPostUpdate"); - } + 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"); try { @@ -76,23 +62,41 @@ public void testEmailNotificationPostUpdate() { /** * 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(); + } + // Expect exception. If not thrown, it failed } } From 1ff1ada488ff64837f7f874535fc9c111b89ccab Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Wed, 1 Jul 2020 12:41:49 -0400 Subject: [PATCH 3/4] Delete tmp file, fix command test Signed-off-by: Joe Osborn --- .../commands/CommandFactoryUpdaterTest.java | 25 +++---------------- .../ice/tests/commands/EmailHandlerTest.java | 8 +++++- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/CommandFactoryUpdaterTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/CommandFactoryUpdaterTest.java index 23e279a74..dfab2c136 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/CommandFactoryUpdaterTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/CommandFactoryUpdaterTest.java @@ -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; @@ -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 @@ -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; } diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java index c5c94947a..09250525d 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java @@ -13,6 +13,7 @@ import static org.junit.Assert.fail; +import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -96,7 +97,12 @@ public void testEmailNotificationPostUpdateBadCreds() { System.out.println("Exception correctly caught"); e.printStackTrace(); } - // Expect exception. If not thrown, it failed + + // Delete the dummy file we made + File fileDel = new File(credFile); + fileDel.delete(); + + } } From eae67debc5d282e86efe62bf311892ad5863f079 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Wed, 1 Jul 2020 15:34:17 -0400 Subject: [PATCH 4/4] Updated isConnectionOpen per Robert's suggestion Signed-off-by: Joe Osborn --- .../java/org/eclipse/ice/commands/ConnectionManager.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ConnectionManager.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ConnectionManager.java index 67a4af355..d0577c342 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ConnectionManager.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ConnectionManager.java @@ -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; } /**