3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.measure;
22 import com.google.common.base.Function;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Predicate;
25 import com.google.common.collect.ImmutableList;
26 import com.tngtech.java.junit.dataprovider.DataProvider;
27 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
28 import com.tngtech.java.junit.dataprovider.UseDataProvider;
29 import java.util.List;
31 import javax.annotation.Nonnull;
32 import javax.annotation.Nullable;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.experimental.categories.Category;
37 import org.junit.rules.ExpectedException;
38 import org.junit.runner.RunWith;
39 import org.sonar.api.rule.RuleKey;
40 import org.sonar.api.utils.System2;
41 import org.sonar.db.DbClient;
42 import org.sonar.db.DbTester;
43 import org.sonar.db.rule.RuleDto;
44 import org.sonar.server.computation.batch.BatchReportReader;
45 import org.sonar.server.computation.component.Component;
46 import org.sonar.server.computation.component.Developer;
47 import org.sonar.server.computation.component.DumbDeveloper;
48 import org.sonar.server.computation.component.ReportComponent;
49 import org.sonar.server.computation.metric.Metric;
50 import org.sonar.server.computation.metric.MetricImpl;
51 import org.sonar.server.computation.metric.MetricRepository;
52 import org.sonar.server.computation.metric.ReportMetricValidator;
53 import org.sonar.test.DbTests;
55 import static com.google.common.collect.FluentIterable.from;
56 import static java.lang.String.format;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.assertj.guava.api.Assertions.assertThat;
59 import static org.junit.Assert.fail;
60 import static org.mockito.Mockito.mock;
61 import static org.mockito.Mockito.verifyNoMoreInteractions;
62 import static org.mockito.Mockito.when;
64 @RunWith(DataProviderRunner.class)
65 @Category(DbTests.class)
66 public class MapBasedRawMeasureRepositoryTest {
68 public DbTester dbTester = DbTester.create(System2.INSTANCE);
70 public ExpectedException expectedException = ExpectedException.none();
72 private static final String FILE_COMPONENT_KEY = "file cpt key";
73 private static final ReportComponent FILE_COMPONENT = ReportComponent.builder(Component.Type.FILE, 1).setKey(FILE_COMPONENT_KEY).build();
74 private static final ReportComponent OTHER_COMPONENT = ReportComponent.builder(Component.Type.FILE, 2).setKey("some other key").build();
76 private static final String METRIC_KEY_1 = "metric 1";
77 private static final String METRIC_KEY_2 = "metric 2";
78 private final Metric metric1 = mock(Metric.class);
79 private final Metric metric2 = mock(Metric.class);
81 private static final Measure SOME_MEASURE = Measure.newMeasureBuilder().create("some value");
83 private static final RuleDto SOME_RULE = RuleDto.createFor(RuleKey.of("A", "1")).setId(963);
84 private static final Developer SOME_DEVELOPER = new DumbDeveloper("DEV1");
86 private ReportMetricValidator reportMetricValidator = mock(ReportMetricValidator.class);
88 private MetricRepository metricRepository = mock(MetricRepository.class);
89 private MapBasedRawMeasureRepository<Integer> underTest = new MapBasedRawMeasureRepository<>(new Function<Component, Integer>() {
91 public Integer apply(Component component) {
92 return component.getReportAttributes().getRef();
96 private DbClient mockedDbClient = mock(DbClient.class);
97 private BatchReportReader mockBatchReportReader = mock(BatchReportReader.class);
98 private MeasureRepositoryImpl underTestWithMock = new MeasureRepositoryImpl(mockedDbClient, mockBatchReportReader, metricRepository, reportMetricValidator);
101 public void setUp() {
102 when(metric1.getKey()).thenReturn(METRIC_KEY_1);
103 when(metric1.getType()).thenReturn(Metric.MetricType.STRING);
104 when(metric2.getKey()).thenReturn(METRIC_KEY_2);
105 when(metric2.getType()).thenReturn(Metric.MetricType.STRING);
107 // references to metrics are consistent with DB by design
108 when(metricRepository.getByKey(METRIC_KEY_1)).thenReturn(metric1);
109 when(metricRepository.getByKey(METRIC_KEY_2)).thenReturn(metric2);
112 @Test(expected = NullPointerException.class)
113 public void add_throws_NPE_if_Component_argument_is_null() {
114 underTest.add(null, metric1, SOME_MEASURE);
117 @Test(expected = NullPointerException.class)
118 public void add_throws_NPE_if_Component_metric_is_null() {
119 underTest.add(FILE_COMPONENT, null, SOME_MEASURE);
122 @Test(expected = NullPointerException.class)
123 public void add_throws_NPE_if_Component_measure_is_null() {
124 underTest.add(FILE_COMPONENT, metric1, null);
127 @Test(expected = UnsupportedOperationException.class)
128 public void add_throws_UOE_if_measure_already_exists() {
129 underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE);
130 underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE);
133 @Test(expected = NullPointerException.class)
134 public void update_throws_NPE_if_Component_argument_is_null() {
135 underTest.update(null, metric1, SOME_MEASURE);
138 @Test(expected = NullPointerException.class)
139 public void update_throws_NPE_if_Component_metric_is_null() {
140 underTest.update(FILE_COMPONENT, null, SOME_MEASURE);
143 @Test(expected = NullPointerException.class)
144 public void update_throws_NPE_if_Component_measure_is_null() {
145 underTest.update(FILE_COMPONENT, metric1, null);
148 @Test(expected = UnsupportedOperationException.class)
149 public void update_throws_UOE_if_measure_does_not_exists() {
150 underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE);
153 private static final List<Measure> MEASURES = ImmutableList.of(
154 Measure.newMeasureBuilder().create(1),
155 Measure.newMeasureBuilder().create(1l),
156 Measure.newMeasureBuilder().create(1d, 1),
157 Measure.newMeasureBuilder().create(true),
158 Measure.newMeasureBuilder().create(false),
159 Measure.newMeasureBuilder().create("sds"),
160 Measure.newMeasureBuilder().create(Measure.Level.OK),
161 Measure.newMeasureBuilder().createNoValue()
165 public static Object[][] measures() {
166 return from(MEASURES).transform(new Function<Measure, Object[]>() {
169 public Object[] apply(Measure input) {
170 return new Measure[] {input};
172 }).toArray(Object[].class);
176 public void add_accepts_NO_VALUE_as_measure_arg() {
177 for (Metric.MetricType metricType : Metric.MetricType.values()) {
178 underTest.add(FILE_COMPONENT, new MetricImpl(1, "key" + metricType, "name" + metricType, metricType), Measure.newMeasureBuilder().createNoValue());
183 @UseDataProvider("measures")
184 public void update_throws_IAE_if_valueType_of_Measure_is_not_the_same_as_the_Metric_valueType_unless_NO_VALUE(Measure measure) {
185 for (Metric.MetricType metricType : Metric.MetricType.values()) {
186 if (metricType.getValueType() == measure.getValueType() || measure.getValueType() == Measure.ValueType.NO_VALUE) {
191 final MetricImpl metric = new MetricImpl(1, "key" + metricType, "name" + metricType, metricType);
192 underTest.add(FILE_COMPONENT, metric, getSomeMeasureByValueType(metricType));
193 underTest.update(FILE_COMPONENT, metric, measure);
194 fail("An IllegalArgumentException should have been raised");
195 } catch (IllegalArgumentException e) {
196 assertThat(e).hasMessage(format(
197 "Measure's ValueType (%s) is not consistent with the Metric's ValueType (%s)",
198 measure.getValueType(), metricType.getValueType()));
204 public void update_accepts_NO_VALUE_as_measure_arg() {
205 for (Metric.MetricType metricType : Metric.MetricType.values()) {
206 MetricImpl metric = new MetricImpl(1, "key" + metricType, "name" + metricType, metricType);
207 underTest.add(FILE_COMPONENT, metric, getSomeMeasureByValueType(metricType));
208 underTest.update(FILE_COMPONENT, metric, Measure.newMeasureBuilder().createNoValue());
212 private Measure getSomeMeasureByValueType(final Metric.MetricType metricType) {
213 return from(MEASURES).filter(new Predicate<Measure>() {
215 public boolean apply(@Nonnull Measure input) {
216 return input.getValueType() == metricType.getValueType();
222 public void update_supports_updating_to_the_same_value() {
223 underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE);
224 underTest.update(FILE_COMPONENT, metric1, SOME_MEASURE);
228 public void update_updates_the_stored_value() {
229 Measure newMeasure = Measure.updatedMeasureBuilder(SOME_MEASURE).create();
231 underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE);
232 underTest.update(FILE_COMPONENT, metric1, newMeasure);
234 assertThat(underTest.getRawMeasure(FILE_COMPONENT, metric1).get()).isSameAs(newMeasure);
238 public void update_updates_the_stored_value_for_rule() {
239 Measure initialMeasure = Measure.newMeasureBuilder().forRule(123).createNoValue();
240 Measure newMeasure = Measure.updatedMeasureBuilder(initialMeasure).create();
242 underTest.add(FILE_COMPONENT, metric1, initialMeasure);
243 underTest.update(FILE_COMPONENT, metric1, newMeasure);
245 assertThat(underTest.getRawMeasures(FILE_COMPONENT).get(metric1.getKey()).iterator().next()).isSameAs(newMeasure);
249 public void getRawMeasure_throws_NPE_without_reading_batch_report_if_component_arg_is_null() {
251 underTestWithMock.getRawMeasure(null, metric1);
252 fail("an NPE should have been raised");
253 } catch (NullPointerException e) {
254 verifyNoMoreInteractions(mockBatchReportReader);
259 public void getRawMeasure_throws_NPE_without_reading_batch_report_if_metric_arg_is_null() {
261 underTestWithMock.getRawMeasure(FILE_COMPONENT, null);
262 fail("an NPE should have been raised");
263 } catch (NullPointerException e) {
264 verifyNoMoreInteractions(mockBatchReportReader);
269 public void getRawMeasure_returns_measure_added_through_add_method() {
270 underTest.add(FILE_COMPONENT, metric1, SOME_MEASURE);
272 Optional<Measure> res = underTest.getRawMeasure(FILE_COMPONENT, metric1);
274 assertThat(res).isPresent();
275 assertThat(res.get()).isSameAs(SOME_MEASURE);
277 // make sure we really match on the specified component and metric
278 assertThat(underTest.getRawMeasure(OTHER_COMPONENT, metric1)).isAbsent();
279 assertThat(underTest.getRawMeasure(FILE_COMPONENT, metric2)).isAbsent();
282 @Test(expected = NullPointerException.class)
283 public void getRawMeasures_for_metric_throws_NPE_if_Component_arg_is_null() {
284 underTest.getRawMeasures(null, metric1);
287 @Test(expected = NullPointerException.class)
288 public void getRawMeasures_for_metric_throws_NPE_if_Metric_arg_is_null() {
289 underTest.getRawMeasures(FILE_COMPONENT, null);
293 public void getRawMeasures_for_metric_returns_empty_if_repository_is_empty() {
294 assertThat(underTest.getRawMeasures(FILE_COMPONENT, metric1)).isEmpty();
298 public void getRawMeasures_for_metric_returns_rule_measure() {
299 Measure ruleMeasure = Measure.newMeasureBuilder().forRule(SOME_RULE.getId()).createNoValue();
301 underTest.add(FILE_COMPONENT, metric1, ruleMeasure);
303 Set<Measure> measures = underTest.getRawMeasures(FILE_COMPONENT, metric1);
304 assertThat(measures).hasSize(1);
305 assertThat(measures.iterator().next()).isSameAs(ruleMeasure);
309 public void getRawMeasures_for_metric_returns_developer_measure() {
310 Measure devMeasure = Measure.newMeasureBuilder().forDeveloper(SOME_DEVELOPER).createNoValue();
312 underTest.add(FILE_COMPONENT, metric1, devMeasure);
314 Set<Measure> measures = underTest.getRawMeasures(FILE_COMPONENT, metric1);
315 assertThat(measures).hasSize(1);
316 assertThat(measures.iterator().next()).isSameAs(devMeasure);
320 public void getRawMeasures_for_metric_returns_developer_with_rule_measure() {
321 Measure devMeasure = Measure.newMeasureBuilder()
322 .forDeveloper(SOME_DEVELOPER)
323 .forRule(SOME_RULE.getId())
326 underTest.add(FILE_COMPONENT, metric1, devMeasure);
328 Set<Measure> measures = underTest.getRawMeasures(FILE_COMPONENT, metric1);
329 assertThat(measures).hasSize(1);
330 assertThat(measures.iterator().next()).isSameAs(devMeasure);