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 {
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();
}
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;
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;
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;
}
@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");
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);
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;
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"));