From f8552532c1ca811b02e8b9d69e82860b02d3e082 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 2 Apr 2020 09:10:37 -0400 Subject: [PATCH 01/12] First pass at tests. Don't work (or build) Signed-off-by: Joe Osborn --- .../ice/tests/commands/EmailHandlerTest.java | 134 ++++++++++++++++++ .../ice/tests/commands/HTTPHandlerTest.java | 133 +++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java create mode 100644 org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java 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 new file mode 100644 index 000000000..72ef918d1 --- /dev/null +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java @@ -0,0 +1,134 @@ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Initial API and implementation and/or initial documentation - + * Jay Jay Billings, Joe Osborn + *******************************************************************************/ +package org.eclipse.ice.tests.commands; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.eclipse.ice.commands.Command; +import org.eclipse.ice.commands.CommandConfiguration; +import org.eclipse.ice.commands.CommandFactory; +import org.eclipse.ice.commands.CommandStatus; +import org.eclipse.ice.commands.ConnectionAuthorizationHandler; +import org.eclipse.ice.commands.ConnectionConfiguration; +import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * This class tests the implementation of Email notification handling for jobs + * as defined in the EmailHandler class + * + * @author Joe Osborn + * + */ +public class EmailHandlerTest { + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + @Test + public void testEmailNotification() { + fail("Not yet implemented"); + + // Get the hostname for your local computer + InetAddress addr = null; + try { + addr = InetAddress.getLocalHost(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + + String hostname = addr.getHostName(); + + EmailHandler email = new EmailHandler("myemailaddress@domain.com"); + + String pwd = System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/"; + + // Make a simple command configuration to just run an ls -lrt + CommandConfiguration cmdCfg = new CommandConfiguration(); + cmdCfg.setNumProcs("1"); + cmdCfg.setInstallDirectory(""); + cmdCfg.setWorkingDirectory(pwd); + cmdCfg.setOS(System.getProperty("os.name")); + cmdCfg.setExecutable("ls -lrt"); + + if (System.getProperty("os.name").toLowerCase().contains("win")) { + // Add powershell interpeter if os is windows + cmdCfg.setInterpreter("powershell.exe"); + // just use ls because powershell automatically adds the -lrt + // and doesn't know what -lrt is anyway + cmdCfg.setExecutable("ls"); + } + cmdCfg.setInstallDirectory(""); + cmdCfg.setAppendInput(false); + cmdCfg.setCommandId(1); + cmdCfg.setErrFileName("someLsErrFile.txt"); + cmdCfg.setOutFileName("someLsOutFile.txt"); + cmdCfg.setEmailHandler(email); + // Make the connection configuration + ConnectionConfiguration ctCfg = new ConnectionConfiguration(); + ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler(); + handler.setHostname(hostname); + ctCfg.setAuthorization(handler); + + // Get and run the command + CommandFactory factory = new CommandFactory(); + Command cmd = null; + try { + cmd = factory.getCommand(cmdCfg, ctCfg); + } catch (IOException e) { + e.printStackTrace(); + } + CommandStatus status = cmd.execute(); + + // Check that it properly finished + assertEquals(CommandStatus.SUCCESS, status); + + // Check that the HTTP address is valid and populated + assertEquals("check email sent somehow?"); + + } + +} 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 new file mode 100644 index 000000000..0691eae3b --- /dev/null +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java @@ -0,0 +1,133 @@ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Initial API and implementation and/or initial documentation - + * Jay Jay Billings, Joe Osborn + *******************************************************************************/ +package org.eclipse.ice.tests.commands; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.eclipse.ice.commands.Command; +import org.eclipse.ice.commands.CommandConfiguration; +import org.eclipse.ice.commands.CommandFactory; +import org.eclipse.ice.commands.CommandStatus; +import org.eclipse.ice.commands.ConnectionAuthorizationHandler; +import org.eclipse.ice.commands.ConnectionConfiguration; +import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * This class tests the implementation of Email notification handling for jobs + * as defined in the EmailHandler class + * + * @author Joe Osborn + * + */ +public class HTTPHandlerTest { + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + @Test + public void testHTTPNotification() { + fail("Not yet implemented"); + // Get the hostname for your local computer + InetAddress addr = null; + try { + addr = InetAddress.getLocalHost(); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + + String hostname = addr.getHostName(); + + HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler("www.example.com/update"); + + String pwd = System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/"; + + // Make a simple command configuration to just run an ls -lrt + CommandConfiguration cmdCfg = new CommandConfiguration(); + cmdCfg.setNumProcs("1"); + cmdCfg.setInstallDirectory(""); + cmdCfg.setWorkingDirectory(pwd); + cmdCfg.setOS(System.getProperty("os.name")); + cmdCfg.setExecutable("ls -lrt"); + + if (System.getProperty("os.name").toLowerCase().contains("win")) { + // Add powershell interpeter if os is windows + cmdCfg.setInterpreter("powershell.exe"); + // just use ls because powershell automatically adds the -lrt + // and doesn't know what -lrt is anyway + cmdCfg.setExecutable("ls"); + } + cmdCfg.setInstallDirectory(""); + cmdCfg.setAppendInput(false); + cmdCfg.setCommandId(1); + cmdCfg.setErrFileName("someLsErrFile.txt"); + cmdCfg.setOutFileName("someLsOutFile.txt"); + cmdCfg.setHTTPUpdateHandler(updater); + // Make the connection configuration + ConnectionConfiguration ctCfg = new ConnectionConfiguration(); + ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler(); + handler.setHostname(hostname); + ctCfg.setAuthorization(handler); + + // Get and run the command + CommandFactory factory = new CommandFactory(); + Command cmd = null; + try { + cmd = factory.getCommand(cmdCfg, ctCfg); + } catch (IOException e) { + e.printStackTrace(); + } + CommandStatus status = cmd.execute(); + + // Check that it properly finished + assertEquals(CommandStatus.SUCCESS, status); + + // Check that the HTTP address is valid and populated + assertEquals("check http address?"); + + } + +} From 1f5bb261da4da01ea7706ee500d616316c7409f0 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 2 Apr 2020 10:40:34 -0400 Subject: [PATCH 02/12] Made new project since it will require additional dependencies Signed-off-by: Joe Osborn --- .../.classpath | 29 +++++++++++++ .../.project | 23 ++++++++++ .../org.eclipse.core.resources.prefs | 4 ++ .../.settings/org.eclipse.jdt.core.prefs | 8 ++++ .../.settings/org.eclipse.m2e.core.prefs | 4 ++ org.eclipse.ice.commands.notification/pom.xml | 38 ++++++++++++++++ .../notification/EmailUpdateHandler.java | 37 ++++++++++++++++ .../HTTPCommandUpdateHandler.java | 43 +++++++++++++++++++ .../notification/ICommandUpdateHandler.java | 35 +++++++++++++++ .../notification}/EmailHandlerTest.java | 6 +-- .../notification}/HTTPHandlerTest.java | 6 +-- .../SWTConnectionAuthorizationHandler.java | 5 ++- 12 files changed, 230 insertions(+), 8 deletions(-) create mode 100644 org.eclipse.ice.commands.notification/.classpath create mode 100644 org.eclipse.ice.commands.notification/.project create mode 100644 org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs create mode 100644 org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs create mode 100644 org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs create mode 100644 org.eclipse.ice.commands.notification/pom.xml create mode 100644 org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java create mode 100644 org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java create mode 100644 org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java rename {org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands => org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification}/EmailHandlerTest.java (96%) rename {org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands => org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification}/HTTPHandlerTest.java (97%) diff --git a/org.eclipse.ice.commands.notification/.classpath b/org.eclipse.ice.commands.notification/.classpath new file mode 100644 index 000000000..52f5b34ec --- /dev/null +++ b/org.eclipse.ice.commands.notification/.classpath @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/org.eclipse.ice.commands.notification/.project b/org.eclipse.ice.commands.notification/.project new file mode 100644 index 000000000..1619c55e6 --- /dev/null +++ b/org.eclipse.ice.commands.notification/.project @@ -0,0 +1,23 @@ + + + commands.notification + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs b/org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..f9fe34593 --- /dev/null +++ b/org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..5723a0f87 --- /dev/null +++ b/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs b/org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 000000000..f897a7f1c --- /dev/null +++ b/org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/org.eclipse.ice.commands.notification/pom.xml b/org.eclipse.ice.commands.notification/pom.xml new file mode 100644 index 000000000..73895d7e5 --- /dev/null +++ b/org.eclipse.ice.commands.notification/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + + org.eclipse.ice + org.eclipse.ice.commands.notification + 0.0.1-SNAPSHOT + jar + + commands.notification + + 1.8 + 1.8 + + + + + junit + junit + 4.4 + + + org.slf4j + slf4j-log4j12 + 1.7.12 + + + log4j + log4j + 1.2.17 + + + javax.mail + mail + 1.5.0-b01 + + + diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java new file mode 100644 index 000000000..b33193fc7 --- /dev/null +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Initial API and implementation and/or initial documentation - + * Jay Jay Billings, Joe Osborn + *******************************************************************************/ +package org.eclipse.ice.commands.notification; + +/** + * This class provides email updates for Command job statuses + * @author Joe Osborn + * + */ +public class EmailUpdateHandler implements ICommandUpdateHandler{ + + private String emailAddress = ""; + + /** + * Default constructor + */ + public EmailUpdateHandler() { + } + + public void setOption(String option) { + } + + public void postUpdate() { + } + + + +} diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java new file mode 100644 index 000000000..88b8a865c --- /dev/null +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Initial API and implementation and/or initial documentation - + * Jay Jay Billings, Joe Osborn + *******************************************************************************/ +package org.eclipse.ice.commands.notification; + +/** + * This class posts Command job updates to an HTTP link via POST + * + * @author Joe Osborn + * + */ +public class HTTPCommandUpdateHandler implements ICommandUpdateHandler{ + + + private String HTTPAddress = ""; + + /** + * Default constructor + */ + public HTTPCommandUpdateHandler() { + } + + public void setOption(String option) { + // TODO Auto-generated method stub + + } + + public void postUpdate() { + // TODO Auto-generated method stub + + } + + + +} diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java new file mode 100644 index 000000000..b6f6e8b8e --- /dev/null +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Initial API and implementation and/or initial documentation - + * Jay Jay Billings, Joe Osborn + *******************************************************************************/ +package org.eclipse.ice.commands.notification; + +/** + * This interface lays out the format for any Command update class. Command + * update classes notify users via their implemented methods about the status of + * jobs, e.g. if they are completed or not. + * + * @author Joe Osborn + * + */ +public interface ICommandUpdateHandler { + + /* + * Public setter that must be implemented so that the child class can obtain a + * place to send the update + */ + public void setOption(String option); + + /* + * This function processes the logic required to post an update about the job + * for the class + */ + abstract void postUpdate(); +} diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java similarity index 96% rename from org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java rename to org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java index 72ef918d1..a9236f1a7 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java +++ b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java @@ -9,7 +9,7 @@ * Initial API and implementation and/or initial documentation - * Jay Jay Billings, Joe Osborn *******************************************************************************/ -package org.eclipse.ice.tests.commands; +package org.eclipse.ice.commands.notification; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -82,8 +82,8 @@ public void testEmailNotification() { String hostname = addr.getHostName(); - EmailHandler email = new EmailHandler("myemailaddress@domain.com"); - + EmailUpdateHandler email = new EmailUpdateHandler(); + email.setOption("myemailaddress@domain.com"); String pwd = System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/"; // Make a simple command configuration to just run an ls -lrt diff --git a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java similarity index 97% rename from org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java rename to org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java index 0691eae3b..a3bac5cf4 100644 --- a/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java +++ b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java @@ -9,7 +9,7 @@ * Initial API and implementation and/or initial documentation - * Jay Jay Billings, Joe Osborn *******************************************************************************/ -package org.eclipse.ice.tests.commands; +package org.eclipse.ice.commands.notification; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; @@ -81,8 +81,8 @@ public void testHTTPNotification() { String hostname = addr.getHostName(); - HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler("www.example.com/update"); - + HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); + updater.setOption("www.example.com"); String pwd = System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/"; // Make a simple command configuration to just run an ls -lrt diff --git a/org.eclipse.ice.commands.swt/src/main/java/org/eclipse/ice/commands/swt/SWTConnectionAuthorizationHandler.java b/org.eclipse.ice.commands.swt/src/main/java/org/eclipse/ice/commands/swt/SWTConnectionAuthorizationHandler.java index d3545d29b..f3bc272ae 100644 --- a/org.eclipse.ice.commands.swt/src/main/java/org/eclipse/ice/commands/swt/SWTConnectionAuthorizationHandler.java +++ b/org.eclipse.ice.commands.swt/src/main/java/org/eclipse/ice/commands/swt/SWTConnectionAuthorizationHandler.java @@ -131,7 +131,8 @@ public void widgetSelected(SelectionEvent e) { * * @param password */ - public void setPassword(char[] password) { - this.password = password; + @Override + public void setOption(String option) { + this.password = option.toCharArray(); } } From a81ace657bdf57b1712336977c85384c15c9d0f1 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 2 Apr 2020 10:41:26 -0400 Subject: [PATCH 03/12] add gitignore Signed-off-by: Joe Osborn --- org.eclipse.ice.commands.notification/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 org.eclipse.ice.commands.notification/.gitignore diff --git a/org.eclipse.ice.commands.notification/.gitignore b/org.eclipse.ice.commands.notification/.gitignore new file mode 100644 index 000000000..703743bd4 --- /dev/null +++ b/org.eclipse.ice.commands.notification/.gitignore @@ -0,0 +1,2 @@ +/target/ +/bin/ \ No newline at end of file From ba6210cc782b4c08c4bb5a38711fead5bd2a3afe Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 2 Apr 2020 14:36:45 -0400 Subject: [PATCH 04/12] Email handler test works, but don't want to publish credentials Signed-off-by: Joe Osborn --- org.eclipse.ice.commands.notification/pom.xml | 13 ++- .../notification/EmailUpdateHandler.java | 98 ++++++++++++++++++- .../HTTPCommandUpdateHandler.java | 7 +- .../notification/ICommandUpdateHandler.java | 13 ++- .../notification/EmailHandlerTest.java | 76 ++------------ .../notification/HTTPHandlerTest.java | 73 +------------- 6 files changed, 132 insertions(+), 148 deletions(-) diff --git a/org.eclipse.ice.commands.notification/pom.xml b/org.eclipse.ice.commands.notification/pom.xml index 73895d7e5..a1b71f42d 100644 --- a/org.eclipse.ice.commands.notification/pom.xml +++ b/org.eclipse.ice.commands.notification/pom.xml @@ -29,10 +29,15 @@ log4j 1.2.17 - - javax.mail - mail - 1.5.0-b01 + + com.sun.mail + javax.mail + 1.6.2 + + + org.apache.httpcomponents + httpclient + 4.5.10 diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java index b33193fc7..662a36dd2 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java @@ -11,14 +11,42 @@ *******************************************************************************/ package org.eclipse.ice.commands.notification; +import java.io.IOException; +import java.util.Properties; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + /** * This class provides email updates for Command job statuses + * * @author Joe Osborn * */ -public class EmailUpdateHandler implements ICommandUpdateHandler{ +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 = ""; + + // The default commands api email that will send the message + private String commandsEmail = "someemail@gmail.com"; + + // The host smtp server for the commands api email address + private String commandsHost = "smtp.gmail.com"; + + + private String commandsPassword = "password"; /** * Default constructor @@ -26,12 +54,76 @@ public class EmailUpdateHandler implements ICommandUpdateHandler{ public EmailUpdateHandler() { } - public void setOption(String option) { + public void postUpdate() throws IOException { + // Create some properties and setup the default gmail + // server properties + Properties properties = System.getProperties(); + properties.put("mail.smtp.host", commandsHost); + 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", commandsHost); + + // Get the default Session object. + Session session = Session.getInstance(properties, new javax.mail.Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(commandsEmail, commandsPassword); + } + }); + 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(commandsEmail)); + message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress)); + + // Give the email a subject and message content + message.setSubject(emailSubject); + message.setText(emailText); + + // Send message + Transport.send(message); + logger.info("Email sent successfully"); + } catch (MessagingException mex) { + logger.error("Couldn't send email.", mex); + throw new IOException(); + } + } - public void postUpdate() { + /** + * Setter for the email subject, see + * {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailSubject} + * + * @param emailSubject + */ + public void setSubject(String emailSubject) { + this.emailSubject = emailSubject; } + /** + * Setter for the email address, see + * {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailAddress} + * + * @param option + */ + public void setOption(String option) { + this.emailAddress = option; + } + /** + * Setter for the email message, see + * {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailText} + * + * @param emailText + */ + public void setEmailText(String emailText) { + this.emailText = emailText; + } } diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java index 88b8a865c..f3ba9460f 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.eclipse.ice.commands.notification; +import java.io.IOException; + /** * This class posts Command job updates to an HTTP link via POST * @@ -29,12 +31,11 @@ public HTTPCommandUpdateHandler() { } public void setOption(String option) { - // TODO Auto-generated method stub + this.HTTPAddress = option; } - public void postUpdate() { - // TODO Auto-generated method stub + public void postUpdate() throws IOException { } diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java index b6f6e8b8e..4d162b347 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java @@ -11,6 +11,11 @@ *******************************************************************************/ package org.eclipse.ice.commands.notification; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * This interface lays out the format for any Command update class. Command * update classes notify users via their implemented methods about the status of @@ -21,6 +26,12 @@ */ public interface ICommandUpdateHandler { + /** + * Logger for handling event messages and other information. + */ + static final Logger logger = LoggerFactory.getLogger(ICommandUpdateHandler.class); + + /* * Public setter that must be implemented so that the child class can obtain a * place to send the update @@ -31,5 +42,5 @@ public interface ICommandUpdateHandler { * This function processes the logic required to post an update about the job * for the class */ - abstract void postUpdate(); + abstract void postUpdate() throws IOException; } diff --git a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java index a9236f1a7..d62057136 100644 --- a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java +++ b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java @@ -11,20 +11,8 @@ *******************************************************************************/ package org.eclipse.ice.commands.notification; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; -import org.eclipse.ice.commands.Command; -import org.eclipse.ice.commands.CommandConfiguration; -import org.eclipse.ice.commands.CommandFactory; -import org.eclipse.ice.commands.CommandStatus; -import org.eclipse.ice.commands.ConnectionAuthorizationHandler; -import org.eclipse.ice.commands.ConnectionConfiguration; -import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -69,66 +57,16 @@ public void tearDown() throws Exception { } @Test - public void testEmailNotification() { - fail("Not yet implemented"); - - // Get the hostname for your local computer - InetAddress addr = null; - try { - addr = InetAddress.getLocalHost(); - } catch (UnknownHostException e) { - e.printStackTrace(); - } - - String hostname = addr.getHostName(); + public void testEmailNotificationPostUpdate() throws IOException { EmailUpdateHandler email = new EmailUpdateHandler(); - email.setOption("myemailaddress@domain.com"); - String pwd = System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/"; - - // Make a simple command configuration to just run an ls -lrt - CommandConfiguration cmdCfg = new CommandConfiguration(); - cmdCfg.setNumProcs("1"); - cmdCfg.setInstallDirectory(""); - cmdCfg.setWorkingDirectory(pwd); - cmdCfg.setOS(System.getProperty("os.name")); - cmdCfg.setExecutable("ls -lrt"); - - if (System.getProperty("os.name").toLowerCase().contains("win")) { - // Add powershell interpeter if os is windows - cmdCfg.setInterpreter("powershell.exe"); - // just use ls because powershell automatically adds the -lrt - // and doesn't know what -lrt is anyway - cmdCfg.setExecutable("ls"); - } - cmdCfg.setInstallDirectory(""); - cmdCfg.setAppendInput(false); - cmdCfg.setCommandId(1); - cmdCfg.setErrFileName("someLsErrFile.txt"); - cmdCfg.setOutFileName("someLsOutFile.txt"); - cmdCfg.setEmailHandler(email); - // Make the connection configuration - ConnectionConfiguration ctCfg = new ConnectionConfiguration(); - ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler(); - handler.setHostname(hostname); - ctCfg.setAuthorization(handler); - - // Get and run the command - CommandFactory factory = new CommandFactory(); - Command cmd = null; - try { - cmd = factory.getCommand(cmdCfg, ctCfg); - } catch (IOException e) { - e.printStackTrace(); - } - CommandStatus status = cmd.execute(); - - // Check that it properly finished - assertEquals(CommandStatus.SUCCESS, status); + // Just send an email to itself + email.setOption("commandsapi@gmail.com"); + email.setEmailText("This is a test email"); + email.setSubject("This is a test subject"); + email.postUpdate(); - // Check that the HTTP address is valid and populated - assertEquals("check email sent somehow?"); - + // If no exception is thrown, it completed correctly } } diff --git a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java index a3bac5cf4..99ac12a3b 100644 --- a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java +++ b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java @@ -11,20 +11,8 @@ *******************************************************************************/ package org.eclipse.ice.commands.notification; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; -import org.eclipse.ice.commands.Command; -import org.eclipse.ice.commands.CommandConfiguration; -import org.eclipse.ice.commands.CommandFactory; -import org.eclipse.ice.commands.CommandStatus; -import org.eclipse.ice.commands.ConnectionAuthorizationHandler; -import org.eclipse.ice.commands.ConnectionConfiguration; -import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -69,65 +57,14 @@ public void tearDown() throws Exception { } @Test - public void testHTTPNotification() { - fail("Not yet implemented"); - // Get the hostname for your local computer - InetAddress addr = null; - try { - addr = InetAddress.getLocalHost(); - } catch (UnknownHostException e) { - e.printStackTrace(); - } - - String hostname = addr.getHostName(); - + public void testHTTPNotificationPostUpdate() throws IOException { + HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); updater.setOption("www.example.com"); - String pwd = System.getProperty("user.dir") + "/src/test/java/org/eclipse/ice/tests/commands/"; - - // Make a simple command configuration to just run an ls -lrt - CommandConfiguration cmdCfg = new CommandConfiguration(); - cmdCfg.setNumProcs("1"); - cmdCfg.setInstallDirectory(""); - cmdCfg.setWorkingDirectory(pwd); - cmdCfg.setOS(System.getProperty("os.name")); - cmdCfg.setExecutable("ls -lrt"); - - if (System.getProperty("os.name").toLowerCase().contains("win")) { - // Add powershell interpeter if os is windows - cmdCfg.setInterpreter("powershell.exe"); - // just use ls because powershell automatically adds the -lrt - // and doesn't know what -lrt is anyway - cmdCfg.setExecutable("ls"); - } - cmdCfg.setInstallDirectory(""); - cmdCfg.setAppendInput(false); - cmdCfg.setCommandId(1); - cmdCfg.setErrFileName("someLsErrFile.txt"); - cmdCfg.setOutFileName("someLsOutFile.txt"); - cmdCfg.setHTTPUpdateHandler(updater); - // Make the connection configuration - ConnectionConfiguration ctCfg = new ConnectionConfiguration(); - ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler(); - handler.setHostname(hostname); - ctCfg.setAuthorization(handler); - - // Get and run the command - CommandFactory factory = new CommandFactory(); - Command cmd = null; - try { - cmd = factory.getCommand(cmdCfg, ctCfg); - } catch (IOException e) { - e.printStackTrace(); - } - CommandStatus status = cmd.execute(); - - // Check that it properly finished - assertEquals(CommandStatus.SUCCESS, status); - // Check that the HTTP address is valid and populated - assertEquals("check http address?"); - + updater.postUpdate(); + + // test is successful if no exception is thrown } } From cae4bca11a8e79217aeb8e13f38a5bf4a7cf02a6 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 2 Apr 2020 15:49:44 -0400 Subject: [PATCH 05/12] HTTP test fails, need to debug. Probably from hostname Signed-off-by: Joe Osborn --- .../notification/EmailUpdateHandler.java | 4 +- .../HTTPCommandUpdateHandler.java | 43 ++++++++++++++++--- .../notification/ICommandUpdateHandler.java | 7 --- .../notification/EmailHandlerTest.java | 2 +- .../notification/HTTPHandlerTest.java | 4 +- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java index 662a36dd2..63b2988a4 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java @@ -112,8 +112,8 @@ public void setSubject(String emailSubject) { * * @param option */ - public void setOption(String option) { - this.emailAddress = option; + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; } /** diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java index f3ba9460f..50774a619 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java @@ -12,6 +12,18 @@ package org.eclipse.ice.commands.notification; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.eclipse.ice.commands.CommandStatus; + /** * This class posts Command job updates to an HTTP link via POST @@ -19,10 +31,11 @@ * @author Joe Osborn * */ -public class HTTPCommandUpdateHandler implements ICommandUpdateHandler{ +public class HTTPCommandUpdateHandler implements ICommandUpdateHandler { - private String HTTPAddress = ""; + + CommandStatus status = null; /** * Default constructor @@ -30,15 +43,31 @@ public class HTTPCommandUpdateHandler implements ICommandUpdateHandler{ public HTTPCommandUpdateHandler() { } - public void setOption(String option) { - this.HTTPAddress = option; - + public void setHTTPAddress(String HTTPAddress) { + this.HTTPAddress = HTTPAddress; } - public void postUpdate() throws IOException { - + public void postStatus(CommandStatus status) { + this.status = status; } + + public void postUpdate() throws IOException { + HttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(HTTPAddress); + // Setup the parameters to be passed in the post + List params = new ArrayList(1); + params.add(new BasicNameValuePair("status", status.toString())); + httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); + + // Execute and get the response + try { + HttpResponse response = httpClient.execute(httpPost); + } catch (Exception e) { + logger.info("HTTP Post was not successful.", e); + throw new IOException(); + } + } } diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java index 4d162b347..979e8b836 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java @@ -31,13 +31,6 @@ public interface ICommandUpdateHandler { */ static final Logger logger = LoggerFactory.getLogger(ICommandUpdateHandler.class); - - /* - * Public setter that must be implemented so that the child class can obtain a - * place to send the update - */ - public void setOption(String option); - /* * This function processes the logic required to post an update about the job * for the class diff --git a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java index d62057136..5b0829ea3 100644 --- a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java +++ b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java @@ -61,7 +61,7 @@ public void testEmailNotificationPostUpdate() throws IOException { EmailUpdateHandler email = new EmailUpdateHandler(); // Just send an email to itself - email.setOption("commandsapi@gmail.com"); + email.setEmailAddress("someemail@gmail.com"); email.setEmailText("This is a test email"); email.setSubject("This is a test subject"); email.postUpdate(); diff --git a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java index 99ac12a3b..f959efa75 100644 --- a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java +++ b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java @@ -13,6 +13,7 @@ import java.io.IOException; +import org.eclipse.ice.commands.CommandStatus; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -60,7 +61,8 @@ public void tearDown() throws Exception { public void testHTTPNotificationPostUpdate() throws IOException { HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); - updater.setOption("www.example.com"); + updater.setHTTPAddress("www.example.com"); + updater.postStatus(CommandStatus.INFOERROR); updater.postUpdate(); From e9317c920709877cbacbd4ce3f3a6e815d91580a Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 14 May 2020 08:59:12 -0400 Subject: [PATCH 06/12] Update email handler test to accommodate text file authentication Signed-off-by: Joe Osborn --- .../.classpath | 2 +- .../.settings/org.eclipse.jdt.core.prefs | 13 ++++-- .../notification/EmailUpdateHandler.java | 40 +++++++++++++------ .../notification/EmailHandlerTest.java | 30 +++++++++++--- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/org.eclipse.ice.commands.notification/.classpath b/org.eclipse.ice.commands.notification/.classpath index 52f5b34ec..917dd2289 100644 --- a/org.eclipse.ice.commands.notification/.classpath +++ b/org.eclipse.ice.commands.notification/.classpath @@ -13,7 +13,7 @@ - + diff --git a/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs index 5723a0f87..b9a10350a 100644 --- a/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs +++ b/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,15 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.5 +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java index 63b2988a4..5a7e5264a 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java +++ b/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java @@ -23,7 +23,9 @@ import javax.mail.internet.MimeMessage; /** - * This class provides email updates for Command job statuses + * This class provides email updates for Command job statuses. It takes + * a provided email address, password, etc. and just sends an email to + * the same address * * @author Joe Osborn * @@ -39,14 +41,11 @@ public class EmailUpdateHandler implements ICommandUpdateHandler { // The subject of the message private String emailSubject = ""; - // The default commands api email that will send the message - private String commandsEmail = "someemail@gmail.com"; - // The host smtp server for the commands api email address - private String commandsHost = "smtp.gmail.com"; + private String emailHost = "smtp.gmail.com"; - - private String commandsPassword = "password"; + // The password for the provided email to be able to send to itself + private String emailPassword = ""; /** * Default constructor @@ -58,18 +57,17 @@ public void postUpdate() throws IOException { // Create some properties and setup the default gmail // server properties Properties properties = System.getProperties(); - properties.put("mail.smtp.host", commandsHost); 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", commandsHost); + properties.setProperty("mail.smtp.host", emailHost); // Get the default Session object. Session session = Session.getInstance(properties, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(commandsEmail, commandsPassword); + return new PasswordAuthentication(emailAddress, emailPassword); } }); session.setDebug(true); @@ -79,7 +77,7 @@ protected PasswordAuthentication getPasswordAuthentication() { MimeMessage message = new MimeMessage(session); // Set the sender and recipient of the email - message.setFrom(new InternetAddress(commandsEmail)); + message.setFrom(new InternetAddress(emailAddress)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(emailAddress)); // Give the email a subject and message content @@ -115,7 +113,25 @@ public void setSubject(String emailSubject) { 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} + * @param emailPassword + */ + public void setPassword(String emailPassword) { + this.emailPassword = emailPassword; + } + /** * Setter for the email message, see * {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailText} diff --git a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java index 5b0829ea3..45633e905 100644 --- a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java +++ b/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java @@ -11,7 +11,9 @@ *******************************************************************************/ package org.eclipse.ice.commands.notification; +import java.io.File; import java.io.IOException; +import java.util.Scanner; import org.junit.After; import org.junit.AfterClass; @@ -59,12 +61,30 @@ public void tearDown() throws Exception { @Test public void testEmailNotificationPostUpdate() throws IOException { - EmailUpdateHandler email = new EmailUpdateHandler(); + // 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(); + } + + EmailUpdateHandler updater = new EmailUpdateHandler(); // Just send an email to itself - email.setEmailAddress("someemail@gmail.com"); - email.setEmailText("This is a test email"); - email.setSubject("This is a test subject"); - email.postUpdate(); + updater.setEmailAddress(email); + updater.setPassword(password); + updater.setSmtpHost(host); + updater.setEmailText("This is a test updater"); + updater.setSubject("This is a test subject"); + updater.postUpdate(); // If no exception is thrown, it completed correctly } From c483a467b264d4ae21bc9be1da31a35d340aecc5 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 14 May 2020 10:23:13 -0400 Subject: [PATCH 07/12] Moved updater into commands Signed-off-by: Joe Osborn --- .../.classpath | 29 ------------- .../.gitignore | 2 - .../.project | 23 ---------- .../org.eclipse.core.resources.prefs | 4 -- .../.settings/org.eclipse.jdt.core.prefs | 15 ------- .../.settings/org.eclipse.m2e.core.prefs | 4 -- org.eclipse.ice.commands.notification/pom.xml | 43 ------------------- org.eclipse.ice.commands/pom.xml | 10 +++++ .../ice/commands}/EmailUpdateHandler.java | 3 +- .../commands}/HTTPCommandUpdateHandler.java | 4 +- .../ice/commands}/ICommandUpdateHandler.java | 2 +- .../ice/tests/commands}/EmailHandlerTest.java | 3 +- .../ice/tests/commands}/HTTPHandlerTest.java | 3 +- 13 files changed, 19 insertions(+), 126 deletions(-) delete mode 100644 org.eclipse.ice.commands.notification/.classpath delete mode 100644 org.eclipse.ice.commands.notification/.gitignore delete mode 100644 org.eclipse.ice.commands.notification/.project delete mode 100644 org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs delete mode 100644 org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs delete mode 100644 org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs delete mode 100644 org.eclipse.ice.commands.notification/pom.xml rename {org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification => org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands}/EmailUpdateHandler.java (98%) rename {org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification => org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands}/HTTPCommandUpdateHandler.java (95%) rename {org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification => org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands}/ICommandUpdateHandler.java (96%) rename {org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification => org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands}/EmailHandlerTest.java (96%) rename {org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification => org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands}/HTTPHandlerTest.java (94%) diff --git a/org.eclipse.ice.commands.notification/.classpath b/org.eclipse.ice.commands.notification/.classpath deleted file mode 100644 index 917dd2289..000000000 --- a/org.eclipse.ice.commands.notification/.classpath +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/org.eclipse.ice.commands.notification/.gitignore b/org.eclipse.ice.commands.notification/.gitignore deleted file mode 100644 index 703743bd4..000000000 --- a/org.eclipse.ice.commands.notification/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target/ -/bin/ \ No newline at end of file diff --git a/org.eclipse.ice.commands.notification/.project b/org.eclipse.ice.commands.notification/.project deleted file mode 100644 index 1619c55e6..000000000 --- a/org.eclipse.ice.commands.notification/.project +++ /dev/null @@ -1,23 +0,0 @@ - - - commands.notification - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - - diff --git a/org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs b/org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index f9fe34593..000000000 --- a/org.eclipse.ice.commands.notification/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,4 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/main/java=UTF-8 -encoding//src/test/java=UTF-8 -encoding/=UTF-8 diff --git a/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs b/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index b9a10350a..000000000 --- a/org.eclipse.ice.commands.notification/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,15 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.7 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore -org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.7 diff --git a/org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs b/org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1c..000000000 --- a/org.eclipse.ice.commands.notification/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/org.eclipse.ice.commands.notification/pom.xml b/org.eclipse.ice.commands.notification/pom.xml deleted file mode 100644 index a1b71f42d..000000000 --- a/org.eclipse.ice.commands.notification/pom.xml +++ /dev/null @@ -1,43 +0,0 @@ - - 4.0.0 - - org.eclipse.ice - org.eclipse.ice.commands.notification - 0.0.1-SNAPSHOT - jar - - commands.notification - - 1.8 - 1.8 - - - - - junit - junit - 4.4 - - - org.slf4j - slf4j-log4j12 - 1.7.12 - - - log4j - log4j - 1.2.17 - - - com.sun.mail - javax.mail - 1.6.2 - - - org.apache.httpcomponents - httpclient - 4.5.10 - - - diff --git a/org.eclipse.ice.commands/pom.xml b/org.eclipse.ice.commands/pom.xml index c02beb0fb..ea7d218f9 100644 --- a/org.eclipse.ice.commands/pom.xml +++ b/org.eclipse.ice.commands/pom.xml @@ -35,5 +35,15 @@ sshd-sftp 2.3.0 + + com.sun.mail + javax.mail + 1.6.2 + + + org.apache.httpcomponents + httpclient + 4.5.10 + \ No newline at end of file diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java similarity index 98% rename from org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java rename to org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java index 5a7e5264a..42bdc6e0b 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/EmailUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/EmailUpdateHandler.java @@ -9,7 +9,7 @@ * Initial API and implementation and/or initial documentation - * Jay Jay Billings, Joe Osborn *******************************************************************************/ -package org.eclipse.ice.commands.notification; +package org.eclipse.ice.commands; import java.io.IOException; import java.util.Properties; @@ -53,6 +53,7 @@ public class EmailUpdateHandler implements ICommandUpdateHandler { public EmailUpdateHandler() { } + @Override public void postUpdate() throws IOException { // Create some properties and setup the default gmail // server properties diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java similarity index 95% rename from org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java rename to org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java index 50774a619..70710acc6 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/HTTPCommandUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java @@ -9,7 +9,7 @@ * Initial API and implementation and/or initial documentation - * Jay Jay Billings, Joe Osborn *******************************************************************************/ -package org.eclipse.ice.commands.notification; +package org.eclipse.ice.commands; import java.io.IOException; import java.util.ArrayList; @@ -22,7 +22,6 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; -import org.eclipse.ice.commands.CommandStatus; /** @@ -51,6 +50,7 @@ public void postStatus(CommandStatus status) { this.status = status; } + @Override public void postUpdate() throws IOException { HttpClient httpClient = HttpClients.createDefault(); diff --git a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java similarity index 96% rename from org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java rename to org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java index 979e8b836..bc514748e 100644 --- a/org.eclipse.ice.commands.notification/src/main/java/org/eclipse/ice/commands/notification/ICommandUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java @@ -9,7 +9,7 @@ * Initial API and implementation and/or initial documentation - * Jay Jay Billings, Joe Osborn *******************************************************************************/ -package org.eclipse.ice.commands.notification; +package org.eclipse.ice.commands; import java.io.IOException; diff --git a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java similarity index 96% rename from org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java rename to org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java index 45633e905..51a35eccd 100644 --- a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/EmailHandlerTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/EmailHandlerTest.java @@ -9,12 +9,13 @@ * Initial API and implementation and/or initial documentation - * Jay Jay Billings, Joe Osborn *******************************************************************************/ -package org.eclipse.ice.commands.notification; +package org.eclipse.ice.tests.commands; import java.io.File; import java.io.IOException; import java.util.Scanner; +import org.eclipse.ice.commands.EmailUpdateHandler; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; diff --git a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java similarity index 94% rename from org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java rename to org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java index f959efa75..7aacaf6af 100644 --- a/org.eclipse.ice.commands.notification/src/test/java/org/eclipse/ice/commands/notification/HTTPHandlerTest.java +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java @@ -9,11 +9,12 @@ * Initial API and implementation and/or initial documentation - * Jay Jay Billings, Joe Osborn *******************************************************************************/ -package org.eclipse.ice.commands.notification; +package org.eclipse.ice.tests.commands; import java.io.IOException; import org.eclipse.ice.commands.CommandStatus; +import org.eclipse.ice.commands.HTTPCommandUpdateHandler; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; From 7801badbe849dec9c1891ea83502e868d654e24a Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 14 May 2020 11:00:23 -0400 Subject: [PATCH 08/12] Added test and src code for Command handling of updates Signed-off-by: Joe Osborn --- .../org/eclipse/ice/commands/Command.java | 34 ++++- .../ice/commands/EmailUpdateHandler.java | 14 +-- .../commands/HTTPCommandUpdateHandler.java | 9 ++ .../ice/commands/ICommandUpdateHandler.java | 6 + .../commands/CommandFactoryUpdaterTest.java | 117 ++++++++++++++++++ .../ice/tests/commands/EmailHandlerTest.java | 57 ++++----- 6 files changed, 197 insertions(+), 40 deletions(-) create mode 100644 org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/CommandFactoryUpdaterTest.java diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/Command.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/Command.java index 468ca790a..78cb543b2 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/Command.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/Command.java @@ -70,6 +70,12 @@ public abstract class Command { */ protected ConnectionManager manager = ConnectionManagerFactory.getConnectionManager(); + + /** + * An optional update handler that can provide status updates for the command + */ + ICommandUpdateHandler updater = null; + /** * Default constructor */ @@ -104,6 +110,18 @@ public CommandStatus execute() { // Confirm the job finished with some status logger.info("The job finished with status: " + status); + if(updater != null) { + String message = "Job number " + commandConfig.getCommandId() + + " finished with status " + status +"\n " + + "Check your log/out/error files for more details"; + updater.setMessage(message); + try { + updater.postUpdate(); + } catch (IOException e) { + // Just warn since it is not indicative of a job failing + logger.warn("Couldn't post update. Check stack trace" , e); + } + } return status; } @@ -343,8 +361,12 @@ public boolean checkStatus(CommandStatus current_status) { logger.info("The current status is: " + current_status); return true; } else { - logger.error("The job failed with status: " + current_status); - logger.error("Check your error logfile for more details! Exiting now!"); + String statusString = "The job failed with status: " + current_status; + String checkString = "Check your error logfile for more details! Exiting now!"; + + logger.error(statusString); + logger.error(checkString); + return false; } @@ -430,4 +452,12 @@ public void setConnectionManager(ConnectionManager manager) { public ConnectionManager getConnectionManager() { return manager; } + + /** + * Setter for update handler, see {@link org.eclipse.ice.commands.Command#updater} + * @param updater + */ + public void setUpdateHandler(ICommandUpdateHandler updater) { + this.updater = updater; + } } 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 42bdc6e0b..9d36f7f32 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 @@ -39,7 +39,7 @@ public class EmailUpdateHandler implements ICommandUpdateHandler { private String emailText = ""; // The subject of the message - private String emailSubject = ""; + private String emailSubject = "Commands API Message"; // The host smtp server for the commands api email address private String emailHost = "smtp.gmail.com"; @@ -134,13 +134,13 @@ public void setPassword(String emailPassword) { } /** - * Setter for the email message, see - * {@link org.eclipse.ice.commands.notification.EmailUpdateHandler#emailText} - * - * @param emailText + * See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)} */ - public void setEmailText(String emailText) { - this.emailText = emailText; + @Override + public void setMessage(String message) { + this.emailText = message; } + + } diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java index 70710acc6..64aab0339 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java @@ -36,6 +36,7 @@ public class HTTPCommandUpdateHandler implements ICommandUpdateHandler { CommandStatus status = null; + String message = ""; /** * Default constructor */ @@ -70,4 +71,12 @@ public void postUpdate() throws IOException { } } + /** + * See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)} + */ + @Override + public void setMessage(String message) { + this.message = message; + } + } diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java index bc514748e..3eb8454d6 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/ICommandUpdateHandler.java @@ -36,4 +36,10 @@ public interface ICommandUpdateHandler { * for the class */ abstract void postUpdate() throws IOException; + + /** + * Function to set the message that will be updated on + * @param message + */ + abstract void setMessage(String message); } 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 new file mode 100644 index 000000000..8185e35b2 --- /dev/null +++ b/org.eclipse.ice.commands/src/test/java/org/eclipse/ice/tests/commands/CommandFactoryUpdaterTest.java @@ -0,0 +1,117 @@ +/** + * + */ +package org.eclipse.ice.tests.commands; + +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; +import org.eclipse.ice.commands.CommandFactory; +import org.eclipse.ice.commands.CommandStatus; +import org.eclipse.ice.commands.ConnectionAuthorizationHandler; +import org.eclipse.ice.commands.ConnectionConfiguration; +import org.eclipse.ice.commands.EmailUpdateHandler; +import org.eclipse.ice.commands.ICommandUpdateHandler; +import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler; +import org.junit.Test; + +/** + * @author 4jo + * + */ +public class CommandFactoryUpdaterTest { + + /** + * This function tests with real files to test an actual job processing with an + * email updater attached to the command, so that when the job finishes an email + * is sent + */ + @Test + public void testCommandWithEmailUpdater() { + + String hostname = CommandFactoryTest.getLocalHostname(); + // Set some things specific to the local command + CommandConfiguration commandConfig = new CommandConfiguration(); + commandConfig.setNumProcs("1"); + commandConfig.setInstallDirectory(""); + 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 + if (System.getProperty("os.name").toLowerCase().contains("win")) { + commandConfig.setExecutable(".\\test_code_execution.ps1"); + commandConfig.setInterpreter("powershell.exe"); + } + commandConfig.addInputFile("someInputFile", "someInputFile.txt"); + commandConfig.setAppendInput(true); + commandConfig.setCommandId(1); + commandConfig.setErrFileName("someLocalErrFile.txt"); + commandConfig.setOutFileName("someLocalOutFile.txt"); + + // Make a default boring connection authorization + ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler(); + handler.setHostname(hostname); + ConnectionConfiguration connectionConfig = new ConnectionConfiguration(); + connectionConfig.setAuthorization(handler); + + // Get the command + CommandFactory factory = new CommandFactory(); + Command localCommand = null; + try { + localCommand = factory.getCommand(commandConfig, connectionConfig); + } catch (IOException e) { + e.printStackTrace(); + } + + ICommandUpdateHandler updater = setupEmailUpdateHandler(); + localCommand.setUpdateHandler(updater); + + // Run it + CommandStatus status = localCommand.execute(); + + assertEquals(CommandStatus.SUCCESS, status); + + // If test finishes without an error, email was successfully sent + System.out.println("Finished functional local command"); + + } + + /** + * Sets up the dummy email address via a text file credential for CI + * + * @return + */ + private EmailUpdateHandler setupEmailUpdateHandler() { + // 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(); + } catch (FileNotFoundException e) { + System.out.println("Email credential file not found, can't continue with test..."); + e.printStackTrace(); + } + EmailUpdateHandler handler = new EmailUpdateHandler(); + handler.setEmailAddress(email); + handler.setPassword(password); + handler.setSmtpHost(host); + + 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 51a35eccd..7e1dfa59a 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 @@ -16,10 +16,6 @@ import java.util.Scanner; import org.eclipse.ice.commands.EmailUpdateHandler; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; /** @@ -31,34 +27,11 @@ */ public class EmailHandlerTest { + /** - * @throws java.lang.Exception + * Tests successful email notification posting + * @throws IOException */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } - @Test public void testEmailNotificationPostUpdate() throws IOException { @@ -83,11 +56,33 @@ public void testEmailNotificationPostUpdate() throws IOException { updater.setEmailAddress(email); updater.setPassword(password); updater.setSmtpHost(host); - updater.setEmailText("This is a test updater"); + updater.setMessage("This is a test updater"); updater.setSubject("This is a test subject"); updater.postUpdate(); // If no exception is thrown, it completed correctly } + + /** + * Tests bad credential error throwing + * @throws IOException + */ + @Test(expected = IOException.class) + public void testEmailNotificationPostUpdateBadCreds() throws IOException { + + String email = "badEmail"; + String password = "badPassword"; + String host = "some.smtp.com"; + EmailUpdateHandler updater = new EmailUpdateHandler(); + // Just send an email to itself + updater.setEmailAddress(email); + updater.setPassword(password); + updater.setSmtpHost(host); + updater.setMessage("Bad email"); + updater.setSubject("This is a bad email"); + updater.postUpdate(); + // Expect exception + } + } From c301b9cf6194d75bdc5b514676c7232587935142 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 14 May 2020 11:26:05 -0400 Subject: [PATCH 09/12] Added comments, cleanups, http command test Signed-off-by: Joe Osborn --- .../ice/commands/EmailUpdateHandler.java | 5 +- .../commands/HTTPCommandUpdateHandler.java | 18 ++- .../commands/CommandFactoryUpdaterTest.java | 121 +++++++++++++----- .../ice/tests/commands/HTTPHandlerTest.java | 43 ++----- 4 files changed, 113 insertions(+), 74 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 9d36f7f32..3982bb2d5 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 @@ -53,6 +53,9 @@ public class EmailUpdateHandler implements ICommandUpdateHandler { public EmailUpdateHandler() { } + /** + * See {@link org.eclipse.ice.commands.ICommandUpdateHandler#postUpdate()} + */ @Override public void postUpdate() throws IOException { // Create some properties and setup the default gmail @@ -71,6 +74,7 @@ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(emailAddress, emailPassword); } }); + // Set session to debug just to explicit session.setDebug(true); try { @@ -85,7 +89,6 @@ protected PasswordAuthentication getPasswordAuthentication() { message.setSubject(emailSubject); message.setText(emailText); - // Send message Transport.send(message); logger.info("Email sent successfully"); } catch (MessagingException mex) { diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java index 64aab0339..7418e73a7 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java @@ -32,25 +32,29 @@ */ public class HTTPCommandUpdateHandler implements ICommandUpdateHandler { + // HTTP address to post the update to private String HTTPAddress = ""; - CommandStatus status = null; - + // Message to be posted String message = ""; + /** * Default constructor */ public HTTPCommandUpdateHandler() { } + /** + * Setter for http address to post to + * @param HTTPAddress + */ public void setHTTPAddress(String HTTPAddress) { this.HTTPAddress = HTTPAddress; } - public void postStatus(CommandStatus status) { - this.status = status; - } - + /** + * See {@link org.eclipse.ice.commands.ICommandUpdateHandler#postUpdate()} + */ @Override public void postUpdate() throws IOException { @@ -59,7 +63,7 @@ public void postUpdate() throws IOException { // Setup the parameters to be passed in the post List params = new ArrayList(1); - params.add(new BasicNameValuePair("status", status.toString())); + params.add(new BasicNameValuePair("status", message)); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); // Execute and get the response 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 8185e35b2..ec72e40b8 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 @@ -1,6 +1,14 @@ -/** - * - */ +/******************************************************************************* + * Copyright (c) 2019- UT-Battelle, LLC. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Initial API and implementation and/or initial documentation - + * Jay Jay Billings, Joe Osborn + *******************************************************************************/ package org.eclipse.ice.tests.commands; import static org.junit.Assert.assertEquals; @@ -17,16 +25,49 @@ import org.eclipse.ice.commands.ConnectionAuthorizationHandler; import org.eclipse.ice.commands.ConnectionConfiguration; import org.eclipse.ice.commands.EmailUpdateHandler; +import org.eclipse.ice.commands.HTTPCommandUpdateHandler; import org.eclipse.ice.commands.ICommandUpdateHandler; import org.eclipse.ice.commands.TxtFileConnectionAuthorizationHandler; import org.junit.Test; /** - * @author 4jo + * This class tests local commands in conjunction with a given + * ICommandUpdaterHandler class. The class is not intended to test the Command + * logic, but rather the updater logic associated with the Command. + * + * @author Joe Osborn * */ public class CommandFactoryUpdaterTest { + /** + * This function tests a command with an HTTPUpdater + */ + @Test + public void testCommandWithHTTPUpdater() { + CommandConfiguration commandConfig = setupCommandConfiguration(); + commandConfig.setCommandId(3); + + ConnectionConfiguration connectionConfig = setupConnectionConfiguration(); + + CommandFactory factory = new CommandFactory(); + Command command = null; + try { + command = factory.getCommand(commandConfig, connectionConfig); + } catch (IOException e) { + e.printStackTrace(); + } + + HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); + updater.setHTTPAddress("someaddress.com"); + command.setUpdateHandler(updater); + + CommandStatus status = command.execute(); + + assertEquals(CommandStatus.SUCCESS, status); + + } + /** * This function tests with real files to test an actual job processing with an * email updater attached to the command, so that when the job finishes an email @@ -35,32 +76,12 @@ public class CommandFactoryUpdaterTest { @Test public void testCommandWithEmailUpdater() { - String hostname = CommandFactoryTest.getLocalHostname(); + // Set some things specific to the local command - CommandConfiguration commandConfig = new CommandConfiguration(); - commandConfig.setNumProcs("1"); - commandConfig.setInstallDirectory(""); - 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 - if (System.getProperty("os.name").toLowerCase().contains("win")) { - commandConfig.setExecutable(".\\test_code_execution.ps1"); - commandConfig.setInterpreter("powershell.exe"); - } - commandConfig.addInputFile("someInputFile", "someInputFile.txt"); - commandConfig.setAppendInput(true); - commandConfig.setCommandId(1); - commandConfig.setErrFileName("someLocalErrFile.txt"); - commandConfig.setOutFileName("someLocalOutFile.txt"); - - // Make a default boring connection authorization - ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler(); - handler.setHostname(hostname); - ConnectionConfiguration connectionConfig = new ConnectionConfiguration(); - connectionConfig.setAuthorization(handler); - + CommandConfiguration commandConfig = setupCommandConfiguration(); + commandConfig.setCommandId(4); + ConnectionConfiguration connectionConfig = setupConnectionConfiguration(); + // Get the command CommandFactory factory = new CommandFactory(); Command localCommand = null; @@ -78,11 +99,47 @@ public void testCommandWithEmailUpdater() { assertEquals(CommandStatus.SUCCESS, status); - // If test finishes without an error, email was successfully sent - System.out.println("Finished functional local command"); - } + /** + * Helper function to create and return a local connection configuration + * @return + */ + private ConnectionConfiguration setupConnectionConfiguration() { + String hostname = CommandFactoryTest.getLocalHostname(); + // Make a default boring connection authorization + ConnectionAuthorizationHandler handler = new TxtFileConnectionAuthorizationHandler(); + handler.setHostname(hostname); + ConnectionConfiguration connectionConfig = new ConnectionConfiguration(); + connectionConfig.setAuthorization(handler); + return connectionConfig; + } + + /** + * Helper function to setup and create a local command configuration + * @return + */ + 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.setOS(System.getProperty("os.name")); + commandConfig.setExecutable("./test_code_execution.sh"); + // If it is windows, configure the test to run on windows + if (System.getProperty("os.name").toLowerCase().contains("win")) { + commandConfig.setExecutable(".\\test_code_execution.ps1"); + commandConfig.setInterpreter("powershell.exe"); + } + commandConfig.addInputFile("someInputFile", "someInputFile.txt"); + commandConfig.setAppendInput(true); + commandConfig.setCommandId(1); + commandConfig.setErrFileName("someLocalErrFile.txt"); + commandConfig.setOutFileName("someLocalOutFile.txt"); + + return commandConfig; + } + /** * Sets up the dummy email address via a text file credential for CI * 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 7aacaf6af..ece6edb3d 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 @@ -15,10 +15,6 @@ import org.eclipse.ice.commands.CommandStatus; import org.eclipse.ice.commands.HTTPCommandUpdateHandler; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; /** @@ -30,43 +26,22 @@ */ public class HTTPHandlerTest { + /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception + * This function tests the HTTP post logic for sending a post call with a given + * message, primarily used for Commands posting updates + * + * @throws IOException */ - @After - public void tearDown() throws Exception { - } - @Test public void testHTTPNotificationPostUpdate() throws IOException { - + HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); updater.setHTTPAddress("www.example.com"); - updater.postStatus(CommandStatus.INFOERROR); - + updater.setMessage("job finished with status " + CommandStatus.INFOERROR); + updater.postUpdate(); - + // test is successful if no exception is thrown } From 6ec7db6547095742b8c2f0bd37fea974d9888bad Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Fri, 15 May 2020 13:54:23 -0400 Subject: [PATCH 10/12] Tests pass when setting up a HTTP endpoint Signed-off-by: Joe Osborn --- .../ice/commands/EmailUpdateHandler.java | 18 +++++++------- .../commands/HTTPCommandUpdateHandler.java | 9 +++---- .../commands/CommandFactoryUpdaterTest.java | 24 +++++++++++-------- .../ice/tests/commands/EmailHandlerTest.java | 22 ++++++++--------- .../ice/tests/commands/HTTPHandlerTest.java | 3 +-- 5 files changed, 40 insertions(+), 36 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 3982bb2d5..2f0dba8e3 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 @@ -23,9 +23,9 @@ import javax.mail.internet.MimeMessage; /** - * This class provides email updates for Command job statuses. It takes - * a provided email address, password, etc. and just sends an email to - * the same address + * This class provides email updates for Command job statuses. It takes a + * provided email address, password, etc. and just sends an email to the same + * address * * @author Joe Osborn * @@ -46,7 +46,7 @@ public class EmailUpdateHandler implements ICommandUpdateHandler { // The password for the provided email to be able to send to itself private String emailPassword = ""; - + /** * Default constructor */ @@ -117,25 +117,27 @@ public void setSubject(String emailSubject) { 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} + * * @param emailPassword */ public void setPassword(String emailPassword) { this.emailPassword = emailPassword; } - + /** * See {@link org.eclipse.ice.commands.ICommandUpdateHandler#setMessage(String)} */ @@ -144,6 +146,4 @@ public void setMessage(String message) { this.emailText = message; } - - } diff --git a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java index 7418e73a7..54ec8a416 100644 --- a/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java +++ b/org.eclipse.ice.commands/src/main/java/org/eclipse/ice/commands/HTTPCommandUpdateHandler.java @@ -23,7 +23,6 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; - /** * This class posts Command job updates to an HTTP link via POST * @@ -37,7 +36,7 @@ public class HTTPCommandUpdateHandler implements ICommandUpdateHandler { // Message to be posted String message = ""; - + /** * Default constructor */ @@ -46,6 +45,7 @@ public HTTPCommandUpdateHandler() { /** * Setter for http address to post to + * * @param HTTPAddress */ public void setHTTPAddress(String HTTPAddress) { @@ -58,17 +58,18 @@ public void setHTTPAddress(String HTTPAddress) { @Override public void postUpdate() throws IOException { + // Get a default httpClient to use to send the post HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(HTTPAddress); // Setup the parameters to be passed in the post List params = new ArrayList(1); - params.add(new BasicNameValuePair("status", message)); + params.add(new BasicNameValuePair("body", message)); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); // Execute and get the response try { - HttpResponse response = httpClient.execute(httpPost); + HttpResponse response = httpClient.execute(httpPost); } catch (Exception e) { logger.info("HTTP Post was not successful.", e); throw new IOException(); 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 ec72e40b8..21a39a7bb 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 @@ -45,11 +45,12 @@ public class CommandFactoryUpdaterTest { */ @Test public void testCommandWithHTTPUpdater() { + // Create and setup the command CommandConfiguration commandConfig = setupCommandConfiguration(); commandConfig.setCommandId(3); - + ConnectionConfiguration connectionConfig = setupConnectionConfiguration(); - + CommandFactory factory = new CommandFactory(); Command command = null; try { @@ -58,6 +59,7 @@ public void testCommandWithHTTPUpdater() { e.printStackTrace(); } + // Create an HTTP updater to be added to the command HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); updater.setHTTPAddress("someaddress.com"); command.setUpdateHandler(updater); @@ -65,7 +67,7 @@ public void testCommandWithHTTPUpdater() { CommandStatus status = command.execute(); assertEquals(CommandStatus.SUCCESS, status); - + } /** @@ -73,15 +75,14 @@ public void testCommandWithHTTPUpdater() { * email updater attached to the command, so that when the job finishes an email * is sent */ - @Test + // @Test public void testCommandWithEmailUpdater() { - // Set some things specific to the local command CommandConfiguration commandConfig = setupCommandConfiguration(); commandConfig.setCommandId(4); ConnectionConfiguration connectionConfig = setupConnectionConfiguration(); - + // Get the command CommandFactory factory = new CommandFactory(); Command localCommand = null; @@ -103,6 +104,7 @@ public void testCommandWithEmailUpdater() { /** * Helper function to create and return a local connection configuration + * * @return */ private ConnectionConfiguration setupConnectionConfiguration() { @@ -114,16 +116,18 @@ private ConnectionConfiguration setupConnectionConfiguration() { connectionConfig.setAuthorization(handler); return connectionConfig; } - + /** * Helper function to setup and create a local command configuration + * * @return */ 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 @@ -136,10 +140,10 @@ private CommandConfiguration setupCommandConfiguration() { commandConfig.setCommandId(1); commandConfig.setErrFileName("someLocalErrFile.txt"); commandConfig.setOutFileName("someLocalOutFile.txt"); - + return commandConfig; } - + /** * Sets up the dummy email address via a text file credential for CI * 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 7e1dfa59a..61eb56e14 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 @@ -27,30 +27,30 @@ */ public class EmailHandlerTest { - /** * Tests successful email notification posting + * * @throws IOException */ @Test public void testEmailNotificationPostUpdate() throws IOException { - + // Get a text file with credentials String credFile = "/tmp/email-creds.txt"; - if(System.getProperty("os.name").toLowerCase().contains("win")) + 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)) { + try (Scanner scanner = new Scanner(file)) { email = scanner.next(); password = scanner.next(); host = scanner.next(); } - + EmailUpdateHandler updater = new EmailUpdateHandler(); // Just send an email to itself updater.setEmailAddress(email); @@ -59,17 +59,18 @@ public void testEmailNotificationPostUpdate() throws IOException { updater.setMessage("This is a test updater"); updater.setSubject("This is a test subject"); updater.postUpdate(); - + // If no exception is thrown, it completed correctly } - + /** * Tests bad credential error throwing + * * @throws IOException */ @Test(expected = IOException.class) public void testEmailNotificationPostUpdateBadCreds() throws IOException { - + String email = "badEmail"; String password = "badPassword"; String host = "some.smtp.com"; @@ -83,6 +84,5 @@ public void testEmailNotificationPostUpdateBadCreds() throws IOException { updater.postUpdate(); // Expect exception } - } 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 ece6edb3d..3ef7242b8 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 @@ -26,7 +26,6 @@ */ public class HTTPHandlerTest { - /** * This function tests the HTTP post logic for sending a post call with a given * message, primarily used for Commands posting updates @@ -37,7 +36,7 @@ public class HTTPHandlerTest { public void testHTTPNotificationPostUpdate() throws IOException { HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); - updater.setHTTPAddress("www.example.com"); + updater.setHTTPAddress("someaddress.com"); updater.setMessage("job finished with status " + CommandStatus.INFOERROR); updater.postUpdate(); From e85482521b5bf5bc9e6cc4e79666a1597fb0bb25 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Fri, 15 May 2020 14:00:12 -0400 Subject: [PATCH 11/12] Update README Signed-off-by: Joe Osborn --- org.eclipse.ice.commands/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/org.eclipse.ice.commands/README.md b/org.eclipse.ice.commands/README.md index a2ecf2519..53bccb6b2 100644 --- a/org.eclipse.ice.commands/README.md +++ b/org.eclipse.ice.commands/README.md @@ -45,6 +45,16 @@ ConnectionManagerFactory.getConnectionManager().setRequireStrictHostKeyChecking( Note that this is also a way through which ssh validation can be performed in the package for running actual remote commands/file transfers. +#### EmailHandler test +To test the `EmailUpdateHandler` class, a similar file to the `/tmp/ice-remote-creds.txt` file must be created. Instead, a file in the location `/tmp/ice-email-creds.txt` must exist which contains the following information: + +``` +email@address +password +SmtpHost +``` + +The EmailHandler will send an email from your own address to the same address with updates on when the job finishes. In order for this to happen, the email address must be authenticated. In the case of the tests, and for CI purposes, these authentications are placed in the above text file. For developer use, one could simply enter this information as it is entered in EmailHandlerTest, or you could implement another method (e.g. through use of the text file). #### KeyGen Tests and Connections From 5d9f23ebf9b034b191b61878fbf109e4295ea3ec Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Thu, 11 Jun 2020 12:19:03 -0400 Subject: [PATCH 12/12] Updated tests to work with CI Signed-off-by: Joe Osborn --- .../java/org/eclipse/ice/commands/EmailUpdateHandler.java | 4 ++-- .../eclipse/ice/tests/commands/CommandFactoryUpdaterTest.java | 2 +- .../java/org/eclipse/ice/tests/commands/HTTPHandlerTest.java | 2 +- 3 files changed, 4 insertions(+), 4 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 2f0dba8e3..d3d45d884 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 @@ -41,8 +41,8 @@ public class EmailUpdateHandler implements ICommandUpdateHandler { // The subject of the message private String emailSubject = "Commands API Message"; - // The host smtp server for the commands api email address - private String emailHost = "smtp.gmail.com"; + // 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 = ""; 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 21a39a7bb..23e279a74 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 @@ -75,7 +75,7 @@ public void testCommandWithHTTPUpdater() { * email updater attached to the command, so that when the job finishes an email * is sent */ - // @Test + @Test public void testCommandWithEmailUpdater() { // Set some things specific to the local command 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 3ef7242b8..5d8f012fa 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 @@ -36,7 +36,7 @@ public class HTTPHandlerTest { public void testHTTPNotificationPostUpdate() throws IOException { HTTPCommandUpdateHandler updater = new HTTPCommandUpdateHandler(); - updater.setHTTPAddress("someaddress.com"); + updater.setHTTPAddress("https://225eee153c45329a1bcedd6da637643f.m.pipedream.net"); updater.setMessage("job finished with status " + CommandStatus.INFOERROR); updater.postUpdate();