Java Code Examples for com.icegreen.greenmail.util.GreenMailUtil#getHeaders()

The following examples show how to use com.icegreen.greenmail.util.GreenMailUtil#getHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestEmailNotifier.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmailNotifierRunError() throws Exception {

  EmailNotifier emailNotifier = new EmailNotifier("x", "x", "0", runtimeInfo, emailSender, ImmutableList.of("foo", "bar"),
    ImmutableSet.of("RUN_ERROR"));

  PipelineState runningState = new PipelineStateImpl("x", "x", "0", PipelineStatus.RUNNING, "Running",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  PipelineState runErrorState = new PipelineStateImpl("x", "x", "0", PipelineStatus.RUN_ERROR, "Run Error",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  emailNotifier.onStateChange(runningState, runErrorState, "", null, null);

  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers != null);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - x - ERROR"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(server.getReceivedMessages()[0]));
}
 
Example 2
Source File: TestEmailNotifier.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmailNotifierStartError() throws Exception {

  EmailNotifier emailNotifier = new EmailNotifier("x", "x","0", runtimeInfo, emailSender, ImmutableList.of("foo", "bar")
    , ImmutableSet.of("START_ERROR"));

  PipelineState startingState = new PipelineStateImpl("x", "x", "0", PipelineStatus.STARTING, "Starting",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  PipelineState startErrorState = new PipelineStateImpl("x", "x", "0", PipelineStatus.START_ERROR, "Start Error",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  emailNotifier.onStateChange(startingState, startErrorState, "", null, null);

  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers != null);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - x - ERROR"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(server.getReceivedMessages()[0]));
}
 
Example 3
Source File: TestEmailNotifier.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmailNotifierFinished() throws Exception {

  EmailNotifier emailNotifier = new EmailNotifier("x", "x","0", runtimeInfo, emailSender, ImmutableList.of("foo", "bar"),
    ImmutableSet.of("FINISHED"));

  PipelineState runningState = new PipelineStateImpl("x", "x", "0", PipelineStatus.RUNNING, "Running",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  PipelineState finishedState = new PipelineStateImpl("x", "x", "0", PipelineStatus.FINISHED, "Finished",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  emailNotifier.onStateChange(runningState, finishedState, "", null, null);

  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers != null);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - x - FINISHED"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(server.getReceivedMessages()[0]));
}
 
Example 4
Source File: TestEmailNotifier.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmailNotifierStopped() throws Exception {

  EmailNotifier emailNotifier = new EmailNotifier("x", "x","0", runtimeInfo, emailSender, ImmutableList.of("foo", "bar"),
    ImmutableSet.of("STOPPED"));

  PipelineState stoppingState = new PipelineStateImpl("x", "x", "0", PipelineStatus.STOPPING, "Stopping",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  PipelineState stoppedState = new PipelineStateImpl("x", "x", "0", PipelineStatus.STOPPED, "Stopped",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  emailNotifier.onStateChange(stoppingState, stoppedState, "", null, null);

  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers != null);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - x - STOPPED"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(server.getReceivedMessages()[0]));
}
 
Example 5
Source File: TestEmailNotifier.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmailNotifierDisconnected() throws Exception {

  EmailNotifier emailNotifier = new EmailNotifier("x", "x","0", runtimeInfo, emailSender, ImmutableList.of("foo", "bar"),
    ImmutableSet.of("DISCONNECTED"));

  PipelineState disconnectingState = new PipelineStateImpl("x", "x", "0", PipelineStatus.DISCONNECTING, "Disconnecting",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  PipelineState disconnectedState = new PipelineStateImpl("x", "x", "0", PipelineStatus.DISCONNECTED, "Disconnected",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  emailNotifier.onStateChange(disconnectingState, disconnectedState, "", null, null);

  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers != null);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - x - DISCONNECTED"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(server.getReceivedMessages()[0]));
}
 
Example 6
Source File: TestEmailNotifier.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmailNotifierConnecting() throws Exception {

  EmailNotifier emailNotifier = new EmailNotifier("x", "x","0", runtimeInfo, emailSender, ImmutableList.of("foo", "bar"),
    ImmutableSet.of("CONNECTING"));

  PipelineState disconnectedState = new PipelineStateImpl("x", "x", "0", PipelineStatus.DISCONNECTED, "Disconnected",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  PipelineState connectingState = new PipelineStateImpl("x", "x", "0", PipelineStatus.CONNECTING, "Connecting",
    System.currentTimeMillis(), new HashMap<String, Object>(), ExecutionMode.STANDALONE, "", 0, 0);
  emailNotifier.onStateChange(disconnectedState, connectingState, "", null, null);

  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers != null);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - x - CONNECTING"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(server.getReceivedMessages()[0]));
}
 
Example 7
Source File: TestStandaloneRunner.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testPipelineStoppedWithMail() throws Exception {
  Runner runner = pipelineManager.getRunner( TestUtil.PIPELINE_WITH_EMAIL, "0");
  runner.start(new StartPipelineContextBuilder("admin").build());
  waitForState(runner, PipelineStatus.RUNNING);
  runner.getRunner(AsyncRunner.class).getDelegatingRunner().prepareForStop("admin");
  runner.getRunner(AsyncRunner.class).getDelegatingRunner().stop("admin");
  waitForState(runner, PipelineStatus.STOPPED);
  //wait for email
  GreenMail mailServer = TestUtil.TestRuntimeModule.getMailServer();
  while(mailServer.getReceivedMessages().length < 1) {
    ThreadUtil.sleep(100);
  }
  String headers = GreenMailUtil.getHeaders(mailServer.getReceivedMessages()[0]);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - " +
      TestUtil.PIPELINE_TITLE_WITH_EMAIL + " - STOPPED"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(mailServer.getReceivedMessages()[0]));

  mailServer.reset();
}
 
Example 8
Source File: TestStandaloneRunner.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testPipelineFinishWithMail() throws Exception {
  Runner runner = pipelineManager.getRunner( TestUtil.PIPELINE_WITH_EMAIL, "0");
  runner.start(new StartPipelineContextBuilder("admin").build());
  waitForState(runner, PipelineStatus.RUNNING);
  assertNull(runner.getState().getMetrics());
  TestUtil.EMPTY_OFFSET = true;
  waitForState(runner, PipelineStatus.FINISHED);
  assertNotNull(runner.getState().getMetrics());
  //wait for email
  GreenMail mailServer = TestUtil.TestRuntimeModule.getMailServer();
  while(mailServer.getReceivedMessages().length < 1) {
    ThreadUtil.sleep(100);
  }
  String headers = GreenMailUtil.getHeaders(mailServer.getReceivedMessages()[0]);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: StreamsSets Data Collector Alert - " +
      TestUtil.PIPELINE_TITLE_WITH_EMAIL + " - FINISHED"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertNotNull(GreenMailUtil.getBody(mailServer.getReceivedMessages()[0]));

  mailServer.reset();
}
 
Example 9
Source File: TestEmailSender.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendEmailNoAuth() throws Exception {
  Configuration conf = new Configuration();
  conf.set("mail.smtp.host", "localhost");
  conf.set("mail.smtp.port", Integer.toString(server.getSmtp().getPort()));
  EmailSender sender = new EmailSender(conf);
  sender.send(ImmutableList.of("foo", "bar"), "SUBJECT", "BODY");
  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: SUBJECT"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertTrue(headers.contains("Content-Type: text/html; charset=UTF-8"));
  Assert.assertEquals("BODY", GreenMailUtil.getBody(server.getReceivedMessages()[0]));
  server.reset();
}
 
Example 10
Source File: TestEmailSender.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentEmailWithAttachment() throws Exception {
  Configuration conf = new Configuration();
  conf.set("mail.smtp.host", "localhost");
  conf.set("mail.smtp.port", Integer.toString(server.getSmtp().getPort()));
  conf.set("mail.transport.protocol", "smtp");
  conf.set("mail.smtp.auth", "false");
  EmailSender sender = new EmailSender(conf);
  sender.send(ImmutableList.of("foo", "bar"), "SUBJECT", "BODY",
      Arrays.asList(
          new Attachment("hello.txt", new ByteArrayInputStream("hello world".getBytes()), "text/*"),
          new Attachment("world.txt", new ByteArrayInputStream("world hello".getBytes()), "text/*")
      )
  );
  String headers = GreenMailUtil.getHeaders(server.getReceivedMessages()[0]);
  Assert.assertTrue(headers.contains("To: foo, bar"));
  Assert.assertTrue(headers.contains("Subject: SUBJECT"));
  Assert.assertTrue(headers.contains("From: sdc@localhost"));
  Assert.assertTrue(headers.contains("Content-Type: multipart/mixed"));
  Assert.assertEquals("BODY", ((MimeMultipart)(server.getReceivedMessages()[0]).getContent()).getBodyPart(0).getContent());
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  ((MimeMultipart)(server.getReceivedMessages()[0]).getContent()).getBodyPart(1).getDataHandler().writeTo(os);
  Assert.assertEquals("hello world", os.toString());
  Assert.assertEquals("hello.txt", ((MimeMultipart)(server.getReceivedMessages()[0]).getContent()).getBodyPart(1).getDataHandler().getDataSource().getName());
  os.reset();
  ((MimeMultipart)(server.getReceivedMessages()[0]).getContent()).getBodyPart(2).getDataHandler().writeTo(os);
  Assert.assertEquals("world hello", os.toString());
  Assert.assertEquals("world.txt", ((MimeMultipart)(server.getReceivedMessages()[0]).getContent()).getBodyPart(2).getDataHandler().getDataSource().getName());
  server.reset();
}