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.

PersistMeasuresStepTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.MeasureDto;
  41. import org.sonar.db.metric.MetricDto;
  42. import static org.assertj.core.api.Assertions.assertThat;
  43. import static org.sonar.ce.task.projectanalysis.component.Component.Type.DIRECTORY;
  44. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  45. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
  46. import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
  47. import static org.sonar.ce.task.projectanalysis.component.Component.Type.SUBVIEW;
  48. import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
  49. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  50. public class PersistMeasuresStepTest extends BaseStepTest {
  51. private static final Metric STRING_METRIC = new Metric.Builder("string-metric", "String metric", Metric.ValueType.STRING).create();
  52. private static final Metric INT_METRIC = new Metric.Builder("int-metric", "int metric", Metric.ValueType.INT).create();
  53. private static final Metric NON_HISTORICAL_METRIC = new Metric.Builder("nh-metric", "nh metric", Metric.ValueType.INT).setDeleteHistoricalData(true).create();
  54. private static final String ANALYSIS_UUID = "a1";
  55. private static final int REF_1 = 1;
  56. private static final int REF_2 = 2;
  57. private static final int REF_3 = 3;
  58. private static final int REF_4 = 4;
  59. @Rule
  60. public DbTester db = DbTester.create(System2.INSTANCE);
  61. @Rule
  62. public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
  63. @Rule
  64. public MetricRepositoryRule metricRepository = new MetricRepositoryRule();
  65. @Rule
  66. public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
  67. @Rule
  68. public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
  69. private DbClient dbClient = db.getDbClient();
  70. @Before
  71. public void setUp() {
  72. analysisMetadataHolder.setUuid(ANALYSIS_UUID);
  73. MetricDto stringMetricDto = db.measures().insertMetric(m -> m.setKey(STRING_METRIC.getKey()).setValueType(Metric.ValueType.STRING.name()));
  74. MetricDto intMetricDto = db.measures().insertMetric(m -> m.setKey(INT_METRIC.getKey()).setValueType(Metric.ValueType.INT.name()));
  75. MetricDto nhMetricDto = db.measures().insertMetric(m -> m.setKey(NON_HISTORICAL_METRIC.getKey()).setValueType(Metric.ValueType.INT.name()));
  76. metricRepository.add(stringMetricDto.getUuid(), STRING_METRIC);
  77. metricRepository.add(intMetricDto.getUuid(), INT_METRIC);
  78. metricRepository.add(nhMetricDto.getUuid(), NON_HISTORICAL_METRIC);
  79. }
  80. @Test
  81. public void measures_on_non_historical_metrics_are_not_persisted() {
  82. prepareProject();
  83. measureRepository.addRawMeasure(REF_1, NON_HISTORICAL_METRIC.getKey(), newMeasureBuilder().create(1));
  84. measureRepository.addRawMeasure(REF_1, INT_METRIC.getKey(), newMeasureBuilder().create(2));
  85. TestComputationStepContext context = execute();
  86. assertThatMeasureIsNotPersisted("project-uuid", NON_HISTORICAL_METRIC);
  87. MeasureDto persistedMeasure = selectMeasure("project-uuid", INT_METRIC).get();
  88. assertThat(persistedMeasure.getValue()).isEqualTo(2);
  89. assertNbOfInserts(context, 1);
  90. }
  91. @Test
  92. public void persist_measures_of_project_analysis_excluding_directories() {
  93. prepareProject();
  94. // the computed measures
  95. measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().create("project-value"));
  96. measureRepository.addRawMeasure(REF_3, STRING_METRIC.getKey(), newMeasureBuilder().create("dir-value"));
  97. measureRepository.addRawMeasure(REF_4, STRING_METRIC.getKey(), newMeasureBuilder().create("file-value"));
  98. TestComputationStepContext context = execute();
  99. // project and dir measures are persisted, but not file measures
  100. assertThat(db.countRowsOfTable("project_measures")).isOne();
  101. assertThat(selectMeasure("project-uuid", STRING_METRIC).get().getData()).isEqualTo("project-value");
  102. assertThatMeasuresAreNotPersisted("dir-uuid");
  103. assertThatMeasuresAreNotPersisted("file-uuid");
  104. assertNbOfInserts(context, 1);
  105. }
  106. @Test
  107. public void measures_without_value_are_not_persisted() {
  108. prepareProject();
  109. measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().createNoValue());
  110. measureRepository.addRawMeasure(REF_1, INT_METRIC.getKey(), newMeasureBuilder().createNoValue());
  111. TestComputationStepContext context = execute();
  112. assertThatMeasureIsNotPersisted("project-uuid", STRING_METRIC);
  113. assertThatMeasureIsNotPersisted("project-uuid", INT_METRIC);
  114. assertNbOfInserts(context, 0);
  115. }
  116. @Test
  117. public void measures_on_new_code_period_are_persisted() {
  118. prepareProject();
  119. measureRepository.addRawMeasure(REF_1, INT_METRIC.getKey(), newMeasureBuilder().setVariation(42.0).createNoValue());
  120. TestComputationStepContext context = execute();
  121. MeasureDto persistedMeasure = selectMeasure("project-uuid", INT_METRIC).get();
  122. assertThat(persistedMeasure.getValue()).isNull();
  123. assertThat(persistedMeasure.getVariation()).isEqualTo(42.0);
  124. assertNbOfInserts(context, 1);
  125. }
  126. @Test
  127. public void persist_all_measures_of_portfolio_analysis() {
  128. preparePortfolio();
  129. // the computed measures
  130. measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().create("view-value"));
  131. measureRepository.addRawMeasure(REF_2, STRING_METRIC.getKey(), newMeasureBuilder().create("subview-value"));
  132. measureRepository.addRawMeasure(REF_3, STRING_METRIC.getKey(), newMeasureBuilder().create("project-value"));
  133. TestComputationStepContext context = execute();
  134. assertThat(db.countRowsOfTable("project_measures")).isEqualTo(2);
  135. assertThat(selectMeasure("view-uuid", STRING_METRIC).get().getData()).isEqualTo("view-value");
  136. assertThat(selectMeasure("subview-uuid", STRING_METRIC).get().getData()).isEqualTo("subview-value");
  137. assertNbOfInserts(context, 2);
  138. }
  139. private void prepareProject() {
  140. // tree of components as defined by scanner report
  141. Component project = ReportComponent.builder(PROJECT, REF_1).setUuid("project-uuid")
  142. .addChildren(
  143. ReportComponent.builder(DIRECTORY, REF_3).setUuid("dir-uuid")
  144. .addChildren(
  145. ReportComponent.builder(FILE, REF_4).setUuid("file-uuid")
  146. .build())
  147. .build())
  148. .build();
  149. treeRootHolder.setRoot(project);
  150. // components as persisted in db
  151. ComponentDto projectDto = insertComponent("project-key", "project-uuid");
  152. ComponentDto dirDto = insertComponent("dir-key", "dir-uuid");
  153. ComponentDto fileDto = insertComponent("file-key", "file-uuid");
  154. db.components().insertSnapshot(projectDto, s -> s.setUuid(ANALYSIS_UUID));
  155. }
  156. private void preparePortfolio() {
  157. // tree of components
  158. Component portfolio = ViewsComponent.builder(VIEW, REF_1).setUuid("view-uuid")
  159. .addChildren(
  160. ViewsComponent.builder(SUBVIEW, REF_2).setUuid("subview-uuid")
  161. .addChildren(
  162. ViewsComponent.builder(PROJECT_VIEW, REF_3).setUuid("project-uuid")
  163. .build())
  164. .build())
  165. .build();
  166. treeRootHolder.setRoot(portfolio);
  167. // components as persisted in db
  168. ComponentDto viewDto = insertComponent("view-key", "view-uuid");
  169. ComponentDto subViewDto = insertComponent("subview-key", "subview-uuid");
  170. ComponentDto projectDto = insertComponent("project-key", "project-uuid");
  171. db.components().insertSnapshot(viewDto, s -> s.setUuid(ANALYSIS_UUID));
  172. }
  173. private void assertThatMeasureIsNotPersisted(String componentUuid, Metric metric) {
  174. assertThat(selectMeasure(componentUuid, metric)).isEmpty();
  175. }
  176. private void assertThatMeasuresAreNotPersisted(String componentUuid) {
  177. assertThatMeasureIsNotPersisted(componentUuid, STRING_METRIC);
  178. assertThatMeasureIsNotPersisted(componentUuid, INT_METRIC);
  179. }
  180. private TestComputationStepContext execute() {
  181. TestComputationStepContext context = new TestComputationStepContext();
  182. new PersistMeasuresStep(dbClient, metricRepository, new MeasureToMeasureDto(analysisMetadataHolder, treeRootHolder), treeRootHolder, measureRepository)
  183. .execute(context);
  184. return context;
  185. }
  186. private Optional<MeasureDto> selectMeasure(String componentUuid, Metric metric) {
  187. return dbClient.measureDao().selectMeasure(db.getSession(), ANALYSIS_UUID, componentUuid, metric.getKey());
  188. }
  189. private ComponentDto insertComponent(String key, String uuid) {
  190. ComponentDto componentDto = new ComponentDto()
  191. .setDbKey(key)
  192. .setUuid(uuid)
  193. .setUuidPath(uuid + ".")
  194. .setRootUuid(uuid)
  195. .setProjectUuid(uuid);
  196. db.components().insertComponent(componentDto);
  197. return componentDto;
  198. }
  199. private static void assertNbOfInserts(TestComputationStepContext context, int expected) {
  200. context.getStatistics().assertValue("inserts", expected);
  201. }
  202. @Override
  203. protected ComputationStep step() {
  204. return new PersistMeasuresStep(dbClient, metricRepository, new MeasureToMeasureDto(analysisMetadataHolder, treeRootHolder), treeRootHolder, measureRepository);
  205. }
  206. }