Search in sources :

Example 16 with ChangedIssue

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.

the class IssueChangesEmailTemplate method addIssuesByRule.

void addIssuesByRule(StringBuilder sb, Collection<ChangedIssue> changedIssues, BiConsumer<StringBuilder, Collection<ChangedIssue>> issuePageHref) {
    ListMultimap<Rule, ChangedIssue> issuesByRule = changedIssues.stream().collect(index(ChangedIssue::getRule, t -> t));
    Iterator<Rule> rules = issuesByRule.keySet().stream().sorted(RULE_COMPARATOR).iterator();
    if (!rules.hasNext()) {
        return;
    }
    sb.append("<ul>");
    while (rules.hasNext()) {
        Rule rule = rules.next();
        Collection<ChangedIssue> issues = issuesByRule.get(rule);
        sb.append("<li>").append("Rule ").append(" <em>").append(rule.getName()).append("</em> - ");
        appendIssueLinks(sb, issuePageHref, issues, rule.getRuleType());
        sb.append("</li>");
    }
    sb.append("</ul>");
}
Also used : SortedSet(java.util.SortedSet) ListMultimap(com.google.common.collect.ListMultimap) RuleGroup.formatIssuesOrHotspots(org.sonar.server.issue.notification.RuleGroup.formatIssuesOrHotspots) I18n(org.sonar.core.i18n.I18n) RuleGroup.resolveGroup(org.sonar.server.issue.notification.RuleGroup.resolveGroup) URLEncoder.encode(java.net.URLEncoder.encode) RuleType(org.sonar.api.rules.RuleType) Lists(com.google.common.collect.Lists) MoreCollectors.index(org.sonar.core.util.stream.MoreCollectors.index) ImmutableList(com.google.common.collect.ImmutableList) EmailSettings(org.sonar.api.config.EmailSettings) Locale(java.util.Locale) SECURITY_HOTSPOTS(org.sonar.server.issue.notification.RuleGroup.SECURITY_HOTSPOTS) BiConsumer(java.util.function.BiConsumer) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) Nullable(javax.annotation.Nullable) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Iterator(java.util.Iterator) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) Set(java.util.Set) SetMultimap(com.google.common.collect.SetMultimap) RuleGroup.formatIssueOrHotspot(org.sonar.server.issue.notification.RuleGroup.formatIssueOrHotspot) Consumer(java.util.function.Consumer) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) ISSUES(org.sonar.server.issue.notification.RuleGroup.ISSUES) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule)

Example 17 with ChangedIssue

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.

the class ChangesOnMyIssuesNotificationTest method equals_is_based_on_change_and_issues.

@Test
public void equals_is_based_on_change_and_issues() {
    AnalysisChange analysisChange = new AnalysisChange(new Random().nextLong());
    ChangedIssue changedIssue = IssuesChangesNotificationBuilderTesting.newChangedIssue("doo", IssuesChangesNotificationBuilderTesting.newProject("prj"), newRandomNotAHotspotRule("rul"));
    ChangesOnMyIssuesNotification underTest = new ChangesOnMyIssuesNotification(analysisChange, ImmutableSet.of(changedIssue));
    assertThat(underTest).isEqualTo(new ChangesOnMyIssuesNotification(analysisChange, ImmutableSet.of(changedIssue))).isNotEqualTo(mock(Notification.class)).isNotNull().isNotEqualTo(new ChangesOnMyIssuesNotification(new AnalysisChange(analysisChange.getDate() + 10), ImmutableSet.of(changedIssue))).isNotEqualTo(new ChangesOnMyIssuesNotification(analysisChange, ImmutableSet.of()));
}
Also used : Random(java.util.Random) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) Test(org.junit.Test)

Example 18 with ChangedIssue

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.

the class FpOrWontFixEmailTemplateTest method formats_returns_html_message_for_multiple_hotspots_of_same_rule_on_same_project_on_branch.

@Test
@UseDataProvider("fpOrWontFixValuesByUserOrAnalysisChange")
public void formats_returns_html_message_for_multiple_hotspots_of_same_rule_on_same_project_on_branch(Change change, FpOrWontFix fpOrWontFix) {
    String branchName = randomAlphabetic(19);
    Project project = newBranch("1", branchName);
    String ruleName = randomAlphabetic(8);
    String host = randomAlphabetic(15);
    Rule rule = newSecurityHotspotRule(ruleName);
    List<ChangedIssue> changedIssues = IntStream.range(0, 2 + new Random().nextInt(5)).mapToObj(i -> newChangedIssue("issue_" + i, project, rule)).collect(toList());
    when(emailSettings.getServerBaseURL()).thenReturn(host);
    EmailMessage emailMessage = underTest.format(new FPOrWontFixNotification(change, ImmutableSet.copyOf(changedIssues), fpOrWontFix));
    String expectedHref = host + "/project/issues?id=" + project.getKey() + "&branch=" + branchName + "&issues=" + changedIssues.stream().map(ChangedIssue::getKey).collect(joining("%2C"));
    String expectedLinkText = "See all " + changedIssues.size() + " hotspots";
    HtmlFragmentAssert.assertThat(emailMessage.getMessage()).hasParagraph().hasParagraph().hasParagraph(project.getProjectName() + ", " + branchName).hasList("Rule " + ruleName + " - " + expectedLinkText).withLink(expectedLinkText, expectedHref).hasParagraph().hasParagraph().noMoreBlock();
}
Also used : IntStream(java.util.stream.IntStream) HtmlFragmentAssert(org.sonar.test.html.HtmlFragmentAssert) I18n(org.sonar.core.i18n.I18n) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) IssuesChangesNotificationBuilderTesting.newSecurityHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newSecurityHotspotRule) RuleType(org.sonar.api.rules.RuleType) FpOrWontFix(org.sonar.server.issue.notification.FPOrWontFixNotification.FpOrWontFix) EmailSettings(org.sonar.api.config.EmailSettings) Locale(java.util.Locale) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) SECURITY_HOTSPOT(org.sonar.api.rules.RuleType.SECURITY_HOTSPOT) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ImmutableSet(com.google.common.collect.ImmutableSet) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Notification(org.sonar.api.notifications.Notification) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) Collectors.joining(java.util.stream.Collectors.joining) IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) WONT_FIX(org.sonar.server.issue.notification.FPOrWontFixNotification.FpOrWontFix.WONT_FIX) Stream(java.util.stream.Stream) RuleKey(org.sonar.api.rule.RuleKey) Change(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Change) IssuesChangesNotificationBuilderTesting.randomRuleTypeHotspotExcluded(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.randomRuleTypeHotspotExcluded) FP(org.sonar.server.issue.notification.FPOrWontFixNotification.FpOrWontFix.FP) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) Random(java.util.Random) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) IssuesChangesNotificationBuilderTesting.newSecurityHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newSecurityHotspotRule) IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 19 with ChangedIssue

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.

