private Consumer<DefaultIssue> sendNotification(BulkChangeData bulkChangeData, Map<String, UserDto> userDtoByUuid, UserDto author) { return issue -> { if (bulkChangeData.sendNotification && issue.type() != RuleType.SECURITY_HOTSPOT) { notificationService.scheduleForSending(new IssueChangeNotification() .setIssue(issue) .setAssignee(userDtoByUuid.get(issue.assignee())) .setChangeAuthor(author) .setRuleName(bulkChangeData.rulesByKey.get(issue.ruleKey()).getName()) .setProject(bulkChangeData.projectsByUuid.get(issue.projectUuid())) .setComponent(bulkChangeData.componentsByUuid.get(issue.componentUuid()))); } }; }
@Test public void set_project_with_pull_request() { IssueChangeNotification result = notification.setProject("MyService", "My Service", null, "pr-123"); assertThat(result.getFieldValue("projectKey")).isEqualTo("MyService"); assertThat(result.getFieldValue("projectName")).isEqualTo("My Service"); assertThat(result.getFieldValue("pullRequest")).isEqualTo("pr-123"); }
@Test public void setComment() { IssueChangeNotification result = notification.setComment("My comment"); assertThat(result.getFieldValue("comment")).isEqualTo("My comment"); } }
public IssueChangeNotification setIssue(DefaultIssue issue) { setFieldValue("key", issue.key()); setFieldValue("message", issue.message()); FieldDiffs currentChange = issue.currentChange(); if (currentChange != null) { for (Map.Entry<String, FieldDiffs.Diff> entry : currentChange.diffs().entrySet()) { String type = entry.getKey(); FieldDiffs.Diff diff = entry.getValue(); setFieldValue("old." + type, neverEmptySerializableToString(diff.oldValue())); setFieldValue("new." + type, neverEmptySerializableToString(diff.newValue())); } } return this; }
@Test public void notification_sender_should_be_the_author_of_change() { UserDto user = db.users().insertUser(); Notification notification = new IssueChangeNotification() .setChangeAuthor(user) .setProject("Struts", "org.apache:struts", null, null); EmailMessage message = underTest.format(notification); assertThat(message.getFrom()).isEqualTo(user.getName()); }
/** * Only false positive and won't fix resolutions */ @Test public void ignore_other_resolutions() { Notification fixedNotif = new IssueChangeNotification().setFieldValue("projectKey", "struts") .setFieldValue("changeAuthor", "godin") .setFieldValue("new.resolution", Issue.RESOLUTION_FIXED) .setFieldValue("assignee", "freddy"); underTest.performDispatch(fixedNotif, context); verifyZeroInteractions(context); } }
@Test public void set_issue() { UserDto assignee = newUserDto(); DefaultIssue issue = new DefaultIssue() .setKey("ABCD") .setAssigneeUuid(assignee.getUuid()) .setMessage("Remove this useless method") .setComponentKey("MyService") .setCurrentChange(new FieldDiffs().setDiff("resolution", "FALSE-POSITIVE", "FIXED")); IssueChangeNotification result = notification.setIssue(issue).setAssignee(assignee); assertThat(result.getFieldValue("key")).isEqualTo("ABCD"); assertThat(result.getFieldValue("message")).isEqualTo("Remove this useless method"); assertThat(result.getFieldValue("old.resolution")).isEqualTo("FALSE-POSITIVE"); assertThat(result.getFieldValue("new.resolution")).isEqualTo("FIXED"); assertThat(result.getFieldValue("assignee")).isEqualTo(assignee.getLogin()); }
@Test public void set_component() { IssueChangeNotification result = notification.setComponent(new ComponentDto().setDbKey("MyService").setLongName("My Service")); assertThat(result.getFieldValue("componentName")).isEqualTo("My Service"); assertThat(result.getFieldValue("componentKey")).isEqualTo("MyService"); }
public IssueChangeNotification setComponent(String componentKey, String componentName) { setFieldValue("componentName", componentName); setFieldValue("componentKey", componentKey); return this; }
@Test public void set_issue_with_current_change_having_no_new_value() { DefaultIssue issue = new DefaultIssue() .setKey("ABCD") .setAssigneeUuid("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_rule_name() { IssueChangeNotification result = notification.setRuleName("Xoo Rule"); assertThat(result.getFieldValue("ruleName")).isEqualTo("Xoo Rule"); }
private void sendIssueChangeNotification(long issueCreatedAt) { UserDto user = db.users().insertUser(); ComponentDto project = newPrivateProjectDto(newOrganizationDto()).setDbKey(PROJECT.getDbKey()).setLongName(PROJECT.getName()); ComponentDto file = newFileDto(project).setDbKey(FILE.getDbKey()).setLongName(FILE.getName()); RuleDefinitionDto ruleDefinitionDto = newRule(); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = prepareIssue(issueCreatedAt, user, project, file, ruleDefinitionDto, randomTypeExceptHotspot); underTest.execute(new TestComputationStepContext()); ArgumentCaptor<IssueChangeNotification> issueChangeNotificationCaptor = forClass(IssueChangeNotification.class); verify(notificationService).deliver(issueChangeNotificationCaptor.capture()); IssueChangeNotification issueChangeNotification = issueChangeNotificationCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("message")).isEqualTo(issue.message()); assertThat(issueChangeNotification.getFieldValue("ruleName")).isEqualTo(ruleDefinitionDto.getName()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.longName()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getKey()); assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getKey()); assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName()); assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(user.getLogin()); }
@Test public void set_change_author_login() { UserDto user = newUserDto(); IssueChangeNotification result = notification.setChangeAuthor(user); assertThat(result.getFieldValue("changeAuthor")).isEqualTo(user.getLogin()); }
public IssueChangeNotification setComponent(ComponentDto component) { return setComponent(component.getKey(), component.longName()); }
public IssueChangeNotification setProject(ComponentDto project) { return setProject(project.getKey(), project.name(), project.getBranch(), project.getPullRequest()); }
@Test public void notification_contains_user_login_when_user_is_removed() { UserDto user = db.users().insertDisabledUser(); Notification notification = new IssueChangeNotification() .setChangeAuthor(user) .setProject("Struts", "org.apache:struts", null, null); EmailMessage message = underTest.format(notification); assertThat(message.getFrom()).isEqualTo(user.getLogin()); }
@Test public void should_dispatch_to_assignee() { Multimap<String, NotificationChannel> recipients = HashMultimap.create(); recipients.put("simon", emailChannel); recipients.put("freddy", twitterChannel); recipients.put("godin", twitterChannel); when(notifications.findSubscribedRecipientsForDispatcher(underTest, "struts", new NotificationManager.SubscriberPermissionsOnProject(UserRole.USER))).thenReturn(recipients); Notification notification = new IssueChangeNotification() .setFieldValue("projectKey", "struts") .setFieldValue("changeAuthor", "olivier") .setFieldValue("assignee", "freddy"); underTest.performDispatch(notification, context); verify(context).addUser("freddy", twitterChannel); verify(context, never()).addUser("godin", twitterChannel); verifyNoMoreInteractions(context); }
public IssueChangeNotification setProject(String projectKey, String projectName, @Nullable String branch, @Nullable String pullRequest) { setFieldValue(FIELD_PROJECT_NAME, projectName); setFieldValue(FIELD_PROJECT_KEY, projectKey); if (branch != null) { setFieldValue(FIELD_BRANCH, branch); } if (pullRequest != null) { setFieldValue(FIELD_PULL_REQUEST, pullRequest); } return this; }
@Test public void set_issue_with_current_change_having_no_old_value() { DefaultIssue issue = new DefaultIssue() .setKey("ABCD") .setAssigneeUuid("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 verify_notification() { UserDto assignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(randomTypeExceptHotspot)) .setSeverity(MAJOR) .setAssigneeUuid(assignee.getUuid()) .toDefaultIssue(); UserDto changeAuthor = db.users().insertUser(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), changeAuthor.getUuid()); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, "increase severity"); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); IssueChangeNotification issueChangeNotification = notificationArgumentCaptor.getValue(); assertThat(issueChangeNotification.getFieldValue("key")).isEqualTo(issue.key()); assertThat(issueChangeNotification.getFieldValue("old.severity")).isEqualTo(MAJOR); assertThat(issueChangeNotification.getFieldValue("new.severity")).isEqualTo(BLOCKER); assertThat(issueChangeNotification.getFieldValue("componentKey")).isEqualTo(file.getDbKey()); assertThat(issueChangeNotification.getFieldValue("componentName")).isEqualTo(file.longName()); assertThat(issueChangeNotification.getFieldValue("projectKey")).isEqualTo(project.getDbKey()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.name()); assertThat(issueChangeNotification.getFieldValue("ruleName")).isEqualTo(rule.getName()); assertThat(issueChangeNotification.getFieldValue("changeAuthor")).isEqualTo(changeAuthor.getLogin()); assertThat(issueChangeNotification.getFieldValue("comment")).isEqualTo("increase severity"); assertThat(issueChangeNotification.getFieldValue("assignee")).isEqualTo(assignee.getLogin()); }