@DataProvider public static Object[][] all_types_LiveMeasureDtos() { return new Object[][] { {new LiveMeasureDto().setValue(1d), SOME_BOOLEAN_METRIC}, {new LiveMeasureDto().setValue(1d), SOME_INT_METRIC}, {new LiveMeasureDto().setValue(1d), SOME_LONG_METRIC}, {new LiveMeasureDto().setValue(1d), SOME_DOUBLE_METRIC}, {new LiveMeasureDto().setData("1"), SOME_STRING_METRIC}, {new LiveMeasureDto().setData(Level.OK.name()), SOME_LEVEL_METRIC} }; }
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; }
@Test public void toMeasure_returns_false_value_if_dto_has_invalid_value_for_Boolean_metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setValue(1.987d), SOME_BOOLEAN_METRIC); assertThat(measure.isPresent()).isTrue(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.BOOLEAN); assertThat(measure.get().getBooleanValue()).isFalse(); }
@Test public void toMeasure_returns_int_part_of_value_in_dto_for_Int_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setValue(1.5d), SOME_INT_METRIC); assertThat(measure.isPresent()).isTrue(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.INT); assertThat(measure.get().getIntValue()).isEqualTo(1); }
@Test public void toMeasure_returns_long_part_of_value_in_dto_for_Long_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setValue(1.5d), SOME_LONG_METRIC); assertThat(measure.isPresent()).isTrue(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LONG); assertThat(measure.get().getLongValue()).isEqualTo(1); }
private void changeCell(ComponentDto component, String metricKey, Function<LiveMeasureDto, Boolean> changer) { MeasureCell cell = table.get(component.uuid(), metricKey); if (cell == null) { LiveMeasureDto measure = new LiveMeasureDto() .setComponentUuid(component.uuid()) .setProjectUuid(component.projectUuid()) .setMetricId(metricsByKeys.get(metricKey).getId()); cell = new MeasureCell(measure, true); table.put(component.uuid(), metricKey, cell); changer.apply(cell.getMeasure()); } else if (changer.apply(cell.getMeasure())) { cell.setChanged(true); } }
private void verify_toMeasure_returns_value_for_LEVEL_Metric(Level expectedLevel) { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData(expectedLevel.name()), SOME_LEVEL_METRIC); assertThat(measure).isPresent(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.LEVEL); assertThat(measure.get().getLevelValue()).isEqualTo(expectedLevel); }
@Test public void toMeasure_returns_no_value_if_dta_has_data_in_wrong_case_for_Level_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("waRn"), SOME_LEVEL_METRIC); assertThat(measure).isPresent(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE); }
@Test public void toMeasure_returns_no_QualityGateStatus_if_alertStatus_has_invalid_data_for_Level_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("trololo"), SOME_STRING_METRIC); assertThat(measure).isPresent(); assertThat(measure.get().hasQualityGateStatus()).isFalse(); }
@Test public void toMeasure_returns_no_QualityGateStatus_if_alertStatus_has_data_in_wrong_case_for_Level_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("waRn"), SOME_STRING_METRIC); assertThat(measure).isPresent(); assertThat(measure.get().hasQualityGateStatus()).isFalse(); }
@Test public void toMeasure_returns_no_value_if_dto_has_invalid_data_for_Level_Metric() { Optional<Measure> measure = underTest.toMeasure(new LiveMeasureDto().setData("trololo"), SOME_LEVEL_METRIC); assertThat(measure).isPresent(); assertThat(measure.get().getValueType()).isEqualTo(Measure.ValueType.NO_VALUE); }
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 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 toMeasure_should_not_loose_decimals_of_float_values() { MetricImpl metric = new MetricImpl(42, "double", "name", Metric.MetricType.FLOAT, 5, null, false); LiveMeasureDto LiveMeasureDto = new LiveMeasureDto() .setValue(0.12345); Optional<Measure> measure = underTest.toMeasure(LiveMeasureDto, metric); assertThat(measure.get().getDoubleValue()).isEqualTo(0.12345, Offset.offset(0.000001)); } }
@Test public void sort_by_alert_status_ascending() { components = newArrayList( newComponentWithoutSnapshotId("PROJECT OK 1", Qualifiers.PROJECT, "PROJECT_OK_PATH_1"), newComponentWithoutSnapshotId("PROJECT ERROR 1", Qualifiers.PROJECT, "PROJECT_ERROR_PATH_1"), newComponentWithoutSnapshotId("PROJECT OK 2", Qualifiers.PROJECT, "PROJECT_OK_PATH_2"), newComponentWithoutSnapshotId("PROJECT ERROR 2", Qualifiers.PROJECT, "PROJECT_ERROR_PATH_2")); metrics = singletonList(newMetricDto() .setKey(CoreMetrics.ALERT_STATUS_KEY) .setValueType(ValueType.LEVEL.name())); measuresByComponentUuidAndMetric = HashBasedTable.create(); List<String> statuses = newArrayList("OK", "ERROR"); for (int i = 0; i < components.size(); i++) { ComponentDto component = components.get(i); String alertStatus = statuses.get(i % 2); measuresByComponentUuidAndMetric.put(component.uuid(), metrics.get(0), createFromMeasureDto(new LiveMeasureDto().setData(alertStatus))); } ComponentTreeRequest wsRequest = newRequest(newArrayList(METRIC_SORT, NAME_SORT), true, CoreMetrics.ALERT_STATUS_KEY); List<ComponentDto> result = sortComponents(wsRequest); assertThat(result).extracting("name").containsExactly( "PROJECT ERROR 1", "PROJECT ERROR 2", "PROJECT OK 1", "PROJECT OK 2"); }
private LiveMeasureDto newMeasure(MetricDto metric, ComponentDto component) { return new LiveMeasureDto().setMetricId(metric.getId()).setData("foo").setComponentUuid(component.uuid()); } }
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 refreshGateStatus_provides_measures_to_evaluator() { ComponentDto project = ComponentTesting.newPublicProjectDto(newOrganizationDto()); MetricDto numericMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name()); MetricDto numericNewMetric = newMetricDto().setValueType(Metric.ValueType.FLOAT.name()).setKey("new_metric"); MetricDto stringMetric = newMetricDto().setValueType(Metric.ValueType.STRING.name()); MetricDto statusMetric = newMetricDto().setKey(CoreMetrics.ALERT_STATUS_KEY); MetricDto detailsMetric = newMetricDto().setKey(CoreMetrics.QUALITY_GATE_DETAILS_KEY); QualityGate gate = new QualityGate("1", "foo", Collections.emptySet()); LiveMeasureDto numericMeasure = new LiveMeasureDto().setMetricId(numericMetric.getId()).setValue(1.23).setVariation(4.56).setComponentUuid(project.uuid()); LiveMeasureDto numericNewMeasure = new LiveMeasureDto().setMetricId(numericNewMetric.getId()).setValue(7.8).setVariation(8.9).setComponentUuid(project.uuid()); LiveMeasureDto stringMeasure = new LiveMeasureDto().setMetricId(stringMetric.getId()).setData("bar").setComponentUuid(project.uuid()); MeasureMatrix matrix = new MeasureMatrix(singleton(project), asList(statusMetric, detailsMetric, numericMetric, numericNewMetric, stringMetric), asList(numericMeasure, numericNewMeasure, stringMeasure)); underTest.refreshGateStatus(project, gate, matrix); QualityGateEvaluator.Measures measures = qualityGateEvaluator.getCalledMeasures(); QualityGateEvaluator.Measure loadedStringMeasure = measures.get(stringMetric.getKey()).get(); assertThat(loadedStringMeasure.getStringValue()).hasValue("bar"); assertThat(loadedStringMeasure.getValue()).isEmpty(); assertThat(loadedStringMeasure.getType()).isEqualTo(Metric.ValueType.STRING); QualityGateEvaluator.Measure loadedNumericMeasure = measures.get(numericMetric.getKey()).get(); assertThat(loadedNumericMeasure.getStringValue()).isEmpty(); assertThat(loadedNumericMeasure.getValue()).hasValue(1.23); assertThat(loadedNumericMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT); QualityGateEvaluator.Measure loadedNumericNewMeasure = measures.get(numericNewMetric.getKey()).get(); assertThat(loadedNumericNewMeasure.getStringValue()).isEmpty(); assertThat(loadedNumericNewMeasure.getNewMetricValue()).hasValue(8.9); assertThat(loadedNumericNewMeasure.getType()).isEqualTo(Metric.ValueType.FLOAT); }
@Before public void setUp() { components = newArrayList( newComponentWithoutSnapshotId("name-1", "qualifier-2", "path-9"), newComponentWithoutSnapshotId("name-3", "qualifier-3", "path-8"), newComponentWithoutSnapshotId("name-2", "qualifier-4", "path-7"), newComponentWithoutSnapshotId("name-4", "qualifier-5", "path-6"), newComponentWithoutSnapshotId("name-7", "qualifier-6", "path-5"), newComponentWithoutSnapshotId("name-6", "qualifier-7", "path-4"), newComponentWithoutSnapshotId("name-5", "qualifier-8", "path-3"), newComponentWithoutSnapshotId("name-9", "qualifier-9", "path-2"), newComponentWithoutSnapshotId("name-8", "qualifier-1", "path-1")); MetricDto violationsMetric = newMetricDto() .setKey(NUM_METRIC_KEY) .setValueType(ValueType.INT.name()); MetricDto sqaleIndexMetric = newMetricDto() .setKey(TEXT_METRIC_KEY) .setValueType(ValueType.STRING.name()); metrics = newArrayList(violationsMetric, sqaleIndexMetric); measuresByComponentUuidAndMetric = HashBasedTable.create(components.size(), 2); // same number than path field double currentValue = 9; for (ComponentDto component : components) { measuresByComponentUuidAndMetric.put(component.uuid(), violationsMetric, createFromMeasureDto(new LiveMeasureDto().setValue(currentValue) .setVariation(-currentValue))); measuresByComponentUuidAndMetric.put(component.uuid(), sqaleIndexMetric, createFromMeasureDto(new LiveMeasureDto().setData(String.valueOf(currentValue)))); currentValue--; } }