aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/test
diff options
context:
space:
mode:
authorEric Giffon <eric.giffon@sonarsource.com>2024-10-09 10:17:13 +0200
committersonartech <sonartech@sonarsource.com>2024-10-14 20:03:02 +0000
commit12af365eaa3cf93a561305fe99897dae87e0626f (patch)
tree61f3ecff60318027b7f4877973b968afec36ef72 /server/sonar-db-dao/src/test
parent121226eab94dc57c61ee446dfa2a5c5e11765ca1 (diff)
downloadsonarqube-12af365eaa3cf93a561305fe99897dae87e0626f.tar.gz
sonarqube-12af365eaa3cf93a561305fe99897dae87e0626f.zip
SONAR-23213 Measures double write - live update
Diffstat (limited to 'server/sonar-db-dao/src/test')
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java23
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/measure/JsonMeasureDaoTest.java54
2 files changed, 77 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java
index f11f90f271c..5c374541c90 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java
@@ -712,6 +712,29 @@ public class BranchDaoTest {
assertThat(underTest.isMeasuresMigrated(dbSession, uuid2)).isFalse();
}
+ @Test
+ public void isMeasuresMigrated() throws SQLException {
+ createMeasuresMigratedColumn();
+
+ // master branch with flag set to false
+ ComponentDto project = db.components().insertPrivateProject();
+ // branches & PRs
+ ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BRANCH));
+ ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH));
+ ComponentDto branch3 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
+ ComponentDto branch4 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST));
+
+ db.getDbClient().branchDao().updateMeasuresMigrated(dbSession, branch1.branchUuid(), true);
+ db.getDbClient().branchDao().updateMeasuresMigrated(dbSession, branch2.branchUuid(), false);
+ db.getDbClient().branchDao().updateMeasuresMigrated(dbSession, branch3.branchUuid(), true);
+
+ assertThat(underTest.isMeasuresMigrated(dbSession, project.uuid())).isFalse();
+ assertThat(underTest.isMeasuresMigrated(dbSession, branch1.uuid())).isTrue();
+ assertThat(underTest.isMeasuresMigrated(dbSession, branch2.uuid())).isFalse();
+ assertThat(underTest.isMeasuresMigrated(dbSession, branch3.uuid())).isTrue();
+ assertThat(underTest.isMeasuresMigrated(dbSession, branch4.uuid())).isFalse();
+ }
+
private void createMeasuresMigratedColumn() throws SQLException {
AddMeasuresMigratedColumnToProjectBranchesTable migration = new AddMeasuresMigratedColumnToProjectBranchesTable(db.getDbClient().getDatabase());
migration.execute();
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/JsonMeasureDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/JsonMeasureDaoTest.java
index f0b6d913c6b..3841aeeef9b 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/JsonMeasureDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/JsonMeasureDaoTest.java
@@ -30,6 +30,7 @@ import org.sonar.server.platform.db.migration.adhoc.CreateIndexOnPortfoliosMeasu
import org.sonar.server.platform.db.migration.adhoc.CreateIndexOnProjectBranchesMeasuresMigrated;
import org.sonar.server.platform.db.migration.adhoc.CreateMeasuresTable;
+import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.measure.MeasureTesting.newJsonMeasure;
@@ -73,6 +74,59 @@ public class JsonMeasureDaoTest {
}
@Test
+ public void insertOrUpdate_inserts_or_updates_measure() {
+ // insert
+ JsonMeasureDto dto = newJsonMeasure();
+ int count = underTest.insertOrUpdate(db.getSession(), dto);
+ assertThat(count).isEqualTo(1);
+ verifyTableSize(1);
+ verifyPersisted(dto);
+
+ // update
+ String key = dto.getMetricValues().keySet().stream().findFirst().orElseThrow();
+ dto.addValue(key, 10d);
+ count = underTest.insertOrUpdate(db.getSession(), dto);
+ assertThat(count).isEqualTo(1);
+ verifyTableSize(1);
+ verifyPersisted(dto);
+ }
+
+ @Test
+ public void insertOrUpdate_merges_measures() {
+ // insert
+ Double value2 = 10d;
+ JsonMeasureDto dto = newJsonMeasure();
+ dto.getMetricValues().clear();
+ dto.addValue("key1", 11d)
+ .addValue("key2", value2);
+ int count = underTest.insert(db.getSession(), dto);
+ verifyPersisted(dto);
+ verifyTableSize(1);
+ assertThat(count).isEqualTo(1);
+
+ // update key1 value, remove key2 (must not disappear from DB) and add key3
+ Double value1 = 12d;
+ Double value3 = 13d;
+ dto.addValue("key1", value1)
+ .addValue("key3", value3)
+ .getMetricValues().remove("key2");
+ count = underTest.insertOrUpdate(db.getSession(), dto);
+ assertThat(count).isEqualTo(1);
+ verifyTableSize(1);
+
+ assertThat(underTest.selectByComponentUuid(db.getSession(), dto.getComponentUuid()))
+ .hasValueSatisfying(selected -> {
+ assertThat(selected.getComponentUuid()).isEqualTo(dto.getComponentUuid());
+ assertThat(selected.getBranchUuid()).isEqualTo(dto.getBranchUuid());
+ assertThat(selected.getMetricValues()).contains(
+ entry("key1", value1),
+ entry("key2", value2),
+ entry("key3", value3));
+ assertThat(selected.getJsonValueHash()).isEqualTo(dto.computeJsonValueHash());
+ });
+ }
+
+ @Test
public void select_measure() {
JsonMeasureDto measure1 = newJsonMeasure();
JsonMeasureDto measure2 = newJsonMeasure();