public boolean setPastSeverity(DefaultIssue issue, @Nullable String previousSeverity, IssueChangeContext context) { String currentSeverity = issue.severity(); issue.setSeverity(previousSeverity); return setSeverity(issue, currentSeverity, context); }
@Test public void not_revert_manual_severity() { issue.setSeverity("MINOR").setManualSeverity(true); try { underTest.setSeverity(issue, "MAJOR", context); } catch (IllegalStateException e) { assertThat(e).hasMessage("Severity can't be changed"); } }
@Test public void not_change_severity() { issue.setSeverity("MINOR"); boolean updated = underTest.setSeverity(issue, "MINOR", context); assertThat(updated).isFalse(); assertThat(issue.mustSendNotifications()).isFalse(); assertThat(issue.currentChange()).isNull(); }
@Test public void update_severity() { issue.setSeverity("BLOCKER"); boolean updated = underTest.setSeverity(issue, "MINOR", context); assertThat(updated).isTrue(); assertThat(issue.severity()).isEqualTo("MINOR"); assertThat(issue.mustSendNotifications()).isFalse(); FieldDiffs.Diff diff = issue.currentChange().get(SEVERITY); assertThat(diff.oldValue()).isEqualTo("BLOCKER"); assertThat(diff.newValue()).isEqualTo("MINOR"); }
@Test public void set_severity() { boolean updated = underTest.setSeverity(issue, "BLOCKER", context); assertThat(updated).isTrue(); assertThat(issue.severity()).isEqualTo("BLOCKER"); assertThat(issue.manualSeverity()).isFalse(); assertThat(issue.mustSendNotifications()).isFalse(); FieldDiffs.Diff diff = issue.currentChange().get(SEVERITY); assertThat(diff.oldValue()).isNull(); assertThat(diff.newValue()).isEqualTo("BLOCKER"); }
@Test public void update_issue() { DefaultIssue issue = db.issues().insertIssue(i -> i.setSeverity(MAJOR)).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, null); IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issue.key()).get(); assertThat(issueReloaded.getSeverity()).isEqualTo(BLOCKER); }
@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()); }
@Test public void verify_notification_on_branch() { RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); RuleType randomTypeExceptHotspot = RuleType.values()[nextInt(RuleType.values().length - 1)]; DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), branch, file) .setType(randomTypeExceptHotspot)).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); 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("projectKey")).isEqualTo(project.getDbKey()); assertThat(issueChangeNotification.getFieldValue("projectName")).isEqualTo(project.name()); assertThat(issueChangeNotification.getFieldValue("branch")).isEqualTo(branch.getBranch()); }
@Test public void saveIssue_populates_specified_SearchResponseData_with_no_rule_but_with_project_and_component_if_rule_is_removed() { RuleDto rule = db.rules().insertRule(r -> r.setStatus(RuleStatus.REMOVED)); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issueDto = IssueTesting.newIssue(rule.getDefinition(), project, file); DefaultIssue issue = db.issues().insertIssue(issueDto).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, null, false); assertThat(preloadedSearchResponseData.getIssues()) .hasSize(1); assertThat(preloadedSearchResponseData.getIssues().iterator().next()) .isNotSameAs(issueDto); assertThat(preloadedSearchResponseData.getRules()).isNullOrEmpty(); assertThat(preloadedSearchResponseData.getComponents()) .extracting(ComponentDto::uuid) .containsOnly(project.uuid(), file.uuid()); }
@Test public void verify_notification_when_issue_is_linked_on_removed_rule() { RuleDto rule = db.rules().insertRule(r -> r.setStatus(RuleStatus.REMOVED)); 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).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); underTest.saveIssue(db.getSession(), issue, context, null); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); assertThat(notificationArgumentCaptor.getValue().getFieldValue("ruleName")).isNull(); }
@Test public void saveIssue_populates_specified_SearchResponseData_with_rule_project_and_component_retrieved_from_DB() { RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); IssueDto issueDto = IssueTesting.newIssue(rule.getDefinition(), project, file); DefaultIssue issue = db.issues().insertIssue(issueDto).setSeverity(MAJOR).toDefaultIssue(); IssueChangeContext context = IssueChangeContext.createUser(new Date(), "user_uuid"); issueFieldsSetter.setSeverity(issue, BLOCKER, context); SearchResponseData preloadedSearchResponseData = underTest.saveIssueAndPreloadSearchResponseData(db.getSession(), issue, context, null, true); assertThat(preloadedSearchResponseData.getIssues()) .hasSize(1); assertThat(preloadedSearchResponseData.getIssues().iterator().next()) .isNotSameAs(issueDto); assertThat(preloadedSearchResponseData.getRules()) .extracting(RuleDefinitionDto::getKey) .containsOnly(rule.getKey()); assertThat(preloadedSearchResponseData.getComponents()) .extracting(ComponentDto::uuid) .containsOnly(project.uuid(), file.uuid()); assertThat(issueChangePostProcessor.calledComponents()).containsExactlyInAnyOrder(file); }
@Test public void verify_no_notification_on_hotspot() { UserDto assignee = db.users().insertUser(); RuleDto rule = db.rules().insertRule(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); DefaultIssue issue = db.issues().insertIssue(IssueTesting.newIssue(rule.getDefinition(), project, file) .setType(RuleType.SECURITY_HOTSPOT)) .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, never()).scheduleForSending(any()); }
public boolean setPastSeverity(DefaultIssue issue, @Nullable String previousSeverity, IssueChangeContext context) { String currentSeverity = issue.severity(); issue.setSeverity(previousSeverity); return setSeverity(issue, currentSeverity, context); }