aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDtoTest.java
blob: 354152b86e307cc30a99192930ed3f8e27525bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.measure;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiFunction;
import org.apache.commons.lang.math.RandomUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat;

class MeasureDtoTest {

  @Test
  void getDouble_returns_double_value() {
    String metricKey = randomAlphabetic(7);
    double value = RandomUtils.nextDouble();
    MeasureDto measureDto = new MeasureDto().addValue(metricKey, value);
    assertThat(measureDto.getDouble(metricKey)).isEqualTo(value);
  }

  @Test
  void getInt_returns_int_value() {
    String metricKey = randomAlphabetic(7);
    int value = RandomUtils.nextInt();
    MeasureDto measureDto = new MeasureDto().addValue(metricKey, value);
    assertThat(measureDto.getInt(metricKey)).isEqualTo(value);
  }

  @Test
  void getLong_returns_long_value() {
    String metricKey = randomAlphabetic(7);
    long value = RandomUtils.nextInt();
    MeasureDto measureDto = new MeasureDto().addValue(metricKey, value);
    assertThat(measureDto.getLong(metricKey)).isEqualTo(value);
  }

  @ParameterizedTest
  @MethodSource("valuesOfDifferentTypes")
  void getString_returns_string_value(Object value) {
    String metricKey = randomAlphabetic(7);
    MeasureDto measureDto = new MeasureDto().addValue(metricKey, value);
    assertThat(measureDto.getString(metricKey)).isEqualTo(String.valueOf(value));
  }

  private static List<Object> valuesOfDifferentTypes() {
    return List.of(2, 3.14, "foo");
  }

  @ParameterizedTest
  @MethodSource("gettersOfDifferentTypes")
  void getters_return_null_for_nonexistent_metric(BiFunction<MeasureDto, String, Object> getter) {
    String metricKey = randomAlphabetic(7);
    MeasureDto measureDto = new MeasureDto();
    assertThat(getter.apply(measureDto, metricKey)).isNull();
  }

  public static List<BiFunction<MeasureDto, String, Object>> gettersOfDifferentTypes() {
    return List.of(
      MeasureDto::getInt,
      MeasureDto::getLong,
      MeasureDto::getDouble,
      MeasureDto::getString
    );
  }

  @Test
  void compute_json_value_hash() {
    MeasureDto measureDto = new MeasureDto();
    measureDto.setJsonValue("{\"key\":\"value\"}");
    assertThat(measureDto.getJsonValueHash()).isNull();
    assertThat(measureDto.computeJsonValueHash()).isEqualTo(2887272982314571750L);
    assertThat(measureDto.getJsonValueHash()).isEqualTo(2887272982314571750L);
  }

  @Test
  void getMetricValues_returns_all_values_ordered() {
    MeasureDto measureDto = new MeasureDto()
      .addValue("string-metric", "value")
      .addValue("int-metric", 1)
      .addValue("long-metric", 2L);
    assertThat(measureDto.getMetricValues()).containsExactlyEntriesOf(new TreeMap<>(Map.of(
      "int-metric", 1,
      "long-metric", 2L,
      "string-metric", "value"
    )));
  }

  @Test
  void toString_prints_all_fields() {
    MeasureDto measureDto = new MeasureDto()
      .setBranchUuid("branch-uuid")
      .setComponentUuid("component-uuid")
      .addValue("int-metric", 12)
      .addValue("double-metric", 34.5)
      .addValue("boolean-metric", true)
      .addValue("string-metric", "value");
    measureDto.computeJsonValueHash();

    assertThat(measureDto).hasToString("MeasureDto{componentUuid='component-uuid'" +
      ", branchUuid='branch-uuid'" +
      ", metricValues={boolean-metric=true, double-metric=34.5, int-metric=12, string-metric=value}" +
      ", jsonValueHash=-1071134275520515337}");
  }
}