the class FpOrWontFixEmailTemplateTest method formats_returns_html_message_for_single_hotspot_on_branch.

@Test
@UseDataProvider("fpOrWontFixValuesByUserOrAnalysisChange")
public void formats_returns_html_message_for_single_hotspot_on_branch(Change change, FpOrWontFix fpOrWontFix) {
    String branchName = randomAlphabetic(6);
    Project project = newBranch("1", branchName);
    String ruleName = randomAlphabetic(8);
    String host = randomAlphabetic(15);
    String key = "key";
    ChangedIssue changedIssue = newChangedIssue(key, project, ruleName, SECURITY_HOTSPOT);
    when(emailSettings.getServerBaseURL()).thenReturn(host);
    EmailMessage emailMessage = underTest.format(new FPOrWontFixNotification(change, ImmutableSet.of(changedIssue), fpOrWontFix));
    HtmlFragmentAssert.assertThat(emailMessage.getMessage()).hasParagraph().hasParagraph().hasParagraph(project.getProjectName() + ", " + branchName).hasList("Rule " + ruleName + " - See the single hotspot").withLink("See the single hotspot", host + "/project/issues?id=" + project.getKey() + "&branch=" + branchName + "&issues=" + changedIssue.getKey() + "&open=" + changedIssue.getKey()).hasParagraph().hasParagraph().noMoreBlock();
}
Also used : Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 20 with ChangedIssue

use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.

the class ChangesOnMyIssueNotificationHandlerTest method deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications.

@Test
public void deliver_checks_by_projectKey_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications() {
    when(emailNotificationChannel.isActivated()).thenReturn(true);
    Project project = newProject();
    Set<ChangedIssue> issues = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(i -> new ChangedIssue.Builder("issue_key_" + i).setNewStatus("foo").setAssignee(newUser("assignee_" + i)).setRule(newRule()).setProject(project).build()).collect(toSet());
    IssuesChangesNotificationBuilder builder = new IssuesChangesNotificationBuilder(issues, new UserChange(new Random().nextLong(), new User("user_uuid", "user_login", null)));
    int deliver = underTest.deliver(ImmutableSet.of(serializer.serialize(builder)));
    assertThat(deliver).isZero();
    Set<String> assigneeLogins = issues.stream().map(i -> i.getAssignee().get().getLogin()).collect(toSet());
    verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project.getKey(), assigneeLogins, ALL_MUST_HAVE_ROLE_USER);
    verifyNoMoreInteractions(notificationManager);
    verify(emailNotificationChannel).isActivated();
    verifyNoMoreInteractions(emailNotificationChannel);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) ListMultimap(com.google.common.collect.ListMultimap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Random(java.util.Random) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) EmailDeliveryRequest(org.sonar.server.notification.email.EmailNotificationChannel.EmailDeliveryRequest) MoreCollectors.index(org.sonar.core.util.stream.MoreCollectors.index) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) AnalysisChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange) NotificationManager(org.sonar.server.notification.NotificationManager) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) MoreCollectors.unorderedIndex(org.sonar.core.util.stream.MoreCollectors.unorderedIndex) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) ImmutableSet(com.google.common.collect.ImmutableSet) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) PER_PROJECT_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION) Mockito.verify(org.mockito.Mockito.verify) IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule) Mockito(org.mockito.Mockito) Rule(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule) List(java.util.List) Stream(java.util.stream.Stream) GLOBAL_NOTIFICATION(org.sonar.server.notification.NotificationDispatcherMetadata.GLOBAL_NOTIFICATION) Change(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Change) NotificationDispatcherMetadata(org.sonar.server.notification.NotificationDispatcherMetadata) EmailNotificationChannel(org.sonar.server.notification.email.EmailNotificationChannel) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Project(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project) UserChange(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange) User(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User) Random(java.util.Random) ChangedIssue(org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue) Test(org.junit.Test)

Aggregations

ChangedIssue (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue)71 Test (org.junit.Test)65 Project (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project)54 UserChange (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange)47 Random (java.util.Random)45 AnalysisChange (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange)44 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)43 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)43 Mockito.mock (org.mockito.Mockito.mock)40 IntStream (java.util.stream.IntStream)39 Rule (org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule)39 ImmutableSet (com.google.common.collect.ImmutableSet)38 Collections (java.util.Collections)38 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)37 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)37 List (java.util.List)37 RunWith (org.junit.runner.RunWith)37 IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule (org.sonar.server.issue.notification.IssuesChangesNotificationBuilderTesting.newRandomNotAHotspotRule)37 Set (java.util.Set)36 Stream (java.util.stream.Stream)36