選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ComponentActionTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 java.util.function.Function;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.api.server.ws.WebService.Param;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.api.web.UserRole;
  29. import org.sonar.db.DbTester;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.component.SnapshotDto;
  32. import org.sonar.db.measure.LiveMeasureDto;
  33. import org.sonar.db.metric.MetricDto;
  34. import org.sonar.db.organization.OrganizationDto;
  35. import org.sonar.server.component.TestComponentFinder;
  36. import org.sonar.server.exceptions.BadRequestException;
  37. import org.sonar.server.exceptions.ForbiddenException;
  38. import org.sonar.server.exceptions.NotFoundException;
  39. import org.sonar.server.tester.UserSessionRule;
  40. import org.sonar.server.ws.WsActionTester;
  41. import org.sonarqube.ws.Common;
  42. import org.sonarqube.ws.Measures;
  43. import org.sonarqube.ws.Measures.Component;
  44. import org.sonarqube.ws.Measures.ComponentWsResponse;
  45. import static java.lang.Double.parseDouble;
  46. import static java.lang.String.format;
  47. import static org.assertj.core.api.Assertions.assertThat;
  48. import static org.assertj.core.api.Assertions.tuple;
  49. import static org.sonar.api.utils.DateUtils.parseDateTime;
  50. import static org.sonar.api.web.UserRole.USER;
  51. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  52. import static org.sonar.db.component.ComponentTesting.newFileDto;
  53. import static org.sonar.db.component.ComponentTesting.newProjectCopy;
  54. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_ADDITIONAL_FIELDS;
  55. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_BRANCH;
  56. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_COMPONENT;
  57. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_KEYS;
  58. import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_PULL_REQUEST;
  59. import static org.sonar.test.JsonAssert.assertJson;
  60. public class ComponentActionTest {
  61. @Rule
  62. public UserSessionRule userSession = UserSessionRule.standalone();
  63. @Rule
  64. public ExpectedException expectedException = ExpectedException.none();
  65. @Rule
  66. public DbTester db = DbTester.create(System2.INSTANCE);
  67. private WsActionTester ws = new WsActionTester(new ComponentAction(db.getDbClient(), TestComponentFinder.from(db), userSession));
  68. @Test
  69. public void definition() {
  70. WebService.Action def = ws.getDef();
  71. assertThat(def.since()).isEqualTo("5.4");
  72. assertThat(def.params()).extracting(Param::key)
  73. .containsExactlyInAnyOrder("component", "branch", "pullRequest", "metricKeys", "additionalFields");
  74. WebService.Param component = def.param(PARAM_COMPONENT);
  75. assertThat(component.isRequired()).isTrue();
  76. WebService.Param branch = def.param("branch");
  77. assertThat(branch.since()).isEqualTo("6.6");
  78. assertThat(branch.isInternal()).isTrue();
  79. assertThat(branch.isRequired()).isFalse();
  80. }
  81. @Test
  82. public void provided_project() {
  83. ComponentDto project = db.components().insertPrivateProject();
  84. userSession.addProjectPermission(UserRole.USER, project);
  85. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  86. ComponentWsResponse response = newRequest(project.getKey(), metric.getKey());
  87. assertThat(response.getMetrics().getMetricsCount()).isEqualTo(1);
  88. assertThat(response.getPeriods().getPeriodsCount()).isEqualTo(0);
  89. assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey());
  90. }
  91. @Test
  92. public void without_additional_fields() {
  93. ComponentDto project = db.components().insertPrivateProject();
  94. userSession.addProjectPermission(UserRole.USER, project);
  95. db.components().insertSnapshot(project);
  96. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  97. String response = ws.newRequest()
  98. .setParam(PARAM_COMPONENT, project.getKey())
  99. .setParam(PARAM_METRIC_KEYS, metric.getKey())
  100. .execute().getInput();
  101. assertThat(response)
  102. .doesNotContain("periods")
  103. .doesNotContain("metrics");
  104. }
  105. @Test
  106. public void branch() {
  107. ComponentDto project = db.components().insertPrivateProject();
  108. userSession.addProjectPermission(UserRole.USER, project);
  109. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
  110. SnapshotDto analysis = db.components().insertSnapshot(branch);
  111. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  112. MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
  113. LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d).setVariation(2.0d));
  114. ComponentWsResponse response = ws.newRequest()
  115. .setParam(PARAM_COMPONENT, file.getKey())
  116. .setParam(PARAM_BRANCH, file.getBranch())
  117. .setParam(PARAM_METRIC_KEYS, complexity.getKey())
  118. .executeProtobuf(ComponentWsResponse.class);
  119. assertThat(response.getComponent()).extracting(Component::getKey, Component::getBranch)
  120. .containsExactlyInAnyOrder(file.getKey(), file.getBranch());
  121. assertThat(response.getComponent().getMeasuresList())
  122. .extracting(Measures.Measure::getMetric, m -> parseDouble(m.getValue()))
  123. .containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue()));
  124. }
  125. @Test
  126. public void pull_request() {
  127. ComponentDto project = db.components().insertPrivateProject();
  128. userSession.addProjectPermission(UserRole.USER, project);
  129. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
  130. SnapshotDto analysis = db.components().insertSnapshot(branch);
  131. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  132. MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
  133. LiveMeasureDto measure = db.measures().insertLiveMeasure(file, complexity, m -> m.setValue(12.0d).setVariation(2.0d));
  134. ComponentWsResponse response = ws.newRequest()
  135. .setParam(PARAM_COMPONENT, file.getKey())
  136. .setParam(PARAM_PULL_REQUEST, "pr-123")
  137. .setParam(PARAM_METRIC_KEYS, complexity.getKey())
  138. .executeProtobuf(ComponentWsResponse.class);
  139. assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest)
  140. .containsExactlyInAnyOrder(file.getKey(), "pr-123");
  141. assertThat(response.getComponent().getMeasuresList())
  142. .extracting(Measures.Measure::getMetric, m -> parseDouble(m.getValue()))
  143. .containsExactlyInAnyOrder(tuple(complexity.getKey(), measure.getValue()));
  144. }
  145. @Test
  146. public void new_issue_count_measures_are_transformed_in_pr() {
  147. ComponentDto project = db.components().insertPrivateProject();
  148. userSession.addProjectPermission(UserRole.USER, project);
  149. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
  150. SnapshotDto analysis = db.components().insertSnapshot(branch);
  151. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  152. MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setValueType("INT"));
  153. MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setValueType("INT"));
  154. MetricDto violations = db.measures().insertMetric(m1 -> m1.setKey("violations").setValueType("INT"));
  155. MetricDto newViolations = db.measures().insertMetric(m1 -> m1.setKey("new_violations").setValueType("INT"));
  156. LiveMeasureDto bugMeasure = db.measures().insertLiveMeasure(file, bugs, m -> m.setValue(12.0d).setVariation(null));
  157. LiveMeasureDto newBugMeasure = db.measures().insertLiveMeasure(file, newBugs, m -> m.setVariation(1d).setValue(null));
  158. LiveMeasureDto violationMeasure = db.measures().insertLiveMeasure(file, violations, m -> m.setValue(20.0d).setVariation(null));
  159. ComponentWsResponse response = ws.newRequest()
  160. .setParam(PARAM_COMPONENT, file.getKey())
  161. .setParam(PARAM_PULL_REQUEST, "pr-123")
  162. .setParam(PARAM_METRIC_KEYS, newBugs.getKey() + "," + bugs.getKey() + "," + newViolations.getKey())
  163. .executeProtobuf(ComponentWsResponse.class);
  164. assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest)
  165. .containsExactlyInAnyOrder(file.getKey(), "pr-123");
  166. Function<Measures.Measure, Double> extractVariation = m -> {
  167. if (m.getPeriods().getPeriodsValueCount() > 0) {
  168. return parseDouble(m.getPeriods().getPeriodsValue(0).getValue());
  169. }
  170. return null;
  171. };
  172. assertThat(response.getComponent().getMeasuresList())
  173. .extracting(Measures.Measure::getMetric, extractVariation, m -> m.getValue().isEmpty() ? null : parseDouble(m.getValue()))
  174. .containsExactlyInAnyOrder(
  175. tuple(newBugs.getKey(), bugMeasure.getValue(), null),
  176. tuple(bugs.getKey(), null, bugMeasure.getValue()),
  177. tuple(newViolations.getKey(), violationMeasure.getValue(), null));
  178. }
  179. @Test
  180. public void new_issue_count_measures_are_not_transformed_if_they_dont_exist_in_pr() {
  181. ComponentDto project = db.components().insertPrivateProject();
  182. userSession.addProjectPermission(UserRole.USER, project);
  183. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
  184. SnapshotDto analysis = db.components().insertSnapshot(branch);
  185. ComponentDto file = db.components().insertComponent(newFileDto(branch));
  186. MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setOptimizedBestValue(false).setValueType("INT"));
  187. MetricDto newBugs = db.measures().insertMetric(m1 -> m1.setKey("new_bugs").setOptimizedBestValue(false).setValueType("INT"));
  188. ComponentWsResponse response = ws.newRequest()
  189. .setParam(PARAM_COMPONENT, file.getKey())
  190. .setParam(PARAM_PULL_REQUEST, "pr-123")
  191. .setParam(PARAM_METRIC_KEYS, newBugs.getKey() + "," + bugs.getKey())
  192. .executeProtobuf(ComponentWsResponse.class);
  193. assertThat(response.getComponent()).extracting(Component::getKey, Component::getPullRequest)
  194. .containsExactlyInAnyOrder(file.getKey(), "pr-123");
  195. assertThat(response.getComponent().getMeasuresList()).isEmpty();
  196. }
  197. @Test
  198. public void reference_uuid_in_the_response() {
  199. userSession.logIn().setRoot();
  200. ComponentDto project = db.components().insertPrivateProject();
  201. ComponentDto view = db.components().insertView();
  202. db.components().insertSnapshot(view);
  203. ComponentDto projectCopy = db.components().insertComponent(newProjectCopy("project-uuid-copy", project, view));
  204. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  205. ComponentWsResponse response = newRequest(projectCopy.getKey(), metric.getKey());
  206. assertThat(response.getComponent().getRefId()).isEqualTo(project.uuid());
  207. assertThat(response.getComponent().getRefKey()).isEqualTo(project.getKey());
  208. }
  209. @Test
  210. public void return_deprecated_id_in_the_response() {
  211. ComponentDto project = db.components().insertPrivateProject();
  212. userSession.addProjectPermission(UserRole.USER, project);
  213. db.components().insertSnapshot(project);
  214. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  215. ComponentWsResponse response = newRequest(project.getKey(), metric.getKey());
  216. assertThat(response.getComponent().getId()).isEqualTo(project.uuid());
  217. }
  218. @Test
  219. public void use_deprecated_component_id_parameter() {
  220. ComponentDto project = db.components().insertPrivateProject();
  221. userSession.addProjectPermission(UserRole.USER, project);
  222. userSession.addProjectPermission(USER, project);
  223. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  224. ComponentWsResponse response = ws.newRequest()
  225. .setParam("component", project.getDbKey())
  226. .setParam(PARAM_METRIC_KEYS, metric.getKey())
  227. .executeProtobuf(ComponentWsResponse.class);
  228. assertThat(response.getComponent().getKey()).isEqualTo(project.getDbKey());
  229. }
  230. @Test
  231. public void metric_without_a_domain() {
  232. ComponentDto project = db.components().insertPrivateProject();
  233. userSession.addProjectPermission(UserRole.USER, project);
  234. MetricDto metricWithoutDomain = db.measures().insertMetric(m -> m
  235. .setValueType("INT")
  236. .setDomain(null));
  237. db.measures().insertLiveMeasure(project, metricWithoutDomain);
  238. ComponentWsResponse response = ws.newRequest()
  239. .setParam(PARAM_COMPONENT, project.getKey())
  240. .setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
  241. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
  242. .executeProtobuf(ComponentWsResponse.class);
  243. assertThat(response.getComponent().getMeasuresList()).extracting(Measures.Measure::getMetric).containsExactly(metricWithoutDomain.getKey());
  244. Common.Metric responseMetric = response.getMetrics().getMetrics(0);
  245. assertThat(responseMetric.getKey()).isEqualTo(metricWithoutDomain.getKey());
  246. assertThat(responseMetric.hasDomain()).isFalse();
  247. }
  248. @Test
  249. public void use_best_values() {
  250. ComponentDto project = db.components().insertPrivateProject();
  251. ComponentDto file = db.components().insertComponent(newFileDto(project));
  252. userSession.addProjectPermission(UserRole.USER, project);
  253. MetricDto metric = db.measures().insertMetric(m -> m
  254. .setValueType("INT")
  255. .setBestValue(7.0d)
  256. .setOptimizedBestValue(true)
  257. .setDomain(null));
  258. ComponentWsResponse response = ws.newRequest()
  259. .setParam(PARAM_COMPONENT, file.getKey())
  260. .setParam(PARAM_METRIC_KEYS, metric.getKey())
  261. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
  262. .executeProtobuf(ComponentWsResponse.class);
  263. assertThat(response.getComponent().getMeasuresList())
  264. .extracting(Measures.Measure::getMetric, Measures.Measure::getValue, Measures.Measure::getBestValue)
  265. .containsExactly(tuple(metric.getKey(), "7", true));
  266. }
  267. @Test
  268. public void fail_when_a_metric_is_not_found() {
  269. ComponentDto project = db.components().insertPrivateProject();
  270. userSession.addProjectPermission(UserRole.USER, project);
  271. db.components().insertSnapshot(project);
  272. db.measures().insertMetric(m -> m.setKey("ncloc").setValueType("INT"));
  273. db.measures().insertMetric(m -> m.setKey("complexity").setValueType("INT"));
  274. expectedException.expect(NotFoundException.class);
  275. expectedException.expectMessage("The following metric keys are not found: unknown-metric, another-unknown-metric");
  276. newRequest(project.getKey(), "ncloc, complexity, unknown-metric, another-unknown-metric");
  277. }
  278. @Test
  279. public void fail_when_empty_metric_keys_parameter() {
  280. ComponentDto project = db.components().insertPrivateProject();
  281. userSession.addProjectPermission(UserRole.USER, project);
  282. db.components().insertSnapshot(project);
  283. expectedException.expect(BadRequestException.class);
  284. expectedException.expectMessage("At least one metric key must be provided");
  285. newRequest(project.getKey(), "");
  286. }
  287. @Test
  288. public void fail_when_not_enough_permission() {
  289. userSession.logIn();
  290. ComponentDto project = db.components().insertPrivateProject();
  291. db.components().insertSnapshot(project);
  292. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  293. expectedException.expect(ForbiddenException.class);
  294. newRequest(project.getKey(), metric.getKey());
  295. }
  296. @Test
  297. public void fail_when_component_does_not_exist() {
  298. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  299. expectedException.expect(NotFoundException.class);
  300. expectedException.expectMessage("Component key 'project-key' not found");
  301. ws.newRequest()
  302. .setParam(PARAM_COMPONENT, "project-key")
  303. .setParam(PARAM_METRIC_KEYS, metric.getKey())
  304. .execute();
  305. }
  306. @Test
  307. public void fail_when_component_is_removed() {
  308. ComponentDto project = db.components().insertPrivateProject(p -> p.setEnabled(false));
  309. userSession.addProjectPermission(UserRole.USER, project);
  310. userSession.addProjectPermission(USER, project);
  311. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  312. expectedException.expect(NotFoundException.class);
  313. expectedException.expectMessage(String.format("Component key '%s' not found", project.getKey()));
  314. ws.newRequest()
  315. .setParam(PARAM_COMPONENT, project.getKey())
  316. .setParam(PARAM_METRIC_KEYS, metric.getKey())
  317. .execute();
  318. }
  319. @Test
  320. public void fail_if_branch_does_not_exist() {
  321. ComponentDto project = db.components().insertPrivateProject();
  322. ComponentDto file = db.components().insertComponent(newFileDto(project));
  323. userSession.addProjectPermission(UserRole.USER, project);
  324. db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
  325. expectedException.expect(NotFoundException.class);
  326. expectedException.expectMessage(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
  327. ws.newRequest()
  328. .setParam(PARAM_COMPONENT, file.getKey())
  329. .setParam(PARAM_BRANCH, "another_branch")
  330. .setParam(PARAM_METRIC_KEYS, "ncloc")
  331. .execute();
  332. }
  333. @Test
  334. public void fail_when_using_branch_db_key() throws Exception {
  335. OrganizationDto organization = db.organizations().insert();
  336. ComponentDto project = db.components().insertMainBranch(organization);
  337. userSession.logIn().addProjectPermission(UserRole.USER, project);
  338. ComponentDto branch = db.components().insertProjectBranch(project);
  339. MetricDto metric = db.measures().insertMetric(m -> m.setValueType("INT"));
  340. expectedException.expect(NotFoundException.class);
  341. expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
  342. ws.newRequest()
  343. .setParam(PARAM_COMPONENT, branch.getDbKey())
  344. .setParam(PARAM_METRIC_KEYS, metric.getKey())
  345. .execute();
  346. }
  347. @Test
  348. public void json_example() {
  349. ComponentDto project = db.components().insertPrivateProject();
  350. userSession.addProjectPermission(UserRole.USER, project);
  351. SnapshotDto analysis = db.components().insertSnapshot(project,
  352. s -> s.setPeriodDate(parseDateTime("2016-01-11T10:49:50+0100").getTime())
  353. .setPeriodMode("previous_version")
  354. .setPeriodParam("1.0-SNAPSHOT"));
  355. ComponentDto file = db.components().insertComponent(newFileDto(project)
  356. .setDbKey("MY_PROJECT:ElementImpl.java")
  357. .setName("ElementImpl.java")
  358. .setLanguage("java")
  359. .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java"));
  360. MetricDto complexity = db.measures().insertMetric(m -> m.setKey("complexity")
  361. .setShortName("Complexity")
  362. .setDescription("Cyclomatic complexity")
  363. .setDomain("Complexity")
  364. .setValueType("INT")
  365. .setDirection(-1)
  366. .setQualitative(false)
  367. .setHidden(false)
  368. .setUserManaged(false));
  369. db.measures().insertLiveMeasure(file, complexity,
  370. m -> m.setValue(12.0d)
  371. .setVariation(2.0d)
  372. .setData((String) null));
  373. MetricDto ncloc = db.measures().insertMetric(m1 -> m1.setKey("ncloc")
  374. .setShortName("Lines of code")
  375. .setDescription("Non Commenting Lines of Code")
  376. .setDomain("Size")
  377. .setValueType("INT")
  378. .setDirection(-1)
  379. .setQualitative(false)
  380. .setHidden(false)
  381. .setUserManaged(false));
  382. db.measures().insertLiveMeasure(file, ncloc,
  383. m -> m.setValue(114.0d)
  384. .setVariation(3.0d)
  385. .setData((String) null));
  386. MetricDto newViolations = db.measures().insertMetric(m -> m.setKey("new_violations")
  387. .setShortName("New issues")
  388. .setDescription("New Issues")
  389. .setDomain("Issues")
  390. .setValueType("INT")
  391. .setDirection(-1)
  392. .setQualitative(true)
  393. .setHidden(false)
  394. .setUserManaged(false));
  395. db.measures().insertLiveMeasure(file, newViolations,
  396. m -> m.setVariation(25.0d)
  397. .setValue(null)
  398. .setData((String) null));
  399. String response = ws.newRequest()
  400. .setParam(PARAM_COMPONENT, file.getKey())
  401. .setParam(PARAM_METRIC_KEYS, "ncloc, complexity, new_violations")
  402. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,periods")
  403. .execute()
  404. .getInput();
  405. assertJson(response).isSimilarTo(getClass().getResource("component-example.json"));
  406. }
  407. private ComponentWsResponse newRequest(String componentKey, String metricKeys) {
  408. return ws.newRequest()
  409. .setParam(PARAM_COMPONENT, componentKey)
  410. .setParam(PARAM_METRIC_KEYS, metricKeys)
  411. .setParam(PARAM_ADDITIONAL_FIELDS, "metrics,periods")
  412. .executeProtobuf(ComponentWsResponse.class);
  413. }
  414. }