3 * Copyright (C) 2009-2024 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.RuleDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange;
31 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue;
32 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project;
33 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Rule;
34 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User;
35 import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.UserChange;
37 import static com.google.common.base.Preconditions.checkArgument;
38 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
39 import static org.sonar.api.rules.RuleType.BUG;
40 import static org.sonar.api.rules.RuleType.CODE_SMELL;
41 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
42 import static org.sonar.api.rules.RuleType.VULNERABILITY;
44 public class IssuesChangesNotificationBuilderTesting {
46 private static final RuleType[] RULE_TYPES = {CODE_SMELL, BUG, VULNERABILITY, SECURITY_HOTSPOT};
48 private IssuesChangesNotificationBuilderTesting() {
51 public static Rule ruleOf(RuleDto rule) {
52 return new Rule(rule.getKey(), RuleType.valueOfNullable(rule.getType()), rule.getName());
55 public static User userOf(UserDto changeAuthor) {
56 return new User(changeAuthor.getUuid(), changeAuthor.getLogin(), changeAuthor.getName());
59 public static Project projectBranchOf(DbTester db, ComponentDto branch) {
60 BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), branch.uuid()).get();
61 checkArgument(!branchDto.isMain(), "should be a branch");
62 return new Project.Builder(branch.uuid())
63 .setKey(branch.getKey())
64 .setProjectName(branch.name())
65 .setBranchName(branchDto.getKey())
69 public static Project projectOf(ComponentDto project) {
70 return new Project.Builder(project.uuid())
71 .setKey(project.getKey())
72 .setProjectName(project.name())
76 static ChangedIssue newChangedIssue(String key, Project project, Rule rule) {
77 return new ChangedIssue.Builder(key)
78 .setNewStatus(randomAlphabetic(19))
84 static ChangedIssue newChangedIssue(String key, String status, Project project, String ruleName, RuleType ruleType) {
85 return newChangedIssue(key, status, project, newRule(ruleName, ruleType));
88 static ChangedIssue newChangedIssue(String key, String status, Project project, Rule rule) {
89 return new ChangedIssue.Builder(key)
96 static Rule newRule(String ruleName, RuleType ruleType) {
97 return new Rule(RuleKey.of(randomAlphabetic(6), randomAlphabetic(7)), ruleType, ruleName);
100 static Rule newRandomNotAHotspotRule(String ruleName) {
101 return newRule(ruleName, randomRuleTypeHotspotExcluded());
104 static Rule newSecurityHotspotRule(String ruleName) {
105 return newRule(ruleName, SECURITY_HOTSPOT);
108 static Project newProject(String uuid) {
109 return new Project.Builder(uuid).setProjectName(uuid + "_name").setKey(uuid + "_key").build();
112 static Project newBranch(String uuid, String branchName) {
113 return new Project.Builder(uuid).setProjectName(uuid + "_name").setKey(uuid + "_key").setBranchName(branchName).build();
116 static UserChange newUserChange() {
117 return new UserChange(new Random().nextLong(), new User(randomAlphabetic(4), randomAlphabetic(5), randomAlphabetic(6)));
120 static AnalysisChange newAnalysisChange() {
121 return new AnalysisChange(new Random().nextLong());
124 static RuleType randomRuleTypeHotspotExcluded() {
125 return RULE_TYPES[new Random().nextInt(RULE_TYPES.length - 1)];