*/
package org.sonar.server.issue.notification;
+import com.google.common.base.Strings;
import org.sonar.api.component.Component;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.issue.internal.FieldDiffs;
import org.sonar.api.notifications.Notification;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.io.Serializable;
for (Map.Entry<String, FieldDiffs.Diff> entry : currentChange.diffs().entrySet()) {
String type = entry.getKey();
FieldDiffs.Diff diff = entry.getValue();
- Serializable newValue = diff.newValue();
- Serializable oldValue = diff.oldValue();
- setFieldValue("old." + type, oldValue != null ? oldValue.toString() : null);
- setFieldValue("new." + type, newValue != null ? newValue.toString() : null);
+ setFieldValue("old." + type, neverEmptySerializableToString(diff.oldValue()));
+ setFieldValue("new." + type, neverEmptySerializableToString(diff.newValue()));
}
}
return this;
}
return this;
}
+
+ @CheckForNull
+ private static String neverEmptySerializableToString(@Nullable Serializable s) {
+ return s != null ? Strings.emptyToNull(s.toString()) : null;
+ }
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.issue.notification;
+
+import org.junit.Test;
+import org.sonar.api.issue.internal.DefaultIssue;
+import org.sonar.api.issue.internal.FieldDiffs;
+import org.sonar.core.component.ComponentDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class IssueChangeNotificationTest {
+
+ IssueChangeNotification notification = new IssueChangeNotification();
+
+ @Test
+ public void set_issue() throws Exception {
+ DefaultIssue issue = new DefaultIssue()
+ .setKey("ABCD")
+ .setReporter("arthur")
+ .setAssignee("simon")
+ .setMessage("Remove this useless method")
+ .setComponentKey("MyService")
+ .setCurrentChange(new FieldDiffs().setDiff("resolution", "FALSE-POSITIVE", "FIXED"));
+
+ IssueChangeNotification result = notification.setIssue(issue);
+
+ assertThat(result.getFieldValue("key")).isEqualTo("ABCD");
+ assertThat(result.getFieldValue("reporter")).isEqualTo("arthur");
+ assertThat(result.getFieldValue("assignee")).isEqualTo("simon");
+ assertThat(result.getFieldValue("message")).isEqualTo("Remove this useless method");
+ assertThat(result.getFieldValue("componentKey")).isEqualTo("MyService");
+ assertThat(result.getFieldValue("old.resolution")).isEqualTo("FALSE-POSITIVE");
+ assertThat(result.getFieldValue("new.resolution")).isEqualTo("FIXED");
+ }
+
+ @Test
+ public void set_issue_with_current_change_having_no_old_value() throws Exception {
+ DefaultIssue issue = new DefaultIssue()
+ .setKey("ABCD")
+ .setReporter("arthur")
+ .setAssignee("simon")
+ .setMessage("Remove this useless method")
+ .setComponentKey("MyService");
+
+ IssueChangeNotification result = notification.setIssue(issue.setCurrentChange(new FieldDiffs().setDiff("resolution", null, "FIXED")));
+ assertThat(result.getFieldValue("old.resolution")).isNull();
+ assertThat(result.getFieldValue("new.resolution")).isEqualTo("FIXED");
+
+ result = notification.setIssue(issue.setCurrentChange(new FieldDiffs().setDiff("resolution", "", "FIXED")));
+ assertThat(result.getFieldValue("old.resolution")).isNull();
+ assertThat(result.getFieldValue("new.resolution")).isEqualTo("FIXED");
+ }
+
+ @Test
+ public void set_issue_with_current_change_having_no_new_value() throws Exception {
+ DefaultIssue issue = new DefaultIssue()
+ .setKey("ABCD")
+ .setReporter("arthur")
+ .setAssignee("simon")
+ .setMessage("Remove this useless method")
+ .setComponentKey("MyService");
+
+ IssueChangeNotification result = notification.setIssue(issue.setCurrentChange(new FieldDiffs().setDiff("assignee", "john", null)));
+ assertThat(result.getFieldValue("old.assignee")).isEqualTo("john");
+ assertThat(result.getFieldValue("new.assignee")).isNull();
+
+ result = notification.setIssue(issue.setCurrentChange(new FieldDiffs().setDiff("assignee", "john", "")));
+ assertThat(result.getFieldValue("old.assignee")).isEqualTo("john");
+ assertThat(result.getFieldValue("new.assignee")).isNull();
+ }
+
+ @Test
+ public void set_project() throws Exception {
+ IssueChangeNotification result = notification.setProject(new ComponentDto().setKey("MyService").setLongName("My Service"));
+ assertThat(result.getFieldValue("projectKey")).isEqualTo("MyService");
+ assertThat(result.getFieldValue("projectName")).isEqualTo("My Service");
+ }
+
+ @Test
+ public void set_component() throws Exception {
+ IssueChangeNotification result = notification.setComponent(new ComponentDto().setKey("MyService").setLongName("My Service"));
+ assertThat(result.getFieldValue("componentName")).isEqualTo("My Service");
+ }
+
+ @Test
+ public void set_change_author_login() throws Exception {
+ IssueChangeNotification result = notification.setChangeAuthorLogin("stephane");
+ assertThat(result.getFieldValue("changeAuthor")).isEqualTo("stephane");
+ }
+
+ @Test
+ public void set_rule_name() throws Exception {
+ IssueChangeNotification result = notification.setRuleName("Xoo Rule");
+ assertThat(result.getFieldValue("ruleName")).isEqualTo("Xoo Rule");
+ }
+
+ @Test
+ public void setComment() throws Exception {
+ IssueChangeNotification result = notification.setComment("My comment");
+ assertThat(result.getFieldValue("comment")).isEqualTo("My comment");
+ }
+}
assertThat(email.getFrom()).isNull();
}
+ @Test
+ public void email_should_display_resolution_change() throws Exception {
+ Notification notification = generateNotification()
+ .setFieldValue("old.resolution", "FALSE-POSITIVE")
+ .setFieldValue("new.resolution", "FIXED");
+
+ EmailMessage email = template.format(notification);
+ assertThat(email.getMessageId()).isEqualTo("issue-changes/ABCDE");
+ assertThat(email.getSubject()).isEqualTo("Struts, change on issue #ABCDE");
+
+ String message = email.getMessage();
+ String expected = Resources.toString(Resources.getResource(
+ "org/sonar/server/issue/notification/IssueChangesEmailTemplateTest/email_should_display_resolution_change.txt"),
+ Charsets.UTF_8
+ );
+ expected = StringUtils.remove(expected, '\r');
+ assertThat(message).isEqualTo(expected);
+ assertThat(email.getFrom()).isNull();
+ }
+
@Test
public void display_component_key_if_no_component_name() throws Exception {
Notification notification = generateNotification()