Search in sources :

Example 1 with FormattingContext

use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.

the class ShowAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String hotspotKey = request.mandatoryParam(PARAM_HOTSPOT_KEY);
    try (DbSession dbSession = dbClient.openSession(false)) {
        IssueDto hotspot = hotspotWsSupport.loadHotspot(dbSession, hotspotKey);
        Components components = loadComponents(dbSession, hotspot);
        Users users = loadUsers(dbSession, hotspot);
        RuleDefinitionDto rule = loadRule(dbSession, hotspot);
        ShowWsResponse.Builder responseBuilder = ShowWsResponse.newBuilder();
        formatHotspot(responseBuilder, hotspot, users);
        formatComponents(components, responseBuilder);
        formatRule(responseBuilder, rule);
        formatTextRange(responseBuilder, hotspot);
        formatFlows(dbSession, responseBuilder, hotspot);
        FormattingContext formattingContext = formatChangeLogAndComments(dbSession, hotspot, users, components, responseBuilder);
        formatUsers(responseBuilder, users, formattingContext);
        writeProtobuf(responseBuilder.build(), request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) ShowWsResponse(org.sonarqube.ws.Hotspots.ShowWsResponse) IssueDto(org.sonar.db.issue.IssueDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto)

Example 2 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_only_changelog.

@Test
public void newFormattingContext_with_Load_CHANGE_LOG_loads_only_changelog() {
    IssueDto issue = dbTester.issues().insertIssue();
    List<IssueChangeDto> comments = IntStream.range(0, 1 + RANDOM.nextInt(20)).mapToObj(i -> newComment(issue).setKey("comment_" + i)).collect(Collectors.toList());
    List<IssueChangeDto> fieldChanges = IntStream.range(0, 1 + RANDOM.nextInt(20)).mapToObj(i -> newFieldChange(issue).setChangeData(new FieldDiffs().setDiff("f_change_" + i, null, null).toEncodedString())).collect(Collectors.toList());
    insertInRandomOrder(comments, fieldChanges);
    FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), Load.CHANGE_LOG);
    assertThat(formattingContext.getChanges(issue)).extracting(FieldDiffs::toEncodedString).containsExactlyInAnyOrder(fieldChanges.stream().map(t -> t.toFieldDiffs().toEncodedString()).toArray(String[]::new));
    assertThat(formattingContext.getComments(issue)).isEmpty();
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) IssueDto(org.sonar.db.issue.IssueDto) UserDto(org.sonar.db.user.UserDto) Markdown(org.sonar.markdown.Markdown) Date(java.util.Date) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Changelog(org.sonarqube.ws.Common.Changelog) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) TYPE_COMMENT(org.sonar.db.issue.IssueChangeDto.TYPE_COMMENT) ArrayList(java.util.ArrayList) Collections.singleton(java.util.Collections.singleton) Load(org.sonar.server.issue.IssueChangeWSSupport.Load) TYPE_FIELD_CHANGE(org.sonar.db.issue.IssueChangeDto.TYPE_FIELD_CHANGE) ComponentTesting(org.sonar.db.component.ComponentTesting) UuidFactoryFast(org.sonar.core.util.UuidFactoryFast) DateUtils.formatDateTime(org.sonar.api.utils.DateUtils.formatDateTime) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) IssueChangeDto(org.sonar.db.issue.IssueChangeDto) ImmutableSet(com.google.common.collect.ImmutableSet) Collections.emptySet(java.util.Collections.emptySet) System2(org.sonar.api.utils.System2) IssueTesting(org.sonar.db.issue.IssueTesting) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Assertions.tuple(org.assertj.core.api.Assertions.tuple) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Collection(java.util.Collection) Test(org.junit.Test) Comment(org.sonarqube.ws.Common.Comment) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) FieldDiffs(org.sonar.core.issue.FieldDiffs) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Rule(org.junit.Rule) Collections(java.util.Collections) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) FieldDiffs(org.sonar.core.issue.FieldDiffs) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test)

Example 3 with FormattingContext

use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.

