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.

ComponentTreeSortTest.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.measure.ws;
  21. import com.google.common.collect.HashBasedTable;
  22. import com.google.common.collect.Table;
  23. import java.util.List;
  24. import javax.annotation.Nullable;
  25. import org.junit.jupiter.api.BeforeEach;
  26. import org.junit.jupiter.api.Test;
  27. import org.sonar.api.issue.impact.Severity;
  28. import org.sonar.api.measures.CoreMetrics;
  29. import org.sonar.api.measures.Metric.ValueType;
  30. import org.sonar.api.resources.Qualifiers;
  31. import org.sonar.core.util.Uuids;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.measure.LiveMeasureDto;
  34. import org.sonar.db.metric.MetricDto;
  35. import org.sonar.server.measure.ImpactMeasureBuilder;
  36. import static com.google.common.collect.Lists.newArrayList;
  37. import static java.util.Collections.singletonList;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.sonar.db.metric.MetricTesting.newMetricDto;
  40. import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_PERIOD_SORT;
  41. import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_SORT;
  42. import static org.sonar.server.measure.ws.ComponentTreeAction.NAME_SORT;
  43. import static org.sonar.server.measure.ws.ComponentTreeAction.PATH_SORT;
  44. import static org.sonar.server.measure.ws.ComponentTreeAction.QUALIFIER_SORT;
  45. import static org.sonar.server.measure.ws.ComponentTreeData.Measure.createFromMeasureDto;
  46. class ComponentTreeSortTest {
  47. private static final String NUM_METRIC_KEY = "violations";
  48. private static final String NEW_METRIC_KEY = "new_violations";
  49. private static final String TEXT_METRIC_KEY = "sqale_index";
  50. private static final String DATA_IMPACT_METRIC_KEY = "reliability_issues";
  51. private static final String NEW_DATA_IMPACT_METRIC_KEY = "new_reliability_issues";
  52. private List<MetricDto> metrics;
  53. private Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric;
  54. private List<ComponentDto> components;
  55. @BeforeEach
  56. void setUp() {
  57. components = newArrayList(
  58. newComponentWithoutSnapshotId("name-1", "qualifier-2", "path-9"),
  59. newComponentWithoutSnapshotId("name-3", "qualifier-3", "path-8"),
  60. newComponentWithoutSnapshotId("name-2", "qualifier-4", "path-7"),
  61. newComponentWithoutSnapshotId("name-4", "qualifier-5", "path-6"),
  62. newComponentWithoutSnapshotId("name-7", "qualifier-6", "path-5"),
  63. newComponentWithoutSnapshotId("name-6", "qualifier-7", "path-4"),
  64. newComponentWithoutSnapshotId("name-5", "qualifier-8", "path-3"),
  65. newComponentWithoutSnapshotId("name-9", "qualifier-9", "path-2"),
  66. newComponentWithoutSnapshotId("name-8", "qualifier-1", "path-1"));
  67. MetricDto violationsMetric = newMetricDto()
  68. .setKey(NUM_METRIC_KEY)
  69. .setValueType(ValueType.INT.name());
  70. MetricDto newViolationsMetric = newMetricDto()
  71. .setKey(NEW_METRIC_KEY)
  72. .setValueType(ValueType.INT.name());
  73. MetricDto sqaleIndexMetric = newMetricDto()
  74. .setKey(TEXT_METRIC_KEY)
  75. .setValueType(ValueType.STRING.name());
  76. MetricDto reliabilityIssueMetric = newMetricDto()
  77. .setKey(DATA_IMPACT_METRIC_KEY)
  78. .setValueType(ValueType.DATA.name());
  79. MetricDto newReliabilityIssueMetric = newMetricDto()
  80. .setKey(NEW_DATA_IMPACT_METRIC_KEY)
  81. .setValueType(ValueType.DATA.name());
  82. metrics = newArrayList(violationsMetric, sqaleIndexMetric, newViolationsMetric, reliabilityIssueMetric, newReliabilityIssueMetric);
  83. measuresByComponentUuidAndMetric = HashBasedTable.create(components.size(), 2);
  84. // same number than path field
  85. double currentValue = 9;
  86. for (ComponentDto component : components) {
  87. measuresByComponentUuidAndMetric.put(component.uuid(), violationsMetric, createFromMeasureDto(new LiveMeasureDto().setValue(currentValue)));
  88. measuresByComponentUuidAndMetric.put(component.uuid(), newViolationsMetric, createFromMeasureDto(new LiveMeasureDto().setValue(currentValue)));
  89. measuresByComponentUuidAndMetric.put(component.uuid(), sqaleIndexMetric, createFromMeasureDto(new LiveMeasureDto().setData(String.valueOf(currentValue))));
  90. measuresByComponentUuidAndMetric.put(component.uuid(), reliabilityIssueMetric, createFromMeasureDto(new LiveMeasureDto().setData(buildJsonImpact((int) currentValue))));
  91. measuresByComponentUuidAndMetric.put(component.uuid(), newReliabilityIssueMetric, createFromMeasureDto(new LiveMeasureDto().setData(buildJsonImpact((int) currentValue))));
  92. currentValue--;
  93. }
  94. }
  95. private static String buildJsonImpact(int currentValue) {
  96. return String.valueOf(ImpactMeasureBuilder.newInstance()
  97. .setSeverity(Severity.HIGH, currentValue)
  98. .setSeverity(Severity.MEDIUM, currentValue)
  99. .setSeverity(Severity.LOW, currentValue)
  100. .setTotal(currentValue)
  101. .buildAsString());
  102. }
  103. @Test
  104. void sort_by_names() {
  105. ComponentTreeRequest wsRequest = newRequest(singletonList(NAME_SORT), true, null);
  106. List<ComponentDto> result = sortComponents(wsRequest);
  107. assertThat(result).extracting("name")
  108. .containsExactly("name-1", "name-2", "name-3", "name-4", "name-5", "name-6", "name-7", "name-8", "name-9");
  109. }
  110. @Test
  111. void sort_by_qualifier() {
  112. ComponentTreeRequest wsRequest = newRequest(singletonList(QUALIFIER_SORT), false, null);
  113. List<ComponentDto> result = sortComponents(wsRequest);
  114. assertThat(result).extracting("qualifier")
  115. .containsExactly("qualifier-9", "qualifier-8", "qualifier-7", "qualifier-6", "qualifier-5", "qualifier-4", "qualifier-3", "qualifier-2", "qualifier-1");
  116. }
  117. @Test
  118. void sort_by_path() {
  119. ComponentTreeRequest wsRequest = newRequest(singletonList(PATH_SORT), true, null);
  120. List<ComponentDto> result = sortComponents(wsRequest);
  121. assertThat(result).extracting("path")
  122. .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9");
  123. }
  124. @Test
  125. void sort_by_numerical_metric_key_ascending() {
  126. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  127. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_SORT), true, NUM_METRIC_KEY);
  128. List<ComponentDto> result = sortComponents(wsRequest);
  129. assertThat(result).extracting("path")
  130. .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9", "path-without-measure");
  131. }
  132. @Test
  133. void sort_by_numerical_metric_key_descending() {
  134. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  135. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_SORT), false, NUM_METRIC_KEY);
  136. List<ComponentDto> result = sortComponents(wsRequest);
  137. assertThat(result).extracting("path")
  138. .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
  139. }
  140. @Test
  141. void sort_by_name_ascending_in_case_of_equality() {
  142. components = newArrayList(
  143. newComponentWithoutSnapshotId("PROJECT 12", Qualifiers.PROJECT, "PROJECT_PATH_1"),
  144. newComponentWithoutSnapshotId("PROJECT 11", Qualifiers.PROJECT, "PROJECT_PATH_1"),
  145. newComponentWithoutSnapshotId("PROJECT 0", Qualifiers.PROJECT, "PROJECT_PATH_2"));
  146. ComponentTreeRequest wsRequest = newRequest(newArrayList(PATH_SORT), false, null);
  147. List<ComponentDto> result = sortComponents(wsRequest);
  148. assertThat(result).extracting("name").containsExactly("PROJECT 0", "PROJECT 11", "PROJECT 12");
  149. }
  150. @Test
  151. void sort_by_alert_status_ascending() {
  152. components = newArrayList(
  153. newComponentWithoutSnapshotId("PROJECT OK 1", Qualifiers.PROJECT, "PROJECT_OK_PATH_1"),
  154. newComponentWithoutSnapshotId("PROJECT ERROR 1", Qualifiers.PROJECT, "PROJECT_ERROR_PATH_1"),
  155. newComponentWithoutSnapshotId("PROJECT OK 2", Qualifiers.PROJECT, "PROJECT_OK_PATH_2"),
  156. newComponentWithoutSnapshotId("PROJECT ERROR 2", Qualifiers.PROJECT, "PROJECT_ERROR_PATH_2"));
  157. metrics = singletonList(newMetricDto()
  158. .setKey(CoreMetrics.ALERT_STATUS_KEY)
  159. .setValueType(ValueType.LEVEL.name()));
  160. measuresByComponentUuidAndMetric = HashBasedTable.create();
  161. List<String> statuses = newArrayList("OK", "ERROR");
  162. for (int i = 0; i < components.size(); i++) {
  163. ComponentDto component = components.get(i);
  164. String alertStatus = statuses.get(i % 2);
  165. measuresByComponentUuidAndMetric.put(component.uuid(), metrics.get(0), createFromMeasureDto(new LiveMeasureDto().setData(alertStatus)));
  166. }
  167. ComponentTreeRequest wsRequest = newRequest(newArrayList(METRIC_SORT, NAME_SORT), true, CoreMetrics.ALERT_STATUS_KEY);
  168. List<ComponentDto> result = sortComponents(wsRequest);
  169. assertThat(result).extracting("name").containsExactly(
  170. "PROJECT ERROR 1", "PROJECT ERROR 2",
  171. "PROJECT OK 1", "PROJECT OK 2");
  172. }
  173. @Test
  174. void sort_by_numerical_metric_period_1_key_descending() {
  175. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  176. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_PERIOD_SORT), false, NEW_METRIC_KEY).setMetricPeriodSort(1);
  177. List<ComponentDto> result = sortComponents(wsRequest);
  178. assertThat(result).extracting("path")
  179. .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
  180. }
  181. @Test
  182. void sort_by_numerical_metric_period_1_key_ascending() {
  183. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  184. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_PERIOD_SORT), true, NEW_METRIC_KEY).setMetricPeriodSort(1);
  185. List<ComponentDto> result = sortComponents(wsRequest);
  186. assertThat(result).extracting("path")
  187. .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9", "path-without-measure");
  188. }
  189. @Test
  190. void sort_by_numerical_metric_period_5_key() {
  191. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  192. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_SORT), false, NUM_METRIC_KEY).setMetricPeriodSort(5);
  193. List<ComponentDto> result = sortComponents(wsRequest);
  194. assertThat(result).extracting("path")
  195. .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
  196. }
  197. @Test
  198. void sort_by_textual_metric_key_ascending() {
  199. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  200. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_SORT), true, TEXT_METRIC_KEY);
  201. List<ComponentDto> result = sortComponents(wsRequest);
  202. assertThat(result).extracting("path")
  203. .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9", "path-without-measure");
  204. }
  205. @Test
  206. void sort_by_textual_metric_key_descending() {
  207. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  208. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_SORT), false, TEXT_METRIC_KEY);
  209. List<ComponentDto> result = sortComponents(wsRequest);
  210. assertThat(result).extracting("path")
  211. .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
  212. }
  213. @Test
  214. void sort_on_multiple_fields() {
  215. components = newArrayList(
  216. newComponentWithoutSnapshotId("name-1", "qualifier-1", "path-2"),
  217. newComponentWithoutSnapshotId("name-1", "qualifier-1", "path-3"),
  218. newComponentWithoutSnapshotId("name-1", "qualifier-1", "path-1"));
  219. ComponentTreeRequest wsRequest = newRequest(newArrayList(NAME_SORT, QUALIFIER_SORT, PATH_SORT), true, null);
  220. List<ComponentDto> result = sortComponents(wsRequest);
  221. assertThat(result).extracting("path")
  222. .containsExactly("path-1", "path-2", "path-3");
  223. }
  224. @Test
  225. void sortComponent_whenMetricIsImpactDataType_shouldOrderByTotalAscending() {
  226. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  227. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_SORT), true, DATA_IMPACT_METRIC_KEY);
  228. List<ComponentDto> result = sortComponents(wsRequest);
  229. assertThat(result).extracting("path")
  230. .containsExactly("path-1", "path-2", "path-3", "path-4", "path-5", "path-6", "path-7", "path-8", "path-9", "path-without-measure");
  231. }
  232. @Test
  233. void sortComponent_whenMetricIsImpactDataType_shouldOrderByTotalDescending() {
  234. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  235. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_SORT), false, DATA_IMPACT_METRIC_KEY);
  236. List<ComponentDto> result = sortComponents(wsRequest);
  237. assertThat(result).extracting("path")
  238. .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
  239. }
  240. @Test
  241. void sortComponent_whenMetricIsNewAndMetricPeriodSort_shouldOrderByTotal() {
  242. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  243. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_PERIOD_SORT), false, NEW_DATA_IMPACT_METRIC_KEY).setMetricPeriodSort(1);
  244. List<ComponentDto> result = sortComponents(wsRequest);
  245. assertThat(result).extracting("path")
  246. .containsExactly("path-9", "path-8", "path-7", "path-6", "path-5", "path-4", "path-3", "path-2", "path-1", "path-without-measure");
  247. }
  248. @Test
  249. void sortComponent_whenMetricIsNotNewAndMetricPeriodSort_shouldNotOrder() {
  250. components.add(newComponentWithoutSnapshotId("name-without-measure", "qualifier-without-measure", "path-without-measure"));
  251. ComponentTreeRequest wsRequest = newRequest(singletonList(METRIC_PERIOD_SORT), false, DATA_IMPACT_METRIC_KEY).setMetricPeriodSort(1);
  252. List<ComponentDto> result = sortComponents(wsRequest);
  253. assertThat(result).extracting("path")
  254. .containsExactly("path-9", "path-7", "path-8", "path-6", "path-3", "path-4", "path-5", "path-1", "path-2", "path-without-measure");
  255. }
  256. private List<ComponentDto> sortComponents(ComponentTreeRequest wsRequest) {
  257. return ComponentTreeSort.sortComponents(components, wsRequest, metrics, measuresByComponentUuidAndMetric);
  258. }
  259. private static ComponentDto newComponentWithoutSnapshotId(String name, String qualifier, String path) {
  260. return new ComponentDto()
  261. .setUuid(Uuids.createFast())
  262. .setName(name)
  263. .setQualifier(qualifier)
  264. .setPath(path);
  265. }
  266. private static ComponentTreeRequest newRequest(List<String> sortFields, boolean isAscending, @Nullable String metricKey) {
  267. return new ComponentTreeRequest()
  268. .setAsc(isAscending)
  269. .setSort(sortFields)
  270. .setMetricSort(metricKey);
  271. }
  272. }