]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9440 All analyses are returned in WS api/measures/search_history even if there...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 20 Jun 2017 09:28:54 +0000 (11:28 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Tue, 4 Jul 2017 12:15:34 +0000 (14:15 +0200)
server/sonar-server/src/main/java/org/sonar/server/measure/ws/SearchHistoryResponseFactory.java
server/sonar-server/src/test/java/org/sonar/server/measure/ws/SearchHistoryActionTest.java

index 3169971a3627fedf37f3478d02b1ab536a24e971..187e899508d9c4272c68ca394c54cb38841eac95 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.server.measure.ws;
 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;
@@ -68,7 +68,7 @@ class SearchHistoryResponseFactory {
 
     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));
@@ -85,13 +85,11 @@ class SearchHistoryResponseFactory {
   }
 
   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;
@@ -105,12 +103,27 @@ class SearchHistoryResponseFactory {
     };
   }
 
-  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;
     };
   }
index 696527466df5b5ba77f604cb9d97da9716702242..0f3917642f51572c4b9be3f5198a663871bba2c5 100644 (file)
@@ -121,6 +121,22 @@ public class SearchHistoryActionTest {
       .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));
@@ -162,8 +178,8 @@ public class SearchHistoryActionTest {
     // 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());
@@ -226,7 +242,9 @@ public class SearchHistoryActionTest {
     // 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
@@ -249,7 +267,9 @@ public class SearchHistoryActionTest {
     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