aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/test
diff options
context:
space:
mode:
authorEric Giffon <eric.giffon@sonarsource.com>2024-08-29 14:37:24 +0200
committersonartech <sonartech@sonarsource.com>2024-10-09 20:02:46 +0000
commit264769a69d03fd61c87491be3da5f101469bb60c (patch)
treeac39fd8068d8155ddb93e306fa542651bcb5facd /server/sonar-db-dao/src/test
parenta4da413ce8b4eab78aadc9e492d437f45d9b7e6f (diff)
downloadsonarqube-264769a69d03fd61c87491be3da5f101469bb60c.tar.gz
sonarqube-264769a69d03fd61c87491be3da5f101469bb60c.zip
SONAR-22872 CE step to persist measures in JSON format
Diffstat (limited to 'server/sonar-db-dao/src/test')
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java107
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDtoTest.java68
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureHashTest.java54
3 files changed, 229 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
new file mode 100644
index 00000000000..1fdd6d7b9c1
--- /dev/null
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDaoTest.java
@@ -0,0 +1,107 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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 org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.measure.MeasureTesting.newMeasure;
+
+class MeasureDaoTest {
+
+ @RegisterExtension
+ public final DbTester db = DbTester.create(System2.INSTANCE);
+
+ private final MeasureDao underTest = db.getDbClient().measureDao();
+
+ @Test
+ void insert_measure() {
+ MeasureDto dto = newMeasure();
+ int count = underTest.insert(db.getSession(), dto);
+ assertThat(count).isEqualTo(1);
+ verifyTableSize(1);
+ verifyPersisted(dto);
+ }
+
+ @Test
+ void update_measure() {
+ MeasureDto dto = newMeasure();
+ underTest.insert(db.getSession(), dto);
+
+ dto.addValue("metric1", "value1");
+ dto.computeJsonValueHash();
+ int count = underTest.update(db.getSession(), dto);
+
+ assertThat(count).isEqualTo(1);
+ verifyTableSize(1);
+ verifyPersisted(dto);
+ }
+
+ @Test
+ void select_measure() {
+ MeasureDto measure1 = newMeasure();
+ MeasureDto measure2 = newMeasure();
+ underTest.insert(db.getSession(), measure1);
+ underTest.insert(db.getSession(), measure2);
+
+ assertThat(underTest.selectMeasure(db.getSession(), measure1.getComponentUuid()))
+ .hasValueSatisfying(selected -> assertThat(selected).usingRecursiveComparison().isEqualTo(measure1));
+ assertThat(underTest.selectMeasure(db.getSession(), "unknown-component")).isEmpty();
+ }
+
+ @Test
+ void select_branch_measure_hashes() {
+ MeasureDto measure1 = new MeasureDto()
+ .setComponentUuid("c1")
+ .setBranchUuid("b1")
+ .addValue("metric1", "value1");
+ MeasureDto measure2 = new MeasureDto()
+ .setComponentUuid("c2")
+ .setBranchUuid("b1")
+ .addValue("metric2", "value2");
+ MeasureDto measure3 = new MeasureDto()
+ .setComponentUuid("c3")
+ .setBranchUuid("b3")
+ .addValue("metric3", "value3");
+ long hash1 = measure1.computeJsonValueHash();
+ long hash2 = measure2.computeJsonValueHash();
+ measure3.computeJsonValueHash();
+
+ underTest.insert(db.getSession(), measure1);
+ underTest.insert(db.getSession(), measure2);
+ underTest.insert(db.getSession(), measure3);
+
+ assertThat(underTest.selectBranchMeasureHashes(db.getSession(), "b1"))
+ .containsOnly(new MeasureHash("c1", hash1), new MeasureHash("c2", hash2));
+ }
+
+ private void verifyTableSize(int expectedSize) {
+ assertThat(db.countRowsOfTable(db.getSession(), "measures")).isEqualTo(expectedSize);
+ }
+
+ private void verifyPersisted(MeasureDto dto) {
+ assertThat(underTest.selectMeasure(db.getSession(), dto.getComponentUuid())).hasValueSatisfying(selected -> {
+ assertThat(selected).usingRecursiveComparison().isEqualTo(dto);
+ });
+ }
+}
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDtoTest.java
new file mode 100644
index 00000000000..9299402942d
--- /dev/null
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureDtoTest.java
@@ -0,0 +1,68 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.Map;
+import java.util.TreeMap;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class MeasureDtoTest {
+
+ @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}");
+ }
+}
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureHashTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureHashTest.java
new file mode 100644
index 00000000000..ff11db17994
--- /dev/null
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/MeasureHashTest.java
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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.TreeSet;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class MeasureHashTest {
+
+ @Test
+ void hashCode_depends_on_both_fields() {
+ MeasureHash measureHash1 = new MeasureHash("component1", 123L);
+ MeasureHash measureHash2 = new MeasureHash("component", 123L);
+ MeasureHash measureHash3 = new MeasureHash("component", 124L);
+
+ assertThat(measureHash1)
+ .doesNotHaveSameHashCodeAs(measureHash2)
+ .doesNotHaveSameHashCodeAs(measureHash3)
+ .isNotEqualTo(measureHash2)
+ .isNotEqualTo(measureHash3);
+ }
+
+ @Test
+ void sort_by_component_and_hash() {
+ MeasureHash measureHash1 = new MeasureHash("A", 1L);
+ MeasureHash measureHash2 = new MeasureHash("A", 2L);
+ MeasureHash measureHash3 = new MeasureHash("B", 1L);
+ MeasureHash measureHash4 = new MeasureHash("B", 2L);
+
+ TreeSet<MeasureHash> set = new TreeSet<>(List.of(measureHash1, measureHash2, measureHash3, measureHash4));
+
+ assertThat(set).containsExactly(measureHash1, measureHash2, measureHash3, measureHash4);
+ }
+}