3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.measure.custom.CustomMeasureDto;
29 import org.sonar.db.measure.custom.CustomMeasureTesting;
30 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
31 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
32 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
33 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
34 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
35 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
36 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
37 import org.sonar.server.computation.task.projectanalysis.metric.MetricImpl;
38 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.guava.api.Assertions.assertThat;
42 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.DIRECTORY;
43 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE;
44 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.MODULE;
45 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT;
46 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.PROJECT_VIEW;
47 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.SUBVIEW;
48 import static org.sonar.server.computation.task.projectanalysis.component.Component.Type.VIEW;
49 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
50 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
51 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.entryOf;
52 import static org.sonar.server.computation.task.projectanalysis.measure.MeasureRepoEntry.toEntries;
53 import static org.sonar.server.computation.task.projectanalysis.step.CustomMeasuresCopyStep.dtoToMeasure;
56 public class CustomMeasuresCopyStepTest {
58 static final int PROJECT_REF = 1;
59 static final int MODULE_REF = 11;
60 static final int SUB_MODULE_REF = 111;
61 static final int DIR_REF = 1111;
62 static final int FILE1_REF = 11111;
63 static final int FILE2_REF = 11112;
65 static final String PROJECT_UUID = "PROJECT";
66 static final String MODULE_UUID = "MODULE";
67 static final String SUB_MODULE_UUID = "SUB_MODULE";
68 static final String DIR_UUID = "DIR";
69 static final String FILE1_UUID = "FILE1";
70 static final String FILE2_UUID = "FILE2";
72 static final String VIEW_UUID = "VIEW";
73 static final String SUBVIEW_UUID = "SUBVIEW";
75 static final int VIEW_REF = 10;
76 static final int SUBVIEW_REF = 101;
77 static final int PROJECT_VIEW_REF = 1011;
79 static final Metric FLOAT_METRIC = new MetricImpl(10, "float_metric", "Float Metric", Metric.MetricType.FLOAT);
80 static final Metric STRING_METRIC = new MetricImpl(11, "string_metric", "String Metric", Metric.MetricType.STRING);
83 public DbTester dbTester = DbTester.create(System2.INSTANCE);
86 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
89 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
92 public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
97 public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
99 CustomMeasuresCopyStep underTest;
102 public void setUp() {
103 DbClient dbClient = dbTester.getDbClient();
104 underTest = new CustomMeasuresCopyStep(treeRootHolder, dbClient, metricRepository, measureRepository);
108 public void copy_custom_measures_on_report() {
109 // File2 has no custom measure
110 insertCustomMeasure(FILE1_UUID, FLOAT_METRIC, 3.14);
111 insertCustomMeasure(DIR_UUID, FLOAT_METRIC, 123d);
112 insertCustomMeasure(SUB_MODULE_UUID, STRING_METRIC, "sub-module");
113 insertCustomMeasure(MODULE_UUID, STRING_METRIC, "module");
114 insertCustomMeasure(PROJECT_UUID, STRING_METRIC, "project");
116 treeRootHolder.setRoot(
117 builder(PROJECT, PROJECT_REF).setUuid(PROJECT_UUID)
119 ReportComponent.builder(MODULE, MODULE_REF).setUuid(MODULE_UUID)
121 ReportComponent.builder(MODULE, SUB_MODULE_REF).setUuid(SUB_MODULE_UUID)
123 ReportComponent.builder(DIRECTORY, DIR_REF).setUuid(DIR_UUID)
125 ReportComponent.builder(FILE, FILE1_REF).setUuid(FILE1_UUID).build(),
126 ReportComponent.builder(FILE, FILE2_REF).setUuid(FILE2_UUID).build()
137 assertRawMeasureValue(FILE1_REF, FLOAT_METRIC.getKey(), 3.1d);
138 assertNoRawMeasureValue(FILE2_REF);
139 assertRawMeasureValue(DIR_REF, FLOAT_METRIC.getKey(), 123d);
140 assertRawMeasureValue(SUB_MODULE_REF, STRING_METRIC.getKey(), "sub-module");
141 assertRawMeasureValue(MODULE_REF, STRING_METRIC.getKey(), "module");
142 assertRawMeasureValue(PROJECT_REF, STRING_METRIC.getKey(), "project");
146 public void copy_custom_measures_on_view() {
147 // View and subview have custom measures, but not project_view
148 insertCustomMeasure(SUBVIEW_UUID, FLOAT_METRIC, 3.14);
149 insertCustomMeasure(VIEW_UUID, STRING_METRIC, "good");
151 treeRootHolder.setRoot(
152 ViewsComponent.builder(VIEW, VIEW_REF).setUuid("VIEW")
154 ViewsComponent.builder(SUBVIEW, SUBVIEW_REF).setUuid("SUBVIEW").build(),
155 ViewsComponent.builder(PROJECT_VIEW, PROJECT_VIEW_REF).setUuid("PROJECT_VIEW").build()
161 assertNoRawMeasureValue(PROJECT_VIEW_REF);
162 assertRawMeasureValue(SUBVIEW_REF, FLOAT_METRIC.getKey(), 3.1d);
163 assertRawMeasureValue(VIEW_REF, STRING_METRIC.getKey(), "good");
167 public void test_float_value_type() throws Exception {
168 CustomMeasureDto dto = new CustomMeasureDto();
170 assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.FLOAT)).getDoubleValue()).isEqualTo(10.0);
174 public void test_int_value_type() throws Exception {
175 CustomMeasureDto dto = new CustomMeasureDto();
177 assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.INT)).getIntValue()).isEqualTo(10);
181 public void test_long_value_type() throws Exception {
182 CustomMeasureDto dto = new CustomMeasureDto();
184 assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.WORK_DUR)).getLongValue()).isEqualTo(10);
188 public void test_percent_value_type() throws Exception {
189 CustomMeasureDto dto = new CustomMeasureDto();
191 assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.PERCENT)).getDoubleValue()).isEqualTo(10);
195 public void test_string_value_type() throws Exception {
196 CustomMeasureDto dto = new CustomMeasureDto();
197 dto.setTextValue("foo");
198 assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.STRING)).getStringValue()).isEqualTo("foo");
202 public void test_LEVEL_value_type() throws Exception {
203 CustomMeasureDto dto = new CustomMeasureDto();
204 dto.setTextValue("OK");
205 assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.LEVEL)).getLevelValue()).isEqualTo(Measure.Level.OK);
209 public void test_boolean_value_type() throws Exception {
210 MetricImpl booleanMetric = new MetricImpl(1, "m", "M", Metric.MetricType.BOOL);
211 CustomMeasureDto dto = new CustomMeasureDto();
212 assertThat(dtoToMeasure(dto.setValue(1.0), booleanMetric).getBooleanValue()).isTrue();
213 assertThat(dtoToMeasure(dto.setValue(0.0), booleanMetric).getBooleanValue()).isFalse();
216 private void assertNoRawMeasureValue(int componentRef) {
217 assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty();
220 private void assertRawMeasureValue(int componentRef, String metricKey, double value) {
221 assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(value, 1)));
224 private void assertRawMeasureValue(int componentRef, String metricKey, String value) {
225 assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(value)));
228 private void insertCustomMeasure(String componentUuid, Metric metric, double value) {
229 dbTester.getDbClient().customMeasureDao().insert(dbTester.getSession(), CustomMeasureTesting.newCustomMeasureDto()
230 .setComponentUuid(componentUuid)
231 .setMetricId(metric.getId())
233 dbTester.getSession().commit();
236 private void insertCustomMeasure(String componentUuid, Metric metric, String value) {
237 dbTester.getDbClient().customMeasureDao().insert(dbTester.getSession(), CustomMeasureTesting.newCustomMeasureDto()
238 .setComponentUuid(componentUuid)
239 .setMetricId(metric.getId())
240 .setTextValue(value));
241 dbTester.getSession().commit();