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.

ComponentTreeActionIT.java 59KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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.base.Joiner;
  22. import java.util.List;
  23. import java.util.stream.IntStream;
  24. import org.junit.jupiter.api.Test;
  25. import org.junit.jupiter.api.extension.RegisterExtension;
  26. import org.sonar.api.measures.Metric;
  27. import org.sonar.api.resources.ResourceTypeTree;
  28. import org.sonar.api.resources.ResourceTypes;
  29. import org.sonar.api.server.ws.WebService.Param;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.core.component.DefaultResourceTypes;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.DbTester;
  35. import org.sonar.db.component.BranchDto;
  36. import org.sonar.db.component.ComponentDto;
  37. import org.sonar.db.component.ComponentTesting;
  38. import org.sonar.db.component.ProjectData;
  39. import org.sonar.db.component.ResourceTypesRule;
  40. import org.sonar.db.component.SnapshotDto;
  41. import org.sonar.db.measure.LiveMeasureDto;
  42. import org.sonar.db.metric.MetricDto;
  43. import org.sonar.db.metric.MetricTesting;
  44. import org.sonar.server.component.ComponentFinder;
  45. import org.sonar.server.exceptions.BadRequestException;
  46. import org.sonar.server.exceptions.ForbiddenException;
  47. import org.sonar.server.exceptions.NotFoundException;
  48. import org.sonar.server.l18n.I18nRule;
  49. import org.sonar.server.tester.UserSessionRule;
  50. import org.sonar.server.ws.TestRequest;
  51. import org.sonar.server.ws.WsActionTester;
  52. import org.sonarqube.ws.Common;
  53. import org.sonarqube.ws.Measures.ComponentTreeWsResponse;
  54. import org.sonarqube.ws.Measures.PeriodValue;
  55. import static java.lang.Double.parseDouble;
  56. import static java.lang.String.format;
  57. import static org.assertj.core.api.Assertions.assertThat;
  58. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  59. import static org.assertj.core.api.Assertions.tuple;
  60. import static org.sonar.api.measures.CoreMetrics.MAINTAINABILITY_ISSUES_KEY;
  61. import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_ISSUES_KEY;
  62. import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_ISSUES_KEY;
  63. import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_ISSUES_KEY;
  64. import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY;
  65. import static org.sonar.api.measures.CoreMetrics.RELIABILITY_ISSUES_KEY;
  66. import static org.sonar.api.measures.CoreMetrics.SECURITY_ISSUES_KEY;
  67. import static org.sonar.api.measures.Metric.ValueType.DATA;
  68. import static org.sonar.api.measures.Metric.ValueType.DISTRIB;
  69. import static org.sonar.api.measures.Metric.ValueType.FLOAT;
  70. import static org.sonar.api.measures.Metric.ValueType.INT;
  71. import static org.sonar.api.measures.Metric.ValueType.RATING;
  72. import static org.sonar.api.resources.Qualifiers.APP;
  73. import static org.sonar.api.resources.Qualifiers.DIRECTORY;
  74. import static org.sonar.api.resources.Qualifiers.FILE;
  75. import static org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE;
  76. import static org.sonar.api.server.ws.WebService.Param.SORT;
  77. import static org.sonar.api.utils.DateUtils.parseDateTime;
  78. import static org.sonar.api.web.UserRole.USER;
  79. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  80. import static org.sonar.db.component.ComponentDbTester.toProjectDto;
  81. import static org.sonar.db.component.ComponentTesting.newDirectory;
  82. import static org.sonar.db.component.ComponentTesting.newFileDto;
  83. import static org.sonar.db.component.ComponentTesting.newProjectCopy;
  84. import static org.sonar.db.component.SnapshotTesting.newAnalysis;
  85. import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIOD;
  86. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS;
  87. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH;
  88. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT;
  89. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS;
  90. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_PERIOD_SORT;
  91. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT;
  92. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT_FILTER;
  93. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST;
  94. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_QUALIFIERS;
  95. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_STRATEGY;
  96. import static org.sonar.server.measure.ws.ComponentTreeAction.LEAVES_STRATEGY;
  97. import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_PERIOD_SORT;
  98. import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_SORT;
  99. import static org.sonar.server.measure.ws.ComponentTreeAction.NAME_SORT;
  100. import static org.sonar.server.measure.ws.ComponentTreeAction.WITH_MEASURES_ONLY_METRIC_SORT_FILTER;
  101. import static org.sonar.test.JsonAssert.assertJson;
  102. import static org.sonarqube.ws.Measures.Component;
  103. import static org.sonarqube.ws.Measures.Measure;
  104. class ComponentTreeActionIT {
  105. @RegisterExtension
  106. private final UserSessionRule userSession = UserSessionRule.standalone().logIn();
  107. @RegisterExtension
  108. private final DbTester db = DbTester.create(System2.INSTANCE);
  109. private final I18nRule i18n = new I18nRule();
  110. private final ResourceTypes defaultResourceTypes = new ResourceTypes(new ResourceTypeTree[]{DefaultResourceTypes.get()});
  111. private final ResourceTypesRule resourceTypes = new ResourceTypesRule()
  112. .setRootQualifiers(defaultResourceTypes.getRoots())
  113. .setAllQualifiers(defaultResourceTypes.getAll())
  114. .setLeavesQualifiers(FILE, UNIT_TEST_FILE);
  115. private final DbClient dbClient = db.getDbClient();
  116. private final DbSession dbSession = db.getSession();
  117. private final WsActionTester ws = new WsActionTester(
  118. new ComponentTreeAction(
  119. dbClient, new ComponentFinder(dbClient, resourceTypes), userSession,
  120. i18n, resourceTypes));
  121. @Test
  122. void json_example() {
  123. ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey("MY_PROJECT")
  124. .setName("My Project"));
  125. ComponentDto mainBranch = projectData.getMainBranchComponent();
  126. addProjectPermission(projectData);
  127. SnapshotDto analysis = db.components().insertSnapshot(mainBranch, s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
  128. .setPeriodMode("previous_version")
  129. .setPeriodParam("1.0-SNAPSHOT"));
  130. ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch)
  131. .setUuid("AVIwDXE-bJbJqrw6wFv5")
  132. .setKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/ElementImpl.java")
  133. .setName("ElementImpl.java")
  134. .setLanguage("java")
  135. .setQualifier(FILE)
  136. .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java"));
  137. ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch)
  138. .setUuid("AVIwDXE_bJbJqrw6wFwJ")
  139. .setKey("com.sonarsource:java-markdown:src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java")
  140. .setName("ElementImplTest.java")
  141. .setLanguage("java")
  142. .setQualifier(UNIT_TEST_FILE)
  143. .setPath("src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java"));
  144. ComponentDto dir = db.components().insertComponent(newDirectory(mainBranch, "src/main/java/com/sonarsource/markdown/impl")
  145. .setUuid("AVIwDXE-bJbJqrw6wFv8")
  146. .setKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl")
  147. .setQualifier(DIRECTORY));
  148. MetricDto complexity = insertComplexityMetric();
  149. db.measures().insertLiveMeasure(file1, complexity, m -> m.setValue(12.0d));
  150. db.measures().insertLiveMeasure(dir, complexity, m -> m.setValue(35.0d));
  151. db.measures().insertLiveMeasure(mainBranch, complexity, m -> m.setValue(42.0d));
  152. MetricDto ncloc = insertNclocMetric();
  153. db.measures().insertLiveMeasure(file1, ncloc, m -> m.setValue(114.0d));
  154. db.measures().insertLiveMeasure(dir, ncloc, m -> m.setValue(217.0d));
  155. db.measures().insertLiveMeasure(mainBranch, ncloc, m -> m.setValue(1984.0d));
  156. MetricDto newViolations = insertNewViolationsMetric();
  157. db.measures().insertLiveMeasure(file1, newViolations, m -> m.setValue(25.0d));
  158. db.measures().insertLiveMeasure(dir, newViolations, m -> m.setValue(25.0d));
  159. db.measures().insertLiveMeasure(mainBranch, newViolations, m -> m.setValue(255.0d));
  160. MetricDto accepted_issues = insertAcceptedIssuesMetric();
  161. db.measures().insertLiveMeasure(file1, accepted_issues, m -> m.setValue(10d));
  162. db.measures().insertLiveMeasure(dir, accepted_issues, m -> m.setValue(10d));
  163. db.measures().insertLiveMeasure(mainBranch, accepted_issues, m -> m.setValue(10d));
  164. db.commit();
  165. String response = ws.newRequest()
  166. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  167. .setParam(PARAM_METRIC_KEYS, "ncloc, complexity, new_violations, accepted_issues")
  168. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period")
  169. .execute()
  170. .getInput();
  171. assertJson(response).isSimilarTo(getClass().getResource("component_tree-example.json"));
  172. }
  173. private UserSessionRule addProjectPermission(ProjectData projectData) {
  174. return userSession.addProjectPermission(USER, projectData.getProjectDto())
  175. .addProjectBranchMapping(projectData.projectUuid(), projectData.getMainBranchComponent());
  176. }
  177. @Test
  178. void shouldReturnRenamedMetric() {
  179. ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey("MY_PROJECT")
  180. .setName("My Project"));
  181. addProjectPermission(projectData);
  182. ComponentDto mainBranch = projectData.getMainBranchComponent();
  183. SnapshotDto analysis = db.components().insertSnapshot(mainBranch, s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
  184. .setPeriodMode("previous_version")
  185. .setPeriodParam("1.0-SNAPSHOT"));
  186. MetricDto accepted_issues = insertAcceptedIssuesMetric();
  187. db.measures().insertLiveMeasure(mainBranch, accepted_issues, m -> m.setValue(10d));
  188. db.commit();
  189. ComponentTreeWsResponse response = ws.newRequest()
  190. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  191. .setParam(PARAM_METRIC_KEYS, "wont_fix_issues")
  192. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
  193. .executeProtobuf(ComponentTreeWsResponse.class);
  194. assertThat(response.getMetrics().getMetrics(0).getKey()).isEqualTo("wont_fix_issues");
  195. assertThat(response.getBaseComponent().getMeasures(0).getMetric()).isEqualTo("wont_fix_issues");
  196. }
  197. @Test
  198. void empty_response() {
  199. ProjectData projectData = db.components().insertPrivateProject();
  200. ComponentDto mainBranch = projectData.getMainBranchComponent();
  201. addProjectPermission(projectData);
  202. ComponentTreeWsResponse response = ws.newRequest()
  203. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  204. .setParam(PARAM_METRIC_KEYS, "ncloc, complexity")
  205. .executeProtobuf(ComponentTreeWsResponse.class);
  206. assertThat(response.getBaseComponent().getKey()).isEqualTo(mainBranch.getKey());
  207. assertThat(response.getComponentsList()).isEmpty();
  208. assertThat(response.getMetrics().getMetricsList()).isEmpty();
  209. assertThat(response.hasPeriod()).isFalse();
  210. }
  211. @Test
  212. void load_measures_and_periods() {
  213. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  214. SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession,
  215. newAnalysis(mainBranch)
  216. .setPeriodDate(System.currentTimeMillis())
  217. .setPeriodMode("last_version")
  218. .setPeriodDate(System.currentTimeMillis()));
  219. userSession.anonymous().addProjectPermission(USER, mainBranch);
  220. ComponentDto directory = newDirectory(mainBranch, "directory-uuid", "path/to/directory").setName("directory-1");
  221. db.components().insertComponent(directory);
  222. ComponentDto file = newFileDto(directory, null, "file-uuid").setName("file-1");
  223. db.components().insertComponent(file);
  224. MetricDto ncloc = insertNclocMetric();
  225. MetricDto coverage = insertCoverageMetric();
  226. db.commit();
  227. db.measures().insertLiveMeasure(file, ncloc, m -> m.setValue(5.0d));
  228. db.measures().insertLiveMeasure(file, coverage, m -> m.setValue(15.5d));
  229. db.measures().insertLiveMeasure(directory, coverage, m -> m.setValue(15.5d));
  230. ComponentTreeWsResponse response = ws.newRequest()
  231. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  232. .setParam(PARAM_METRIC_KEYS, "ncloc,coverage")
  233. .setParam(PARAM_ADDITIONAL_FIELDS, ADDITIONAL_PERIOD)
  234. .executeProtobuf(ComponentTreeWsResponse.class);
  235. assertThat(response.getComponentsList().get(0).getMeasuresList()).extracting("metric").containsOnly("coverage");
  236. // file measures
  237. List<Measure> fileMeasures = response.getComponentsList().get(1).getMeasuresList();
  238. assertThat(fileMeasures).extracting("metric").containsOnly("ncloc", "coverage");
  239. assertThat(fileMeasures).extracting("value").containsOnly("5", "15.5");
  240. assertThat(response.getPeriod().getMode()).isEqualTo("last_version");
  241. }
  242. @Test
  243. void load_measures_with_best_value() {
  244. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  245. SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
  246. userSession.anonymous().addProjectPermission(USER, mainBranch);
  247. ComponentDto directory = newDirectory(mainBranch, "directory-uuid", "path/to/directory").setName("directory-1");
  248. db.components().insertComponent(directory);
  249. ComponentDto file = newFileDto(directory, null, "file-uuid").setName("file-1");
  250. db.components().insertComponent(file);
  251. MetricDto coverage = insertCoverageMetric();
  252. dbClient.metricDao().insert(dbSession, MetricTesting.newMetricDto()
  253. .setKey("ncloc")
  254. .setValueType(INT.name())
  255. .setOptimizedBestValue(true)
  256. .setBestValue(100d)
  257. .setWorstValue(1000d));
  258. dbClient.metricDao().insert(dbSession, newMetricDto()
  259. .setKey("new_violations")
  260. .setOptimizedBestValue(true)
  261. .setBestValue(1984.0d)
  262. .setValueType(INT.name()));
  263. db.commit();
  264. db.measures().insertLiveMeasure(file, coverage, m -> m.setValue(15.5d));
  265. db.measures().insertLiveMeasure(directory, coverage, m -> m.setValue(42.0d));
  266. ComponentTreeWsResponse response = ws.newRequest()
  267. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  268. .setParam(PARAM_METRIC_KEYS, "ncloc,coverage,new_violations")
  269. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
  270. .executeProtobuf(ComponentTreeWsResponse.class);
  271. // directory measures
  272. assertThat(response.getComponentsList().get(0).getMeasuresList()).extracting("metric").containsOnly("coverage");
  273. // file measures
  274. List<Measure> fileMeasures = response.getComponentsList().get(1).getMeasuresList();
  275. assertThat(fileMeasures)
  276. .extracting(Measure::getMetric, Measure::getValue, Measure::getBestValue, Measure::hasBestValue)
  277. .containsExactlyInAnyOrder(tuple("ncloc", "100", true, true),
  278. tuple("coverage", "15.5", false, false),
  279. tuple("new_violations", "", false, false));
  280. List<Common.Metric> metrics = response.getMetrics().getMetricsList();
  281. assertThat(metrics).extracting("bestValue").contains("100", "");
  282. assertThat(metrics).extracting("worstValue").contains("1000");
  283. }
  284. @Test
  285. void return_is_best_value_on_leak_measures() {
  286. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  287. db.components().insertSnapshot(mainBranch);
  288. userSession.anonymous().addProjectPermission(USER, mainBranch);
  289. ComponentDto file = newFileDto(mainBranch);
  290. db.components().insertComponent(file);
  291. MetricDto matchingBestValue = db.measures().insertMetric(m -> m
  292. .setKey("new_lines")
  293. .setValueType(INT.name())
  294. .setBestValue(100d));
  295. MetricDto doesNotMatchBestValue = db.measures().insertMetric(m -> m
  296. .setKey("new_lines_2")
  297. .setValueType(INT.name())
  298. .setBestValue(100d));
  299. MetricDto noBestValue = db.measures().insertMetric(m -> m
  300. .setKey("new_violations")
  301. .setValueType(INT.name())
  302. .setBestValue(null));
  303. db.measures().insertLiveMeasure(file, matchingBestValue, m -> m.setData((String) null).setValue(100d));
  304. db.measures().insertLiveMeasure(file, doesNotMatchBestValue, m -> m.setData((String) null).setValue(10d));
  305. db.measures().insertLiveMeasure(file, noBestValue, m -> m.setData((String) null).setValue(42.0d));
  306. ComponentTreeWsResponse response = ws.newRequest()
  307. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  308. .setParam(PARAM_METRIC_KEYS, "new_lines,new_lines_2,new_violations")
  309. .executeProtobuf(ComponentTreeWsResponse.class);
  310. // file measures
  311. // verify backward compatibility
  312. List<Measure> fileMeasures = response.getComponentsList().get(0).getMeasuresList();
  313. assertThat(fileMeasures)
  314. .extracting(Measure::getMetric, Measure::getPeriod)
  315. .containsExactlyInAnyOrder(
  316. tuple(matchingBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("100").setBestValue(true).build()),
  317. tuple(doesNotMatchBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("10").setBestValue(false).build()),
  318. tuple(noBestValue.getKey(), PeriodValue.newBuilder().setIndex(1).setValue("42").build()));
  319. }
  320. @Test
  321. void use_best_value_for_rating() {
  322. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  323. userSession.anonymous().addProjectPermission(USER, mainBranch);
  324. SnapshotDto projectSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(mainBranch)
  325. .setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
  326. .setPeriodMode("previous_version")
  327. .setPeriodParam("1.0-SNAPSHOT"));
  328. ComponentDto directory = newDirectory(mainBranch, "directory-uuid", "path/to/directory").setName("directory-1");
  329. db.components().insertComponent(directory);
  330. ComponentDto file = newFileDto(directory, null, "file-uuid").setName("file-1");
  331. db.components().insertComponent(file);
  332. MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto()
  333. .setKey(NEW_SECURITY_RATING_KEY)
  334. .setOptimizedBestValue(true)
  335. .setBestValue(1d)
  336. .setValueType(RATING.name()));
  337. db.commit();
  338. db.measures().insertLiveMeasure(directory, metric, m -> m.setValue(2d));
  339. ComponentTreeWsResponse response = ws.newRequest()
  340. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  341. .setParam(PARAM_METRIC_KEYS, NEW_SECURITY_RATING_KEY)
  342. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
  343. .executeProtobuf(ComponentTreeWsResponse.class);
  344. // directory
  345. assertThat(response.getComponentsList().get(0).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("2.0");
  346. assertThat(response.getComponentsList().get(0).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("2.0");
  347. // file measures
  348. assertThat(response.getComponentsList().get(1).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("1.0");
  349. assertThat(response.getComponentsList().get(1).getMeasuresList().get(0).getPeriod().getValue()).isEqualTo("1.0");
  350. }
  351. @Test
  352. void load_measures_multi_sort_with_metric_key_and_paginated() {
  353. ProjectData projectData = db.components().insertPrivateProject();
  354. ComponentDto mainBranch = projectData.getMainBranchComponent();
  355. addProjectPermission(projectData);
  356. SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
  357. ComponentDto file9 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-9").setName("file-1").setKey("file-9-key"));
  358. ComponentDto file8 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-8").setName("file-1").setKey("file-8-key"));
  359. ComponentDto file7 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-7").setName("file-1").setKey("file-7-key"));
  360. ComponentDto file6 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-6").setName("file-1").setKey("file-6-key"));
  361. ComponentDto file5 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-5").setName("file-1").setKey("file-5-key"));
  362. ComponentDto file4 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-4").setName("file-1").setKey("file-4-key"));
  363. ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setName("file-1").setKey("file-3-key"));
  364. ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setName("file-1").setKey("file-2-key"));
  365. ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setName("file-1").setKey("file-1-key"));
  366. MetricDto coverage = insertCoverageMetric();
  367. db.commit();
  368. db.measures().insertLiveMeasure(file1, coverage, m -> m.setValue(1.0d));
  369. db.measures().insertLiveMeasure(file2, coverage, m -> m.setValue(2.0d));
  370. db.measures().insertLiveMeasure(file3, coverage, m -> m.setValue(3.0d));
  371. db.measures().insertLiveMeasure(file4, coverage, m -> m.setValue(4.0d));
  372. db.measures().insertLiveMeasure(file5, coverage, m -> m.setValue(5.0d));
  373. db.measures().insertLiveMeasure(file6, coverage, m -> m.setValue(6.0d));
  374. db.measures().insertLiveMeasure(file7, coverage, m -> m.setValue(7.0d));
  375. db.measures().insertLiveMeasure(file8, coverage, m -> m.setValue(8.0d));
  376. db.measures().insertLiveMeasure(file9, coverage, m -> m.setValue(9.0d));
  377. ComponentTreeWsResponse response = ws.newRequest()
  378. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  379. .setParam(SORT, NAME_SORT + ", " + METRIC_SORT)
  380. .setParam(PARAM_METRIC_SORT, "coverage")
  381. .setParam(PARAM_METRIC_KEYS, "coverage")
  382. .setParam(PARAM_STRATEGY, "leaves")
  383. .setParam(PARAM_QUALIFIERS, "FIL,UTS")
  384. .setParam(Param.PAGE, "2")
  385. .setParam(Param.PAGE_SIZE, "3")
  386. .executeProtobuf(ComponentTreeWsResponse.class);
  387. assertThat(response.getComponentsList()).extracting("key").containsExactly("file-4-key", "file-5-key", "file-6-key");
  388. assertThat(response.getPaging().getPageIndex()).isEqualTo(2);
  389. assertThat(response.getPaging().getPageSize()).isEqualTo(3);
  390. assertThat(response.getPaging().getTotal()).isEqualTo(9);
  391. }
  392. @Test
  393. void sort_by_metric_value() {
  394. ProjectData projectData = db.components().insertPrivateProject();
  395. ComponentDto mainBranch = projectData.getMainBranchComponent();
  396. addProjectPermission(projectData);
  397. SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
  398. ComponentDto file4 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-4").setKey("file-4-key"));
  399. ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key"));
  400. ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key"));
  401. ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key"));
  402. MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1);
  403. dbClient.metricDao().insert(dbSession, ncloc);
  404. db.commit();
  405. db.measures().insertLiveMeasure(file1, ncloc, m -> m.setValue(1.0d));
  406. db.measures().insertLiveMeasure(file2, ncloc, m -> m.setValue(2.0d));
  407. db.measures().insertLiveMeasure(file3, ncloc, m -> m.setValue(3.0d));
  408. ComponentTreeWsResponse response = ws.newRequest()
  409. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  410. .setParam(SORT, METRIC_SORT)
  411. .setParam(PARAM_METRIC_SORT, "ncloc")
  412. .setParam(PARAM_METRIC_KEYS, "ncloc")
  413. .executeProtobuf(ComponentTreeWsResponse.class);
  414. assertThat(response.getComponentsList()).extracting("key").containsExactly("file-1-key", "file-2-key", "file-3-key", "file-4-key");
  415. assertThat(response.getPaging().getTotal()).isEqualTo(4);
  416. }
  417. @Test
  418. void remove_components_without_measure_on_the_metric_sort() {
  419. ProjectData projectData = db.components().insertPrivateProject();
  420. ComponentDto mainBranch = projectData.getMainBranchComponent();
  421. addProjectPermission(projectData);
  422. SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
  423. ComponentDto file1 = newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key");
  424. ComponentDto file2 = newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key");
  425. ComponentDto file3 = newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key");
  426. ComponentDto file4 = newFileDto(mainBranch, null, "file-uuid-4").setKey("file-4-key");
  427. db.components().insertComponent(file1);
  428. db.components().insertComponent(file2);
  429. db.components().insertComponent(file3);
  430. db.components().insertComponent(file4);
  431. MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1);
  432. dbClient.metricDao().insert(dbSession, ncloc);
  433. db.measures().insertLiveMeasure(file1, ncloc, m -> m.setData((String) null).setValue(1.0d));
  434. db.measures().insertLiveMeasure(file2, ncloc, m -> m.setData((String) null).setValue(2.0d));
  435. db.measures().insertLiveMeasure(file3, ncloc, m -> m.setData((String) null).setValue(3.0d));
  436. db.commit();
  437. ComponentTreeWsResponse response = ws.newRequest()
  438. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  439. .setParam(SORT, METRIC_SORT)
  440. .setParam(PARAM_METRIC_SORT, "ncloc")
  441. .setParam(PARAM_METRIC_KEYS, "ncloc")
  442. .setParam(PARAM_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER)
  443. .executeProtobuf(ComponentTreeWsResponse.class);
  444. assertThat(response.getComponentsList()).extracting("key")
  445. .containsExactly(file1.getKey(), file2.getKey(), file3.getKey())
  446. .doesNotContain(file4.getKey());
  447. assertThat(response.getPaging().getTotal()).isEqualTo(3);
  448. }
  449. @Test
  450. void sort_by_metric_period() {
  451. ProjectData projectData = db.components().insertPrivateProject();
  452. ComponentDto mainBranch = projectData.getMainBranchComponent();
  453. addProjectPermission(projectData);
  454. SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
  455. ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key"));
  456. ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key"));
  457. ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key"));
  458. MetricDto ncloc = newMetricDto().setKey("ncloc").setValueType(INT.name()).setDirection(1);
  459. dbClient.metricDao().insert(dbSession, ncloc);
  460. db.commit();
  461. db.measures().insertLiveMeasure(file1, ncloc, m -> m.setValue(1.0d));
  462. db.measures().insertLiveMeasure(file2, ncloc, m -> m.setValue(2.0d));
  463. db.measures().insertLiveMeasure(file3, ncloc, m -> m.setValue(3.0d));
  464. ComponentTreeWsResponse response = ws.newRequest()
  465. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  466. .setParam(SORT, METRIC_PERIOD_SORT)
  467. .setParam(PARAM_METRIC_SORT, "ncloc")
  468. .setParam(PARAM_METRIC_KEYS, "ncloc")
  469. .setParam(PARAM_METRIC_PERIOD_SORT, "1")
  470. .executeProtobuf(ComponentTreeWsResponse.class);
  471. assertThat(response.getComponentsList()).extracting("key").containsExactly("file-1-key", "file-2-key", "file-3-key");
  472. }
  473. @Test
  474. void remove_components_without_measure_on_the_metric_period_sort() {
  475. ProjectData projectData = db.components().insertPrivateProject();
  476. ComponentDto mainBranch = projectData.getMainBranchComponent();
  477. addProjectPermission(projectData);
  478. SnapshotDto projectSnapshot = db.components().insertSnapshot(mainBranch);
  479. ComponentDto file4 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-4").setKey("file-4-key"));
  480. ComponentDto file3 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-3").setKey("file-3-key"));
  481. ComponentDto file2 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-2").setKey("file-2-key"));
  482. ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch, null, "file-uuid-1").setKey("file-1-key"));
  483. MetricDto ncloc = newMetricDto().setKey("new_ncloc").setValueType(INT.name()).setDirection(1);
  484. dbClient.metricDao().insert(dbSession, ncloc);
  485. db.measures().insertLiveMeasure(file1, ncloc, m -> m.setData((String) null).setValue(1.0d));
  486. db.measures().insertLiveMeasure(file2, ncloc, m -> m.setData((String) null).setValue(2.0d));
  487. db.measures().insertLiveMeasure(file3, ncloc, m -> m.setData((String) null).setValue(3.0d));
  488. db.commit();
  489. ComponentTreeWsResponse response = ws.newRequest()
  490. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  491. .setParam(SORT, METRIC_PERIOD_SORT + "," + NAME_SORT)
  492. .setParam(PARAM_METRIC_SORT, "new_ncloc")
  493. .setParam(PARAM_METRIC_KEYS, "new_ncloc")
  494. .setParam(PARAM_METRIC_PERIOD_SORT, "1")
  495. .setParam(PARAM_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER)
  496. .executeProtobuf(ComponentTreeWsResponse.class);
  497. assertThat(response.getComponentsList()).extracting("key")
  498. .containsExactly("file-1-key", "file-2-key", "file-3-key")
  499. .doesNotContain("file-4-key");
  500. }
  501. @Test
  502. void load_measures_when_no_leave_qualifier() {
  503. resourceTypes.setLeavesQualifiers();
  504. ProjectData projectData = db.components().insertPrivateProject();
  505. ComponentDto mainBranch = projectData.getMainBranchComponent();
  506. addProjectPermission(projectData);
  507. db.components().insertSnapshot(mainBranch);
  508. db.components().insertComponent(newFileDto(mainBranch));
  509. insertNclocMetric();
  510. ComponentTreeWsResponse result = ws.newRequest()
  511. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  512. .setParam(PARAM_STRATEGY, LEAVES_STRATEGY)
  513. .setParam(PARAM_METRIC_KEYS, "ncloc")
  514. .executeProtobuf(ComponentTreeWsResponse.class);
  515. assertThat(result.getBaseComponent().getKey()).isEqualTo(mainBranch.getKey());
  516. assertThat(result.getComponentsCount()).isZero();
  517. }
  518. @Test
  519. void branch() {
  520. ProjectData projectData = db.components().insertPrivateProject();
  521. ComponentDto mainBranch = projectData.getMainBranchComponent();
  522. addProjectPermission(projectData);
  523. String branchName = "my-branch";
  524. ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey(branchName));
  525. userSession.addProjectBranchMapping(projectData.getProjectDto().getUuid(), branch);
  526. db.components().insertSnapshot(branch);
  527. ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
  528. MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
  529. LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
  530. ComponentTreeWsResponse response = ws.newRequest()
  531. .setParam(PARAM_COMPONENT, file.getKey())
  532. .setParam(PARAM_BRANCH, branchName)
  533. .setParam(PARAM_METRIC_KEYS, complexity.getKey())
  534. .executeProtobuf(ComponentTreeWsResponse.class);
  535. assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getBranch)
  536. .containsExactlyInAnyOrder(file.getKey(), branchName);
  537. assertThat(response.getBaseComponent().getMeasuresList())
  538. .extracting(Measure::getMetric, m -> parseDouble(m.getValue()))
  539. .containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue()));
  540. }
  541. @Test
  542. void dont_show_branch_if_main_branch() {
  543. ProjectData projectData = db.components().insertPrivateProject();
  544. ComponentDto mainBranch = projectData.getMainBranchComponent();
  545. addProjectPermission(projectData);
  546. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
  547. MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
  548. ComponentTreeWsResponse response = ws.newRequest()
  549. .setParam(PARAM_COMPONENT, file.getKey())
  550. .setParam(PARAM_BRANCH, BranchDto.DEFAULT_MAIN_BRANCH_NAME)
  551. .setParam(PARAM_METRIC_KEYS, complexity.getKey())
  552. .executeProtobuf(ComponentTreeWsResponse.class);
  553. assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getBranch)
  554. .containsExactlyInAnyOrder(file.getKey(), "");
  555. }
  556. @Test
  557. void show_branch_on_empty_response_if_not_main_branch() {
  558. ComponentDto mainProjectBranch = db.components().insertPrivateProject().getMainBranchComponent();
  559. userSession.addProjectPermission(USER, mainProjectBranch);
  560. ComponentDto branch = db.components().insertProjectBranch(mainProjectBranch, b -> b.setKey("develop"));
  561. userSession.addProjectBranchMapping(mainProjectBranch.uuid(), branch);
  562. ComponentDto file = db.components().insertComponent(newFileDto(branch, mainProjectBranch.uuid()));
  563. MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
  564. ComponentTreeWsResponse response = ws.newRequest()
  565. .setParam(PARAM_COMPONENT, file.getKey())
  566. .setParam(PARAM_BRANCH, "develop")
  567. .setParam(PARAM_METRIC_KEYS, complexity.getKey())
  568. .executeProtobuf(ComponentTreeWsResponse.class);
  569. assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getBranch)
  570. .containsExactlyInAnyOrder(file.getKey(), "develop");
  571. }
  572. @Test
  573. void pull_request() {
  574. ProjectData projectData = db.components().insertPrivateProject();
  575. ComponentDto mainBranch = projectData.getMainBranchComponent();
  576. addProjectPermission(projectData);
  577. ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
  578. userSession.addProjectBranchMapping(projectData.projectUuid(), branch);
  579. SnapshotDto analysis = db.components().insertSnapshot(branch);
  580. ComponentDto file = db.components().insertComponent(newFileDto(branch, mainBranch.uuid()));
  581. MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
  582. LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d));
  583. ComponentTreeWsResponse response = ws.newRequest()
  584. .setParam(PARAM_COMPONENT, file.getKey())
  585. .setParam(PARAM_PULL_REQUEST, "pr-123")
  586. .setParam(PARAM_METRIC_KEYS, complexity.getKey())
  587. .executeProtobuf(ComponentTreeWsResponse.class);
  588. assertThat(response.getBaseComponent()).extracting(Component::getKey, Component::getPullRequest)
  589. .containsExactlyInAnyOrder(file.getKey(), "pr-123");
  590. assertThat(response.getBaseComponent().getMeasuresList())
  591. .extracting(Measure::getMetric, m -> parseDouble(m.getValue()))
  592. .containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue()));
  593. }
  594. @Test
  595. void metric_without_a_domain() {
  596. ProjectData projectData = db.components().insertPrivateProject();
  597. ComponentDto mainBranch = projectData.getMainBranchComponent();
  598. addProjectPermission(projectData);
  599. SnapshotDto analysis = db.getDbClient().snapshotDao().insert(dbSession, newAnalysis(mainBranch));
  600. MetricDto metricWithoutDomain = db.measures().insertMetric(m -> m
  601. .setValueType(Metric.ValueType.INT.name())
  602. .setDomain(null));
  603. db.measures().insertLiveMeasure(mainBranch, metricWithoutDomain);
  604. ComponentTreeWsResponse result = ws.newRequest()
  605. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  606. .setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
  607. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
  608. .executeProtobuf(ComponentTreeWsResponse.class);
  609. assertThat(result.getBaseComponent().getMeasures(0).getMetric()).isEqualTo(metricWithoutDomain.getKey());
  610. Common.Metric responseMetric = result.getMetrics().getMetrics(0);
  611. assertThat(responseMetric.getKey()).isEqualTo(metricWithoutDomain.getKey());
  612. assertThat(responseMetric.hasDomain()).isFalse();
  613. }
  614. @Test
  615. void project_reference_from_portfolio() {
  616. ProjectData projectData = db.components().insertPrivateProject();
  617. ComponentDto mainBranch = projectData.getMainBranchComponent();
  618. addProjectPermission(projectData);
  619. ComponentDto view = db.components().insertPrivatePortfolio();
  620. userSession.addProjectPermission(USER, view);
  621. SnapshotDto viewAnalysis = db.components().insertSnapshot(view);
  622. ComponentDto projectCopy = db.components().insertComponent(newProjectCopy(mainBranch, view));
  623. MetricDto ncloc = insertNclocMetric();
  624. db.measures().insertLiveMeasure(projectCopy, ncloc, m -> m.setValue(5d));
  625. ComponentTreeWsResponse result = ws.newRequest()
  626. .setParam(PARAM_COMPONENT, view.getKey())
  627. .setParam(PARAM_METRIC_KEYS, ncloc.getKey())
  628. .executeProtobuf(ComponentTreeWsResponse.class);
  629. assertThat(result.getComponentsList())
  630. .extracting(Component::getKey, Component::getRefKey)
  631. .containsExactlyInAnyOrder(tuple(projectCopy.getKey(), mainBranch.getKey()));
  632. }
  633. @Test
  634. void portfolio_local_reference_in_portfolio() {
  635. ComponentDto view = db.components().insertComponent(ComponentTesting.newPortfolio("VIEW1-UUID")
  636. .setKey("Apache-Projects").setName("Apache Projects"));
  637. userSession.registerPortfolios(view);
  638. ComponentDto view2 = db.components().insertPrivatePortfolio();
  639. userSession.addProjectPermission(USER, view2);
  640. ComponentDto localView = db.components().insertComponent(
  641. ComponentTesting.newSubPortfolio(view, "SUB-VIEW-UUID", "All-Projects").setName("All projects").setCopyComponentUuid(view2.uuid()));
  642. db.components().insertSnapshot(view);
  643. MetricDto ncloc = insertNclocMetric();
  644. db.measures().insertLiveMeasure(localView, ncloc, m -> m.setValue(5d));
  645. ComponentTreeWsResponse result = ws.newRequest()
  646. .setParam(PARAM_COMPONENT, view.getKey())
  647. .setParam(PARAM_METRIC_KEYS, ncloc.getKey())
  648. .executeProtobuf(ComponentTreeWsResponse.class);
  649. assertThat(result.getComponentsList())
  650. .extracting(Component::getKey, Component::getRefKey, Component::getQualifier)
  651. .containsExactlyInAnyOrder(tuple(localView.getKey(), view2.getKey(), "SVW"));
  652. }
  653. @Test
  654. void application_local_reference_in_portfolio() {
  655. ComponentDto apache_projects = ComponentTesting.newPortfolio("VIEW1-UUID")
  656. .setKey("Apache-Projects").setName("Apache Projects").setPrivate(true);
  657. userSession.addProjectPermission(USER, apache_projects);
  658. ComponentDto view = db.components().insertComponent(apache_projects);
  659. ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent();
  660. userSession.addProjectPermission(USER, application);
  661. ComponentDto localView = db.components().insertComponent(
  662. ComponentTesting.newSubPortfolio(view, "SUB-VIEW-UUID", "All-Projects").setName("All projects").setCopyComponentUuid(application.uuid()));
  663. db.components().insertSnapshot(view);
  664. MetricDto ncloc = insertNclocMetric();
  665. db.measures().insertLiveMeasure(localView, ncloc, m -> m.setValue(5d));
  666. ComponentTreeWsResponse result = ws.newRequest()
  667. .setParam(PARAM_COMPONENT, view.getKey())
  668. .setParam(PARAM_METRIC_KEYS, ncloc.getKey())
  669. .executeProtobuf(ComponentTreeWsResponse.class);
  670. assertThat(result.getComponentsList())
  671. .extracting(Component::getKey, Component::getRefKey, Component::getQualifier)
  672. .containsExactlyInAnyOrder(tuple(localView.getKey(), application.getKey(), "APP"));
  673. }
  674. @Test
  675. void project_branch_reference_from_application_branch() {
  676. MetricDto ncloc = insertNclocMetric();
  677. ProjectData applicationData = db.components().insertPublicProject(c -> c.setQualifier(APP).setKey("app-key"));
  678. ComponentDto application = applicationData.getMainBranchComponent();
  679. userSession.registerApplication(applicationData.getProjectDto());
  680. String branchName = "app-branch";
  681. ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey(branchName), a -> a.setUuid("custom-uuid"));
  682. userSession.addProjectBranchMapping(applicationData.projectUuid(), applicationBranch);
  683. ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey("project-key"));
  684. ComponentDto mainBranch = projectData.getMainBranchComponent();
  685. ComponentDto projectBranch = db.components().insertProjectBranch(mainBranch, b -> b.setKey("project-branch"));
  686. userSession.addProjectBranchMapping(projectData.projectUuid(), projectBranch);
  687. ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch)
  688. .setKey(applicationBranch.getKey() + branchName + projectBranch.getKey()));
  689. SnapshotDto applicationBranchAnalysis = db.components().insertSnapshot(applicationBranch);
  690. db.measures().insertLiveMeasure(applicationBranch, ncloc, m -> m.setValue(5d));
  691. db.measures().insertLiveMeasure(techProjectBranch, ncloc, m -> m.setValue(1d));
  692. ComponentTreeWsResponse result = ws.newRequest()
  693. .setParam(PARAM_COMPONENT, applicationBranch.getKey())
  694. .setParam(PARAM_BRANCH, branchName)
  695. .setParam(PARAM_METRIC_KEYS, ncloc.getKey())
  696. .executeProtobuf(ComponentTreeWsResponse.class);
  697. assertThat(result.getBaseComponent())
  698. .extracting(Component::getKey, Component::getBranch)
  699. .containsExactlyInAnyOrder(applicationBranch.getKey(), branchName);
  700. assertThat(result.getComponentsList())
  701. .extracting(Component::getKey, Component::getBranch, Component::getRefKey)
  702. .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), "project-branch", mainBranch.getKey()));
  703. }
  704. @Test
  705. void fail_when_metric_keys_parameter_is_empty() {
  706. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  707. db.components().insertSnapshot(mainBranch);
  708. assertThatThrownBy(() -> {
  709. ws.newRequest()
  710. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  711. .setParam(PARAM_METRIC_KEYS, "")
  712. .executeProtobuf(ComponentTreeWsResponse.class);
  713. })
  714. .isInstanceOf(BadRequestException.class)
  715. .hasMessage("The 'metricKeys' parameter must contain at least one metric key");
  716. }
  717. @Test
  718. void fail_when_a_metric_is_not_found() {
  719. ProjectData projectData = db.components().insertPrivateProject();
  720. ComponentDto mainBranch = projectData.getMainBranchComponent();
  721. addProjectPermission(projectData);
  722. db.components().insertSnapshot(mainBranch);
  723. insertNclocMetric();
  724. insertNewViolationsMetric();
  725. assertThatThrownBy(() -> {
  726. ws.newRequest()
  727. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  728. .setParam(PARAM_METRIC_KEYS, "ncloc, new_violations, unknown-metric, another-unknown-metric").executeProtobuf(ComponentTreeWsResponse.class);
  729. })
  730. .isInstanceOf(NotFoundException.class)
  731. .hasMessage("The following metric keys are not found: unknown-metric, another-unknown-metric");
  732. }
  733. @Test
  734. void fail_when_using_DISTRIB_metrics() {
  735. ProjectData projectData = db.components().insertPrivateProject();
  736. ComponentDto mainBranch = projectData.getMainBranchComponent();
  737. addProjectPermission(projectData);
  738. db.components().insertSnapshot(mainBranch);
  739. dbClient.metricDao().insert(dbSession, newMetricDto().setKey("distrib1").setValueType(DISTRIB.name()));
  740. dbClient.metricDao().insert(dbSession, newMetricDto().setKey("distrib2").setValueType(DISTRIB.name()));
  741. db.commit();
  742. assertThatThrownBy(() -> {
  743. ws.newRequest()
  744. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  745. .setParam(PARAM_METRIC_KEYS, "distrib1,distrib2")
  746. .execute();
  747. })
  748. .isInstanceOf(IllegalArgumentException.class)
  749. .hasMessage("Metrics distrib1, distrib2 can't be requested in this web service. Please use api/measures/component");
  750. }
  751. @Test
  752. void fail_when_using_unsupported_DATA_metrics() {
  753. ProjectData projectData = db.components().insertPrivateProject();
  754. ComponentDto mainBranch = projectData.getMainBranchComponent();
  755. addProjectPermission(projectData);
  756. db.components().insertSnapshot(mainBranch);
  757. dbClient.metricDao().insert(dbSession, newMetricDto().setKey("data1").setValueType(DATA.name()));
  758. dbClient.metricDao().insert(dbSession, newMetricDto().setKey("data2").setValueType(DATA.name()));
  759. db.commit();
  760. assertThatThrownBy(() -> {
  761. ws.newRequest()
  762. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  763. .setParam(PARAM_METRIC_KEYS, "data1,data2")
  764. .execute();
  765. })
  766. .isInstanceOf(IllegalArgumentException.class)
  767. .hasMessage("Metrics data1, data2 can't be requested in this web service. Please use api/measures/component");
  768. }
  769. @Test
  770. void execute_whenUsingSupportedDATAMetrics_shouldReturnMetrics() {
  771. ProjectData projectData = db.components().insertPrivateProject();
  772. ComponentDto mainBranch = projectData.getMainBranchComponent();
  773. addProjectPermission(projectData);
  774. db.getDbClient().snapshotDao().insert(dbSession, newAnalysis(mainBranch));
  775. insertMetricAndLiveMeasure(mainBranch, SECURITY_ISSUES_KEY, "_data");
  776. insertMetricAndLiveMeasure(mainBranch, MAINTAINABILITY_ISSUES_KEY, "_data");
  777. insertMetricAndLiveMeasure(mainBranch, RELIABILITY_ISSUES_KEY, "_data");
  778. ComponentTreeWsResponse response = ws.newRequest()
  779. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  780. .setParam(PARAM_METRIC_KEYS, format("%s,%s,%s", SECURITY_ISSUES_KEY, MAINTAINABILITY_ISSUES_KEY, RELIABILITY_ISSUES_KEY))
  781. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
  782. .executeProtobuf(ComponentTreeWsResponse.class);
  783. assertThat(response.getMetrics().getMetricsList()).size().isEqualTo(3);
  784. List<Measure> dataMeasures = response.getBaseComponent().getMeasuresList();
  785. assertThat(dataMeasures)
  786. .extracting(Measure::getMetric, Measure::getValue)
  787. .containsExactlyInAnyOrder(tuple(SECURITY_ISSUES_KEY, SECURITY_ISSUES_KEY + "_data"),
  788. tuple(MAINTAINABILITY_ISSUES_KEY, MAINTAINABILITY_ISSUES_KEY + "_data"),
  789. tuple(RELIABILITY_ISSUES_KEY, RELIABILITY_ISSUES_KEY + "_data"));
  790. }
  791. @Test
  792. void execute_whenUsingSupportedNewDATAMetrics_shouldReturnMetrics() {
  793. ProjectData projectData = db.components().insertPrivateProject();
  794. ComponentDto mainBranch = projectData.getMainBranchComponent();
  795. addProjectPermission(projectData);
  796. db.components().insertSnapshot(mainBranch);
  797. ComponentDto file1 = db.components().insertComponent(newFileDto(mainBranch));
  798. insertMetricAndLiveMeasure(file1, NEW_SECURITY_ISSUES_KEY, "_data");
  799. insertMetricAndLiveMeasure(file1, NEW_MAINTAINABILITY_ISSUES_KEY, "_data");
  800. insertMetricAndLiveMeasure(file1, NEW_RELIABILITY_ISSUES_KEY, "_data");
  801. ComponentTreeWsResponse response = ws.newRequest()
  802. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  803. .setParam(PARAM_METRIC_KEYS, format("%s,%s,%s",
  804. NEW_SECURITY_ISSUES_KEY,
  805. NEW_MAINTAINABILITY_ISSUES_KEY,
  806. NEW_RELIABILITY_ISSUES_KEY
  807. ))
  808. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,period")
  809. .executeProtobuf(ComponentTreeWsResponse.class);
  810. assertThat(response.getComponents(0).getMeasuresCount()).isEqualTo(3);
  811. List<Measure> dataMeasures = response.getComponents(0).getMeasuresList();
  812. assertThat(dataMeasures)
  813. .extracting(Measure::getMetric, m-> m.getPeriod().getValue())
  814. .containsExactlyInAnyOrder(tuple(NEW_SECURITY_ISSUES_KEY, NEW_SECURITY_ISSUES_KEY + "_data"),
  815. tuple(NEW_MAINTAINABILITY_ISSUES_KEY, NEW_MAINTAINABILITY_ISSUES_KEY + "_data"),
  816. tuple(NEW_RELIABILITY_ISSUES_KEY, NEW_RELIABILITY_ISSUES_KEY + "_data")
  817. );
  818. }
  819. @Test
  820. void fail_when_setting_more_than_15_metric_keys() {
  821. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  822. db.components().insertSnapshot(mainBranch);
  823. List<String> metrics = IntStream.range(0, 20)
  824. .mapToObj(i -> "metric" + i)
  825. .toList();
  826. db.commit();
  827. assertThatThrownBy(() -> {
  828. ws.newRequest()
  829. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  830. .setParam(PARAM_METRIC_KEYS, Joiner.on(",").join(metrics))
  831. .execute();
  832. })
  833. .isInstanceOf(IllegalArgumentException.class)
  834. .hasMessage("'metricKeys' can contains only 15 values, got 20");
  835. }
  836. @Test
  837. void fail_when_search_query_have_less_than_3_characters() {
  838. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  839. db.components().insertSnapshot(mainBranch);
  840. insertNclocMetric();
  841. insertNewViolationsMetric();
  842. assertThatThrownBy(() -> {
  843. ws.newRequest()
  844. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  845. .setParam(PARAM_METRIC_KEYS, "ncloc, new_violations")
  846. .setParam(Param.TEXT_QUERY, "fi")
  847. .executeProtobuf(ComponentTreeWsResponse.class);
  848. })
  849. .isInstanceOf(IllegalArgumentException.class)
  850. .hasMessage("'q' length (2) is shorter than the minimum authorized (3)");
  851. }
  852. @Test
  853. void fail_when_insufficient_privileges() {
  854. userSession.logIn();
  855. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  856. db.components().insertSnapshot(mainBranch);
  857. var request = ws.newRequest()
  858. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  859. .setParam(PARAM_METRIC_KEYS, "ncloc");
  860. assertThatThrownBy(() -> request.executeProtobuf(ComponentTreeWsResponse.class))
  861. .isInstanceOf(ForbiddenException.class);
  862. }
  863. @Test
  864. void fail_when_app_with_insufficient_privileges_for_projects() {
  865. userSession.logIn();
  866. ComponentDto app = db.components().insertPrivateApplication().getMainBranchComponent();
  867. ComponentDto project1 = db.components().insertPrivateProject().getMainBranchComponent();
  868. ComponentDto project2 = db.components().insertPrivateProject().getMainBranchComponent();
  869. db.components().insertSnapshot(app);
  870. userSession.registerApplication(
  871. toProjectDto(app, 1L),
  872. toProjectDto(project1, 1L),
  873. toProjectDto(project2, 1L));
  874. userSession.addProjectPermission(USER, app, project1);
  875. var request = ws.newRequest()
  876. .setParam(PARAM_COMPONENT, app.getKey())
  877. .setParam(PARAM_METRIC_KEYS, "ncloc");
  878. assertThatThrownBy(() -> request.executeProtobuf(ComponentTreeWsResponse.class))
  879. .isInstanceOf(ForbiddenException.class);
  880. }
  881. @Test
  882. void fail_when_sort_by_metric_and_no_metric_sort_provided() {
  883. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  884. db.components().insertSnapshot(mainBranch);
  885. assertThatThrownBy(() -> {
  886. ws.newRequest()
  887. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  888. .setParam(PARAM_METRIC_KEYS, "ncloc")
  889. // PARAM_METRIC_SORT is not set
  890. .setParam(SORT, METRIC_SORT)
  891. .executeProtobuf(ComponentTreeWsResponse.class);
  892. })
  893. .isInstanceOf(BadRequestException.class)
  894. .hasMessage("To sort by a metric, the 's' parameter must contain 'metric' or 'metricPeriod', and a metric key must be provided in the 'metricSort' parameter");
  895. }
  896. @Test
  897. void fail_when_sort_by_metric_and_not_in_the_list_of_metric_keys() {
  898. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  899. db.components().insertSnapshot(mainBranch);
  900. assertThatThrownBy(() -> {
  901. ws.newRequest()
  902. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  903. .setParam(PARAM_METRIC_KEYS, "ncloc,violations")
  904. .setParam(PARAM_METRIC_SORT, "complexity")
  905. .setParam(SORT, METRIC_SORT)
  906. .executeProtobuf(ComponentTreeWsResponse.class);
  907. })
  908. .isInstanceOf(BadRequestException.class)
  909. .hasMessage("To sort by the 'complexity' metric, it must be in the list of metric keys in the 'metricKeys' parameter");
  910. }
  911. @Test
  912. void fail_when_sort_by_metric_period_and_no_metric_period_sort_provided() {
  913. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  914. db.components().insertSnapshot(mainBranch);
  915. assertThatThrownBy(() -> {
  916. ws.newRequest()
  917. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  918. .setParam(PARAM_METRIC_KEYS, "ncloc")
  919. .setParam(PARAM_METRIC_SORT, "ncloc")
  920. // PARAM_METRIC_PERIOD_SORT_IS_NOT_SET
  921. .setParam(SORT, METRIC_PERIOD_SORT)
  922. .executeProtobuf(ComponentTreeWsResponse.class);
  923. })
  924. .isInstanceOf(BadRequestException.class)
  925. .hasMessage("To sort by a metric period, the 's' parameter must contain 'metricPeriod' and the 'metricPeriodSort' must be provided.");
  926. }
  927. @Test
  928. void fail_when_paging_parameter_is_too_big() {
  929. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  930. db.components().insertSnapshot(mainBranch);
  931. insertNclocMetric();
  932. assertThatThrownBy(() -> {
  933. ws.newRequest()
  934. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  935. .setParam(PARAM_METRIC_KEYS, "ncloc")
  936. .setParam(Param.PAGE_SIZE, "2540")
  937. .execute();
  938. })
  939. .isInstanceOf(IllegalArgumentException.class)
  940. .hasMessage("'ps' value (2540) must be less than 500");
  941. }
  942. @Test
  943. void fail_when_with_measures_only_and_no_metric_sort() {
  944. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  945. db.components().insertSnapshot(mainBranch);
  946. insertNclocMetric();
  947. assertThatThrownBy(() -> {
  948. ws.newRequest()
  949. .setParam(PARAM_COMPONENT, mainBranch.getKey())
  950. .setParam(PARAM_METRIC_KEYS, "ncloc")
  951. .setParam(PARAM_METRIC_SORT_FILTER, WITH_MEASURES_ONLY_METRIC_SORT_FILTER)
  952. .executeProtobuf(ComponentTreeWsResponse.class);
  953. })
  954. .isInstanceOf(BadRequestException.class)
  955. .hasMessage("To filter components based on the sort metric, the 's' parameter must contain 'metric' or 'metricPeriod' and the 'metricSort' parameter must be provided");
  956. }
  957. @Test
  958. void fail_when_component_does_not_exist() {
  959. insertNclocMetric();
  960. assertThatThrownBy(() -> {
  961. ws.newRequest()
  962. .setParam(PARAM_COMPONENT, "project-key")
  963. .setParam(PARAM_METRIC_KEYS, "ncloc")
  964. .execute();
  965. })
  966. .isInstanceOf(NotFoundException.class)
  967. .hasMessage("Component key 'project-key' not found");
  968. }
  969. @Test
  970. void fail_when_component_is_removed() {
  971. ComponentDto mainBranch = db.components().insertPrivateProject().getMainBranchComponent();
  972. db.components().insertSnapshot(mainBranch);
  973. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch).setKey("file-key").setEnabled(false));
  974. userSession.anonymous().addProjectPermission(USER, mainBranch);
  975. insertNclocMetric();
  976. assertThatThrownBy(() -> {
  977. ws.newRequest()
  978. .setParam(PARAM_COMPONENT, file.getKey())
  979. .setParam(PARAM_METRIC_KEYS, "ncloc")
  980. .execute();
  981. })
  982. .isInstanceOf(NotFoundException.class)
  983. .hasMessage(format("Component key '%s' not found", file.getKey()));
  984. }
  985. @Test
  986. void fail_if_branch_does_not_exist() {
  987. ProjectData projectData = db.components().insertPrivateProject();
  988. ComponentDto mainBranch = projectData.getMainBranchComponent();
  989. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
  990. addProjectPermission(projectData);
  991. db.components().insertProjectBranch(mainBranch, b -> b.setKey("my_branch"));
  992. assertThatThrownBy(() -> {
  993. ws.newRequest()
  994. .setParam(PARAM_COMPONENT, file.getKey())
  995. .setParam(PARAM_BRANCH, "another_branch")
  996. .setParam(PARAM_METRIC_KEYS, "ncloc")
  997. .execute();
  998. })
  999. .isInstanceOf(NotFoundException.class)
  1000. .hasMessage(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
  1001. }
  1002. @Test
  1003. void doHandle_whenPassingUnexpectedQualifier_shouldThrowException() {
  1004. ProjectData projectData = db.components().insertPrivateProject();
  1005. ComponentDto mainBranch = projectData.getMainBranchComponent();
  1006. addProjectPermission(projectData);
  1007. ComponentDto file = db.components().insertComponent(newFileDto(mainBranch));
  1008. MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
  1009. TestRequest testRequest = ws.newRequest()
  1010. .setParam(PARAM_COMPONENT, file.getKey())
  1011. .setParam(PARAM_BRANCH, BranchDto.DEFAULT_MAIN_BRANCH_NAME)
  1012. .setParam(PARAM_QUALIFIERS, "BRC")
  1013. .setParam(PARAM_METRIC_KEYS, complexity.getKey());
  1014. assertThatThrownBy(() -> testRequest.execute()).isInstanceOf(IllegalArgumentException.class)
  1015. .hasMessage("Value of parameter 'qualifiers' (BRC) must be one of: [UTS, FIL, DIR, TRK]");
  1016. }
  1017. private static MetricDto newMetricDto() {
  1018. return MetricTesting.newMetricDto()
  1019. .setWorstValue(null)
  1020. .setBestValue(null)
  1021. .setOptimizedBestValue(false);
  1022. }
  1023. private void insertMetricAndLiveMeasure(ComponentDto dto, String key, String additionalData) {
  1024. MetricDto dataMetric = dbClient.metricDao().insert(dbSession, newDataMetricDto(key));
  1025. db.measures().insertLiveMeasure(dto, dataMetric, c -> c.setData(key + additionalData));
  1026. }
  1027. private static MetricDto newDataMetricDto(String key) {
  1028. return newMetricDto().setValueType(DATA.name()).setKey(key);
  1029. }
  1030. private MetricDto insertNewViolationsMetric() {
  1031. MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto()
  1032. .setKey("new_violations")
  1033. .setShortName("New issues")
  1034. .setDescription("New Issues")
  1035. .setDomain("Issues")
  1036. .setValueType("INT")
  1037. .setDirection(-1)
  1038. .setQualitative(true)
  1039. .setHidden(false)
  1040. .setOptimizedBestValue(true)
  1041. .setBestValue(0.0d));
  1042. db.commit();
  1043. return metric;
  1044. }
  1045. private MetricDto insertAcceptedIssuesMetric() {
  1046. MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto()
  1047. .setKey("accepted_issues")
  1048. .setShortName("Accepted Issues")
  1049. .setDescription("Accepted issues")
  1050. .setDomain("Issues")
  1051. .setValueType(INT.name())
  1052. .setDirection(-1)
  1053. .setQualitative(false)
  1054. .setHidden(false));
  1055. db.commit();
  1056. return metric;
  1057. }
  1058. private MetricDto insertNclocMetric() {
  1059. MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto()
  1060. .setKey("ncloc")
  1061. .setShortName("Lines of code")
  1062. .setDescription("Non Commenting Lines of Code")
  1063. .setDomain("Size")
  1064. .setValueType(INT.name())
  1065. .setDirection(-1)
  1066. .setQualitative(false)
  1067. .setHidden(false));
  1068. db.commit();
  1069. return metric;
  1070. }
  1071. private MetricDto insertComplexityMetric() {
  1072. MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto()
  1073. .setKey("complexity")
  1074. .setShortName("Complexity")
  1075. .setDescription("Cyclomatic complexity")
  1076. .setDomain("Complexity")
  1077. .setValueType(INT.name())
  1078. .setDirection(-1)
  1079. .setQualitative(false)
  1080. .setHidden(false));
  1081. db.commit();
  1082. return metric;
  1083. }
  1084. private MetricDto insertCoverageMetric() {
  1085. MetricDto metric = dbClient.metricDao().insert(dbSession, newMetricDto()
  1086. .setKey("coverage")
  1087. .setShortName("Coverage")
  1088. .setDescription("Code Coverage")
  1089. .setDomain("Coverage")
  1090. .setValueType(FLOAT.name())
  1091. .setDirection(1)
  1092. .setQualitative(false)
  1093. .setHidden(false));
  1094. db.commit();
  1095. return metric;
  1096. }
  1097. }