You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IssueTesting.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.db.issue;
  21. import java.util.Date;
  22. import org.apache.commons.lang.math.RandomUtils;
  23. import org.sonar.api.issue.Issue;
  24. import org.sonar.api.resources.Qualifiers;
  25. import org.sonar.api.rule.Severity;
  26. import org.sonar.api.rules.RuleType;
  27. import org.sonar.api.utils.DateUtils;
  28. import org.sonar.core.util.UuidFactoryFast;
  29. import org.sonar.core.util.Uuids;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.rule.RuleDefinitionDto;
  32. import org.sonar.db.rule.RuleDto;
  33. import static com.google.common.base.Preconditions.checkArgument;
  34. import static com.google.common.collect.Sets.newHashSet;
  35. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  36. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  37. import static org.apache.commons.lang.math.RandomUtils.nextInt;
  38. import static org.apache.commons.lang.math.RandomUtils.nextLong;
  39. public class IssueTesting {
  40. private IssueTesting() {
  41. // only statics
  42. }
  43. public static IssueDto newIssue(RuleDefinitionDto rule, ComponentDto project, ComponentDto file) {
  44. checkArgument(project.qualifier().equals(Qualifiers.PROJECT), "Second parameter should be a project");
  45. checkArgument(file.projectUuid().equals(project.uuid()), "The file doesn't belong to the project");
  46. return new IssueDto()
  47. .setKee("uuid_" + randomAlphabetic(5))
  48. .setRule(rule)
  49. .setType(RuleType.values()[nextInt(RuleType.values().length)])
  50. .setProject(project)
  51. .setComponent(file)
  52. .setStatus(Issue.STATUS_OPEN)
  53. .setResolution(null)
  54. .setSeverity(Severity.ALL.get(nextInt(Severity.ALL.size())))
  55. .setEffort((long) RandomUtils.nextInt(10))
  56. .setAssigneeUuid("assignee-uuid_" + randomAlphabetic(26))
  57. .setAuthorLogin("author_" + randomAlphabetic(5))
  58. // Adding one to the generated random value in order to never get 0 (as it's a forbidden value)
  59. .setLine(nextInt(1_000) + 1)
  60. .setMessage("message_" + randomAlphabetic(5))
  61. .setChecksum("checksum_" + randomAlphabetic(5))
  62. .setTags(newHashSet("tag_" + randomAlphanumeric(5), "tag_" + randomAlphanumeric(5)))
  63. .setIssueCreationDate(new Date(System.currentTimeMillis() - 2_000))
  64. .setIssueUpdateDate(new Date(System.currentTimeMillis() - 1_500))
  65. .setCreatedAt(System.currentTimeMillis() - 1_000)
  66. .setUpdatedAt(System.currentTimeMillis() - 500);
  67. }
  68. public static IssueChangeDto newIssuechangeDto(IssueDto issue) {
  69. return new IssueChangeDto()
  70. .setKey(UuidFactoryFast.getInstance().create())
  71. .setIssueKey(issue.getKey())
  72. .setChangeData("data_" + randomAlphanumeric(40))
  73. .setChangeType(IssueChangeDto.TYPE_FIELD_CHANGE)
  74. .setUserUuid("userUuid_" + randomAlphanumeric(40))
  75. .setIssueChangeCreationDate(nextLong())
  76. .setCreatedAt(nextLong())
  77. .setUpdatedAt(nextLong());
  78. }
  79. /**
  80. * @deprecated use newIssue(...)
  81. */
  82. @Deprecated
  83. public static IssueDto newDto(RuleDto rule, ComponentDto file, ComponentDto project) {
  84. return new IssueDto()
  85. .setKee(Uuids.createFast())
  86. .setRule(rule.getDefinition())
  87. .setType(RuleType.CODE_SMELL)
  88. .setComponent(file)
  89. .setProject(project)
  90. .setStatus(Issue.STATUS_OPEN)
  91. .setResolution(null)
  92. .setSeverity(Severity.MAJOR)
  93. .setEffort(10L)
  94. .setIssueCreationDate(DateUtils.parseDate("2014-09-04"))
  95. .setIssueUpdateDate(DateUtils.parseDate("2014-12-04"))
  96. .setCreatedAt(1_400_000_000_000L)
  97. .setUpdatedAt(1_400_000_000_000L);
  98. }
  99. }