Search in sources :

Example 6 with FormattingContext

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));
}
Also used : Arrays(java.util.Arrays) SecurityStandards(org.sonar.server.security.SecurityStandards) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) ArgumentMatcher(org.mockito.ArgumentMatcher) DbIssues(org.sonar.db.protobuf.DbIssues) AvatarResolverImpl(org.sonar.server.issue.AvatarResolverImpl) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) ImmutableSet(com.google.common.collect.ImmutableSet) System2(org.sonar.api.utils.System2) Hotspots(org.sonarqube.ws.Hotspots) Set(java.util.Set) AvatarResolver(org.sonar.server.issue.AvatarResolver) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) NotFoundException(org.sonar.server.exceptions.NotFoundException) Common(org.sonarqube.ws.Common) DbClient(org.sonar.db.DbClient) Location(org.sonarqube.ws.Common.Location) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Stream(java.util.stream.Stream) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) SQCategory(org.sonar.server.security.SecurityStandards.SQCategory) UserTesting(org.sonar.db.user.UserTesting) Diff(org.sonarqube.ws.Common.Changelog.Diff) IntStream(java.util.stream.IntStream) IssueChangeWSSupport(org.sonar.server.issue.IssueChangeWSSupport) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) UserDto(org.sonar.db.user.UserDto) EsTester(org.sonar.server.es.EsTester) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) ComponentTesting.newFileDto(org.sonar.db.component.ComponentTesting.newFileDto) RunWith(org.junit.runner.RunWith) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) RuleType(org.sonar.api.rules.RuleType) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TextRangeResponseFormatter(org.sonar.server.issue.TextRangeResponseFormatter) Load(org.sonar.server.issue.IssueChangeWSSupport.Load) User(org.sonarqube.ws.Common.User) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) Nullable(javax.annotation.Nullable) UserSessionRule(org.sonar.server.tester.UserSessionRule) Tuple(org.assertj.core.groups.Tuple) BranchType(org.sonar.db.component.BranchType) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Assertions.tuple(org.assertj.core.api.Assertions.tuple) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) DbCommons(org.sonar.db.protobuf.DbCommons) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) Rule(org.junit.Rule) UserRole(org.sonar.api.web.UserRole) Issue(org.sonar.api.issue.Issue) UserResponseFormatter(org.sonar.server.issue.ws.UserResponseFormatter) MARKDOWN(org.sonar.db.rule.RuleDto.Format.MARKDOWN) Collections(java.util.Collections) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) Hotspots(org.sonarqube.ws.Hotspots) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Common(org.sonarqube.ws.Common) DbSession(org.sonar.db.DbSession) Random(java.util.Random) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test)

Example 7 with 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())));
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) Comment(org.sonarqube.ws.Common.Comment) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 8 with FormattingContext

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();
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) FieldDiffs(org.sonar.core.issue.FieldDiffs) UserDto(org.sonar.db.user.UserDto) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test)

Example 9 with FormattingContext

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());
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) FieldDiffs(org.sonar.core.issue.FieldDiffs) UserDto(org.sonar.db.user.UserDto) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test)

Example 10 with FormattingContext

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();
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) UserDto(org.sonar.db.user.UserDto) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

FormattingContext (org.sonar.server.issue.IssueChangeWSSupport.FormattingContext)26 IssueDto (org.sonar.db.issue.IssueDto)24 Test (org.junit.Test)22 UserDto (org.sonar.db.user.UserDto)18 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)16 IssueChangeDto (org.sonar.db.issue.IssueChangeDto)16 FieldDiffs (org.sonar.core.issue.FieldDiffs)12 ComponentDto (org.sonar.db.component.ComponentDto)12 ImmutableSet (com.google.common.collect.ImmutableSet)8 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)8 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)8 Arrays (java.util.Arrays)8 Collections (java.util.Collections)8 List (java.util.List)8 Random (java.util.Random)8 Consumer (java.util.function.Consumer)8 Collectors (java.util.stream.Collectors)8 IntStream (java.util.stream.IntStream)8 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8