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.

PersistLiveMeasuresStepTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.step;
  21. import java.util.Optional;
  22. import org.junit.Before;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.sonar.api.measures.Metric;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.ce.task.projectanalysis.analysis.MutableAnalysisMetadataHolderRule;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.ReportComponent;
  30. import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
  31. import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
  32. import org.sonar.ce.task.projectanalysis.measure.MeasureRepositoryRule;
  33. import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
  34. import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule;
  35. import org.sonar.ce.task.step.ComputationStep;
  36. import org.sonar.ce.task.step.TestComputationStepContext;
  37. import org.sonar.db.DbClient;
  38. import org.sonar.db.DbTester;
  39. import org.sonar.db.component.ComponentDto;
  40. import org.sonar.db.measure.LiveMeasureDto;
  41. import org.sonar.db.metric.MetricDto;
  42. import org.sonar.server.project.Project;
  43. import static java.util.Collections.emptyList;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
  46. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  47. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  48. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
  49. import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
  50. import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
  51. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  52. import static org.sonar.db.measure.MeasureTesting.newLiveMeasure;
  53. public class PersistLiveMeasuresStepTest extends BaseStepTest {
  54. private static final Metric STRING_METRIC = new Metric.Builder("string-metric", "String metric", Metric.ValueType.STRING).create();
  55. private static final Metric INT_METRIC = new Metric.Builder("int-metric", "int metric", Metric.ValueType.INT).create();
  56. private static final Metric METRIC_WITH_BEST_VALUE = new Metric.Builder("best-value-metric", "best value metric", Metric.ValueType.INT)
  57. .setBestValue(0.0)
  58. .setOptimizedBestValue(true)
  59. .create();
  60. private static final int REF_1 = 1;
  61. private static final int REF_2 = 2;
  62. private static final int REF_3 = 3;
  63. private static final int REF_4 = 4;
  64. @Rule
  65. public DbTester db = DbTester.create(System2.INSTANCE);
  66. @Rule
  67. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  68. @Rule
  69. public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
  70. @Rule
  71. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  72. @Rule
  73. public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
  74. private DbClient dbClient = db.getDbClient();
  75. @Before
  76. public void setUp() {
  77. MetricDto stringMetricDto = db.measures().insertMetric(m -> m.setKey(STRING_METRIC.getKey()).setValueType(Metric.ValueType.STRING.name()));
  78. MetricDto intMetricDto = db.measures().insertMetric(m -> m.setKey(INT_METRIC.getKey()).setValueType(Metric.ValueType.INT.name()));
  79. MetricDto bestValueMMetricDto = db.measures()
  80. .insertMetric(m -> m.setKey(METRIC_WITH_BEST_VALUE.getKey()).setValueType(Metric.ValueType.INT.name()).setOptimizedBestValue(true).setBestValue(0.0));
  81. metricRepository.add(stringMetricDto.getUuid(), STRING_METRIC);
  82. metricRepository.add(intMetricDto.getUuid(), INT_METRIC);
  83. metricRepository.add(bestValueMMetricDto.getUuid(), METRIC_WITH_BEST_VALUE);
  84. }
  85. @Test
  86. public void persist_live_measures_of_project_analysis() {
  87. prepareProject();
  88. // the computed measures
  89. measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().create("project-value"));
  90. measureRepository.addRawMeasure(REF_3, STRING_METRIC.getKey(), newMeasureBuilder().create("dir-value"));
  91. measureRepository.addRawMeasure(REF_4, STRING_METRIC.getKey(), newMeasureBuilder().create("file-value"));
  92. TestComputationStepContext context = new TestComputationStepContext();
  93. step().execute(context);
  94. // all measures are persisted, from project to file
  95. assertThat(db.countRowsOfTable("live_measures")).isEqualTo(3);
  96. assertThat(selectMeasure("project-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("project-value");
  97. assertThat(selectMeasure("dir-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("dir-value");
  98. assertThat(selectMeasure("file-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("file-value");
  99. verifyStatistics(context, 3);
  100. }
  101. @Test
  102. public void measures_without_value_are_not_persisted() {
  103. prepareProject();
  104. measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().createNoValue());
  105. measureRepository.addRawMeasure(REF_1, INT_METRIC.getKey(), newMeasureBuilder().createNoValue());
  106. TestComputationStepContext context = new TestComputationStepContext();
  107. step().execute(context);
  108. assertThatMeasureIsNotPersisted("project-uuid", STRING_METRIC);
  109. assertThatMeasureIsNotPersisted("project-uuid", INT_METRIC);
  110. verifyStatistics(context, 0);
  111. }
  112. @Test
  113. public void measures_on_new_code_period_are_persisted() {
  114. prepareProject();
  115. measureRepository.addRawMeasure(REF_1, INT_METRIC.getKey(), newMeasureBuilder().setVariation(42.0).createNoValue());
  116. TestComputationStepContext context = new TestComputationStepContext();
  117. step().execute(context);
  118. LiveMeasureDto persistedMeasure = selectMeasure("project-uuid", INT_METRIC).get();
  119. assertThat(persistedMeasure.getValue()).isNull();
  120. assertThat(persistedMeasure.getVariation()).isEqualTo(42.0);
  121. verifyStatistics(context, 1);
  122. }
  123. @Test
  124. public void delete_measures_from_db_if_no_longer_computed() {
  125. prepareProject();
  126. // measure to be updated
  127. LiveMeasureDto measureOnFileInProject = insertMeasure("file-uuid", "project-uuid", INT_METRIC);
  128. // measure to be deleted because not computed anymore
  129. LiveMeasureDto otherMeasureOnFileInProject = insertMeasure("file-uuid", "project-uuid", STRING_METRIC);
  130. // measure in another project, not touched
  131. LiveMeasureDto measureInOtherProject = insertMeasure("other-file-uuid", "other-project-uuid", INT_METRIC);
  132. db.commit();
  133. measureRepository.addRawMeasure(REF_4, INT_METRIC.getKey(), newMeasureBuilder().create(42));
  134. TestComputationStepContext context = new TestComputationStepContext();
  135. step().execute(context);
  136. assertThatMeasureHasValue(measureOnFileInProject, 42);
  137. assertThatMeasureDoesNotExist(otherMeasureOnFileInProject);
  138. assertThatMeasureHasValue(measureInOtherProject, (int) measureInOtherProject.getValue().doubleValue());
  139. verifyStatistics(context, 1);
  140. }
  141. @Test
  142. public void do_not_persist_file_measures_with_best_value() {
  143. prepareProject();
  144. // measure to be deleted because new value matches the metric best value
  145. LiveMeasureDto oldMeasure = insertMeasure("file-uuid", "project-uuid", INT_METRIC);
  146. db.commit();
  147. // project measure with metric best value -> persist with value 0
  148. measureRepository.addRawMeasure(REF_1, METRIC_WITH_BEST_VALUE.getKey(), newMeasureBuilder().create(0));
  149. // file measure with metric best value -> do not persist
  150. measureRepository.addRawMeasure(REF_4, METRIC_WITH_BEST_VALUE.getKey(), newMeasureBuilder().create(0));
  151. TestComputationStepContext context = new TestComputationStepContext();
  152. step().execute(context);
  153. assertThatMeasureDoesNotExist(oldMeasure);
  154. assertThatMeasureHasValue("project-uuid", METRIC_WITH_BEST_VALUE, 0);
  155. verifyStatistics(context, 1);
  156. }
  157. @Test
  158. public void persist_live_measures_of_portfolio_analysis() {
  159. preparePortfolio();
  160. // the computed measures
  161. measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().create("view-value"));
  162. measureRepository.addRawMeasure(REF_2, STRING_METRIC.getKey(), newMeasureBuilder().create("subview-value"));
  163. measureRepository.addRawMeasure(REF_3, STRING_METRIC.getKey(), newMeasureBuilder().create("project-value"));
  164. TestComputationStepContext context = new TestComputationStepContext();
  165. step().execute(context);
  166. assertThat(db.countRowsOfTable("live_measures")).isEqualTo(3);
  167. assertThat(selectMeasure("view-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("view-value");
  168. assertThat(selectMeasure("subview-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("subview-value");
  169. assertThat(selectMeasure("project-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("project-value");
  170. verifyStatistics(context, 3);
  171. }
  172. private LiveMeasureDto insertMeasure(String componentUuid, String projectUuid, Metric metric) {
  173. LiveMeasureDto measure = newLiveMeasure()
  174. .setComponentUuid(componentUuid)
  175. .setProjectUuid(projectUuid)
  176. .setMetricUuid(metricRepository.getByKey(metric.getKey()).getUuid());
  177. dbClient.liveMeasureDao().insertOrUpdate(db.getSession(), measure);
  178. return measure;
  179. }
  180. private void assertThatMeasureHasValue(LiveMeasureDto template, int expectedValue) {
  181. Optional<LiveMeasureDto> persisted = dbClient.liveMeasureDao().selectMeasure(db.getSession(),
  182. template.getComponentUuid(), metricRepository.getByUuid(template.getMetricUuid()).getKey());
  183. assertThat(persisted).isPresent();
  184. assertThat(persisted.get().getValue()).isEqualTo(expectedValue);
  185. }
  186. private void assertThatMeasureHasValue(String componentUuid, Metric metric, int expectedValue) {
  187. Optional<LiveMeasureDto> persisted = dbClient.liveMeasureDao().selectMeasure(db.getSession(),
  188. componentUuid, metric.getKey());
  189. assertThat(persisted).isPresent();
  190. assertThat(persisted.get().getValue()).isEqualTo(expectedValue);
  191. }
  192. private void assertThatMeasureDoesNotExist(LiveMeasureDto template) {
  193. assertThat(dbClient.liveMeasureDao().selectMeasure(db.getSession(),
  194. template.getComponentUuid(), metricRepository.getByUuid(template.getMetricUuid()).getKey()))
  195. .isEmpty();
  196. }
  197. private void prepareProject() {
  198. // tree of components as defined by scanner report
  199. Component project = ReportComponent.builder(PROJECT, REF_1).setUuid("project-uuid")
  200. .addChildren(
  201. ReportComponent.builder(DIRECTORY, REF_3).setUuid("dir-uuid")
  202. .addChildren(
  203. ReportComponent.builder(FILE, REF_4).setUuid("file-uuid")
  204. .build())
  205. .build())
  206. .build();
  207. treeRootHolder.setRoot(project);
  208. analysisMetadataHolder.setProject(new Project(project.getUuid(), project.getDbKey(), project.getName(), project.getDescription(), emptyList()));
  209. // components as persisted in db
  210. ComponentDto projectDto = insertComponent("project-key", "project-uuid");
  211. ComponentDto dirDto = insertComponent("dir-key", "dir-uuid");
  212. ComponentDto fileDto = insertComponent("file-key", "file-uuid");
  213. }
  214. private void preparePortfolio() {
  215. // tree of components
  216. Component portfolio = ViewsComponent.builder(VIEW, REF_1).setUuid("view-uuid")
  217. .addChildren(
  218. ViewsComponent.builder(SUBVIEW, REF_2).setUuid("subview-uuid")
  219. .addChildren(
  220. ViewsComponent.builder(PROJECT_VIEW, REF_3).setUuid("project-uuid")
  221. .build())
  222. .build())
  223. .build();
  224. treeRootHolder.setRoot(portfolio);
  225. // components as persisted in db
  226. ComponentDto portfolioDto = insertComponent("view-key", "view-uuid");
  227. ComponentDto subViewDto = insertComponent("subview-key", "subview-uuid");
  228. ComponentDto projectDto = insertComponent("project-key", "project-uuid");
  229. analysisMetadataHolder.setProject(Project.from(portfolioDto));
  230. }
  231. private void assertThatMeasureIsNotPersisted(String componentUuid, Metric metric) {
  232. assertThat(selectMeasure(componentUuid, metric)).isEmpty();
  233. }
  234. private Optional<LiveMeasureDto> selectMeasure(String componentUuid, Metric metric) {
  235. return dbClient.liveMeasureDao().selectMeasure(db.getSession(), componentUuid, metric.getKey());
  236. }
  237. private ComponentDto insertComponent(String key, String uuid) {
  238. ComponentDto componentDto = new ComponentDto()
  239. .setDbKey(key)
  240. .setUuid(uuid)
  241. .setUuidPath(uuid + ".")
  242. .setRootUuid(uuid)
  243. .setProjectUuid(uuid);
  244. db.components().insertComponent(componentDto);
  245. return componentDto;
  246. }
  247. @Override
  248. protected ComputationStep step() {
  249. return new PersistLiveMeasuresStep(dbClient, metricRepository, new MeasureToMeasureDto(analysisMetadataHolder, treeRootHolder), treeRootHolder, measureRepository);
  250. }
  251. private static void verifyStatistics(TestComputationStepContext context, int expectedInsertsOrUpdates) {
  252. context.getStatistics().assertValue("insertsOrUpdates", expectedInsertsOrUpdates);
  253. }
  254. }