From 4dbed8827df656b19e47bd350b99c1756fc7819a Mon Sep 17 00:00:00 2001 From: Matthew Casperson Date: Sat, 6 Oct 2018 12:09:02 +1000 Subject: [PATCH] Post 34 --- pom.xml | 7 ++++- serverless.yml | 5 ++++ src/main/java/com/octopus/LambdaEntry.java | 34 +++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 17e13aa6..7e80e11f 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2.3.1 3.1.0 1.2.0 - 1.11.305 + 1.11.422 2.6 @@ -61,6 +61,11 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd"> aws-java-sdk-lambda ${aws.sdk.version} + + com.amazonaws + aws-java-sdk-ses + ${aws.sdk.version} + commons-io commons-io diff --git a/serverless.yml b/serverless.yml index e6732885..f03499c6 100644 --- a/serverless.yml +++ b/serverless.yml @@ -5,6 +5,11 @@ provider: name: aws runtime: java8 region: us-east-1 + iamRoleStatements: + - Effect: Allow + Resource: "*" + Action: + - ses:SendEmail package: artifact: target/webdrivertraining-1.0-SNAPSHOT.jar diff --git a/src/main/java/com/octopus/LambdaEntry.java b/src/main/java/com/octopus/LambdaEntry.java index 8280cb6a..ae3e421e 100644 --- a/src/main/java/com/octopus/LambdaEntry.java +++ b/src/main/java/com/octopus/LambdaEntry.java @@ -1,5 +1,9 @@ package com.octopus; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.simpleemail.AmazonSimpleEmailService; +import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder; +import com.amazonaws.services.simpleemail.model.*; import org.apache.commons.io.FileUtils; import java.io.*; import java.net.MalformedURLException; @@ -15,17 +19,19 @@ public class LambdaEntry { private static final String CHROME_DRIVER = "https://s3.amazonaws.com/webdriver-testing-resources/chromedriver_linux64.zip"; - public String runCucumber(final String feature) throws Throwable { + public String runCucumber(String feature) throws Throwable { File driverDirectory = null; File chromeDirectory = null; File outputFile = null; + File txtOutputFile = null; File featureFile = null; try { driverDirectory = downloadChromeDriver(); chromeDirectory = downloadChromeHeadless(); outputFile = Files.createTempFile("output", ".json").toFile(); + txtOutputFile = Files.createTempFile("output", ".txt").toFile(); featureFile = writeFeatureToFile(feature); cucumber.api.cli.Main.run( @@ -33,14 +39,19 @@ public String runCucumber(final String feature) throws Throwable { "--monochrome", "--glue", "com.octopus.decoratorbase", "--format", "json:" + outputFile.toString(), + "--format", "pretty:" + txtOutputFile.toString(), featureFile.getAbsolutePath()}, Thread.currentThread().getContextClassLoader()); + sendEmail("admin@matthewcasperson.com", FileUtils.readFileToString(txtOutputFile, Charset.defaultCharset())); + return FileUtils.readFileToString(outputFile, Charset.defaultCharset()); + } finally { FileUtils.deleteQuietly(driverDirectory); FileUtils.deleteQuietly(chromeDirectory); FileUtils.deleteQuietly(outputFile); + FileUtils.deleteQuietly(txtOutputFile); FileUtils.deleteQuietly(featureFile); } } @@ -108,4 +119,25 @@ private File writeFeatureToFile(final String feature) throws IOException { } return featureFile; } + + private void sendEmail(final String to, final String results) { + try { + final AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() + .withRegion(Regions.US_EAST_1).build(); + + final SendEmailRequest request = new SendEmailRequest() + .withDestination(new Destination() + .withToAddresses(to)) + .withMessage(new Message() + .withBody(new Body() + .withText(new Content() + .withCharset("UTF-8").withData(results))) + .withSubject(new Content() + .withCharset("UTF-8").withData("WebDriver Test Results"))) + .withSource("admin@matthewcasperson.com"); + client.sendEmail(request); + } catch (final Exception ex) { + System.out.println("The email was not sent. Error message: " + ex.getMessage()); + } + } } \ No newline at end of file