the class IssueChangeWSSupportTest method formatComments_returns_empty_if_context_has_no_comment_for_specified_IssueDto.

@Test
@UseDataProvider("loadAllOrComments")
public void formatComments_returns_empty_if_context_has_no_comment_for_specified_IssueDto(Load load) {
    IssueDto issue1 = dbTester.issues().insertIssue();
    IssueDto issue2 = dbTester.issues().insertIssue();
    IntStream.range(0, 1 + RANDOM.nextInt(22)).forEach(t -> dbTester.issues().insertChange(newComment(issue1)));
    FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue1), load);
    assertThat(underTest.formatComments(issue2, Comment.newBuilder(), formattingContext)).isEmpty();
    assertThat(underTest.formatComments(issue1, Comment.newBuilder(), formattingContext)).isNotEmpty();
}
Also used : FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 4 with FormattingContext

use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.

the class ShowActionTest method do_not_duplicate_user_if_author_assignee_ChangeLogComment_user.

@Test
public void do_not_duplicate_user_if_author_assignee_ChangeLogComment_user() {
    ComponentDto project = dbTester.components().insertPublicProject();
    userSessionRule.registerComponents(project);
    RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
    ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
    UserDto author = dbTester.users().insertUser();
    IssueDto hotspot = dbTester.issues().insertHotspot(rule, project, file, t -> t.setAuthorLogin(author.getLogin()).setAssigneeUuid(author.getUuid()));
    FormattingContext formattingContext = mockChangelogAndCommentsFormattingContext();
    when(formattingContext.getUsers()).thenReturn(ImmutableSet.of(author));
    Hotspots.ShowWsResponse response = newRequest(hotspot).executeProtobuf(Hotspots.ShowWsResponse.class);
    assertThat(response.getUsersList()).extracting(User::getLogin, User::getName, User::getActive).containsOnly(tuple(author.getLogin(), author.getName(), author.isActive()));
}
Also used : FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) UserDto(org.sonar.db.user.UserDto) Hotspots(org.sonarqube.ws.Hotspots) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) Test(org.junit.Test)

Example 5 with FormattingContext

use of org.sonar.server.issue.IssueChangeWSSupport.FormattingContext in project sonarqube by SonarSource.

the class ShowActionTest method returns_user_of_users_from_ChangelogAndComments_and_assignee_and_author.

@Test
public void returns_user_of_users_from_ChangelogAndComments_and_assignee_and_author() {
    ComponentDto project = dbTester.components().insertPublicProject();
    userSessionRule.registerComponents(project);
    RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
    ComponentDto file = dbTester.components().insertComponent(newFileDto(project));
    UserDto author = dbTester.users().insertUser();
    UserDto assignee = dbTester.users().insertUser();
    IssueDto hotspot = dbTester.issues().insertHotspot(rule, project, file, t -> t.setAuthorLogin(author.getLogin()).setAssigneeUuid(assignee.getUuid()));
    FormattingContext formattingContext = mockChangelogAndCommentsFormattingContext();
    Set<UserDto> changeLogAndCommentsUsers = IntStream.range(0, 1 + RANDOM.nextInt(14)).mapToObj(i -> UserTesting.newUserDto()).collect(Collectors.toSet());
    when(formattingContext.getUsers()).thenReturn(changeLogAndCommentsUsers);
    Hotspots.ShowWsResponse response = newRequest(hotspot).executeProtobuf(Hotspots.ShowWsResponse.class);
    assertThat(response.getUsersList()).extracting(User::getLogin, User::getName, User::getActive).containsExactlyInAnyOrder(Stream.concat(Stream.of(author, assignee), changeLogAndCommentsUsers.stream()).map(t -> tuple(t.getLogin(), t.getName(), t.isActive())).toArray(Tuple[]::new));
}
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) UserDto(org.sonar.db.user.UserDto) Hotspots(org.sonarqube.ws.Hotspots) ComponentDto(org.sonar.db.component.ComponentDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) IssueDto(org.sonar.db.issue.IssueDto) Tuple(org.assertj.core.groups.Tuple) Test(org.junit.Test)

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