aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-08-15 16:43:06 +0200
committerTeryk Bellahsene <teryk@users.noreply.github.com>2017-08-16 10:07:35 +0200
commitee55e4492b069f30e5c25fa51e92170412f006d5 (patch)
treecb92bc76bf717632a341fc1c99592600aa5fa990
parent26e48e66d0e5497118e2d1593ba68fd2d08f6f53 (diff)
downloadsonarqube-ee55e4492b069f30e5c25fa51e92170412f006d5.tar.gz
sonarqube-ee55e4492b069f30e5c25fa51e92170412f006d5.zip
SONAR-9564 WS api/components/measures/component and component_tree do not fail with empty domain metric
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java19
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java29
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java22
3 files changed, 55 insertions, 15 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java
index 2de2881b434..42c79c8413b 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java
@@ -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();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java
index 5cfa199ddc2..b58bebd4727 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentActionTest.java
@@ -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");
@@ -149,6 +153,27 @@ public class ComponentActionTest {
}
@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);
expectedException.expectMessage("Component id 'unknown-developer-id' not found");
diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java
index 6f14704292a..c4c3b054fa6 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java
@@ -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;
@@ -432,6 +433,27 @@ public class ComponentTreeActionTest {
}
@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"));