*/
package org.sonar.api.notifications;
-import java.io.Serializable;
-import java.util.HashMap;
-
+import com.google.common.collect.Maps;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
-import com.google.common.collect.Maps;
+import java.io.Serializable;
+import java.util.HashMap;
/**
* <p>
* knowledge of the possible ways to be delivered (see {@link NotificationChannel}) or of the users who should
* receive it (see {@link NotificationDispatcher}).
* </p>
+ * <p>
+ * When creating a new notification, it is strongly advised to give a default message that can be used by channels
+ * that don't want to specifically format messages for different notification types. You can use
+ * {@link Notification#setDefaultMessage(String)} for that purpose.
+ * </p>
*
* @since 2.10
*/
public class Notification implements Serializable {
+ private static final String DEFAULT_MESSAGE_KEY = "default_message";
+
private String type;
private HashMap<String, String> fields = Maps.newHashMap(); // NOSONAR false-positive due to serialization : usage of HashMap instead of
return type;
}
+ /**
+ * <p>
+ * When creating a new notification, it is strongly advised to give a default message that can be
+ * used by channels that don't want to specifically format messages for different notification types.
+ * </p>
+ * <p>
+ * This method is equivalent to setting a value for the field {@link #DEFAULT_MESSAGE_KEY} with
+ * {@link #setFieldValue(String, String)}.
+ * </p>
+ *
+ * @since 3.5
+ */
+ public Notification setDefaultMessage(String value) {
+ setFieldValue(DEFAULT_MESSAGE_KEY, value);
+ return this;
+ }
+
+ /**
+ * Returns the default message to display for this notification.
+ */
+ public String getDefaultMessage() {
+ String defaultMessage = getFieldValue(DEFAULT_MESSAGE_KEY);
+ if (defaultMessage == null) {
+ defaultMessage = this.toString();
+ }
+ return defaultMessage;
+ }
+
/**
* Adds a field (kind of property) to the notification
*
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.server.notifications;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.notifications.Notification;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class NotificationTest {
+
+ private Notification notification;
+
+ @Before
+ public void init() {
+ notification = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "42");
+ }
+
+ @Test
+ public void shouldReturnType() {
+ assertThat(notification.getType()).isEqualTo("alerts");
+ }
+
+ @Test
+ public void shouldReturnDefaultMessage() {
+ assertThat(notification.getDefaultMessage()).isEqualTo("There are new alerts");
+ }
+
+ @Test
+ public void shouldReturnToStringIfDefaultMessageNotSet() {
+ notification = new Notification("alerts").setFieldValue("alertCount", "42");
+ System.out.println(notification);
+ assertThat(notification.getDefaultMessage()).contains("type=alerts");
+ assertThat(notification.getDefaultMessage()).contains("fields={alertCount=42}");
+ }
+
+ @Test
+ public void shouldReturnField() {
+ assertThat(notification.getFieldValue("alertCount")).isEqualTo("42");
+ assertThat(notification.getFieldValue("fake")).isNull();
+
+ // default message is stored as field as well
+ assertThat(notification.getFieldValue("default_message")).isEqualTo("There are new alerts");
+ }
+
+ @Test
+ public void shouldEqual() {
+ assertThat(notification.equals("")).isFalse();
+ assertThat(notification.equals(null)).isFalse();
+ assertThat(notification.equals(notification)).isTrue();
+
+ Notification otherNotif = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "42");
+ assertThat(otherNotif).isEqualTo(notification);
+
+ otherNotif = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "15000");
+ assertThat(otherNotif).isNotEqualTo(notification);
+ }
+
+}