void setValue(ComponentDto component, String metricKey, @Nullable String data) { changeCell(component, metricKey, m -> { if (Objects.equals(m.getDataAsString(), data)) { return false; } m.setData(data); return true; }); }
public static String formatMeasureValue(LiveMeasureDto measure, MetricDto metric) { Double doubleValue = measure.getValue(); String stringValue = measure.getDataAsString(); return formatMeasureValue(doubleValue == null ? Double.NaN : doubleValue, stringValue, metric); }
static void updateMeasureBuilder(Measure.Builder measureBuilder, MetricDto metricDto, LiveMeasureDto measureDto) { Double value = measureDto.getValue(); Double variation = measureDto.getVariation(); updateMeasureBuilder(measureBuilder, metricDto, value == null ? Double.NaN : value, measureDto.getDataAsString(), variation == null ? Double.NaN : variation); }
private static void setQualityGate(ProjectPullRequests.PullRequest.Builder builder, @Nullable LiveMeasureDto qualityGateMeasure, @Nullable BranchStatistics branchStatistics) { ProjectPullRequests.Status.Builder statusBuilder = ProjectPullRequests.Status.newBuilder(); if (qualityGateMeasure != null) { ofNullable(qualityGateMeasure.getDataAsString()).ifPresent(statusBuilder::setQualityGateStatus); } statusBuilder.setBugs(branchStatistics == null ? 0L : branchStatistics.getBugs()); statusBuilder.setVulnerabilities(branchStatistics == null ? 0L : branchStatistics.getVulnerabilities()); statusBuilder.setCodeSmells(branchStatistics == null ? 0L : branchStatistics.getCodeSmells()); builder.setStatus(statusBuilder); } }
private static void setBranchStatus(ProjectBranches.Branch.Builder builder, BranchDto branch, @Nullable LiveMeasureDto qualityGateMeasure, @Nullable BranchStatistics branchStatistics) { ProjectBranches.Status.Builder statusBuilder = ProjectBranches.Status.newBuilder(); if (qualityGateMeasure != null) { ofNullable(qualityGateMeasure.getDataAsString()).ifPresent(statusBuilder::setQualityGateStatus); } if (branch.getBranchType() == BranchType.SHORT) { statusBuilder.setBugs(branchStatistics == null ? 0L : branchStatistics.getBugs()); statusBuilder.setVulnerabilities(branchStatistics == null ? 0L : branchStatistics.getVulnerabilities()); statusBuilder.setCodeSmells(branchStatistics == null ? 0L : branchStatistics.getCodeSmells()); } builder.setStatus(statusBuilder); }
private Measure(LiveMeasureDto measureDto) { this.value = toPrimitive(measureDto.getValue()); this.data = measureDto.getDataAsString(); this.variation = toPrimitive(measureDto.getVariation()); }
@Test public void setValue_String_does_nothing_if_value_is_not_changed() { LiveMeasureDto measure = newMeasure(METRIC_1, PROJECT).setData("foo"); MeasureMatrix underTest = new MeasureMatrix(asList(PROJECT, FILE), asList(METRIC_1), asList(measure)); underTest.setValue(PROJECT, METRIC_1.getKey(), "foo"); assertThat(underTest.getMeasure(PROJECT, METRIC_1.getKey()).get().getDataAsString()).isEqualTo("foo"); assertThat(underTest.getChanged()).isEmpty(); }
public Optional<Measure> toMeasure(@Nullable LiveMeasureDto measureDto, Metric metric) { requireNonNull(metric); if (measureDto == null) { return Optional.empty(); } Double value = measureDto.getValue(); String data = measureDto.getDataAsString(); switch (metric.getType().getValueType()) { case INT: return toIntegerMeasure(measureDto, value, data); case LONG: return toLongMeasure(measureDto, value, data); case DOUBLE: return toDoubleMeasure(measureDto, value, data); case BOOLEAN: return toBooleanMeasure(measureDto, value, data); case STRING: return toStringMeasure(measureDto, data); case LEVEL: return toLevelMeasure(measureDto, data); case NO_VALUE: return toNoValueMeasure(measureDto); default: throw new IllegalArgumentException("Unsupported Measure.ValueType " + metric.getType().getValueType()); } }
@Test public void setValue_String_updates_value() { LiveMeasureDto measure = newMeasure(METRIC_1, PROJECT).setData("foo"); MeasureMatrix underTest = new MeasureMatrix(asList(PROJECT, FILE), asList(METRIC_1), asList(measure)); underTest.setValue(PROJECT, METRIC_1.getKey(), "bar"); assertThat(underTest.getMeasure(PROJECT, METRIC_1.getKey()).get().getDataAsString()).isEqualTo("bar"); assertThat(underTest.getChanged()).extracting(LiveMeasureDto::getDataAsString).containsExactly("bar"); }
private LiveMeasureDto assertThatRatingMeasureHasValue(ComponentDto component, Rating expectedRating) { LiveMeasureDto measure = db.getDbClient().liveMeasureDao().selectMeasure(db.getSession(), component.uuid(), ratingMetric.getKey()).get(); assertThat(measure.getComponentUuid()).isEqualTo(component.uuid()); assertThat(measure.getProjectUuid()).isEqualTo(component.projectUuid()); assertThat(measure.getMetricId()).isEqualTo(ratingMetric.getId()); assertThat(measure.getValue()).isEqualTo(expectedRating.getIndex()); assertThat(measure.getDataAsString()).isEqualTo(expectedRating.name()); return measure; }
@Test public void persist_live_measures_of_portfolio_analysis() { preparePortfolio(); // the computed measures measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().create("view-value")); measureRepository.addRawMeasure(REF_2, STRING_METRIC.getKey(), newMeasureBuilder().create("subview-value")); measureRepository.addRawMeasure(REF_3, STRING_METRIC.getKey(), newMeasureBuilder().create("project-value")); TestComputationStepContext context = new TestComputationStepContext(); step().execute(context); assertThat(db.countRowsOfTable("live_measures")).isEqualTo(3); assertThat(selectMeasure("view-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("view-value"); assertThat(selectMeasure("subview-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("subview-value"); assertThat(selectMeasure("project-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("project-value"); verifyStatistics(context, 3); }
@Test public void persist_live_measures_of_project_analysis() { prepareProject(); // the computed measures measureRepository.addRawMeasure(REF_1, STRING_METRIC.getKey(), newMeasureBuilder().create("project-value")); measureRepository.addRawMeasure(REF_3, STRING_METRIC.getKey(), newMeasureBuilder().create("dir-value")); measureRepository.addRawMeasure(REF_4, STRING_METRIC.getKey(), newMeasureBuilder().create("file-value")); TestComputationStepContext context = new TestComputationStepContext(); step().execute(context); // all measures are persisted, from project to file assertThat(db.countRowsOfTable("live_measures")).isEqualTo(3); assertThat(selectMeasure("project-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("project-value"); assertThat(selectMeasure("dir-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("dir-value"); assertThat(selectMeasure("file-uuid", STRING_METRIC).get().getDataAsString()).isEqualTo("file-value"); verifyStatistics(context, 3); }
@Test public void insert_data() { byte[] data = "text_value".getBytes(StandardCharsets.UTF_8); MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); LiveMeasureDto measure = newLiveMeasure(file, metric).setData(data); underTest.insert(db.getSession(), measure); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(new String(result.getData(), StandardCharsets.UTF_8)).isEqualTo("text_value"); assertThat(result.getDataAsString()).isEqualTo("text_value"); }
@Test public void refreshGateStatus_generates_gate_related_measures() { ComponentDto project = ComponentTesting.newPublicProjectDto(newOrganizationDto()); MetricDto conditionMetric = newMetricDto(); MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY); MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY); Condition condition = new Condition(conditionMetric.getKey(), Condition.Operator.GREATER_THAN, "10"); QualityGate gate = new QualityGate("1", "foo", ImmutableSet.of(condition)); MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(conditionMetric, statusMetric, detailsMetric), emptyList()); EvaluatedQualityGate result = underTest.refreshGateStatus(project, gate, matrix); QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures(); assertThat(measures.get(conditionMetric.getKey())).isEmpty(); assertThat(result.getStatus()).isEqualTo(Metric.Level.OK); assertThat(result.getEvaluatedConditions()) .extracting(EvaluatedCondition::getStatus) .containsExactly(EvaluatedCondition.EvaluationStatus.OK); assertThat(matrix.getMeasure(project, CoreMetrics.ALERT_STATUS_KEY).get().getDataAsString()).isEqualTo(Metric.Level.OK.name()); assertThat(matrix.getMeasure(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY).get().getDataAsString()) .isNotEmpty() // json format .startsWith("{").endsWith("}"); }
@Test public void selectByComponentUuidsAndMetricKeys() { LiveMeasureDto measure1 = newLiveMeasure().setMetricId(metric.getId()); LiveMeasureDto measure2 = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure1); underTest.insert(db.getSession(), measure2); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), asList(measure1.getComponentUuid(), measure2.getComponentUuid()), singletonList(metric.getKey())); assertThat(selected) .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString) .containsExactlyInAnyOrder( tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricId(), measure1.getValue(), measure1.getDataAsString()), tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricId(), measure2.getValue(), measure2.getDataAsString())); assertThat(underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), emptyList(), singletonList(metric.getKey()))).isEmpty(); assertThat(underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList(measure1.getComponentUuid()), emptyList())).isEmpty(); }
@Test public void selectByComponentUuidsAndMetricIds() { LiveMeasureDto measure1 = newLiveMeasure().setMetricId(metric.getId()); LiveMeasureDto measure2 = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure1); underTest.insert(db.getSession(), measure2); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), asList(measure1.getComponentUuid(), measure2.getComponentUuid()), singletonList(metric.getId())); assertThat(selected) .extracting(LiveMeasureDto::getComponentUuid, LiveMeasureDto::getProjectUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getDataAsString) .containsExactlyInAnyOrder( tuple(measure1.getComponentUuid(), measure1.getProjectUuid(), measure1.getMetricId(), measure1.getValue(), measure1.getDataAsString()), tuple(measure2.getComponentUuid(), measure2.getProjectUuid(), measure2.getMetricId(), measure2.getValue(), measure2.getDataAsString())); assertThat(underTest.selectByComponentUuidsAndMetricIds(db.getSession(), emptyList(), singletonList(metric.getId()))).isEmpty(); assertThat(underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList(measure1.getComponentUuid()), emptyList())).isEmpty(); }
@Test public void upsert_inserts_or_updates_row() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } // insert LiveMeasureDto dto = newLiveMeasure(); int count = underTest.upsert(db.getSession(), dto); verifyPersisted(dto); verifyTableSize(1); assertThat(count).isEqualTo(1); // update dto.setValue(dto.getValue() + 1); dto.setVariation(dto.getVariation() + 10); dto.setData(dto.getDataAsString() + "_new"); count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void insertOrUpdate() { // insert LiveMeasureDto dto = newLiveMeasure(); underTest.insertOrUpdate(db.getSession(), dto); verifyPersisted(dto); verifyTableSize(1); // update dto.setValue(dto.getValue() + 1); dto.setVariation(dto.getVariation() + 10); dto.setData(dto.getDataAsString() + "_new"); underTest.insertOrUpdate(db.getSession(), dto); verifyPersisted(dto); verifyTableSize(1); }
public static String formatMeasureValue(LiveMeasureDto measure, MetricDto metric) { Double doubleValue = measure.getValue(); String stringValue = measure.getDataAsString(); return formatMeasureValue(doubleValue == null ? Double.NaN : doubleValue, stringValue, metric); }
static void updateMeasureBuilder(Measure.Builder measureBuilder, MetricDto metricDto, LiveMeasureDto measureDto) { Double value = measureDto.getValue(); Double variation = measureDto.getVariation(); updateMeasureBuilder(measureBuilder, metricDto, value == null ? Double.NaN : value, measureDto.getDataAsString(), variation == null ? Double.NaN : variation); }