From f040e55227e5a77714222a61c16c6a158f99a51f Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 23 Nov 2017 16:38:24 +0100 Subject: [PATCH] SONAR-10087 Use protobuf in api/qualitygates/rename --- .../qualitygate/ws/GetByProjectAction.java | 2 +- .../server/qualitygate/ws/RenameAction.java | 11 +++++++---- .../server/qualitygate/ws/RenameActionTest.java | 17 ++++++----------- .../src/main/protobuf/ws-qualitygates.proto | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java index 23d7bb84ea7..1d69b4a4c9b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/GetByProjectAction.java @@ -106,7 +106,7 @@ public class GetByProjectAction implements QualityGatesWsAction { GetByProjectResponse.Builder response = GetByProjectResponse.newBuilder(); response.getQualityGateBuilder() - .setId(String.valueOf(qualityGate.getId())) + .setId(qualityGate.getId()) .setName(qualityGate.getName()) .setDefault(data.get().isDefault()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java index deb2a084a86..c438e00e7fd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java @@ -22,11 +22,11 @@ package org.sonar.server.qualitygate.ws; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.text.JsonWriter; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.qualitygate.QualityGateFinder; +import org.sonarqube.ws.Qualitygates.QualityGate; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; @@ -34,6 +34,7 @@ import static org.sonar.server.qualitygate.ws.CreateAction.NAME_MAXIMUM_LENGTH; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; import static org.sonar.server.util.Validation.CANT_BE_EMPTY_MESSAGE; +import static org.sonar.server.ws.WsUtils.writeProtobuf; public class RenameAction implements QualityGatesWsAction { @@ -72,9 +73,11 @@ public class RenameAction implements QualityGatesWsAction { public void handle(Request request, Response response) { wsSupport.checkCanEdit(); long id = QualityGatesWs.parseId(request, PARAM_ID); - QualityGateDto renamedQualityGate = rename(id, request.mandatoryParam(PARAM_NAME)); - JsonWriter writer = response.newJsonWriter(); - QualityGatesWs.writeQualityGate(renamedQualityGate, writer).close(); + QualityGateDto qualityGate = rename(id, request.mandatoryParam(PARAM_NAME)); + writeProtobuf(QualityGate.newBuilder() + .setId(qualityGate.getId()) + .setName(qualityGate.getName()) + .build(), request, response); } private QualityGateDto rename(long id, String name) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java index 59d733bb055..546ddfc86c8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java @@ -34,13 +34,13 @@ import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; +import org.sonarqube.ws.Qualitygates.QualityGate; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.tuple; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; -import static org.sonar.test.JsonAssert.assertJson; public class RenameActionTest { @@ -85,18 +85,13 @@ public class RenameActionTest { logAsQualityGateAdminister(); QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name")); - String result = ws.newRequest() + QualityGate result = ws.newRequest() .setParam("id", qualityGate.getId().toString()) .setParam("name", "new name") - .execute() - .getInput(); - - assertJson(result).isSimilarTo( - format("{\n" + - " \"id\": %s,\n" + - " \"name\": \"new name\"\n" + - "}", - qualityGate.getId())); + .executeProtobuf(QualityGate.class); + + assertThat(result.getId()).isEqualTo(qualityGate.getId()); + assertThat(result.getName()).isEqualTo("new name"); } @Test diff --git a/sonar-ws/src/main/protobuf/ws-qualitygates.proto b/sonar-ws/src/main/protobuf/ws-qualitygates.proto index 16b4d7e9671..e7355755459 100644 --- a/sonar-ws/src/main/protobuf/ws-qualitygates.proto +++ b/sonar-ws/src/main/protobuf/ws-qualitygates.proto @@ -73,7 +73,7 @@ message GetByProjectResponse { } message QualityGate { - optional string id = 1; + optional int64 id = 1; optional string name = 2; optional bool default = 3; } -- 2.39.5