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.

IssueQueryTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.server.issue.index;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.google.common.collect.Lists;
  23. import java.util.Date;
  24. import org.junit.Test;
  25. import org.sonar.api.issue.Issue;
  26. import org.sonar.api.rule.Severity;
  27. import org.sonar.core.util.Uuids;
  28. import org.sonar.db.rule.RuleDefinitionDto;
  29. import org.sonar.server.issue.index.IssueQuery.PeriodStart;
  30. import static com.google.common.collect.Lists.newArrayList;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.entry;
  33. public class IssueQueryTest {
  34. @Test
  35. public void build_query() {
  36. RuleDefinitionDto rule = new RuleDefinitionDto().setUuid(Uuids.createFast());
  37. PeriodStart filterDate = new IssueQuery.PeriodStart(new Date(10_000_000_000L), false);
  38. IssueQuery query = IssueQuery.builder()
  39. .issueKeys(newArrayList("ABCDE"))
  40. .severities(newArrayList(Severity.BLOCKER))
  41. .statuses(Lists.newArrayList(Issue.STATUS_RESOLVED))
  42. .resolutions(newArrayList(Issue.RESOLUTION_FALSE_POSITIVE))
  43. .projectUuids(newArrayList("PROJECT"))
  44. .componentUuids(newArrayList("org/struts/Action.java"))
  45. .moduleUuids(newArrayList("org.struts:core"))
  46. .rules(newArrayList(rule))
  47. .assigneeUuids(newArrayList("gargantua"))
  48. .languages(newArrayList("xoo"))
  49. .tags(newArrayList("tag1", "tag2"))
  50. .types(newArrayList("RELIABILITY", "SECURITY"))
  51. .owaspTop10(newArrayList("a1", "a2"))
  52. .sansTop25(newArrayList("insecure-interaction", "porous-defenses"))
  53. .cwe(newArrayList("12", "125"))
  54. .branchUuid("my_branch")
  55. .createdAfterByProjectUuids(ImmutableMap.of("PROJECT", filterDate))
  56. .assigned(true)
  57. .createdAfter(new Date())
  58. .createdBefore(new Date())
  59. .createdAt(new Date())
  60. .resolved(true)
  61. .sort(IssueQuery.SORT_BY_CREATION_DATE)
  62. .asc(true)
  63. .build();
  64. assertThat(query.issueKeys()).containsOnly("ABCDE");
  65. assertThat(query.severities()).containsOnly(Severity.BLOCKER);
  66. assertThat(query.statuses()).containsOnly(Issue.STATUS_RESOLVED);
  67. assertThat(query.resolutions()).containsOnly(Issue.RESOLUTION_FALSE_POSITIVE);
  68. assertThat(query.projectUuids()).containsOnly("PROJECT");
  69. assertThat(query.componentUuids()).containsOnly("org/struts/Action.java");
  70. assertThat(query.moduleUuids()).containsOnly("org.struts:core");
  71. assertThat(query.assignees()).containsOnly("gargantua");
  72. assertThat(query.languages()).containsOnly("xoo");
  73. assertThat(query.tags()).containsOnly("tag1", "tag2");
  74. assertThat(query.types()).containsOnly("RELIABILITY", "SECURITY");
  75. assertThat(query.owaspTop10()).containsOnly("a1", "a2");
  76. assertThat(query.sansTop25()).containsOnly("insecure-interaction", "porous-defenses");
  77. assertThat(query.cwe()).containsOnly("12", "125");
  78. assertThat(query.branchUuid()).isEqualTo("my_branch");
  79. assertThat(query.createdAfterByProjectUuids()).containsOnly(entry("PROJECT", filterDate));
  80. assertThat(query.assigned()).isTrue();
  81. assertThat(query.rules()).containsOnly(rule);
  82. assertThat(query.createdAfter()).isNotNull();
  83. assertThat(query.createdBefore()).isNotNull();
  84. assertThat(query.createdAt()).isNotNull();
  85. assertThat(query.resolved()).isTrue();
  86. assertThat(query.sort()).isEqualTo(IssueQuery.SORT_BY_CREATION_DATE);
  87. assertThat(query.asc()).isTrue();
  88. }
  89. @Test
  90. public void build_query_without_dates() {
  91. IssueQuery query = IssueQuery.builder()
  92. .issueKeys(newArrayList("ABCDE"))
  93. .createdAfter(null)
  94. .createdBefore(null)
  95. .createdAt(null)
  96. .build();
  97. assertThat(query.issueKeys()).containsOnly("ABCDE");
  98. assertThat(query.createdAfter()).isNull();
  99. assertThat(query.createdBefore()).isNull();
  100. assertThat(query.createdAt()).isNull();
  101. }
  102. @Test
  103. public void throw_exception_if_sort_is_not_valid() {
  104. try {
  105. IssueQuery.builder()
  106. .sort("UNKNOWN")
  107. .build();
  108. } catch (Exception e) {
  109. assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Bad sort field: UNKNOWN");
  110. }
  111. }
  112. @Test
  113. public void collection_params_should_not_be_null_but_empty() {
  114. IssueQuery query = IssueQuery.builder()
  115. .issueKeys(null)
  116. .projectUuids(null)
  117. .componentUuids(null)
  118. .moduleUuids(null)
  119. .statuses(null)
  120. .assigneeUuids(null)
  121. .resolutions(null)
  122. .rules(null)
  123. .severities(null)
  124. .languages(null)
  125. .tags(null)
  126. .types(null)
  127. .owaspTop10(null)
  128. .sansTop25(null)
  129. .cwe(null)
  130. .createdAfterByProjectUuids(null)
  131. .build();
  132. assertThat(query.issueKeys()).isEmpty();
  133. assertThat(query.projectUuids()).isEmpty();
  134. assertThat(query.componentUuids()).isEmpty();
  135. assertThat(query.moduleUuids()).isEmpty();
  136. assertThat(query.statuses()).isEmpty();
  137. assertThat(query.assignees()).isEmpty();
  138. assertThat(query.resolutions()).isEmpty();
  139. assertThat(query.rules()).isEmpty();
  140. assertThat(query.severities()).isEmpty();
  141. assertThat(query.languages()).isEmpty();
  142. assertThat(query.tags()).isEmpty();
  143. assertThat(query.types()).isEmpty();
  144. assertThat(query.owaspTop10()).isEmpty();
  145. assertThat(query.sansTop25()).isEmpty();
  146. assertThat(query.cwe()).isEmpty();
  147. assertThat(query.createdAfterByProjectUuids()).isEmpty();
  148. }
  149. @Test
  150. public void test_default_query() {
  151. IssueQuery query = IssueQuery.builder().build();
  152. assertThat(query.issueKeys()).isEmpty();
  153. assertThat(query.projectUuids()).isEmpty();
  154. assertThat(query.componentUuids()).isEmpty();
  155. assertThat(query.moduleUuids()).isEmpty();
  156. assertThat(query.statuses()).isEmpty();
  157. assertThat(query.assignees()).isEmpty();
  158. assertThat(query.resolutions()).isEmpty();
  159. assertThat(query.rules()).isEmpty();
  160. assertThat(query.severities()).isEmpty();
  161. assertThat(query.languages()).isEmpty();
  162. assertThat(query.tags()).isEmpty();
  163. assertThat(query.types()).isEmpty();
  164. assertThat(query.branchUuid()).isNull();
  165. assertThat(query.assigned()).isNull();
  166. assertThat(query.createdAfter()).isNull();
  167. assertThat(query.createdBefore()).isNull();
  168. assertThat(query.resolved()).isNull();
  169. assertThat(query.sort()).isNull();
  170. assertThat(query.createdAfterByProjectUuids()).isEmpty();
  171. }
  172. @Test
  173. public void should_accept_null_sort() {
  174. IssueQuery query = IssueQuery.builder().sort(null).build();
  175. assertThat(query.sort()).isNull();
  176. }
  177. }