Skip to content

Commit

Permalink
Fix message backlog in default body template in FormattedEmailAlertSe…
Browse files Browse the repository at this point in the history
…nder

Fixes #1163
  • Loading branch information
Jochen Schalanda committed Jun 2, 2015
1 parent 5b44e6b commit 4bdb8ca
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
Expand Up @@ -33,6 +33,8 @@
import java.util.List;
import java.util.Map;

import static com.google.common.base.MoreObjects.firstNonNull;

public class FormattedEmailAlertSender extends StaticEmailAlertSender implements AlertSender {
public static final String bodyTemplate = "##########\n" +
"Alert Description: ${check_result.resultDescription}\n" +
Expand All @@ -44,12 +46,14 @@ public class FormattedEmailAlertSender extends StaticEmailAlertSender implements
"\n" +
"Triggered condition: ${check_result.triggeredCondition}\n" +
"##########\n\n" +
"${if backlog_size > 0}" +
"${if backlog_empty}" +
"<No backlog>\n" +
"${else}" +
"Last messages accounting for this alert:\n" +
"${foreach backlog message}\n" +
"${foreach backlog message}" +
"${message}\n" +
"${end}\n" +
"${else}<No backlog.>${end}\n" +
"${end}" +
"${end}" +
"\n";

private final Engine engine = new Engine();
Expand Down Expand Up @@ -104,13 +108,11 @@ private Map<String, Object> getModel(Stream stream, AlertCondition.CheckResult c
model.put("check_result", checkResult);
model.put("stream_url", buildStreamDetailsURL(configuration.getWebInterfaceUri(), checkResult, stream));

if (backlog != null) {
model.put("backlog", backlog);
model.put("backlog_size", backlog.size());
} else {
model.put("backlog", Collections.<Message>emptyList());
model.put("backlog_size", 0);
}
final List<Message> messages = firstNonNull(backlog, Collections.<Message>emptyList());
model.put("backlog", messages);
model.put("backlog_size", messages.size());
model.put("backlog_empty", messages.isEmpty());

return model;
}
}
Expand Up @@ -205,4 +205,51 @@ public URI getWebInterfaceUri() {

assertThat(body).contains("Stream URL: Please configure 'transport_email_web_interface_url' in your Graylog configuration file.");
}

@Test
public void defaultBodyTemplateDoesNotShowBacklogIfBacklogIsEmpty() throws Exception {
FormattedEmailAlertSender emailAlertSender = new FormattedEmailAlertSender(new EmailConfiguration(), mockStreamRuleService,
mockUserService, mockNotificationService, mockNodeId);

Stream stream = mock(Stream.class);
when(stream.getId()).thenReturn("123456");
when(stream.getTitle()).thenReturn("Stream Title");

AlertCondition alertCondition = mock(AlertCondition.class);

AlertCondition.CheckResult checkResult = mock(AbstractAlertCondition.CheckResult.class);
when(checkResult.getTriggeredAt()).thenReturn(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
when(checkResult.getTriggeredCondition()).thenReturn(alertCondition);

String body = emailAlertSender.buildBody(stream, checkResult, Collections.<Message>emptyList());

assertThat(body)
.contains("<No backlog>\n")
.doesNotContain("Last messages accounting for this alert:\n");
}

@Test
public void defaultBodyTemplateShowsBacklogIfBacklogIsNotEmpty() throws Exception {
FormattedEmailAlertSender emailAlertSender = new FormattedEmailAlertSender(new EmailConfiguration(), mockStreamRuleService,
mockUserService, mockNotificationService, mockNodeId);

Stream stream = mock(Stream.class);
when(stream.getId()).thenReturn("123456");
when(stream.getTitle()).thenReturn("Stream Title");

AlertCondition alertCondition = mock(AlertCondition.class);

AlertCondition.CheckResult checkResult = mock(AbstractAlertCondition.CheckResult.class);
when(checkResult.getTriggeredAt()).thenReturn(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
when(checkResult.getTriggeredCondition()).thenReturn(alertCondition);

Message message = new Message("Test", "source", new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC));
String body = emailAlertSender.buildBody(stream, checkResult, Collections.singletonList(message));

assertThat(body)
.doesNotContain("<No backlog>\n")
.containsSequence(
"Last messages accounting for this alert:\n",
message.toString());
}
}

0 comments on commit 4bdb8ca

Please sign in to comment.