void setLeakValue(ComponentDto component, String metricKey, double variation) { changeCell(component, metricKey, c -> { double newVariation = scale(getMetric(metricKey), variation); if (c.getVariation() != null && Double.compare(c.getVariation(), newVariation) == 0) { return false; } MetricDto metric = metricsByKeys.get(metricKey); c.setVariation(scale(metric, variation)); return true; }); }
MetricDtoWithBestValue(MetricDto metric) { this.metric = metric; LiveMeasureDto measure = new LiveMeasureDto().setMetricId(metric.getId()); boolean isNewTypeMetric = metric.getKey().toLowerCase(Locale.ENGLISH).startsWith(LOWER_CASE_NEW_METRIC_PREFIX); if (isNewTypeMetric) { measure.setVariation(metric.getBestValue()); } else { measure.setValue(metric.getBestValue()); } this.bestValue = measure; }
public static LiveMeasureDto newLiveMeasure() { return new LiveMeasureDto() .setMetricId(cursor++) .setComponentUuid(String.valueOf(cursor++)) .setProjectUuid(String.valueOf(cursor++)) .setData(String.valueOf(cursor++)) .setValue((double) cursor++) .setVariation((double) cursor++); }
@Test @UseDataProvider("all_types_LiveMeasureDtos") public void toMeasure_creates_MeasureVariation_and_maps_the_right_one(LiveMeasureDto builder, Metric metric) { assertThat(underTest.toMeasure(builder.setVariation(1d), metric).get().getVariation()).isEqualTo(1); }
@Test public void toMeasure_creates_MeasureVariation_and_maps_the_right_one() { LiveMeasureDto LiveMeasureDto = new LiveMeasureDto() .setData("1") .setVariation(2d); Optional<Measure> measure = underTest.toMeasure(LiveMeasureDto, SOME_STRING_METRIC); assertThat(measure.get().getVariation()).isEqualTo(2); }
@Test public void setLeakValue_double_does_nothing_if_value_is_unchanged() { MetricDto metric = newMetricDto().setDecimalScale(2); LiveMeasureDto measure = newMeasure(metric, PROJECT).setValue(null).setVariation(3.14); MeasureMatrix underTest = new MeasureMatrix(asList(PROJECT), asList(metric), asList(measure)); underTest.setLeakValue(PROJECT, metric.getKey(), 3.14159); assertThat(underTest.getChanged()).isEmpty(); verifyVariation(underTest, PROJECT, metric, 3.14); }
@Test public void setValue_double_updates_variation() { MetricDto metric = newMetricDto().setDecimalScale(2); LiveMeasureDto measure = newMeasure(metric, PROJECT).setValue(3.14).setVariation(1.14); MeasureMatrix underTest = new MeasureMatrix(asList(PROJECT), asList(metric), asList(measure)); underTest.setValue(PROJECT, metric.getKey(), 3.56); assertThat(underTest.getChanged()).hasSize(1); verifyValue(underTest, PROJECT, metric, 3.56); verifyVariation(underTest, PROJECT, metric, 3.56 - (3.14 - 1.14)); }
@Test public void setValue_double_rounds_up_variation() { MetricDto metric = newMetricDto().setDecimalScale(2); LiveMeasureDto measure = newMeasure(metric, PROJECT).setValue(3.14).setVariation(1.14); MeasureMatrix underTest = new MeasureMatrix(asList(PROJECT), asList(metric), asList(measure)); underTest.setValue(PROJECT, metric.getKey(), 3.569); assertThat(underTest.getChanged()).hasSize(1); verifyValue(underTest, PROJECT, metric, 3.57); verifyVariation(underTest, PROJECT, metric, 1.57); }
@Test public void upsert_updates_row_if_variation_is_changed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setVariation(40.0); underTest.upsert(db.getSession(), dto); // update dto.setVariation(50.0); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void upsert_updates_row_if_variation_is_removed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure().setVariation(40.0); underTest.upsert(db.getSession(), dto); // update dto.setVariation(null); int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(1); verifyPersisted(dto); verifyTableSize(1); }
@Test public void filter_projects_by_new_coverage() { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto coverage = db.measures().insertMetric(c -> c.setKey(NEW_COVERAGE).setValueType("PERCENT")); ComponentDto project1 = insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(80d))); ComponentDto project2 = insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(85d))); ComponentDto project3 = insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(10d))); SearchProjectsWsResponse result = call(request.setFilter("new_coverage <= 80")); assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getDbKey(), project3.getDbKey()); }
@Test public void filter_projects_by_new_duplications() { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto newDuplications = db.measures().insertMetric(c -> c.setKey(NEW_DUPLICATED_LINES_DENSITY_KEY).setValueType("PERCENT")); ComponentDto project1 = insertProject(organizationDto, new Measure(newDuplications, c -> c.setVariation(80d))); ComponentDto project2 = insertProject(organizationDto, new Measure(newDuplications, c -> c.setVariation(85d))); ComponentDto project3 = insertProject(organizationDto, new Measure(newDuplications, c -> c.setVariation(10d))); SearchProjectsWsResponse result = call(request.setFilter("new_duplicated_lines_density <= 80")); assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getDbKey(), project3.getDbKey()); }
public LiveMeasureDto toLiveMeasureDto(Measure measure, Metric metric, Component component) { LiveMeasureDto out = new LiveMeasureDto(); out.setMetricId(metric.getId()); out.setComponentUuid(component.getUuid()); out.setProjectUuid(treeRootHolder.getRoot().getUuid()); if (measure.hasVariation()) { out.setVariation(measure.getVariation()); } out.setValue(valueAsDouble(measure)); out.setData(data(measure)); return out; }
public static LiveMeasureDto newLiveMeasure(ComponentDto component, MetricDto metric) { return new LiveMeasureDto() .setMetricId(metric.getId()) .setComponentUuid(component.uuid()) .setProjectUuid(component.projectUuid()) .setData(String.valueOf(cursor++)) .setValue((double) cursor++) .setVariation((double) cursor++); } }
@Test public void filter_projects_by_new_lines() { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto newLines = db.measures().insertMetric(c -> c.setKey(NEW_LINES_KEY).setValueType(INT.name())); ComponentDto project1 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(80d))); ComponentDto project2 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(85d))); ComponentDto project3 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(10d))); SearchProjectsWsResponse result = call(request.setFilter("new_lines <= 80")); assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getDbKey(), project3.getDbKey()); }
@Test @UseDataProvider("new_rating_metric_keys") public void filter_projects_by_new_rating(String newMetricKey) { userSession.logIn(); OrganizationDto organizationDto = db.organizations().insert(); MetricDto ratingMetric = db.measures().insertMetric(c -> c.setKey(newMetricKey).setValueType(INT.name())); ComponentDto project1 = insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(1d))); ComponentDto project2 = insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(2d))); ComponentDto project3 = insertProject(organizationDto, new Measure(ratingMetric, c -> c.setVariation(3d))); SearchProjectsWsResponse result = call(request.setFilter(newMetricKey + " = 2")); assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactly(project2.getDbKey()); }
@Test @UseDataProvider("metricLevels") public void event_contains_previousStatus_if_measure_exists(Metric.Level level) { markProjectAsAnalyzed(project); db.measures().insertLiveMeasure(project, alertStatusMetric, m -> m.setData(level.name())); db.measures().insertLiveMeasure(project, intMetric, m -> m.setVariation(42.0).setValue(null)); List<QGChangeEvent> result = run(file1, newQualifierBasedIntLeakFormula()); assertThat(result) .extracting(QGChangeEvent::getPreviousStatus) .containsExactly(Optional.of(level)); }
@Test public void selectMeasure_map_fields() { MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14).setVariation(0.1).setData("text_value")); LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found")); assertThat(result).as("Fail to map fields of %s", result.toString()).extracting( LiveMeasureDto::getProjectUuid, LiveMeasureDto::getComponentUuid, LiveMeasureDto::getMetricId, LiveMeasureDto::getValue, LiveMeasureDto::getVariation, LiveMeasureDto::getDataAsString, LiveMeasureDto::getTextValue) .contains(project.uuid(), file.uuid(), metric.getId(), 3.14, 0.1, "text_value", "text_value"); }
@Test public void does_not_fail_when_quality_gate_has_no_value() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(LEVEL.name()).setKey("alert_status")); dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(null).setVariation(null).setData((String) null)); Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); assertThat(docsById.get(project.uuid()).getMeasures().getNumericMeasures()).isEmpty(); }
@Test public void return_project_measure_having_leak() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = dbTester.components().insertPrivateProject(organization, p -> p.setDbKey("Project-Key").setName("Project Name").setTagsString("platform,java")); MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setKey("new_lines")); dbTester.measures().insertLiveMeasure(project, metric, m -> m.setVariation(10d)); Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById(); assertThat(docsById.get(project.uuid()).getMeasures().getNumericMeasures()).containsOnly(entry("new_lines", 10d)); }