3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.issue.notification;
22 import java.util.Random;
23 import org.sonar.api.rule.RuleKey;
24 import org.sonar.api.rules.RuleType;
25 import org.sonar.db.DbTester;
26 import org.sonar.db.component.BranchDto;
27 import org.sonar.db.component.ComponentDto;
28 import org.sonar.db.rule.RuleDefinitionDto;
29 import org.sonar.db.rule.RuleDto;
30 import org.sonar.db.user.UserDto;
31 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange;
32 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue;
33 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project;
34 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule;
35 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User;
36 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange;
38 import static com.google.common.base.Preconditions.checkArgument;
39 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
40 import static org.sonar.api.rules.RuleType.BUG;
41 import static org.sonar.api.rules.RuleType.CODE_SMELL;
42 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
43 import static org.sonar.api.rules.RuleType.VULNERABILITY;
45 public class IssuesChangesNotificationBuilderTesting {
47 private static final RuleType[] RULE_TYPES = {CODE_SMELL, BUG, VULNERABILITY, SECURITY_HOTSPOT};
49 private IssuesChangesNotificationBuilderTesting() {
52 public static Rule ruleOf(RuleDto rule) {
53 return new Rule(rule.getKey(), RuleType.valueOfNullable(rule.getType()), rule.getName());
56 public static Rule ruleOf(RuleDefinitionDto rule) {
57 return new Rule(rule.getKey(), RuleType.valueOfNullable(rule.getType()), rule.getName());
60 public static User userOf(UserDto changeAuthor) {
61 return new User(changeAuthor.getUuid(), changeAuthor.getLogin(), changeAuthor.getName());
64 public static Project projectBranchOf(DbTester db, ComponentDto branch) {
65 BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid()).get();
66 checkArgument(!branchDto.isMain(), "should be a branch");
67 return new Project.Builder(branch.uuid())
68 .setKey(branch.getKey())
69 .setProjectName(branch.name())
70 .setBranchName(branchDto.getKey())
74 public static Project projectOf(ComponentDto project) {
75 return new Project.Builder(project.uuid())
76 .setKey(project.getKey())
77 .setProjectName(project.name())
81 static ChangedIssue newChangedIssue(String key, Project project, Rule rule) {
82 return new ChangedIssue.Builder(key)
83 .setNewStatus(randomAlphabetic(19))
89 static ChangedIssue newChangedIssue(String key, String status, Project project, String ruleName, RuleType ruleType) {
90 return newChangedIssue(key, status, project, newRule(ruleName, ruleType));
93 static ChangedIssue newChangedIssue(String key, String status, Project project, Rule rule) {
94 return new ChangedIssue.Builder(key)
101 static Rule newRule(String ruleName, RuleType ruleType) {
102 return new Rule(RuleKey.of(randomAlphabetic(6), randomAlphabetic(7)), ruleType, ruleName);
105 static Rule newRandomNotAHotspotRule(String ruleName) {
106 return newRule(ruleName, randomRuleTypeHotspotExcluded());
109 static Rule newSecurityHotspotRule(String ruleName) {
110 return newRule(ruleName, SECURITY_HOTSPOT);
113 static Project newProject(String uuid) {
114 return new Project.Builder(uuid).setProjectName(uuid + "_name").setKey(uuid + "_key").build();
117 static Project newBranch(String uuid, String branchName) {
118 return new Project.Builder(uuid).setProjectName(uuid + "_name").setKey(uuid + "_key").setBranchName(branchName).build();
121 static UserChange newUserChange() {
122 return new UserChange(new Random().nextLong(), new User(randomAlphabetic(4), randomAlphabetic(5), randomAlphabetic(6)));
125 static AnalysisChange newAnalysisChange() {
126 return new AnalysisChange(new Random().nextLong());
129 static RuleType randomRuleTypeHotspotExcluded() {
130 return RULE_TYPES[new Random().nextInt(RULE_TYPES.length - 1)];