Search in sources :

Example 21 with FormattingContext

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

the class IssueChangeWSSupportTest method newFormattingContext_with_Load_COMMENTS_loads_only_comments.

@Test
public void newFormattingContext_with_Load_COMMENTS_loads_only_comments() {
    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.COMMENTS);
    assertThat(formattingContext.getComments(issue)).extracting(IssueChangeDto::getKey).containsExactlyInAnyOrder(comments.stream().map(IssueChangeDto::getKey).toArray(String[]::new));
    assertThat(formattingContext.getChanges(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 22 with FormattingContext

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

the class IssueChangeWSSupportTest method newFormattingContext_sorts_changes.

@Test
public void newFormattingContext_sorts_changes() {
    IssueDto issue = dbTester.issues().insertIssue();
    insertFieldChange(issue, c -> c.setCreatedAt(3L));
    insertFieldChange(issue, c -> c.setIssueChangeCreationDate(1L));
    insertFieldChange(issue, c -> c.setCreatedAt(2L));
    FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue), Load.CHANGE_LOG);
    assertThat(formattingContext.getChanges(issue)).extracting(FieldDiffs::creationDate).containsExactly(new Date(1L), new Date(2L), new Date(3L));
}
Also used : FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) IssueDto(org.sonar.db.issue.IssueDto) Date(java.util.Date) Test(org.junit.Test)

Example 23 with FormattingContext

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

the class IssueChangeWSSupportTest method formatChangelog_returns_empty_if_context_has_no_changeLog_for_specified_IssueDto.

@Test
@UseDataProvider("loadAllOrChangelog")
public void formatChangelog_returns_empty_if_context_has_no_changeLog_for_specified_IssueDto(Load load) {
    IssueDto issue1 = dbTester.issues().insertIssue();
    IssueDto issue2 = dbTester.issues().insertIssue();
    List<IssueChangeDto> comments = IntStream.range(0, 1 + RANDOM.nextInt(20)).mapToObj(i -> newComment(issue1).setKey("comment_" + i)).collect(Collectors.toList());
    List<IssueChangeDto> fieldChanges = IntStream.range(0, 1 + RANDOM.nextInt(20)).mapToObj(i -> newFieldChange(issue1).setChangeData(new FieldDiffs().setDiff("f_change_" + i, null, null).toEncodedString())).collect(Collectors.toList());
    insertInRandomOrder(comments, fieldChanges);
    FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue1), Load.CHANGE_LOG);
    assertThat(underTest.formatChangelog(issue2, formattingContext)).isEmpty();
    assertThat(underTest.formatChangelog(issue1, formattingContext)).isNotEmpty();
}
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) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 24 with FormattingContext

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

the class IssueChangeWSSupportTest method formatChangelog_returns_field_diff_details.

@Test
@UseDataProvider("loadAllOrChangelog")
public void formatChangelog_returns_field_diff_details(Load load) {
    IssueDto issue1 = dbTester.issues().insertIssue();
    int createdAt = 2_333_999;
    IssueChangeDto issueChangeDto = dbTester.issues().insertChange(newFieldChange(issue1).setIssueChangeCreationDate(createdAt).setChangeData(new FieldDiffs().setDiff("f_change_1", "a", "b").setDiff("f_change_2", "c", null).setDiff("f_change_3", null, null).setDiff("f_change_4", null, "e").toEncodedString()));
    FormattingContext formattingContext = underTest.newFormattingContext(dbTester.getSession(), singleton(issue1), load);
    List<Changelog> wsChangelogList = underTest.formatChangelog(issue1, formattingContext).collect(Collectors.toList());
    assertThat(wsChangelogList).hasSize(1);
    Changelog wsChangelog = wsChangelogList.iterator().next();
    assertThat(wsChangelog.getCreationDate()).isEqualTo(formatDateTime(createdAt));
    assertThat(wsChangelog.getDiffsList()).hasSize(4);
    assertThat(wsChangelog.getDiffsList().get(0).getKey()).isEqualTo("f_change_1");
    assertThat(wsChangelog.getDiffsList().get(0).getOldValue()).isEqualTo("a");
    assertThat(wsChangelog.getDiffsList().get(0).getNewValue()).isEqualTo("b");
    assertThat(wsChangelog.getDiffsList().get(1).getKey()).isEqualTo("f_change_2");
    assertThat(wsChangelog.getDiffsList().get(1).getOldValue()).isEqualTo("c");
    assertThat(wsChangelog.getDiffsList().get(1).hasNewValue()).isFalse();
    assertThat(wsChangelog.getDiffsList().get(2).getKey()).isEqualTo("f_change_3");
    assertThat(wsChangelog.getDiffsList().get(2).hasOldValue()).isFalse();
    assertThat(wsChangelog.getDiffsList().get(2).hasNewValue()).isFalse();
    assertThat(wsChangelog.getDiffsList().get(3).getKey()).isEqualTo("f_change_4");
    assertThat(wsChangelog.getDiffsList().get(3).hasOldValue()).isFalse();
    assertThat(wsChangelog.getDiffsList().get(3).getNewValue()).isEqualTo("e");
}
Also used : IssueChangeDto(org.sonar.db.issue.IssueChangeDto) FormattingContext(org.sonar.server.issue.IssueChangeWSSupport.FormattingContext) FieldDiffs(org.sonar.core.issue.FieldDiffs) IssueDto(org.sonar.db.issue.IssueDto) Changelog(org.sonarqube.ws.Common.Changelog) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 25 with FormattingContext

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

the class ShowActionTest method returns_user_details_of_users_from_ChangelogAndComments.

@Test
public void returns_user_details_of_users_from_ChangelogAndComments() {
    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);
    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(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