2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.emailnotifications.reviews;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.api.database.model.User;
25 import org.sonar.api.notifications.Notification;
26 import org.sonar.api.platform.EmailSettings;
27 import org.sonar.api.security.UserFinder;
28 import org.sonar.plugins.emailnotifications.api.EmailMessage;
30 import static org.hamcrest.Matchers.is;
31 import static org.hamcrest.Matchers.nullValue;
32 import static org.junit.Assert.assertThat;
33 import static org.mockito.Matchers.eq;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 public class ReviewEmailTemplateTest {
39 private ReviewEmailTemplate template;
43 EmailSettings configuration = mock(EmailSettings.class);
44 when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
45 UserFinder userFinder = mock(UserFinder.class);
46 when(userFinder.findByLogin(eq("freddy.mallet"))).thenReturn(new User().setName("Freddy Mallet"));
47 when(userFinder.findByLogin(eq("simon.brandhof"))).thenReturn(new User().setName("Simon Brandhof"));
48 when(userFinder.findByLogin(eq("evgeny.mandrikov"))).thenReturn(new User().setName("Evgeny Mandrikov"));
49 template = new ReviewEmailTemplate(configuration, userFinder);
58 * Resource: org.sonar.server.ui.DefaultPages
60 * Utility classes should not have a public or default constructor.
63 * This is my first comment
65 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
69 public void shouldFormatCommentAdded() {
70 Notification notification = new Notification("review-changed")
71 .setFieldValue("reviewId", "1")
72 .setFieldValue("project", "Sonar")
73 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
74 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
75 .setFieldValue("author", "freddy.mallet")
76 .setFieldValue("old.comment", null)
77 .setFieldValue("new.comment", "This is my first comment");
78 EmailMessage message = template.format(notification);
79 assertThat(message.getMessageId(), is("review/1"));
80 assertThat(message.getSubject(), is("Review #1"));
81 assertThat(message.getFrom(), is("Freddy Mallet"));
82 assertThat(message.getMessage(), is("" +
84 "Resource: org.sonar.server.ui.DefaultPages\n" +
86 "Utility classes should not have a public or default constructor.\n" +
89 " This is my first comment\n" +
91 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
100 * Resource: org.sonar.server.ui.DefaultPages
102 * Utility classes should not have a public or default constructor.
105 * This is another comment
107 * This is my first comment
109 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
113 public void shouldFormatCommentEdited() {
114 Notification notification = new Notification("review-changed")
115 .setFieldValue("reviewId", "1")
116 .setFieldValue("project", "Sonar")
117 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
118 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
119 .setFieldValue("author", "freddy.mallet")
120 .setFieldValue("old.comment", "This is my first comment")
121 .setFieldValue("new.comment", "This is another comment");
122 EmailMessage message = template.format(notification);
123 assertThat(message.getMessageId(), is("review/1"));
124 assertThat(message.getSubject(), is("Review #1"));
125 assertThat(message.getFrom(), is("Freddy Mallet"));
126 assertThat(message.getMessage(), is("" +
128 "Resource: org.sonar.server.ui.DefaultPages\n" +
130 "Utility classes should not have a public or default constructor.\n" +
133 " This is another comment\n" +
135 " This is my first comment\n" +
137 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
143 * From: Freddy Mallet
146 * Resource: org.sonar.server.ui.DefaultPages
148 * Utility classes should not have a public or default constructor.
150 * Comment deleted, was:
151 * This is deleted comment
153 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
157 public void shouldFormatCommentDeleted() {
158 Notification notification = new Notification("review-changed")
159 .setFieldValue("reviewId", "1")
160 .setFieldValue("project", "Sonar")
161 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
162 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
163 .setFieldValue("old.comment", "This is deleted comment")
164 .setFieldValue("new.comment", null)
165 .setFieldValue("author", "freddy.mallet");
166 EmailMessage message = template.format(notification);
167 assertThat(message.getMessageId(), is("review/1"));
168 assertThat(message.getSubject(), is("Review #1"));
169 assertThat(message.getFrom(), is("Freddy Mallet"));
170 assertThat(message.getMessage(), is("" +
172 "Resource: org.sonar.server.ui.DefaultPages\n" +
174 "Utility classes should not have a public or default constructor.\n" +
176 "Comment deleted, was:\n" +
177 " This is deleted comment\n" +
179 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
185 * From: Freddy Mallet
188 * Resource: org.sonar.server.ui.DefaultPages
190 * Utility classes should not have a public or default constructor.
192 * Assignee: Evgeny Mandrikov
194 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
198 public void shouldFormatAssigneed() {
199 Notification notification = new Notification("review-changed")
200 .setFieldValue("reviewId", "1")
201 .setFieldValue("project", "Sonar")
202 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
203 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
204 .setFieldValue("author", "freddy.mallet")
205 .setFieldValue("old.assignee", null)
206 .setFieldValue("new.assignee", "evgeny.mandrikov");
207 EmailMessage message = template.format(notification);
208 assertThat(message.getMessageId(), is("review/1"));
209 assertThat(message.getSubject(), is("Review #1"));
210 assertThat(message.getFrom(), is("Freddy Mallet"));
211 assertThat(message.getMessage(), is("" +
213 "Resource: org.sonar.server.ui.DefaultPages\n" +
215 "Utility classes should not have a public or default constructor.\n" +
217 "Assignee: Evgeny Mandrikov\n" +
219 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
225 * From: Freddy Mallet
228 * Resource: org.sonar.server.ui.DefaultPages
230 * Utility classes should not have a public or default constructor.
232 * Assignee: Simon Brandhof (was Evgeny Mandrikov)
234 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
238 public void shouldFormatAssigneedToAnotherPerson() {
239 Notification notification = new Notification("review-changed")
240 .setFieldValue("reviewId", "1")
241 .setFieldValue("project", "Sonar")
242 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
243 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
244 .setFieldValue("author", "freddy.mallet")
245 .setFieldValue("old.assignee", "evgeny.mandrikov")
246 .setFieldValue("new.assignee", "simon.brandhof");
247 EmailMessage message = template.format(notification);
248 assertThat(message.getMessageId(), is("review/1"));
249 assertThat(message.getSubject(), is("Review #1"));
250 assertThat(message.getFrom(), is("Freddy Mallet"));
251 assertThat(message.getMessage(), is("" +
253 "Resource: org.sonar.server.ui.DefaultPages\n" +
255 "Utility classes should not have a public or default constructor.\n" +
257 "Assignee: Simon Brandhof (was Evgeny Mandrikov)\n" +
259 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
265 * From: Freddy Mallet
268 * Resource: org.sonar.server.ui.DefaultPages
270 * Utility classes should not have a public or default constructor.
272 * Assignee: (was Simon Brandhof)
274 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
278 public void shouldFormatUnassigned() {
279 Notification notification = new Notification("review-changed")
280 .setFieldValue("reviewId", "1")
281 .setFieldValue("project", "Sonar")
282 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
283 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
284 .setFieldValue("author", "freddy.mallet")
285 .setFieldValue("old.assignee", "simon.brandhof")
286 .setFieldValue("new.assignee", null);
287 EmailMessage message = template.format(notification);
288 assertThat(message.getMessageId(), is("review/1"));
289 assertThat(message.getSubject(), is("Review #1"));
290 assertThat(message.getFrom(), is("Freddy Mallet"));
291 assertThat(message.getMessage(), is("" +
293 "Resource: org.sonar.server.ui.DefaultPages\n" +
295 "Utility classes should not have a public or default constructor.\n" +
297 "Assignee: (was Simon Brandhof)\n" +
299 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
308 * Resource: org.sonar.server.ui.DefaultPages
310 * Utility classes should not have a public or default constructor.
312 * Status: CLOSED (was OPEN)
314 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
318 public void shouldFormatClosed() {
319 Notification notification = new Notification("review-changed")
320 .setFieldValue("reviewId", "1")
321 .setFieldValue("project", "Sonar")
322 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
323 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
324 .setFieldValue("old.status", "OPEN")
325 .setFieldValue("new.status", "CLOSED");
326 EmailMessage message = template.format(notification);
327 assertThat(message.getMessageId(), is("review/1"));
328 assertThat(message.getSubject(), is("Review #1"));
329 assertThat(message.getFrom(), nullValue());
330 assertThat(message.getMessage(), is("" +
332 "Resource: org.sonar.server.ui.DefaultPages\n" +
334 "Utility classes should not have a public or default constructor.\n" +
336 "Status: CLOSED (was OPEN)\n" +
338 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
344 * From: Simon Brandhof
347 * Resource: org.sonar.server.ui.DefaultPages
349 * Utility classes should not have a public or default constructor.
351 * Status: REOPENED (was RESOLVED)
352 * Resolution: (was FIXED)
354 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
358 public void shouldFormatReopened() {
359 Notification notification = new Notification("review-changed")
360 .setFieldValue("reviewId", "1")
361 .setFieldValue("project", "Sonar")
362 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
363 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
364 .setFieldValue("old.resolution", "FIXED")
365 .setFieldValue("new.resolution", null)
366 .setFieldValue("old.status", "RESOLVED")
367 .setFieldValue("new.status", "REOPENED");
368 EmailMessage message = template.format(notification);
369 assertThat(message.getMessageId(), is("review/1"));
370 assertThat(message.getSubject(), is("Review #1"));
371 assertThat(message.getFrom(), nullValue());
372 assertThat(message.getMessage(), is("" +
374 "Resource: org.sonar.server.ui.DefaultPages\n" +
376 "Utility classes should not have a public or default constructor.\n" +
378 "Status: REOPENED (was RESOLVED)\n" +
379 "Resolution: (was FIXED)\n" +
381 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
387 * From: Simon Brandhof
390 * Resource: org.sonar.server.ui.DefaultPages
392 * Utility classes should not have a public or default constructor.
394 * Status: RESOLVED (was OPEN)
397 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
401 public void shouldFormatResolvedAsFixed() {
402 Notification notification = new Notification("review-changed")
403 .setFieldValue("reviewId", "1")
404 .setFieldValue("project", "Sonar")
405 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
406 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
407 .setFieldValue("author", "simon.brandhof")
408 .setFieldValue("old.status", "OPEN")
409 .setFieldValue("old.resolution", null)
410 .setFieldValue("new.status", "RESOLVED")
411 .setFieldValue("new.resolution", "FIXED");
412 EmailMessage message = template.format(notification);
413 assertThat(message.getMessageId(), is("review/1"));
414 assertThat(message.getSubject(), is("Review #1"));
415 assertThat(message.getFrom(), is("Simon Brandhof"));
416 assertThat(message.getMessage(), is("" +
418 "Resource: org.sonar.server.ui.DefaultPages\n" +
420 "Utility classes should not have a public or default constructor.\n" +
422 "Status: RESOLVED (was OPEN)\n" +
423 "Resolution: FIXED\n" +
425 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
431 * From: Simon Brandhof
434 * Resource: org.sonar.server.ui.DefaultPages
436 * Utility classes should not have a public or default constructor.
438 * Status: RESOLVED (was REOPENED)
439 * Resolution: FALSE-POSITIVE
443 * See it in Sonar: http://nemo.sonarsource.org/review/view/1
447 public void shouldFormatResolvedAsFalsePositive() {
448 Notification notification = new Notification("review-changed")
449 .setFieldValue("reviewId", "1")
450 .setFieldValue("project", "Sonar")
451 .setFieldValue("resource", "org.sonar.server.ui.DefaultPages")
452 .setFieldValue("title", "Utility classes should not have a public or default constructor.")
453 .setFieldValue("author", "freddy.mallet")
454 .setFieldValue("old.status", "REOPENED")
455 .setFieldValue("old.resolution", null)
456 .setFieldValue("new.status", "RESOLVED")
457 .setFieldValue("new.resolution", "FALSE-POSITIVE")
458 .setFieldValue("new.comment", "Because!");
459 EmailMessage message = template.format(notification);
460 assertThat(message.getMessageId(), is("review/1"));
461 assertThat(message.getSubject(), is("Review #1"));
462 assertThat(message.getFrom(), is("Freddy Mallet"));
463 assertThat(message.getMessage(), is("" +
465 "Resource: org.sonar.server.ui.DefaultPages\n" +
467 "Utility classes should not have a public or default constructor.\n" +
469 "Status: RESOLVED (was REOPENED)\n" +
470 "Resolution: FALSE-POSITIVE\n" +
474 "See it in Sonar: http://nemo.sonarsource.org/reviews/view/1\n"));
478 public void shouldNotFormat() {
479 Notification notification = new Notification("other");
480 EmailMessage message = template.format(notification);
481 assertThat(message, nullValue());
485 public void shouldReturnFullNameOrLogin() {
486 assertThat(template.getUserFullName("freddy.mallet"), is("Freddy Mallet"));
487 assertThat(template.getUserFullName("deleted"), is("deleted"));
488 assertThat(template.getUserFullName(null), nullValue());