import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.util.Map;
-import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
+import javax.annotation.Nullable;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.MeasureDto;
return response -> {
result.getMetrics().stream()
- .peek(metric -> measure.clear())
+ .map(clearMetric())
.map(addMetric())
.map(metric -> addValues(measuresByMetricByAnalysis.row(metric)).apply(metric))
.forEach(metric -> response.addMeasures(measure));
}
private UnaryOperator<MetricDto> addValues(Map<SnapshotDto, MeasureDto> measuresByAnalysis) {
- Predicate<SnapshotDto> hasMeasure = analysis -> measuresByAnalysis.get(analysis) != null;
return metric -> {
result.getAnalyses().stream()
- .filter(hasMeasure)
- .peek(analysis -> value.clear())
+ .map(clearValue())
.map(addDate())
- .map(analysis -> addValue(metric, measuresByAnalysis.get(analysis)).apply(analysis))
+ .map(analysis -> addValue(analysis, metric, measuresByAnalysis.get(analysis)))
.forEach(analysis -> measure.addHistory(value));
return metric;
};
}
- private UnaryOperator<SnapshotDto> addValue(MetricDto dbMetric, MeasureDto dbMeasure) {
- return analysis -> {
+ private SnapshotDto addValue(SnapshotDto analysis, MetricDto dbMetric, @Nullable MeasureDto dbMeasure) {
+ if (dbMeasure != null) {
String measureValue = dbMetric.getKey().startsWith("new_")
? formatNumericalValue(dbMeasure.getVariation(), dbMetric)
: formatMeasureValue(dbMeasure, dbMetric);
value.setValue(measureValue);
+ }
+
+ return analysis;
+ }
+
+ private UnaryOperator<MetricDto> clearMetric() {
+ return metric -> {
+ measure.clear();
+ return metric;
+ };
+ }
+
+ private UnaryOperator<SnapshotDto> clearValue() {
+ return analysis -> {
+ value.clear();
return analysis;
};
}
.containsExactly(1, 100, 0);
}
+ @Test
+ public void analyses_but_no_measure() {
+ project = db.components().insertPrivateProject();
+ analysis = db.components().insertSnapshot(project);
+ userSession.addProjectPermission(UserRole.USER, project);
+ wsRequest
+ .setComponent(project.getKey())
+ .setMetrics(singletonList(complexityMetric.getKey()));
+
+ SearchHistoryResponse result = call();
+
+ assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 1);
+ assertThat(result.getMeasuresList()).hasSize(1);
+ assertThat(result.getMeasures(0).getHistoryList()).extracting(HistoryValue::hasDate, HistoryValue::hasValue).containsExactly(tuple(true, false));
+ }
+
@Test
public void return_metrics() {
dbClient.measureDao().insert(dbSession, newMeasureDto(complexityMetric, project, analysis).setValue(42.0d));
// ncloc measures
HistoryMeasure nclocMeasures = result.getMeasures(1);
assertThat(nclocMeasures.getMetric()).isEqualTo(nclocMetric.getKey());
- assertThat(nclocMeasures.getHistoryList()).extracting(HistoryValue::getDate, HistoryValue::getValue)
- .containsExactly(tuple(analysisDate, "201"));
+ assertThat(nclocMeasures.getHistoryList()).extracting(HistoryValue::getDate, HistoryValue::getValue, HistoryValue::hasValue).containsExactly(
+ tuple(analysisDate, "201", true), tuple(laterAnalysisDate, "", false));
// new_violation measures
HistoryMeasure newViolationMeasures = result.getMeasures(2);
assertThat(newViolationMeasures.getMetric()).isEqualTo(newViolationMetric.getKey());
// Best value is not applied to project
wsRequest.setComponent(project.getKey());
result = call();
- assertThat(result.getMeasuresList().get(0).getHistoryCount()).isEqualTo(0);
+ assertThat(result.getMeasuresList().get(0).getHistoryCount()).isEqualTo(1);
+ assertThat(result.getMeasuresList().get(0).getHistory(0).hasDate()).isTrue();
+ assertThat(result.getMeasuresList().get(0).getHistory(0).hasValue()).isFalse();
}
@Test
SearchHistoryResponse result = call();
assertThat(result.getMeasuresCount()).isEqualTo(1);
- assertThat(result.getMeasures(0).getHistoryCount()).isEqualTo(0);
+ assertThat(result.getMeasures(0).getHistoryCount()).isEqualTo(1);
+ assertThat(result.getMeasures(0).getHistory(0).hasDate()).isTrue();
+ assertThat(result.getMeasures(0).getHistory(0).hasValue()).isFalse();
}
@Test