]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9564 WS api/components/measures/component and component_tree do not fail with...
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 15 Aug 2017 14:43:06 +0000 (16:43 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Wed, 16 Aug 2017 08:07:35 +0000 (10:07 +0200)
server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java
server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java

index 2de2881b4343ee3f3747cbbcfddbb070382caa7f..42c79c8413bf3800202faf0a55ac355395d691df 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.measure.ws;
 import org.sonar.db.metric.MetricDto;
 import org.sonarqube.ws.Common.Metric;
 
+import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue;
 
 class MetricDtoToWsMetric {
@@ -34,25 +35,17 @@ class MetricDtoToWsMetric {
     metric.setKey(metricDto.getKey());
     metric.setType(metricDto.getValueType());
     metric.setName(metricDto.getShortName());
-    if (metricDto.getDescription() != null) {
-      metric.setDescription(metricDto.getDescription());
-    }
-    metric.setDomain(metricDto.getDomain());
+    setNullable(metricDto.getDescription(), metric::setDescription);
+    setNullable(metricDto.getDomain(), metric::setDomain);
     if (metricDto.getDirection() != 0) {
       metric.setHigherValuesAreBetter(metricDto.getDirection() > 0);
     }
     metric.setQualitative(metricDto.isQualitative());
     metric.setHidden(metricDto.isHidden());
     metric.setCustom(metricDto.isUserManaged());
-    if (metricDto.getDecimalScale() != null) {
-      metric.setDecimalScale(metricDto.getDecimalScale());
-    }
-    if (metricDto.getBestValue() != null) {
-      metric.setBestValue(formatNumericalValue(metricDto.getBestValue(), metricDto));
-    }
-    if (metricDto.getWorstValue() != null) {
-      metric.setWorstValue(formatNumericalValue(metricDto.getWorstValue(), metricDto));
-    }
+    setNullable(metricDto.getDecimalScale(), metric::setDecimalScale);
+    setNullable(metricDto.getBestValue(), bv -> metric.setBestValue(formatNumericalValue(bv, metricDto)));
+    setNullable(metricDto.getWorstValue(), wv -> metric.setWorstValue(formatNumericalValue(wv, metricDto)));
 
     return metric.build();
   }
index 5cfa199ddc24849ebe1c375d4a12e06c57a47a38..b58bebd4727250dfe21d1b6392e9d8ab7ec22c06 100644 (file)
@@ -23,8 +23,10 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.measures.Metric.ValueType;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -40,6 +42,7 @@ import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.Common;
 import org.sonarqube.ws.WsMeasures.ComponentWsResponse;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -49,6 +52,7 @@ import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
 import static org.sonar.db.component.ComponentTesting.newProjectCopy;
 import static org.sonar.db.component.ComponentTesting.newView;
+import static org.sonar.db.component.SnapshotTesting.newAnalysis;
 import static org.sonar.db.measure.MeasureTesting.newMeasureDto;
 import static org.sonar.db.metric.MetricTesting.newMetricDto;
 import static org.sonar.test.JsonAssert.assertJson;
@@ -80,11 +84,11 @@ public class ComponentActionTest {
   }
 
   @Test
-  public void test_definition_of_web_service() {
+  public void definition() {
     WebService.Action def = ws.getDef();
 
     assertThat(def.since()).isEqualTo("5.4");
-    assertThat(def.params()).extracting(WebService.Param::key)
+    assertThat(def.params()).extracting(Param::key)
       .containsExactlyInAnyOrder("componentId", "componentKey", "metricKeys", "additionalFields", "developerId", "developerKey");
     assertThat(def.param("developerId").deprecatedSince()).isEqualTo("6.4");
     assertThat(def.param("developerKey").deprecatedSince()).isEqualTo("6.4");
@@ -148,6 +152,27 @@ public class ComponentActionTest {
     assertThat(response.getComponent().getRefKey()).isEqualTo("project-key");
   }
 
+  @Test
+  public void metric_without_a_domain() {
+    ComponentDto project = db.components().insertPrivateProject();
+    SnapshotDto analysis = db.getDbClient().snapshotDao().insert(dbSession, newAnalysis(project));
+    MetricDto metricWithoutDomain = db.measureDbTester().insertMetric(m -> m
+      .setValueType(ValueType.INT.name())
+      .setDomain(null));
+    db.measureDbTester().insertMeasure(project, analysis, metricWithoutDomain);
+
+    ComponentWsResponse response = ws.newRequest()
+      .setParam(PARAM_COMPONENT_KEY, project.getKey())
+      .setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
+      .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
+      .executeProtobuf(ComponentWsResponse.class);
+
+    assertThat(response.getComponent().getMeasures(0).getMetric()).isEqualTo(metricWithoutDomain.getKey());
+    Common.Metric responseMetric = response.getMetrics().getMetrics(0);
+    assertThat(responseMetric.getKey()).isEqualTo(metricWithoutDomain.getKey());
+    assertThat(responseMetric.hasDomain()).isFalse();
+  }
+
   @Test
   public void fail_when_developer_is_not_found() {
     expectedException.expect(NotFoundException.class);
index 6f14704292a11e3ed5ded011003d9ee60e5b368f..c4c3b054fa627ce76855d86e2495d53e741964d2 100644 (file)
@@ -26,6 +26,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.measures.Metric;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
@@ -431,6 +432,27 @@ public class ComponentTreeActionTest {
     assertThat(result.getComponentsCount()).isEqualTo(0);
   }
 
+  @Test
+  public void metric_without_a_domain() {
+    ComponentDto project = db.components().insertPrivateProject();
+    SnapshotDto analysis = db.getDbClient().snapshotDao().insert(dbSession, newAnalysis(project));
+    MetricDto metricWithoutDomain = db.measureDbTester().insertMetric(m -> m
+      .setValueType(Metric.ValueType.INT.name())
+      .setDomain(null));
+    db.measureDbTester().insertMeasure(project, analysis, metricWithoutDomain);
+
+    ComponentTreeWsResponse result = ws.newRequest()
+      .setParam(PARAM_BASE_COMPONENT_KEY, project.getKey())
+      .setParam(PARAM_METRIC_KEYS, metricWithoutDomain.getKey())
+      .setParam(PARAM_ADDITIONAL_FIELDS, "metrics")
+      .executeProtobuf(ComponentTreeWsResponse.class);
+
+    assertThat(result.getBaseComponent().getMeasures(0).getMetric()).isEqualTo(metricWithoutDomain.getKey());
+    Common.Metric responseMetric = result.getMetrics().getMetrics(0);
+    assertThat(responseMetric.getKey()).isEqualTo(metricWithoutDomain.getKey());
+    assertThat(responseMetric.hasDomain()).isFalse();
+  }
+
   @Test
   public void fail_when_metric_keys_parameter_is_empty() {
     componentDb.insertProjectAndSnapshot(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"));