]> source.dussan.org Git - sonarqube.git/blob
935e6d63ab86c68adeca5785790e926a02779788
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.computation.task.projectanalysis.step;
21
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;
39
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;
54
55
56 public class CustomMeasuresCopyStepTest {
57
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;
64
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";
71
72   static final String VIEW_UUID = "VIEW";
73   static final String SUBVIEW_UUID = "SUBVIEW";
74
75   static final int VIEW_REF = 10;
76   static final int SUBVIEW_REF = 101;
77   static final int PROJECT_VIEW_REF = 1011;
78
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);
81
82   @Rule
83   public DbTester dbTester = DbTester.create(System2.INSTANCE);
84
85   @Rule
86   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
87
88   @Rule
89   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
90
91   @Rule
92   public MetricRepositoryRule metricRepository = new MetricRepositoryRule()
93     .add(FLOAT_METRIC)
94     .add(STRING_METRIC);
95
96   @Rule
97   public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository);
98
99   CustomMeasuresCopyStep underTest;
100
101   @Before
102   public void setUp() {
103     DbClient dbClient = dbTester.getDbClient();
104     underTest = new CustomMeasuresCopyStep(treeRootHolder, dbClient, metricRepository, measureRepository);
105   }
106
107   @Test
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");
115
116     treeRootHolder.setRoot(
117       builder(PROJECT, PROJECT_REF).setUuid(PROJECT_UUID)
118         .addChildren(
119           ReportComponent.builder(MODULE, MODULE_REF).setUuid(MODULE_UUID)
120             .addChildren(
121               ReportComponent.builder(MODULE, SUB_MODULE_REF).setUuid(SUB_MODULE_UUID)
122                 .addChildren(
123                   ReportComponent.builder(DIRECTORY, DIR_REF).setUuid(DIR_UUID)
124                     .addChildren(
125                       ReportComponent.builder(FILE, FILE1_REF).setUuid(FILE1_UUID).build(),
126                       ReportComponent.builder(FILE, FILE2_REF).setUuid(FILE2_UUID).build()
127                     )
128                     .build()
129                 )
130                 .build()
131             )
132             .build())
133         .build());
134
135     underTest.execute();
136
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");
143   }
144
145   @Test
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");
150
151     treeRootHolder.setRoot(
152       ViewsComponent.builder(VIEW, VIEW_REF).setUuid("VIEW")
153         .addChildren(
154           ViewsComponent.builder(SUBVIEW, SUBVIEW_REF).setUuid("SUBVIEW").build(),
155           ViewsComponent.builder(PROJECT_VIEW, PROJECT_VIEW_REF).setUuid("PROJECT_VIEW").build()
156         )
157         .build());
158
159     underTest.execute();
160
161     assertNoRawMeasureValue(PROJECT_VIEW_REF);
162     assertRawMeasureValue(SUBVIEW_REF, FLOAT_METRIC.getKey(), 3.1d);
163     assertRawMeasureValue(VIEW_REF, STRING_METRIC.getKey(), "good");
164   }
165
166   @Test
167   public void test_float_value_type() throws Exception {
168     CustomMeasureDto dto = new CustomMeasureDto();
169     dto.setValue(10.0);
170     assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.FLOAT)).getDoubleValue()).isEqualTo(10.0);
171   }
172
173   @Test
174   public void test_int_value_type() throws Exception {
175     CustomMeasureDto dto = new CustomMeasureDto();
176     dto.setValue(10.0);
177     assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.INT)).getIntValue()).isEqualTo(10);
178   }
179
180   @Test
181   public void test_long_value_type() throws Exception {
182     CustomMeasureDto dto = new CustomMeasureDto();
183     dto.setValue(10.0);
184     assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.WORK_DUR)).getLongValue()).isEqualTo(10);
185   }
186
187   @Test
188   public void test_percent_value_type() throws Exception {
189     CustomMeasureDto dto = new CustomMeasureDto();
190     dto.setValue(10.0);
191     assertThat(dtoToMeasure(dto, new MetricImpl(1, "m", "M", Metric.MetricType.PERCENT)).getDoubleValue()).isEqualTo(10);
192   }
193
194   @Test
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");
199   }
200
201   @Test
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);
206   }
207
208   @Test
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();
214   }
215
216   private void assertNoRawMeasureValue(int componentRef) {
217     assertThat(measureRepository.getAddedRawMeasures(componentRef)).isEmpty();
218   }
219
220   private void assertRawMeasureValue(int componentRef, String metricKey, double value) {
221     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(value, 1)));
222   }
223
224   private void assertRawMeasureValue(int componentRef, String metricKey, String value) {
225     assertThat(toEntries(measureRepository.getAddedRawMeasures(componentRef))).containsOnly(entryOf(metricKey, newMeasureBuilder().create(value)));
226   }
227
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())
232       .setValue(value));
233     dbTester.getSession().commit();
234   }
235
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();
242   }
243
244 }