aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueTesting.java
blob: 7d9b8f4283a152ffcfb3f414ba58693053a2c680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.issue;

import java.security.SecureRandom;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.db.component.ComponentQualifiers;
import org.sonar.api.rule.Severity;
import org.sonar.core.util.UuidFactoryFast;
import org.sonar.core.util.Uuids;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.rule.RuleDto;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Sets.newHashSet;
import static org.apache.commons.lang3.RandomStringUtils.secure;

public class IssueTesting {

  private static final Random RANDOM = new SecureRandom();

  private IssueTesting() {
    // only statics
  }

  public static IssueDto newIssue(RuleDto rule, ComponentDto branch, ComponentDto file) {
    checkArgument(branch.qualifier().equals(ComponentQualifiers.PROJECT), "Second parameter should be a branch that belongs to a project");
    return newIssue(rule, branch.uuid(), branch.getKey(), file);
  }

  public static IssueDto newIssue(RuleDto rule, BranchDto branch, ComponentDto file) {
    return newIssue(rule, branch.getUuid(), branch.getKey(), file);
  }

  public static IssueDto newIssue(RuleDto rule, String branchUuid, String projectKey, ComponentDto file) {
    //checkArgument(file.branchUuid().equals(branchUuid), "The file doesn't belong to the project");

    return new IssueDto()
      .setKee(Uuids.createFast())
      .setRule(rule)
      .setType(rule.getType())
      .setProjectUuid(branchUuid)
      .setProjectKey(projectKey)
      .setComponent(file)
      .setStatus(Issue.STATUS_OPEN)
      .setResolution(null)
      .setSeverity(Severity.ALL.get(RANDOM.nextInt(Severity.ALL.size())))
      //TODO map to correct impact. Will be fixed with persistence of impacts on issues
      .addImpact(new ImpactDto().setSoftwareQuality(SoftwareQuality.MAINTAINABILITY).setSeverity(org.sonar.api.issue.impact.Severity.HIGH))
      .setEffort((long) RANDOM.nextInt(10))
      .setAssigneeUuid("assignee-uuid_" + secure().nextAlphabetic(26))
      .setAuthorLogin("author_" + secure().nextAlphabetic(5))
      // Starting from 1 in order to never get 0 (as it's a forbidden value)
      .setLine(RANDOM.nextInt(1, 1_001))
      .setMessage("message_" + secure().nextAlphabetic(5))
      .setChecksum("checksum_" + secure().nextAlphabetic(5))
      .setTags(newHashSet("tag_" + secure().nextAlphanumeric(5), "tag_" + secure().nextAlphanumeric(5)))
      .setRuleDescriptionContextKey("context_" + secure().nextAlphabetic(5))
      .setIssueCreationDate(new Date(System.currentTimeMillis() - 2_000))
      .setIssueUpdateDate(new Date(System.currentTimeMillis() - 1_500))
      .setCreatedAt(System.currentTimeMillis() - 1_000)
      .setUpdatedAt(System.currentTimeMillis() - 500);
  }

  public static IssueChangeDto newIssueChangeDto(IssueDto issue) {
    return new IssueChangeDto()
      .setUuid(UuidFactoryFast.getInstance().create())
      .setKey(UuidFactoryFast.getInstance().create())
      .setIssueKey(issue.getKey())
      .setChangeData("data_" + secure().nextAlphanumeric(40))
      .setChangeType(IssueChangeDto.TYPE_FIELD_CHANGE)
      .setUserUuid("userUuid_" + secure().nextAlphanumeric(40))
      .setProjectUuid(issue.getProjectUuid())
      .setIssueChangeCreationDate(RANDOM.nextLong(Long.MAX_VALUE))
      .setCreatedAt(RANDOM.nextLong(Long.MAX_VALUE))
      .setUpdatedAt(RANDOM.nextLong(Long.MAX_VALUE));
  }

  public static NewCodeReferenceIssueDto newCodeReferenceIssue(IssueDto issue) {
    return new NewCodeReferenceIssueDto()
      .setUuid(Uuids.createFast())
      .setIssueKey(issue.getKey())
      .setCreatedAt(1_400_000_000_000L);
  }

  public static List<IssueDto> generateIssues(int total, Function<Integer, IssueDto> issueGenerator) {
    return Stream.iterate(0, i -> i + 1)
      .map(issueGenerator)
      .limit(total)
      .toList();
  }

}