diff options
author | Eric Giffon <eric.giffon@sonarsource.com> | 2024-09-24 15:20:13 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-10-09 20:02:46 +0000 |
commit | 449c62b7f35b191a320f8cfd7d378539c6864a51 (patch) | |
tree | 95702c2014c130734ce8830d40f9439ebbb26346 /server | |
parent | f32f5bdeb0d2105eab86d71e1cb75e1fd6e8331b (diff) | |
download | sonarqube-449c62b7f35b191a320f8cfd7d378539c6864a51.tar.gz sonarqube-449c62b7f35b191a320f8cfd7d378539c6864a51.zip |
SONAR-22872 Update DB populator to write JSON measures
Diffstat (limited to 'server')
3 files changed, 30 insertions, 25 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java index 33916de43cf..862099c93d5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/createdb/PopulateDb.java @@ -255,7 +255,7 @@ public class PopulateDb { // create live measure for the branch projectLiveMeasureMetrics.stream() .map(sqContext.metricDtosByKey::get) - .forEach(metricDto -> sqContext.dbTester().measures().insertLiveMeasureWithSensibleValues(branchAndComponentDto.compo, metricDto)); + .forEach(metricDto -> sqContext.dbTester().measures().insertMeasureWithSensibleValues(branchAndComponentDto.compo, metricDto)); // create snapshots for the current branch long time = System2.INSTANCE.now(); @@ -270,7 +270,7 @@ public class PopulateDb { // insert project measure for the snapshot projectProjectMeasureMetrics.stream() .map(sqContext.metricDtosByKey::get) - .forEach(metricDto -> sqContext.dbTester().measures().insertMeasureWithSensibleValues(branchAndComponentDto.compo, snapshotDto, metricDto)); + .forEach(metricDto -> sqContext.dbTester().measures().insertProjectMeasureWithSensibleValues(branchAndComponentDto.compo, snapshotDto, metricDto)); } SnapshotDto lastSnapshotDto = snapshots.get(0); lastSnapshotDto.setLast(true); @@ -300,7 +300,7 @@ public class PopulateDb { // create live measure for this file fileLiveMeasureMetrics.stream() .map(sqContext.metricDtosByKey::get) - .forEach(metricDto -> sqContext.dbTester().measures().insertLiveMeasureWithSensibleValues(fileComponentDto, metricDto)); + .forEach(metricDto -> sqContext.dbTester().measures().insertMeasureWithSensibleValues(fileComponentDto, metricDto)); } sqContext.dbTester.forceCommit(); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureDbTester.java index 3d4f43d5fa4..3e71d2a924a 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureDbTester.java @@ -36,6 +36,7 @@ import org.sonar.db.component.SnapshotDto; import org.sonar.db.metric.MetricDto; import static org.sonar.db.measure.MeasureTesting.newLiveMeasure; +import static org.sonar.db.measure.MeasureTesting.newMeasure; import static org.sonar.db.measure.MeasureTesting.newProjectMeasureDto; import static org.sonar.db.metric.MetricTesting.newMetricDto; @@ -49,7 +50,7 @@ public class MeasureDbTester { } @SafeVarargs - public final ProjectMeasureDto insertMeasureWithSensibleValues(ComponentDto component, SnapshotDto analysis, MetricDto metricDto, Consumer<ProjectMeasureDto>... consumers) { + public final ProjectMeasureDto insertProjectMeasureWithSensibleValues(ComponentDto component, SnapshotDto analysis, MetricDto metricDto, Consumer<ProjectMeasureDto>... consumers) { ProjectMeasureDto measureDto = createProjectMeasure(metricDto, analysis, component); Arrays.stream(consumers).forEach(c -> c.accept(measureDto)); dbClient.projectMeasureDao().insert(db.getSession(), measureDto); @@ -76,10 +77,10 @@ public class MeasureDbTester { } @SafeVarargs - public final LiveMeasureDto insertLiveMeasureWithSensibleValues(ComponentDto component, MetricDto metric, Consumer<LiveMeasureDto>... consumers) { - LiveMeasureDto dto = createLiveMeasure(metric, component); + public final MeasureDto insertMeasureWithSensibleValues(ComponentDto component, MetricDto metric, Consumer<MeasureDto>... consumers) { + MeasureDto dto = createMeasure(metric, component); Arrays.stream(consumers).forEach(c -> c.accept(dto)); - dbClient.liveMeasureDao().insert(db.getSession(), dto); + dbClient.measureDao().insertOrUpdate(db.getSession(), dto); db.commit(); return dto; } @@ -123,6 +124,7 @@ public class MeasureDbTester { return insertMeasure(component.uuid(), component.branchUuid(), consumers); } + @SafeVarargs private MeasureDto insertMeasure(String componentUuid, String branchUuid, Consumer<MeasureDto>... consumers) { MeasureDto dto = new MeasureDto() .setComponentUuid(componentUuid) @@ -142,11 +144,11 @@ public class MeasureDbTester { return metricDto; } - public static LiveMeasureDto createLiveMeasure(MetricDto metricDto, ComponentDto componentDto) { + public static MeasureDto createMeasure(MetricDto metricDto, ComponentDto componentDto) { BiConsumer<MetricDto, MeasureAdapter> populator = specificLiveMeasurePopulator.getOrDefault(metricDto.getKey(), defaultLiveMeasurePopulator); - LiveMeasureDto liveMeasureDto = newLiveMeasure(componentDto, metricDto); - populator.accept(metricDto, new MeasureAdapter(liveMeasureDto)); - return liveMeasureDto; + MeasureDto measureDto = newMeasure(componentDto); + populator.accept(metricDto, new MeasureAdapter(measureDto, metricDto.getKey())); + return measureDto; } public static ProjectMeasureDto createProjectMeasure(MetricDto metricDto, SnapshotDto snapshotDto, ComponentDto projectComponentDto) { @@ -156,12 +158,7 @@ public class MeasureDbTester { return measureDto; } - private static final Consumer<MeasureAdapter> ratingMeasurePopulator = - m -> { - int rating = ThreadLocalRandom.current().nextInt(1, 5); - char textValue = (char) ('A' + rating - 1); - m.setValue((double) rating).setData("" + textValue); - }; + private static final Consumer<MeasureAdapter> ratingMeasurePopulator = m -> m.setValue((double) ThreadLocalRandom.current().nextInt(1, 5)); private static final Map<String, BiConsumer<MetricDto, MeasureAdapter>> specificLiveMeasurePopulator = new HashMap<>() { { @@ -215,23 +212,25 @@ public class MeasureDbTester { private static class MeasureAdapter { private final ProjectMeasureDto projectMeasure; - private final LiveMeasureDto liveMeasure; + private final MeasureDto measure; + private String metricKey; - private MeasureAdapter(LiveMeasureDto liveMeasure) { + private MeasureAdapter(MeasureDto measure, String metricKey) { this.projectMeasure = null; - this.liveMeasure = liveMeasure; + this.metricKey = metricKey; + this.measure = measure; } private MeasureAdapter(ProjectMeasureDto projectMeasure) { this.projectMeasure = projectMeasure; - this.liveMeasure = null; + this.measure = null; } public MeasureAdapter setValue(Double value) { if (projectMeasure != null) { projectMeasure.setValue(value); - } else if (liveMeasure != null) { - liveMeasure.setValue(value); + } else if (measure != null) { + measure.addValue(metricKey, value); } return this; } @@ -239,8 +238,8 @@ public class MeasureDbTester { public MeasureAdapter setData(String data) { if (projectMeasure != null) { projectMeasure.setData(data); - } else if (liveMeasure != null) { - liveMeasure.setData(data); + } else if (measure != null) { + measure.addValue(metricKey, data); } return this; } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureTesting.java index fc98033eddc..24379ae20bb 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/measure/MeasureTesting.java @@ -109,4 +109,10 @@ public class MeasureTesting { .setBranchUuid(branchUuid) .addValue(metricKey, value); } + + public static MeasureDto newMeasure(ComponentDto component) { + return new MeasureDto() + .setComponentUuid(component.uuid()) + .setBranchUuid(component.branchUuid()); + } } |