From 81b9f4bb5f38d9cd514843f945534a0e24cb1032 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 18 Jun 2015 17:00:49 +0200 Subject: [PATCH] SONAR-6610 WS custom_measures/create use specific JsonWriter class to write response --- .../measure/custom/ws/CreateAction.java | 24 +---- .../custom/ws/CustomMeasureJsonWriter.java | 92 +++++++++++++++++++ .../custom/ws/CustomMeasuresWsModule.java | 1 + .../measure/custom/ws/CreateActionTest.java | 2 +- .../custom/ws/CustomMeasuresWsTest.java | 2 +- 5 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java index 900817633aa..4586689a37e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java @@ -67,12 +67,14 @@ public class CreateAction implements CustomMeasuresWsAction { private final UserSession userSession; private final System2 system; private final TypeValidations typeValidations; + private final CustomMeasureJsonWriter customMeasureJsonWriter; - public CreateAction(DbClient dbClient, UserSession userSession, System2 system, TypeValidations typeValidations) { + public CreateAction(DbClient dbClient, UserSession userSession, System2 system, TypeValidations typeValidations, CustomMeasureJsonWriter customMeasureJsonWriter) { this.dbClient = dbClient; this.userSession = userSession; this.system = system; this.typeValidations = typeValidations; + this.customMeasureJsonWriter = customMeasureJsonWriter; } @Override @@ -156,24 +158,8 @@ public class CreateAction implements CustomMeasuresWsAction { } } - private static void writeMeasure(JsonWriter json, CustomMeasureDto measure, ComponentDto component, MetricDto metric, String measureWithoutInternalFormatting) { - json.beginObject(); - json.prop(FIELD_ID, String.valueOf(measure.getId())); - json.name(FIELD_METRIC); - writeMetric(json, metric); - json.prop(FIELD_PROJECT_ID, component.uuid()); - json.prop(FIELD_PROJECT_KEY, component.key()); - json.prop(FIELD_DESCRIPTION, measure.getDescription()); - json.prop(FIELD_VALUE, measureWithoutInternalFormatting); - json.endObject(); - } - - private static void writeMetric(JsonWriter json, MetricDto metric) { - json.beginObject(); - json.prop(FIELD_METRIC_ID, String.valueOf(metric.getId())); - json.prop(FIELD_METRIC_KEY, metric.getKey()); - json.prop(FIELD_METRIC_TYPE, metric.getValueType()); - json.endObject(); + private void writeMeasure(JsonWriter json, CustomMeasureDto measure, ComponentDto component, MetricDto metric, String measureWithoutInternalFormatting) { + customMeasureJsonWriter.write(json, measure, metric, component); } private void setMeasureValue(CustomMeasureDto measure, Request request, MetricDto metric) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java new file mode 100644 index 00000000000..cbb6211c5fb --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasureJsonWriter.java @@ -0,0 +1,92 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.measure.custom.ws; + +import org.sonar.api.measures.Metric; +import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.component.ComponentDto; +import org.sonar.core.measure.custom.db.CustomMeasureDto; +import org.sonar.core.metric.db.MetricDto; + +import static org.sonar.server.measure.custom.ws.CreateAction.PARAM_DESCRIPTION; +import static org.sonar.server.measure.custom.ws.CreateAction.PARAM_PROJECT_ID; +import static org.sonar.server.measure.custom.ws.CreateAction.PARAM_PROJECT_KEY; +import static org.sonar.server.measure.custom.ws.CreateAction.PARAM_VALUE; + +public class CustomMeasureJsonWriter { + private static final String FIELD_ID = "id"; + private static final String FIELD_PROJECT_ID = PARAM_PROJECT_ID; + private static final String FIELD_PROJECT_KEY = PARAM_PROJECT_KEY; + private static final String FIELD_VALUE = PARAM_VALUE; + private static final String FIELD_DESCRIPTION = PARAM_DESCRIPTION; + private static final String FIELD_METRIC = "metric"; + private static final String FIELD_METRIC_KEY = "key"; + private static final String FIELD_METRIC_ID = "id"; + private static final String FIELD_METRIC_TYPE = "type"; + + public void write(JsonWriter json, CustomMeasureDto measure, MetricDto metric, ComponentDto component) { + json.beginObject(); + json.prop(FIELD_ID, String.valueOf(measure.getId())); + json.name(FIELD_METRIC); + writeMetric(json, metric); + json.prop(FIELD_PROJECT_ID, component.uuid()); + json.prop(FIELD_PROJECT_KEY, component.key()); + json.prop(FIELD_DESCRIPTION, measure.getDescription()); + json.prop(FIELD_VALUE, measureValue(measure, metric)); + json.endObject(); + } + + private String measureValue(CustomMeasureDto measure, MetricDto metric) { + Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType()); + Double doubleValue = measure.getValue(); + String stringValue = measure.getTextValue(); + + switch (metricType) { + case BOOL: + return doubleValue == 1.0d ? "true" : "false"; + case INT: + case MILLISEC: + return String.valueOf(doubleValue.intValue()); + case WORK_DUR: + return String.valueOf(doubleValue.longValue()); + case FLOAT: + case PERCENT: + case RATING: + return String.valueOf(doubleValue); + case LEVEL: + case STRING: + case DATA: + case DISTRIB: + return stringValue; + default: + throw new IllegalArgumentException("Unsupported metric type:" + metricType.description()); + } + } + + private static void writeMetric(JsonWriter json, MetricDto metric) { + json.beginObject(); + json.prop(FIELD_METRIC_ID, String.valueOf(metric.getId())); + json.prop(FIELD_METRIC_KEY, metric.getKey()); + json.prop(FIELD_METRIC_TYPE, metric.getValueType()); + json.endObject(); + } + +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsModule.java index ef50f5e42b3..ae49ecf5007 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsModule.java @@ -28,6 +28,7 @@ public class CustomMeasuresWsModule extends Module { add( CustomMeasuresWs.class, DeleteAction.class, + CustomMeasureJsonWriter.class, CreateAction.class); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java index 3ad621ef84b..6fccdd1153d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java @@ -83,7 +83,7 @@ public class CreateActionTest { dbSession = dbClient.openSession(false); TypeValidations typeValidations = new TypeValidations(Arrays.asList(new BooleanTypeValidation(), new IntegerTypeValidation(), new FloatTypeValidation(), new MetricLevelTypeValidation(), new LongTypeValidation())); - ws = new WsTester(new CustomMeasuresWs(new CreateAction(dbClient, userSession, System2.INSTANCE, typeValidations))); + ws = new WsTester(new CustomMeasuresWs(new CreateAction(dbClient, userSession, System2.INSTANCE, typeValidations, new CustomMeasureJsonWriter()))); db.truncateTables(); userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java index e9fcb9309d1..3737f9236ed 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CustomMeasuresWsTest.java @@ -41,7 +41,7 @@ public class CustomMeasuresWsTest { UserSession userSession = mock(UserSession.class); ws = new WsTester(new CustomMeasuresWs( new DeleteAction(dbClient, userSession), - new CreateAction(dbClient, userSession, System2.INSTANCE, mock(TypeValidations.class)) + new CreateAction(dbClient, userSession, System2.INSTANCE, mock(TypeValidations.class), mock(CustomMeasureJsonWriter.class)) )); } -- 2.39.5