您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IssueIndexFacetsTest.java 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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 java.time.ZoneId;
  22. import java.util.Collections;
  23. import java.util.Date;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import org.elasticsearch.action.search.SearchResponse;
  27. import org.junit.Test;
  28. import org.sonar.api.issue.impact.Severity;
  29. import org.sonar.api.rules.RuleType;
  30. import org.sonar.api.server.rule.RulesDefinition.OwaspAsvsVersion;
  31. import org.sonar.core.issue.status.SimpleStatus;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.rule.RuleDto;
  34. import org.sonar.server.es.Facets;
  35. import org.sonar.server.es.SearchOptions;
  36. import org.sonar.server.security.SecurityStandards.SQCategory;
  37. import static java.util.Arrays.asList;
  38. import static java.util.Collections.singletonList;
  39. import static java.util.stream.IntStream.rangeClosed;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.assertj.core.api.Assertions.entry;
  42. import static org.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE;
  43. import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
  44. import static org.sonar.api.issue.Issue.RESOLUTION_REMOVED;
  45. import static org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX;
  46. import static org.sonar.api.issue.Issue.STATUS_CLOSED;
  47. import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
  48. import static org.sonar.api.issue.Issue.STATUS_OPEN;
  49. import static org.sonar.api.issue.Issue.STATUS_REOPENED;
  50. import static org.sonar.api.issue.Issue.STATUS_RESOLVED;
  51. import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY;
  52. import static org.sonar.api.issue.impact.SoftwareQuality.RELIABILITY;
  53. import static org.sonar.api.rule.Severity.BLOCKER;
  54. import static org.sonar.api.rule.Severity.CRITICAL;
  55. import static org.sonar.api.rule.Severity.INFO;
  56. import static org.sonar.api.rule.Severity.MAJOR;
  57. import static org.sonar.api.rule.Severity.MINOR;
  58. import static org.sonar.api.rules.CleanCodeAttributeCategory.ADAPTABLE;
  59. import static org.sonar.api.rules.CleanCodeAttributeCategory.CONSISTENT;
  60. import static org.sonar.api.rules.CleanCodeAttributeCategory.INTENTIONAL;
  61. import static org.sonar.api.rules.CleanCodeAttributeCategory.RESPONSIBLE;
  62. import static org.sonar.api.server.rule.RulesDefinition.OwaspTop10Version.Y2017;
  63. import static org.sonar.api.server.rule.RulesDefinition.OwaspTop10Version.Y2021;
  64. import static org.sonar.api.server.rule.RulesDefinition.PciDssVersion.V3_2;
  65. import static org.sonar.api.server.rule.RulesDefinition.PciDssVersion.V4_0;
  66. import static org.sonar.api.utils.DateUtils.parseDateTime;
  67. import static org.sonar.db.component.ComponentTesting.newDirectory;
  68. import static org.sonar.db.component.ComponentTesting.newFileDto;
  69. import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
  70. import static org.sonar.db.rule.RuleTesting.newRule;
  71. import static org.sonar.server.issue.IssueDocTesting.newDoc;
  72. import static org.sonar.server.issue.IssueDocTesting.newDocForProject;
  73. public class IssueIndexFacetsTest extends IssueIndexTestCommon {
  74. @Test
  75. public void facet_on_projectUuids() {
  76. ComponentDto project = newPrivateProjectDto("ABCD");
  77. ComponentDto project2 = newPrivateProjectDto("EFGH");
  78. indexIssues(
  79. newDoc("I1", project.uuid(), newFileDto(project)),
  80. newDoc("I2", project.uuid(), newFileDto(project)),
  81. newDoc("I3", project2.uuid(), newFileDto(project2)));
  82. assertThatFacetHasExactly(IssueQuery.builder(), "projects", entry("ABCD", 2L), entry("EFGH", 1L));
  83. }
  84. @Test
  85. public void facet_on_projectUuids_return_100_entries_plus_selected_values() {
  86. indexIssues(rangeClosed(1, 110).mapToObj(i -> newDocForProject(newPrivateProjectDto("a" + i))).toArray(IssueDoc[]::new));
  87. IssueDoc issue1 = newDocForProject(newPrivateProjectDto("project1"));
  88. IssueDoc issue2 = newDocForProject(newPrivateProjectDto("project2"));
  89. indexIssues(issue1, issue2);
  90. assertThatFacetHasSize(IssueQuery.builder().build(), "projects", 100);
  91. assertThatFacetHasSize(IssueQuery.builder().projectUuids(asList(issue1.projectUuid(), issue2.projectUuid())).build(), "projects", 102);
  92. }
  93. @Test
  94. public void facets_on_files() {
  95. ComponentDto project = newPrivateProjectDto("A");
  96. ComponentDto dir = newDirectory(project, "src");
  97. ComponentDto file1 = newFileDto(project, dir, "ABCD");
  98. ComponentDto file2 = newFileDto(project, dir, "BCDE");
  99. ComponentDto file3 = newFileDto(project, dir, "CDEF");
  100. indexIssues(
  101. newDocForProject("I1", project),
  102. newDoc("I2", project.uuid(), file1),
  103. newDoc("I3", project.uuid(), file2),
  104. newDoc("I4", project.uuid(), file2),
  105. newDoc("I5", project.uuid(), file3));
  106. assertThatFacetHasOnly(IssueQuery.builder(), "files", entry("src/NAME_ABCD", 1L), entry("src/NAME_BCDE", 2L), entry("src/NAME_CDEF", 1L));
  107. }
  108. @Test
  109. public void facet_on_files_return_100_entries_plus_selected_values() {
  110. ComponentDto project = newPrivateProjectDto();
  111. indexIssues(rangeClosed(1, 110).mapToObj(i -> newDoc(newFileDto(project, null, "a" + i), project.uuid())).toArray(IssueDoc[]::new));
  112. IssueDoc issue1 = newDoc(newFileDto(project, null, "file1"), project.uuid());
  113. IssueDoc issue2 = newDoc(newFileDto(project, null, "file2"), project.uuid());
  114. indexIssues(issue1, issue2);
  115. assertThatFacetHasSize(IssueQuery.builder().build(), "files", 100);
  116. assertThatFacetHasSize(IssueQuery.builder().files(asList(issue1.filePath(), issue2.filePath())).build(), "files", 102);
  117. }
  118. @Test
  119. public void facets_on_directories() {
  120. ComponentDto project = newPrivateProjectDto();
  121. ComponentDto file1 = newFileDto(project).setPath("src/main/xoo/F1.xoo");
  122. ComponentDto file2 = newFileDto(project).setPath("F2.xoo");
  123. indexIssues(
  124. newDoc("I1", project.uuid(), file1).setDirectoryPath("/src/main/xoo"),
  125. newDoc("I2", project.uuid(), file2).setDirectoryPath("/"));
  126. assertThatFacetHasOnly(IssueQuery.builder(), "directories", entry("/src/main/xoo", 1L), entry("/", 1L));
  127. }
  128. @Test
  129. public void facet_on_directories_return_100_entries_plus_selected_values() {
  130. ComponentDto project = newPrivateProjectDto();
  131. indexIssues(
  132. rangeClosed(1, 110).mapToObj(i -> newDoc(newFileDto(project, newDirectory(project, "dir" + i)), project.uuid()).setDirectoryPath("a" + i)).toArray(IssueDoc[]::new));
  133. IssueDoc issue1 = newDoc(newFileDto(project, newDirectory(project, "path1")), project.uuid()).setDirectoryPath("directory1");
  134. IssueDoc issue2 = newDoc(newFileDto(project, newDirectory(project, "path2")), project.uuid()).setDirectoryPath("directory2");
  135. indexIssues(issue1, issue2);
  136. assertThatFacetHasSize(IssueQuery.builder().build(), "directories", 100);
  137. assertThatFacetHasSize(IssueQuery.builder().directories(asList(issue1.directoryPath(), issue2.directoryPath())).build(), "directories", 102);
  138. }
  139. @Test
  140. public void facets_on_cwe() {
  141. ComponentDto project = newPrivateProjectDto();
  142. ComponentDto file = newFileDto(project);
  143. indexIssues(
  144. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setCwe(asList("20", "564", "89", "943")),
  145. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setCwe(asList("943")),
  146. newDoc("I3", project.uuid(), file));
  147. assertThatFacetHasOnly(IssueQuery.builder(), "cwe",
  148. entry("943", 2L),
  149. entry("20", 1L),
  150. entry("564", 1L),
  151. entry("89", 1L));
  152. }
  153. @Test
  154. public void facets_on_pciDss32() {
  155. ComponentDto project = newPrivateProjectDto();
  156. ComponentDto file = newFileDto(project);
  157. indexIssues(
  158. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setPciDss32(asList("1", "2")),
  159. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setPciDss32(singletonList("3")),
  160. newDoc("I3", project.uuid(), file));
  161. assertThatFacetHasOnly(IssueQuery.builder(), V3_2.prefix(),
  162. entry("1", 1L),
  163. entry("2", 1L),
  164. entry("3", 1L));
  165. }
  166. @Test
  167. public void facets_on_pciDss40() {
  168. ComponentDto project = newPrivateProjectDto();
  169. ComponentDto file = newFileDto(project);
  170. indexIssues(
  171. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setPciDss40(asList("1", "2")),
  172. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setPciDss40(singletonList("3")),
  173. newDoc("I3", project.uuid(), file));
  174. assertThatFacetHasOnly(IssueQuery.builder(), V4_0.prefix(),
  175. entry("1", 1L),
  176. entry("2", 1L),
  177. entry("3", 1L));
  178. }
  179. @Test
  180. public void facets_on_owaspAsvs40() {
  181. ComponentDto project = newPrivateProjectDto();
  182. ComponentDto file = newFileDto(project);
  183. indexIssues(
  184. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspAsvs40(asList("1", "2")),
  185. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspAsvs40(singletonList("3")),
  186. newDoc("I3", project.uuid(), file));
  187. assertThatFacetHasOnly(IssueQuery.builder(), OwaspAsvsVersion.V4_0.prefix(),
  188. entry("1", 1L),
  189. entry("2", 1L),
  190. entry("3", 1L));
  191. }
  192. @Test
  193. public void facets_on_owaspTop10() {
  194. ComponentDto project = newPrivateProjectDto();
  195. ComponentDto file = newFileDto(project);
  196. indexIssues(
  197. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspTop10(asList("a1", "a2")),
  198. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspTop10(singletonList("a3")),
  199. newDoc("I3", project.uuid(), file));
  200. assertThatFacetHasOnly(IssueQuery.builder(), Y2017.prefix(),
  201. entry("a1", 1L),
  202. entry("a2", 1L),
  203. entry("a3", 1L));
  204. }
  205. @Test
  206. public void facets_on_owaspTop10_2021() {
  207. ComponentDto project = newPrivateProjectDto();
  208. ComponentDto file = newFileDto(project);
  209. indexIssues(
  210. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(asList("a1", "a2")),
  211. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(singletonList("a3")),
  212. newDoc("I3", project.uuid(), file));
  213. assertThatFacetHasExactly(IssueQuery.builder(), Y2021.prefix(),
  214. entry("a1", 1L),
  215. entry("a2", 1L),
  216. entry("a3", 1L));
  217. }
  218. @Test
  219. public void facets_on_owaspTop10_2021_stay_ordered() {
  220. ComponentDto project = newPrivateProjectDto();
  221. ComponentDto file = newFileDto(project);
  222. indexIssues(
  223. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(asList("a1", "a2")),
  224. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setOwaspTop10For2021(singletonList("a3")),
  225. newDoc("I3", project.uuid(), file));
  226. assertThatFacetHasExactly(IssueQuery.builder().owaspTop10For2021(Collections.singletonList("a3")), Y2021.prefix(),
  227. entry("a1", 1L),
  228. entry("a2", 1L),
  229. entry("a3", 1L));
  230. }
  231. @Test
  232. public void facets_on_sansTop25() {
  233. ComponentDto project = newPrivateProjectDto();
  234. ComponentDto file = newFileDto(project);
  235. indexIssues(
  236. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setSansTop25(asList("porous-defenses", "risky-resource", "insecure-interaction")),
  237. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setSansTop25(singletonList("porous-defenses")),
  238. newDoc("I3", project.uuid(), file));
  239. assertThatFacetHasOnly(IssueQuery.builder(), "sansTop25",
  240. entry("insecure-interaction", 1L),
  241. entry("porous-defenses", 2L),
  242. entry("risky-resource", 1L));
  243. }
  244. @Test
  245. public void facets_on_sonarSourceSecurity() {
  246. ComponentDto project = newPrivateProjectDto();
  247. ComponentDto file = newFileDto(project);
  248. indexIssues(
  249. newDoc("I1", project.uuid(), file).setType(RuleType.VULNERABILITY).setSonarSourceSecurityCategory(SQCategory.BUFFER_OVERFLOW),
  250. newDoc("I2", project.uuid(), file).setType(RuleType.VULNERABILITY).setSonarSourceSecurityCategory(SQCategory.DOS),
  251. newDoc("I3", project.uuid(), file));
  252. assertThatFacetHasOnly(IssueQuery.builder(), "sonarsourceSecurity",
  253. entry("buffer-overflow", 1L),
  254. entry("dos", 1L));
  255. }
  256. @Test
  257. public void facets_on_severities() {
  258. ComponentDto project = newPrivateProjectDto();
  259. ComponentDto file = newFileDto(project);
  260. indexIssues(
  261. newDoc("I1", project.uuid(), file).setSeverity(INFO),
  262. newDoc("I2", project.uuid(), file).setSeverity(INFO),
  263. newDoc("I3", project.uuid(), file).setSeverity(MAJOR));
  264. assertThatFacetHasOnly(IssueQuery.builder(), "severities", entry("INFO", 2L), entry("MAJOR", 1L));
  265. }
  266. @Test
  267. public void facet_on_severities_return_5_entries_max() {
  268. ComponentDto project = newPrivateProjectDto();
  269. ComponentDto file = newFileDto(project);
  270. indexIssues(
  271. newDoc("I2", project.uuid(), file).setSeverity(INFO),
  272. newDoc("I1", project.uuid(), file).setSeverity(MINOR),
  273. newDoc("I3", project.uuid(), file).setSeverity(MAJOR),
  274. newDoc("I4", project.uuid(), file).setSeverity(CRITICAL),
  275. newDoc("I5", project.uuid(), file).setSeverity(BLOCKER),
  276. newDoc("I6", project.uuid(), file).setSeverity(MAJOR));
  277. assertThatFacetHasSize(IssueQuery.builder().build(), "severities", 5);
  278. }
  279. @Test
  280. public void facets_on_statuses() {
  281. ComponentDto project = newPrivateProjectDto();
  282. ComponentDto file = newFileDto(project);
  283. indexIssues(
  284. newDoc("I1", project.uuid(), file).setStatus(STATUS_CLOSED),
  285. newDoc("I2", project.uuid(), file).setStatus(STATUS_CLOSED),
  286. newDoc("I3", project.uuid(), file).setStatus(STATUS_OPEN));
  287. assertThatFacetHasOnly(IssueQuery.builder(), "statuses", entry("CLOSED", 2L), entry("OPEN", 1L));
  288. }
  289. @Test
  290. public void facet_on_statuses_return_5_entries_max() {
  291. ComponentDto project = newPrivateProjectDto();
  292. ComponentDto file = newFileDto(project);
  293. indexIssues(
  294. newDoc("I1", project.uuid(), file).setStatus(STATUS_OPEN),
  295. newDoc("I2", project.uuid(), file).setStatus(STATUS_CONFIRMED),
  296. newDoc("I3", project.uuid(), file).setStatus(STATUS_REOPENED),
  297. newDoc("I4", project.uuid(), file).setStatus(STATUS_RESOLVED),
  298. newDoc("I5", project.uuid(), file).setStatus(STATUS_CLOSED),
  299. newDoc("I6", project.uuid(), file).setStatus(STATUS_OPEN));
  300. assertThatFacetHasSize(IssueQuery.builder().build(), "statuses", 5);
  301. }
  302. @Test
  303. public void facets_on_resolutions() {
  304. ComponentDto project = newPrivateProjectDto();
  305. ComponentDto file = newFileDto(project);
  306. indexIssues(
  307. newDoc("I1", project.uuid(), file).setResolution(RESOLUTION_FALSE_POSITIVE),
  308. newDoc("I2", project.uuid(), file).setResolution(RESOLUTION_FALSE_POSITIVE),
  309. newDoc("I3", project.uuid(), file).setResolution(RESOLUTION_FIXED));
  310. assertThatFacetHasOnly(IssueQuery.builder(), "resolutions", entry("FALSE-POSITIVE", 2L), entry("FIXED", 1L));
  311. }
  312. @Test
  313. public void search_shouldReturnSimpleStatusesFacet() {
  314. ComponentDto mainBranch = newPrivateProjectDto();
  315. ComponentDto file = newFileDto(mainBranch);
  316. indexIssues(
  317. newDoc("I1", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.CONFIRMED.name()),
  318. newDoc("I2", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.FIXED.name()),
  319. newDoc("I3", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.OPEN.name()),
  320. newDoc("I4", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.OPEN.name()),
  321. newDoc("I5", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.ACCEPTED.name()),
  322. newDoc("I6", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.ACCEPTED.name()),
  323. newDoc("I7", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.ACCEPTED.name()),
  324. newDoc("I8", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.FALSE_POSITIVE.name()),
  325. newDoc("I9", mainBranch.uuid(), file).setSimpleStatus(SimpleStatus.FALSE_POSITIVE.name()));
  326. assertThatFacetHasSize(IssueQuery.builder().build(), "simpleStatuses", 5);
  327. assertThatFacetHasOnly(IssueQuery.builder(), "simpleStatuses",
  328. entry("OPEN", 2L),
  329. entry("CONFIRMED", 1L),
  330. entry("FALSE_POSITIVE", 2L),
  331. entry("ACCEPTED", 3L),
  332. entry("FIXED", 1L));
  333. }
  334. @Test
  335. public void facets_on_resolutions_return_5_entries_max() {
  336. ComponentDto project = newPrivateProjectDto();
  337. ComponentDto file = newFileDto(project);
  338. indexIssues(
  339. newDoc("I1", project.uuid(), file).setResolution(RESOLUTION_FIXED),
  340. newDoc("I2", project.uuid(), file).setResolution(RESOLUTION_FALSE_POSITIVE),
  341. newDoc("I3", project.uuid(), file).setResolution(RESOLUTION_REMOVED),
  342. newDoc("I4", project.uuid(), file).setResolution(RESOLUTION_WONT_FIX),
  343. newDoc("I5", project.uuid(), file).setResolution(null));
  344. assertThatFacetHasSize(IssueQuery.builder().build(), "resolutions", 5);
  345. }
  346. @Test
  347. public void facets_on_languages() {
  348. ComponentDto project = newPrivateProjectDto();
  349. ComponentDto file = newFileDto(project);
  350. RuleDto ruleDefinitionDto = newRule();
  351. db.rules().insert(ruleDefinitionDto);
  352. indexIssues(newDoc("I1", project.uuid(), file).setRuleUuid(ruleDefinitionDto.getUuid()).setLanguage("xoo"));
  353. assertThatFacetHasOnly(IssueQuery.builder(), "languages", entry("xoo", 1L));
  354. }
  355. @Test
  356. public void facets_on_languages_return_100_entries_plus_selected_values() {
  357. ComponentDto project = newPrivateProjectDto();
  358. indexIssues(rangeClosed(1, 100).mapToObj(i -> newDoc(newFileDto(project), project.uuid()).setLanguage("a" + i)).toArray(IssueDoc[]::new));
  359. IssueDoc issue1 = newDoc(newFileDto(project), project.uuid()).setLanguage("language1");
  360. IssueDoc issue2 = newDoc(newFileDto(project), project.uuid()).setLanguage("language2");
  361. indexIssues(issue1, issue2);
  362. assertThatFacetHasSize(IssueQuery.builder().build(), "languages", 100);
  363. assertThatFacetHasSize(IssueQuery.builder().languages(asList(issue1.language(), issue2.language())).build(), "languages", 102);
  364. }
  365. @Test
  366. public void facets_on_assignees() {
  367. ComponentDto project = newPrivateProjectDto();
  368. ComponentDto file = newFileDto(project);
  369. indexIssues(
  370. newDoc("I1", project.uuid(), file).setAssigneeUuid("steph-uuid"),
  371. newDoc("I2", project.uuid(), file).setAssigneeUuid("marcel-uuid"),
  372. newDoc("I3", project.uuid(), file).setAssigneeUuid("marcel-uuid"),
  373. newDoc("I4", project.uuid(), file).setAssigneeUuid(null));
  374. assertThatFacetHasOnly(IssueQuery.builder(), "assignees", entry("steph-uuid", 1L), entry("marcel-uuid", 2L), entry("", 1L));
  375. }
  376. @Test
  377. public void facets_on_assignees_return_only_100_entries_plus_selected_values() {
  378. ComponentDto project = newPrivateProjectDto();
  379. indexIssues(rangeClosed(1, 110).mapToObj(i -> newDoc(newFileDto(project), project.uuid()).setAssigneeUuid("a" + i)).toArray(IssueDoc[]::new));
  380. IssueDoc issue1 = newDoc(newFileDto(project), project.uuid()).setAssigneeUuid("user1");
  381. IssueDoc issue2 = newDoc(newFileDto(project), project.uuid()).setAssigneeUuid("user2");
  382. indexIssues(issue1, issue2);
  383. assertThatFacetHasSize(IssueQuery.builder().build(), "assignees", 100);
  384. assertThatFacetHasSize(IssueQuery.builder().assigneeUuids(asList(issue1.assigneeUuid(), issue2.assigneeUuid())).build(), "assignees", 102);
  385. }
  386. @Test
  387. public void facets_on_assignees_supports_dashes() {
  388. ComponentDto project = newPrivateProjectDto();
  389. ComponentDto file = newFileDto(project);
  390. indexIssues(
  391. newDoc("I1", project.uuid(), file).setAssigneeUuid("j-b-uuid"),
  392. newDoc("I2", project.uuid(), file).setAssigneeUuid("marcel-uuid"),
  393. newDoc("I3", project.uuid(), file).setAssigneeUuid("marcel-uuid"),
  394. newDoc("I4", project.uuid(), file).setAssigneeUuid(null));
  395. assertThatFacetHasOnly(IssueQuery.builder().assigneeUuids(singletonList("j-b")),
  396. "assignees", entry("j-b-uuid", 1L), entry("marcel-uuid", 2L), entry("", 1L));
  397. }
  398. @Test
  399. public void facets_on_author() {
  400. ComponentDto project = newPrivateProjectDto();
  401. ComponentDto file = newFileDto(project);
  402. indexIssues(
  403. newDoc("I1", project.uuid(), file).setAuthorLogin("steph"),
  404. newDoc("I2", project.uuid(), file).setAuthorLogin("marcel"),
  405. newDoc("I3", project.uuid(), file).setAuthorLogin("marcel"),
  406. newDoc("I4", project.uuid(), file).setAuthorLogin(null));
  407. assertThatFacetHasOnly(IssueQuery.builder(), "author", entry("steph", 1L), entry("marcel", 2L));
  408. }
  409. @Test
  410. public void facets_on_authors_return_100_entries_plus_selected_values() {
  411. ComponentDto project = newPrivateProjectDto();
  412. indexIssues(rangeClosed(1, 110).mapToObj(i -> newDoc(newFileDto(project), project.uuid()).setAuthorLogin("a" + i)).toArray(IssueDoc[]::new));
  413. IssueDoc issue1 = newDoc(newFileDto(project), project.uuid()).setAuthorLogin("user1");
  414. IssueDoc issue2 = newDoc(newFileDto(project), project.uuid()).setAuthorLogin("user2");
  415. indexIssues(issue1, issue2);
  416. assertThatFacetHasSize(IssueQuery.builder().build(), "author", 100);
  417. assertThatFacetHasSize(IssueQuery.builder().authors(asList(issue1.authorLogin(), issue2.authorLogin())).build(), "author", 102);
  418. }
  419. @Test
  420. public void facet_on_created_at_with_less_than_20_days_use_system_timezone_by_default() {
  421. SearchOptions options = fixtureForCreatedAtFacet();
  422. IssueQuery query = IssueQuery.builder()
  423. .createdAfter(parseDateTime("2014-09-01T00:00:00+0100"))
  424. .createdBefore(parseDateTime("2014-09-08T00:00:00+0100"))
  425. .build();
  426. SearchResponse result = underTest.search(query, options);
  427. Map<String, Long> buckets = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  428. assertThat(buckets).containsOnly(
  429. entry("2014-08-31", 0L),
  430. entry("2014-09-01", 2L),
  431. entry("2014-09-02", 1L),
  432. entry("2014-09-03", 0L),
  433. entry("2014-09-04", 0L),
  434. entry("2014-09-05", 1L),
  435. entry("2014-09-06", 0L),
  436. entry("2014-09-07", 0L));
  437. }
  438. @Test
  439. public void facet_on_created_at_with_less_than_20_days_use_user_timezone_if_provided() {
  440. // Use timezones very far from each other in order to see some issues moving to a different calendar day
  441. final ZoneId plus14 = ZoneId.of("Pacific/Kiritimati");
  442. final ZoneId minus11 = ZoneId.of("Pacific/Pago_Pago");
  443. SearchOptions options = fixtureForCreatedAtFacet();
  444. final Date startDate = parseDateTime("2014-09-01T00:00:00+0000");
  445. final Date endDate = parseDateTime("2014-09-08T00:00:00+0000");
  446. IssueQuery queryPlus14 = IssueQuery.builder()
  447. .createdAfter(startDate)
  448. .createdBefore(endDate)
  449. .timeZone(plus14)
  450. .build();
  451. SearchResponse resultPlus14 = underTest.search(queryPlus14, options);
  452. Map<String, Long> bucketsPlus14 = new Facets(resultPlus14, plus14).get("createdAt");
  453. assertThat(bucketsPlus14).containsOnly(
  454. entry("2014-09-01", 0L),
  455. entry("2014-09-02", 2L),
  456. entry("2014-09-03", 1L),
  457. entry("2014-09-04", 0L),
  458. entry("2014-09-05", 0L),
  459. entry("2014-09-06", 1L),
  460. entry("2014-09-07", 0L),
  461. entry("2014-09-08", 0L));
  462. IssueQuery queryMinus11 = IssueQuery.builder()
  463. .createdAfter(startDate)
  464. .createdBefore(endDate)
  465. .timeZone(minus11)
  466. .build();
  467. SearchResponse resultMinus11 = underTest.search(queryMinus11, options);
  468. Map<String, Long> bucketsMinus11 = new Facets(resultMinus11, minus11).get("createdAt");
  469. assertThat(bucketsMinus11).containsOnly(
  470. entry("2014-08-31", 1L),
  471. entry("2014-09-01", 1L),
  472. entry("2014-09-02", 1L),
  473. entry("2014-09-03", 0L),
  474. entry("2014-09-04", 0L),
  475. entry("2014-09-05", 1L),
  476. entry("2014-09-06", 0L),
  477. entry("2014-09-07", 0L));
  478. }
  479. @Test
  480. public void facet_on_created_at_with_less_than_20_weeks() {
  481. SearchOptions options = fixtureForCreatedAtFacet();
  482. SearchResponse result = underTest.search(IssueQuery.builder()
  483. .createdAfter(parseDateTime("2014-09-01T00:00:00+0100"))
  484. .createdBefore(parseDateTime("2014-09-21T00:00:00+0100")).build(),
  485. options);
  486. Map<String, Long> createdAt = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  487. assertThat(createdAt).containsOnly(
  488. entry("2014-08-25", 0L),
  489. entry("2014-09-01", 4L),
  490. entry("2014-09-08", 0L),
  491. entry("2014-09-15", 1L));
  492. }
  493. @Test
  494. public void facet_on_created_at_with_less_than_20_months() {
  495. SearchOptions options = fixtureForCreatedAtFacet();
  496. SearchResponse result = underTest.search(IssueQuery.builder()
  497. .createdAfter(parseDateTime("2014-09-01T00:00:00+0100"))
  498. .createdBefore(parseDateTime("2015-01-19T00:00:00+0100")).build(),
  499. options);
  500. Map<String, Long> createdAt = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  501. assertThat(createdAt).containsOnly(
  502. entry("2014-08-01", 0L),
  503. entry("2014-09-01", 5L),
  504. entry("2014-10-01", 0L),
  505. entry("2014-11-01", 0L),
  506. entry("2014-12-01", 0L),
  507. entry("2015-01-01", 1L));
  508. }
  509. @Test
  510. public void facet_on_created_at_with_more_than_20_months() {
  511. SearchOptions options = fixtureForCreatedAtFacet();
  512. SearchResponse result = underTest.search(IssueQuery.builder()
  513. .createdAfter(parseDateTime("2011-01-01T00:00:00+0100"))
  514. .createdBefore(parseDateTime("2016-01-01T00:00:00+0100")).build(),
  515. options);
  516. Map<String, Long> createdAt = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  517. assertThat(createdAt).containsOnly(
  518. entry("2010-01-01", 0L),
  519. entry("2011-01-01", 1L),
  520. entry("2012-01-01", 0L),
  521. entry("2013-01-01", 0L),
  522. entry("2014-01-01", 5L),
  523. entry("2015-01-01", 1L));
  524. }
  525. @Test
  526. public void facet_on_created_at_with_one_day() {
  527. SearchOptions options = fixtureForCreatedAtFacet();
  528. SearchResponse result = underTest.search(IssueQuery.builder()
  529. .createdAfter(parseDateTime("2014-09-01T00:00:00-0100"))
  530. .createdBefore(parseDateTime("2014-09-02T00:00:00-0100")).build(),
  531. options);
  532. Map<String, Long> createdAt = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  533. assertThat(createdAt).containsOnly(
  534. entry("2014-09-01", 2L));
  535. }
  536. @Test
  537. public void facet_on_created_at_with_bounds_outside_of_data() {
  538. SearchOptions options = fixtureForCreatedAtFacet();
  539. SearchResponse result = underTest.search(IssueQuery.builder()
  540. .createdAfter(parseDateTime("2009-01-01T00:00:00+0100"))
  541. .createdBefore(parseDateTime("2016-01-01T00:00:00+0100"))
  542. .build(), options);
  543. Map<String, Long> createdAt = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  544. assertThat(createdAt).containsOnly(
  545. entry("2008-01-01", 0L),
  546. entry("2009-01-01", 0L),
  547. entry("2010-01-01", 0L),
  548. entry("2011-01-01", 1L),
  549. entry("2012-01-01", 0L),
  550. entry("2013-01-01", 0L),
  551. entry("2014-01-01", 5L),
  552. entry("2015-01-01", 1L));
  553. }
  554. @Test
  555. public void facet_on_created_at_without_start_bound() {
  556. SearchOptions searchOptions = fixtureForCreatedAtFacet();
  557. SearchResponse result = underTest.search(IssueQuery.builder()
  558. .createdBefore(parseDateTime("2016-01-01T00:00:00+0100")).build(),
  559. searchOptions);
  560. Map<String, Long> createdAt = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  561. assertThat(createdAt).containsOnly(
  562. entry("2011-01-01", 1L),
  563. entry("2012-01-01", 0L),
  564. entry("2013-01-01", 0L),
  565. entry("2014-01-01", 5L),
  566. entry("2015-01-01", 1L));
  567. }
  568. @Test
  569. public void facet_on_created_at_without_issues() {
  570. SearchOptions searchOptions = new SearchOptions().addFacets("createdAt");
  571. SearchResponse result = underTest.search(IssueQuery.builder().build(), searchOptions);
  572. Map<String, Long> createdAt = new Facets(result, system2.getDefaultTimeZone().toZoneId()).get("createdAt");
  573. assertThat(createdAt).isNull();
  574. }
  575. @Test
  576. public void search_shouldReturnCodeVariantsFacet() {
  577. ComponentDto project = newPrivateProjectDto();
  578. ComponentDto file = newFileDto(project);
  579. indexIssues(
  580. newDoc("I1", project.uuid(), file).setCodeVariants(asList("variant1", "variant2")),
  581. newDoc("I2", project.uuid(), file).setCodeVariants(singletonList("variant2")),
  582. newDoc("I3", project.uuid(), file).setCodeVariants(singletonList("variant3")),
  583. newDoc("I4", project.uuid(), file));
  584. assertThatFacetHasOnly(IssueQuery.builder(), "codeVariants",
  585. entry("variant1", 1L),
  586. entry("variant2", 2L),
  587. entry("variant3", 1L));
  588. }
  589. @Test
  590. public void search_shouldReturnImpactSoftwareQualitiesFacet() {
  591. ComponentDto project = newPrivateProjectDto();
  592. ComponentDto file = newFileDto(project);
  593. indexIssues(
  594. newDoc("I1", project.uuid(), file).setImpacts(Map.of(
  595. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH,
  596. RELIABILITY, org.sonar.api.issue.impact.Severity.MEDIUM)),
  597. newDoc("I2", project.uuid(), file).setImpacts(Map.of(
  598. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)),
  599. newDoc("I3", project.uuid(), file).setImpacts(Map.of(
  600. RELIABILITY, org.sonar.api.issue.impact.Severity.HIGH)),
  601. newDoc("I4", project.uuid(), file).setImpacts(Map.of(
  602. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)));
  603. assertThatFacetHasOnly(IssueQuery.builder(), "impactSoftwareQualities",
  604. entry("MAINTAINABILITY", 3L),
  605. entry("RELIABILITY", 2L),
  606. entry("SECURITY", 0L));
  607. }
  608. @Test
  609. public void search_whenFilteredOnSeverity_shouldReturnImpactSoftwareQualitiesFacet() {
  610. ComponentDto project = newPrivateProjectDto();
  611. ComponentDto file = newFileDto(project);
  612. indexIssues(
  613. newDoc("I1", project.uuid(), file).setImpacts(Map.of(
  614. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH,
  615. RELIABILITY, org.sonar.api.issue.impact.Severity.MEDIUM))
  616. .setTags(singletonList("my-tag")),
  617. newDoc("I2", project.uuid(), file).setImpacts(Map.of(
  618. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)),
  619. newDoc("I3", project.uuid(), file).setImpacts(Map.of(
  620. RELIABILITY, org.sonar.api.issue.impact.Severity.HIGH)),
  621. newDoc("I4", project.uuid(), file).setImpacts(Map.of(
  622. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)));
  623. assertThatFacetHasOnly(IssueQuery.builder().impactSoftwareQualities(Set.of(MAINTAINABILITY.name())).impactSeverities(Set.of(org.sonar.api.issue.impact.Severity.LOW.name())),
  624. "impactSoftwareQualities",
  625. entry("MAINTAINABILITY", 2L),
  626. entry("RELIABILITY", 0L),
  627. entry("SECURITY", 0L));
  628. assertThatFacetHasOnly(IssueQuery.builder().impactSoftwareQualities(Set.of(MAINTAINABILITY.name())), "impactSoftwareQualities",
  629. entry("MAINTAINABILITY", 3L),
  630. entry("RELIABILITY", 2L),
  631. entry("SECURITY", 0L));
  632. assertThatFacetHasOnly(IssueQuery.builder().impactSeverities(Set.of(Severity.MEDIUM.name())), "impactSoftwareQualities",
  633. entry("MAINTAINABILITY", 0L),
  634. entry("RELIABILITY", 1L),
  635. entry("SECURITY", 0L));
  636. assertThatFacetHasOnly(IssueQuery.builder().impactSeverities(Set.of(org.sonar.api.issue.impact.Severity.HIGH.name())), "impactSoftwareQualities",
  637. entry("MAINTAINABILITY", 1L),
  638. entry("RELIABILITY", 1L),
  639. entry("SECURITY", 0L));
  640. assertThatFacetHasOnly(IssueQuery.builder()
  641. .tags(singletonList("my-tag"))
  642. .impactSeverities(Set.of(org.sonar.api.issue.impact.Severity.HIGH.name())), "impactSoftwareQualities",
  643. entry("MAINTAINABILITY", 1L),
  644. entry("RELIABILITY", 0L),
  645. entry("SECURITY", 0L));
  646. }
  647. @Test
  648. public void search_whenFilteredOnSeverityAndSoftwareQuality_shouldReturnImpactFacets() {
  649. ComponentDto project = newPrivateProjectDto();
  650. ComponentDto file = newFileDto(project);
  651. indexIssues(
  652. newDoc("I1", project.uuid(), file).setImpacts(Map.of(
  653. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW,
  654. RELIABILITY, org.sonar.api.issue.impact.Severity.LOW)));
  655. assertThatFacetHasOnly(IssueQuery.builder()
  656. .impactSoftwareQualities(Set.of(MAINTAINABILITY.name()))
  657. .impactSeverities(Set.of(org.sonar.api.issue.impact.Severity.LOW.name())),
  658. "impactSoftwareQualities",
  659. entry("MAINTAINABILITY", 1L),
  660. entry("RELIABILITY", 1L),
  661. entry("SECURITY", 0L));
  662. assertThatFacetHasOnly(IssueQuery.builder()
  663. .impactSoftwareQualities(Set.of(MAINTAINABILITY.name()))
  664. .impactSeverities(Set.of(org.sonar.api.issue.impact.Severity.LOW.name())),
  665. "impactSeverities",
  666. entry("HIGH", 0L),
  667. entry("MEDIUM", 0L),
  668. entry("LOW", 1L));
  669. }
  670. @Test
  671. public void search_shouldReturnImpactSeverityFacet() {
  672. ComponentDto project = newPrivateProjectDto();
  673. ComponentDto file = newFileDto(project);
  674. indexIssues(
  675. newDoc("I1", project.uuid(), file).setImpacts(Map.of(
  676. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH,
  677. RELIABILITY, org.sonar.api.issue.impact.Severity.MEDIUM)),
  678. newDoc("I2", project.uuid(), file).setImpacts(Map.of(
  679. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)),
  680. newDoc("I3", project.uuid(), file).setImpacts(Map.of(
  681. RELIABILITY, org.sonar.api.issue.impact.Severity.HIGH)),
  682. newDoc("I4", project.uuid(), file).setImpacts(Map.of(
  683. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)));
  684. assertThatFacetHasOnly(IssueQuery.builder(), "impactSeverities",
  685. entry("HIGH", 2L),
  686. entry("MEDIUM", 1L),
  687. entry("LOW", 2L));
  688. }
  689. @Test
  690. public void search_whenFilteredOnSoftwareQuality_shouldReturnImpactSeverityFacet() {
  691. ComponentDto project = newPrivateProjectDto();
  692. ComponentDto file = newFileDto(project);
  693. indexIssues(
  694. newDoc("I1", project.uuid(), file).setImpacts(Map.of(
  695. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH,
  696. RELIABILITY, org.sonar.api.issue.impact.Severity.MEDIUM)),
  697. newDoc("I2", project.uuid(), file).setImpacts(Map.of(
  698. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)),
  699. newDoc("I3", project.uuid(), file).setImpacts(Map.of(
  700. RELIABILITY, org.sonar.api.issue.impact.Severity.HIGH)),
  701. newDoc("I4", project.uuid(), file).setImpacts(Map.of(
  702. MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)));
  703. assertThatFacetHasOnly(IssueQuery.builder().impactSoftwareQualities(Set.of(MAINTAINABILITY.name())), "impactSeverities",
  704. entry("HIGH", 1L),
  705. entry("MEDIUM", 0L),
  706. entry("LOW", 2L));
  707. }
  708. @Test
  709. public void search_shouldReturnCleanCodeAttributeCategoryFacet() {
  710. ComponentDto project = newPrivateProjectDto();
  711. ComponentDto file = newFileDto(project);
  712. indexIssues(
  713. newDoc("I1", project.uuid(), file).setCleanCodeAttributeCategory(ADAPTABLE.name()),
  714. newDoc("I2", project.uuid(), file).setCleanCodeAttributeCategory(ADAPTABLE.name()),
  715. newDoc("I3", project.uuid(), file).setCleanCodeAttributeCategory(CONSISTENT.name()),
  716. newDoc("I4", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()),
  717. newDoc("I5", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()),
  718. newDoc("I6", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()),
  719. newDoc("I7", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()),
  720. newDoc("I8", project.uuid(), file).setCleanCodeAttributeCategory(RESPONSIBLE.name()));
  721. assertThatFacetHasOnly(IssueQuery.builder(), "cleanCodeAttributeCategories",
  722. entry("INTENTIONAL", 4L),
  723. entry("ADAPTABLE", 2L),
  724. entry("CONSISTENT", 1L),
  725. entry("RESPONSIBLE", 1L));
  726. }
  727. @Test
  728. public void search_whenFilteredByTags_shouldReturnCleanCodeAttributeCategoryFacet() {
  729. ComponentDto project = newPrivateProjectDto();
  730. ComponentDto file = newFileDto(project);
  731. indexIssues(
  732. newDoc("I1", project.uuid(), file).setCleanCodeAttributeCategory(ADAPTABLE.name()).setTags(singletonList("tag-1")),
  733. newDoc("I2", project.uuid(), file).setCleanCodeAttributeCategory(ADAPTABLE.name()).setTags(singletonList("tag-1")),
  734. newDoc("I3", project.uuid(), file).setCleanCodeAttributeCategory(CONSISTENT.name()),
  735. newDoc("I4", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()).setTags(singletonList("tag-1")),
  736. newDoc("I5", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()),
  737. newDoc("I6", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()),
  738. newDoc("I7", project.uuid(), file).setCleanCodeAttributeCategory(INTENTIONAL.name()),
  739. newDoc("I8", project.uuid(), file).setCleanCodeAttributeCategory(RESPONSIBLE.name()).setTags(singletonList("tag-3")));
  740. assertThatFacetHasOnly(IssueQuery.builder().tags(singletonList("tag-1")), "cleanCodeAttributeCategories",
  741. entry("INTENTIONAL", 1L),
  742. entry("ADAPTABLE", 2L));
  743. }
  744. private SearchOptions fixtureForCreatedAtFacet() {
  745. ComponentDto project = newPrivateProjectDto();
  746. ComponentDto file = newFileDto(project);
  747. IssueDoc issue0 = newDoc("ISSUE0", project.uuid(), file).setFuncCreationDate(parseDateTime("2011-04-25T00:05:13+0000"));
  748. IssueDoc issue1 = newDoc("I1", project.uuid(), file).setFuncCreationDate(parseDateTime("2014-09-01T10:34:56+0000"));
  749. IssueDoc issue2 = newDoc("I2", project.uuid(), file).setFuncCreationDate(parseDateTime("2014-09-01T22:46:00+0000"));
  750. IssueDoc issue3 = newDoc("I3", project.uuid(), file).setFuncCreationDate(parseDateTime("2014-09-02T11:34:56+0000"));
  751. IssueDoc issue4 = newDoc("I4", project.uuid(), file).setFuncCreationDate(parseDateTime("2014-09-05T11:34:56+0000"));
  752. IssueDoc issue5 = newDoc("I5", project.uuid(), file).setFuncCreationDate(parseDateTime("2014-09-20T11:34:56+0000"));
  753. IssueDoc issue6 = newDoc("I6", project.uuid(), file).setFuncCreationDate(parseDateTime("2015-01-18T11:34:56+0000"));
  754. indexIssues(issue0, issue1, issue2, issue3, issue4, issue5, issue6);
  755. return new SearchOptions().addFacets("createdAt");
  756. }
  757. @SafeVarargs
  758. private void assertThatFacetHasExactly(IssueQuery.Builder query, String facet, Map.Entry<String, Long>... expectedEntries) {
  759. SearchResponse result = underTest.search(query.build(), new SearchOptions().addFacets(singletonList(facet)));
  760. Facets facets = new Facets(result, system2.getDefaultTimeZone().toZoneId());
  761. assertThat(facets.getNames()).containsOnly(facet, "effort");
  762. assertThat(facets.get(facet)).containsExactly(expectedEntries);
  763. }
  764. @SafeVarargs
  765. private void assertThatFacetHasOnly(IssueQuery.Builder query, String facet, Map.Entry<String, Long>... expectedEntries) {
  766. SearchResponse result = underTest.search(query.build(), new SearchOptions().addFacets(singletonList(facet)));
  767. Facets facets = new Facets(result, system2.getDefaultTimeZone().toZoneId());
  768. assertThat(facets.getNames()).containsOnly(facet, "effort");
  769. assertThat(facets.get(facet)).containsOnly(expectedEntries);
  770. }
  771. private void assertThatFacetHasSize(IssueQuery issueQuery, String facet, int expectedSize) {
  772. SearchResponse result = underTest.search(issueQuery, new SearchOptions().addFacets(singletonList(facet)));
  773. Facets facets = new Facets(result, system2.getDefaultTimeZone().toZoneId());
  774. assertThat(facets.get(facet)).hasSize(expectedSize);
  775. }
  776. }