use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.
the class ShowActionTest method returns_hotspot_changelog_and_comments.
@Test
public void returns_hotspot_changelog_and_comments() {
ComponentDto project = dbTester.components().insertPublicProject();
userSessionRule.registerComponents(project);
RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
IssueDto hotspot = dbTester.issues().insertHotspot(rule, project, file, t -> t.setLocations(DbIssues.Locations.newBuilder().setTextRange(DbCommons.TextRange.newBuilder().build()).build()));
List<Common.Changelog> changelog = IntStream.range(0, 1 + new Random().nextInt(12)).mapToObj(i -> Common.Changelog.newBuilder().setUser("u" + i).build()).collect(Collectors.toList());
List<Common.Comment> comments = IntStream.range(0, 1 + new Random().nextInt(12)).mapToObj(i -> Common.Comment.newBuilder().setKey("u" + i).build()).collect(Collectors.toList());
FormattingContext formattingContext = mockChangelogAndCommentsFormattingContext();
when(issueChangeSupport.formatChangelog(any(), any())).thenReturn(changelog.stream());
when(issueChangeSupport.formatComments(any(), any(), any())).thenReturn(comments.stream());
Hotspots.ShowWsResponse response = newRequest(hotspot).executeProtobuf(Hotspots.ShowWsResponse.class);
assertThat(response.getChangelogList()).extracting(Common.Changelog::getUser).containsExactly(changelog.stream().map(Common.Changelog::getUser).toArray(String[]::new));
assertThat(response.getCommentList()).extracting(Common.Comment::getKey).containsExactly(comments.stream().map(Common.Comment::getKey).toArray(String[]::new));
verify(issueChangeSupport).newFormattingContext(any(DbSession.class), argThat(new IssueDtoSetArgumentMatcher(hotspot)), eq(Load.ALL), eq(Collections.emptySet()), eq(ImmutableSet.of(project, file)));
verify(issueChangeSupport).formatChangelog(argThat(new IssueDtoArgumentMatcher(hotspot)), eq(formattingContext));
verify(issueChangeSupport).formatComments(argThat(new IssueDtoArgumentMatcher(hotspot)), any(Common.Comment.Builder.class), eq(formattingContext));
}
use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.
the class IssueChangeWSSupportTest method formatComments_returns_comment_markdown_and_html_when_available.
@Test
@UseDataProvider("loadAllOrComments")
public void formatComments_returns_comment_markdown_and_html_when_available(Load load) {
IssueDto issue = dbTester.issues().insertIssue();
IssueChangeDto withText = dbTester.issues().insertChange(newComment(issue).setChangeData("* foo"));
IssueChangeDto noText = dbTester.issues().insertChange(newComment(issue).setChangeData(null));
FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), load);
List<Comment> comments = underTest.formatComments(issue, Comment.newBuilder(), formattingContext).collect(Collectors.toList());
assertThat(comments).extracting(Comment::getKey, Comment::hasMarkdown, Comment::hasHtmlText).containsExactlyInAnyOrder(tuple(withText.getKey(), true, true), tuple(noText.getKey(), false, false));
assertThat(comments.stream().filter(Comment::hasHtmlText)).extracting(Comment::getMarkdown, Comment::getHtmlText).containsOnly(tuple(withText.getChangeData(), Markdown.convertToHtml(withText.getChangeData())));
}
use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.
the class IssueChangeWSSupportTest method newFormattingContext_with_load_CHANGE_LOG_loads_users_of_field_changes.
@Test
public void newFormattingContext_with_load_CHANGE_LOG_loads_users_of_field_changes() {
IssueDto issue = dbTester.issues().insertIssue();
UserDto user1 = dbTester.users().insertUser();
UserDto user2 = dbTester.users().insertUser();
UserDto user3 = dbTester.users().insertUser();
String uuid = randomAlphabetic(30);
IssueChangeDto fieldChangeUser1 = newFieldChange(issue).setUserUuid(user1.getUuid()).setChangeData(new FieldDiffs().setDiff("f_change_user_1", null, null).toEncodedString());
IssueChangeDto fieldChangeUser2a = newFieldChange(issue).setUserUuid(user2.getUuid()).setChangeData(new FieldDiffs().setDiff("f_change_user_2a", null, null).toEncodedString());
IssueChangeDto fieldChangeUser2b = newFieldChange(issue).setUserUuid(user2.getUuid()).setChangeData(new FieldDiffs().setDiff("f_change_user_2b", null, null).toEncodedString());
IssueChangeDto fieldChangeNonExistingUser = newFieldChange(issue).setUserUuid(uuid).setChangeData(new FieldDiffs().setDiff("f_change_user_unknown", null, null).toEncodedString());
insertInRandomOrder(Arrays.asList(fieldChangeUser1, fieldChangeUser2a, fieldChangeUser2b, fieldChangeNonExistingUser));
FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), Load.CHANGE_LOG);
assertThat(formattingContext.getUsers()).extracting(UserDto::getUuid).containsExactlyInAnyOrder(user1.getUuid(), user2.getUuid());
assertThat(formattingContext.getUserByUuid(user1.getUuid())).isNotEmpty();
assertThat(formattingContext.getUserByUuid(user2.getUuid())).isNotEmpty();
assertThat(formattingContext.getUserByUuid(user3.getUuid())).isEmpty();
assertThat(formattingContext.getUserByUuid(uuid)).isEmpty();
}
use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.
the class IssueChangeWSSupportTest method newFormattingContext_does_not_load_preloaded_users_from_DB.
@Test
public void newFormattingContext_does_not_load_preloaded_users_from_DB() {
IssueDto issue = dbTester.issues().insertIssue();
UserDto user1 = dbTester.users().insertUser();
UserDto user2 = dbTester.users().insertUser();
UserDto user3 = dbTester.users().insertUser();
UserDto user4 = dbTester.users().insertUser();
IssueChangeDto issueChangeUser1 = newComment(issue).setUserUuid(user1.getUuid());
IssueChangeDto issueChangeUser2 = newComment(issue).setUserUuid(user2.getUuid());
IssueChangeDto fieldChangeUser1 = newFieldChange(issue).setUserUuid(user1.getUuid()).setChangeData(new FieldDiffs().setDiff("f_change_user_1", null, null).toEncodedString());
IssueChangeDto fieldChangeUser3 = newFieldChange(issue).setUserUuid(user3.getUuid()).setChangeData(new FieldDiffs().setDiff("f_change_user_3", null, null).toEncodedString());
IssueChangeDto fieldChangeUser4 = newFieldChange(issue).setUserUuid(user4.getUuid()).setChangeData(new FieldDiffs().setDiff("f_change_user_4", null, null).toEncodedString());
insertInRandomOrder(Arrays.asList(issueChangeUser1, issueChangeUser2), Arrays.asList(fieldChangeUser1, fieldChangeUser3, fieldChangeUser4));
user1.setEmail("post_insert_changed" + user1.getUuid());
user2.setEmail("post_insert_changed" + user2.getUuid());
user3.setEmail("post_insert_changed" + user3.getUuid());
user4.setEmail("post_insert_changed" + user4.getUuid());
// no users are preloaded
FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), Load.ALL, emptySet(), emptySet());
assertThat(formattingContext.getUsers()).extracting(UserDto::getUuid).containsExactlyInAnyOrder(user1.getUuid(), user2.getUuid(), user3.getUuid(), user4.getUuid());
assertThat(formattingContext.getUserByUuid(user1.getUuid()).get().getEmail()).isNotEqualTo(user1.getEmail());
assertThat(formattingContext.getUserByUuid(user2.getUuid()).get().getEmail()).isNotEqualTo(user2.getEmail());
assertThat(formattingContext.getUserByUuid(user3.getUuid()).get().getEmail()).isNotEqualTo(user3.getEmail());
assertThat(formattingContext.getUserByUuid(user4.getUuid()).get().getEmail()).isNotEqualTo(user4.getEmail());
// some users are preloaded
formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), Load.ALL, ImmutableSet.of(user1, user4), emptySet());
assertThat(formattingContext.getUsers()).extracting(UserDto::getUuid).containsExactlyInAnyOrder(user1.getUuid(), user2.getUuid(), user3.getUuid(), user4.getUuid());
assertThat(formattingContext.getUserByUuid(user1.getUuid()).get().getEmail()).isEqualTo(user1.getEmail());
assertThat(formattingContext.getUserByUuid(user2.getUuid()).get().getEmail()).isNotEqualTo(user2.getEmail());
assertThat(formattingContext.getUserByUuid(user3.getUuid()).get().getEmail()).isNotEqualTo(user3.getEmail());
assertThat(formattingContext.getUserByUuid(user4.getUuid()).get().getEmail()).isEqualTo(user4.getEmail());
// all users are preloaded
formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), Load.ALL, ImmutableSet.of(user1, user2, user3, user4), emptySet());
assertThat(formattingContext.getUsers()).extracting(UserDto::getUuid).containsExactlyInAnyOrder(user1.getUuid(), user2.getUuid(), user3.getUuid(), user4.getUuid());
assertThat(formattingContext.getUserByUuid(user1.getUuid()).get().getEmail()).isEqualTo(user1.getEmail());
assertThat(formattingContext.getUserByUuid(user2.getUuid()).get().getEmail()).isEqualTo(user2.getEmail());
assertThat(formattingContext.getUserByUuid(user3.getUuid()).get().getEmail()).isEqualTo(user3.getEmail());
assertThat(formattingContext.getUserByUuid(user4.getUuid()).get().getEmail()).isEqualTo(user4.getEmail());
}
use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.
the class IssueChangeWSSupportTest method newFormattingContext_only_comments_of_logged_in_user_are_updatable.
@Test
@UseDataProvider("loadAllOrComments")
public void newFormattingContext_only_comments_of_logged_in_user_are_updatable(Load load) {
IssueDto issue = dbTester.issues().insertIssue();
UserDto user1 = dbTester.users().insertUser();
UserDto user2 = dbTester.users().insertUser();
userSessionRule.logIn(user2);
String uuid = randomAlphabetic(30);
IssueChangeDto issueChangeUser1a = newComment(issue).setUserUuid(user1.getUuid());
IssueChangeDto issueChangeUser1b = newComment(issue).setUserUuid(user1.getUuid());
IssueChangeDto issueChangeUser2a = newComment(issue).setUserUuid(user2.getUuid());
IssueChangeDto issueChangeUser2b = newComment(issue).setUserUuid(user2.getUuid());
IssueChangeDto issueChangeUserUnknown = newComment(issue).setUserUuid(uuid);
insertInRandomOrder(Arrays.asList(issueChangeUser1a, issueChangeUser1b, issueChangeUser2a, issueChangeUser2b, issueChangeUserUnknown));
FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), load);
assertThat(formattingContext.isUpdatableComment(issueChangeUser1a)).isFalse();
assertThat(formattingContext.isUpdatableComment(issueChangeUser1b)).isFalse();
assertThat(formattingContext.isUpdatableComment(issueChangeUser2a)).isTrue();
assertThat(formattingContext.isUpdatableComment(issueChangeUser2b)).isTrue();
assertThat(formattingContext.isUpdatableComment(issueChangeUserUnknown)).isFalse();
}
Aggregations