]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10087 Use protobuf in api/qualitygates/list
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 22 Nov 2017 16:49:49 +0000 (17:49 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 4 Dec 2017 12:44:55 +0000 (13:44 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ListAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ListActionTest.java
sonar-ws/src/main/protobuf/ws-qualitygates.proto

index 0d0d5db3804f9852cbebc79160ee044e62a39f10..7b1a67bdebd979411f492bca87465ce7ddcda58e 100644 (file)
 package org.sonar.server.qualitygate.ws;
 
 import com.google.common.io.Resources;
+import java.util.Collection;
 import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.server.ws.Change;
 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.property.PropertyDto;
 import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonarqube.ws.Qualitygates.ListWsResponse;
 
+import static org.sonar.core.util.Protobuf.setNullable;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_ID;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_NAME;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
 
 public class ListAction implements QualityGatesWsAction {
 
@@ -58,25 +61,25 @@ public class ListAction implements QualityGatesWsAction {
 
   @Override
   public void handle(Request request, Response response) {
-    try (DbSession dbSession = dbClient.openSession(false);
-      JsonWriter writer = response.newJsonWriter()) {
-      QualityGateDto defaultQgate = getDefault(dbSession);
-      Long defaultQgateId = defaultQgate == null ? null : defaultQgate.getId();
-      writer.beginObject().name("qualitygates").beginArray();
-      for (QualityGateDto qualityGate : dbClient.qualityGateDao().selectAll(dbSession)) {
-        writer.beginObject()
-          .prop(PARAM_ID, qualityGate.getId())
-          .prop(PARAM_NAME, qualityGate.getName())
-          .prop("isDefault", qualityGate.getId().equals(defaultQgateId))
-          .endObject();
-      }
-      writer.endArray();
-      if (defaultQgateId != null) {
-        writer.prop("default", defaultQgateId);
-      }
-      writer.endObject().close();
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      QualityGateDto defaultQualityGate = getDefault(dbSession);
+      Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession);
+      writeProtobuf(buildResponse(qualityGates, defaultQualityGate), request, response);
     }
+  }
 
+  private static ListWsResponse buildResponse(Collection<QualityGateDto> qualityGates, @Nullable QualityGateDto defaultQualityGate) {
+    Long defaultId = defaultQualityGate == null ? null : defaultQualityGate.getId();
+    ListWsResponse.Builder builder = ListWsResponse.newBuilder()
+      .addAllQualitygates(qualityGates.stream()
+        .map(qualityGate -> ListWsResponse.QualityGate.newBuilder()
+          .setId(qualityGate.getId())
+          .setName(qualityGate.getName())
+          .setIsDefault(qualityGate.getId().equals(defaultId))
+          .build())
+        .collect(toList()));
+    setNullable(defaultId, builder::setDefault);
+    return builder.build();
   }
 
   @CheckForNull
index 1696f719673937e114c50b339c28bdc6b4feea28..0723d6d2cd8dd2fed17087903c5491b14bbad0fb 100644 (file)
@@ -29,10 +29,12 @@ import org.sonar.db.DbTester;
 import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.Qualitygates.ListWsResponse.QualityGate;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.AssertionsForClassTypes.tuple;
 import static org.sonar.test.JsonAssert.assertJson;
+import static org.sonarqube.ws.Qualitygates.ListWsResponse;
 
 public class ListActionTest {
 
@@ -76,57 +78,43 @@ public class ListActionTest {
   @Test
   public void list_quality_gates() {
     QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way");
-    db.qualityGates().insertQualityGate("Sonar way - Without Coverage");
+    QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate("Sonar way - Without Coverage");
     db.qualityGates().setDefaultQualityGate(defaultQualityGate);
 
-    String response = ws.newRequest()
-      .execute()
-      .getInput();
+    ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
 
-    assertJson(response).ignoreFields("id", "default")
-      .isSimilarTo("{\n" +
-        "  \"qualitygates\": [\n" +
-        "    {\n" +
-        "      \"name\": \"Sonar way\",\n" +
-        "      \"isDefault\": true\n" +
-        "    },\n" +
-        "    {\n" +
-        "      \"id\": 4,\n" +
-        "      \"name\": \"Sonar way - Without Coverage\",\n" +
-        "      \"isDefault\": false\n" +
-        "    }\n" +
-        "  ]\n" +
-        "}\n");
+    assertThat(response.getQualitygatesList())
+      .extracting(QualityGate::getId, QualityGate::getName, QualityGate::getIsDefault)
+    .containsExactlyInAnyOrder(
+      tuple(defaultQualityGate.getId(), defaultQualityGate.getName(), true),
+      tuple(otherQualityGate.getId(), otherQualityGate.getName(), false));
+  }
+
+  @Test
+  public void test_deprecated_default_field() {
+    QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way");
+    db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+
+    ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
+
+    assertThat(response.getDefault()).isEqualTo(defaultQualityGate.getId());
   }
 
   @Test
   public void no_default_quality_gate() {
-    db.qualityGates().insertQualityGate("Sonar way");
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate("Sonar way");
 
-    String response = ws.newRequest()
-      .execute()
-      .getInput();
+    ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
 
-    assertJson(response).ignoreFields("id", "default")
-      .isSimilarTo("{\n" +
-        "  \"qualitygates\": [\n" +
-        "    {\n" +
-        "      \"name\": \"Sonar way\",\n" +
-        "      \"isDefault\": false\n" +
-        "    }\n" +
-        "  ]\n" +
-        "}\n");
+    assertThat(response.getQualitygatesList())
+      .extracting(QualityGate::getId, QualityGate::getName, QualityGate::getIsDefault)
+      .containsExactlyInAnyOrder(tuple(qualityGate.getId(), qualityGate.getName(), false));
   }
 
   @Test
   public void empty() {
-    String response = ws.newRequest()
-      .execute()
-      .getInput();
+    ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
 
-    assertJson(response).ignoreFields("id", "default")
-      .isSimilarTo("{\n" +
-        "  \"qualitygates\": []\n" +
-        "}\n");
+    assertThat(response.getQualitygatesList()).isEmpty();
   }
 }
index 36998c537ffa372a24f18960f46ee8e7e3fef513..e8f322a112ee40b79723601299ea0aabe39de69b 100644 (file)
@@ -135,5 +135,18 @@ message ShowWsResponse {
   }
 }
 
+// GET api/qualitygates/list
+message ListWsResponse {
+  repeated QualityGate qualitygates = 1;
+  // Deprecated since 7.0
+  optional int64 default = 2;
+
+  message QualityGate {
+    optional int64 id = 1;
+    optional string name = 2;
+    optional bool isDefault = 3;
+  }
+}
+