]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8924 refactored api/qualityprofiles/inheritance to use protobuf
authorDaniel Schwarz <daniel.schwarz@sonarsource.com>
Wed, 12 Apr 2017 12:12:36 +0000 (14:12 +0200)
committerDaniel Schwarz <bartfastiel@users.noreply.github.com>
Fri, 14 Apr 2017 09:41:59 +0000 (11:41 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/InheritanceAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/InheritanceActionTest.java
sonar-ws/src/main/protobuf/ws-qualityprofiles.proto

index 97a431af65dbe6faf72f6c8f6a84544bc652126d..34def49728f1d174486b69ebdbfbabde4c92e407 100644 (file)
@@ -21,13 +21,14 @@ package org.sonar.server.qualityprofile.ws;
 
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 import javax.annotation.Nullable;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.api.server.ws.WebService.NewController;
-import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.util.Protobuf;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.organization.OrganizationDto;
@@ -35,6 +36,10 @@ import org.sonar.db.qualityprofile.ActiveRuleDao;
 import org.sonar.db.qualityprofile.ActiveRuleDto;
 import org.sonar.db.qualityprofile.QualityProfileDto;
 import org.sonar.server.qualityprofile.QProfileLookup;
+import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse;
+import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse.QualityProfile;
+
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
 
 public class InheritanceAction implements QProfileWsAction {
 
@@ -75,54 +80,42 @@ public class InheritanceAction implements QProfileWsAction {
       List<QualityProfileDto> children = dbClient.qualityProfileDao().selectChildren(dbSession, profile.getKey());
       Statistics statistics = new Statistics(dbSession, organization);
 
-      writeResponse(response.newJsonWriter(), profile, ancestors, children, statistics);
+      writeProtobuf(buildResponse(profile, ancestors, children, statistics), request, response);
     }
   }
 
-  private static void writeResponse(JsonWriter json, QualityProfileDto profile, List<QualityProfileDto> ancestors, List<QualityProfileDto> children, Statistics statistics) {
-    json.beginObject();
-    writeProfile(json, profile, statistics);
-    writeAncestors(json, ancestors, statistics);
-    writeChildren(json, children, statistics);
-    json.endObject().close();
+  private static InheritanceWsResponse buildResponse(QualityProfileDto profile, List<QualityProfileDto> ancestors, List<QualityProfileDto> children, Statistics statistics) {
+    return InheritanceWsResponse.newBuilder()
+      .setProfile(buildProfile(profile, statistics))
+      .addAllAncestors(buildAncestors(ancestors, statistics))
+      .addAllChildren(buildChildren(children, statistics))
+      .build();
   }
 
-  private static void writeProfile(JsonWriter json, QualityProfileDto profile, Statistics statistics) {
-    String profileKey = profile.getKey();
-    json.name("profile");
-    writeProfileAttributes(json, profileKey, profile.getName(), profile.getParentKee(), statistics);
+  private static QualityProfile buildProfile(QualityProfileDto profile, Statistics statistics) {
+    return buildProfile(profile.getKey(), profile.getName(), profile.getParentKee(), statistics);
   }
 
-  private static void writeAncestors(JsonWriter json, List<QualityProfileDto> ancestors, Statistics statistics) {
-    json.name("ancestors").beginArray();
-    for (QualityProfileDto ancestor : ancestors) {
-      String ancestorKey = ancestor.getKey();
-      writeProfileAttributes(json, ancestorKey, ancestor.getName(), ancestor.getParentKee(), statistics);
-    }
-    json.endArray();
-  }
-
-  private static void writeChildren(JsonWriter json, List<QualityProfileDto> children, Statistics statistics) {
-    json.name("children").beginArray();
-    for (QualityProfileDto child : children) {
-      String childKey = child.getKey();
-      writeProfileAttributes(json, childKey, child.getName(), null, statistics);
-    }
-    json.endArray();
+  private static Iterable<QualityProfile> buildAncestors(List<QualityProfileDto> ancestors, Statistics statistics) {
+    return ancestors.stream()
+      .map(ancestor -> buildProfile(ancestor.getKey(), ancestor.getName(), ancestor.getParentKee(), statistics))
+      .collect(Collectors.toList());
   }
 
-  private static void writeProfileAttributes(JsonWriter json, String key, String name, @Nullable String parentKey, Statistics statistics) {
-    json.beginObject();
-    json.prop("key", key)
-      .prop("name", name)
-      .prop("parent", parentKey);
-    writeStats(json, key, statistics);
-    json.endObject();
+  private static Iterable<QualityProfile> buildChildren(List<QualityProfileDto> children, Statistics statistics) {
+    return children.stream()
+      .map(child -> buildProfile(child.getKey(), child.getName(), null, statistics))
+      .collect(Collectors.toList());
   }
 
-  private static void writeStats(JsonWriter json, String profileKey, Statistics statistics) {
-    json.prop("activeRuleCount", statistics.countRulesByProfileKey.getOrDefault(profileKey, 0L));
-    json.prop("overridingRuleCount", statistics.countOverridingRulesByProfileKey.getOrDefault(profileKey, 0L));
+  private static QualityProfile buildProfile(String key, String name, @Nullable String parentKey, Statistics statistics) {
+    QualityProfile.Builder builder = QualityProfile.newBuilder()
+      .setKey(key)
+      .setName(name)
+      .setActiveRuleCount(statistics.countRulesByProfileKey.getOrDefault(key, 0L))
+      .setOverridingRuleCount(statistics.countOverridingRulesByProfileKey.getOrDefault(key, 0L));
+    Protobuf.setNullable(parentKey, builder::setParent);
+    return builder.build();
   }
 
   private class Statistics {
index ea733ce2344cd112a971fd95fe653b265a2e9294..ba7375843a24f476fcf58f194b6f28bb928efaf6 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.qualityprofile.ws;
 
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
 import org.junit.Before;
@@ -56,6 +57,10 @@ import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.util.TypeValidations;
 import org.sonar.server.ws.WsActionTester;
 import org.sonar.test.JsonAssert;
+import org.sonarqube.ws.QualityProfiles.InheritanceWsResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonarqube.ws.MediaTypes.PROTOBUF;
 
 public class InheritanceActionTest {
 
@@ -146,71 +151,62 @@ public class InheritanceActionTest {
   @Test
   public void inheritance_parent_child() throws Exception {
     RuleDefinitionDto rule1 = dbTester.rules().insert();
-    ruleIndexer.index(organization, rule1.getKey());
+    ruleIndexer.indexRuleDefinition(rule1.getKey());
 
     RuleDefinitionDto rule2 = dbTester.rules().insert();
-    ruleIndexer.index(organization, rule1.getKey());
+    ruleIndexer.indexRuleDefinition(rule1.getKey());
 
     RuleDefinitionDto rule3 = dbTester.rules().insert();
-    ruleIndexer.index(organization, rule1.getKey());
+    ruleIndexer.indexRuleDefinition(rule1.getKey());
 
     QualityProfileDto parent = dbTester.qualityProfiles().insert(organization);
     dbTester.qualityProfiles().activateRule(parent, rule1);
     dbTester.qualityProfiles().activateRule(parent, rule2);
-    int parentRules = 2;
+    long parentRules = 2;
 
     QualityProfileDto child = dbTester.qualityProfiles().insert(organization, q -> q.setParentKee(parent.getKee()));
     dbTester.qualityProfiles().activateRule(child, rule3);
-    int childRules = 1;
+    long childRules = 1;
 
     activeRuleIndexer.index();
 
-    String response = wsActionTester.newRequest()
+    InputStream response = wsActionTester.newRequest()
       .setMethod("GET")
+      .setMediaType(PROTOBUF)
       .setParam("profileKey", child.getKey())
       .execute()
-      .getInput();
+      .getInputStream();
 
-    JsonAssert.assertJson(response).isSimilarTo("" +
-      "{" +
-      "  \"profile\":" + generateJsonProfile(child, childRules) +
-      "}");
+    InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
 
-    JsonAssert.assertJson(response).isSimilarTo("" +
-      "{" +
-      "  \"ancestors\":[" + generateJsonProfile(parent, parentRules) + "]" +
-      "}");
+    assertThat(result.getProfile().getKey()).isEqualTo(child.getKey());
+    assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(childRules);
+
+    assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getKey).containsExactly(parent.getKey());
+    assertThat(result.getAncestorsList()).extracting(InheritanceWsResponse.QualityProfile::getActiveRuleCount).containsExactly(parentRules);
   }
 
   @Test
   public void inheritance_ignores_removed_rules() throws Exception {
     RuleDefinitionDto rule = dbTester.rules().insert(r -> r.setStatus(RuleStatus.REMOVED));
-    ruleIndexer.index(organization, rule.getKey());
+    ruleIndexer.indexRuleDefinition(rule.getKey());
 
     QualityProfileDto profile = dbTester.qualityProfiles().insert(organization);
     dbTester.qualityProfiles().activateRule(profile, rule);
-    int activeRules = 0;
+    long activeRules = 0;
 
     activeRuleIndexer.index();
 
-    String response = wsActionTester.newRequest()
+    InputStream response = wsActionTester.newRequest()
       .setMethod("GET")
+      .setMediaType(PROTOBUF)
       .setParam("profileKey", profile.getKey())
       .execute()
-      .getInput();
-
-    JsonAssert.assertJson(response).isSimilarTo("" +
-      "{" +
-      "  \"profile\":" + generateJsonProfile(profile, activeRules) +
-      "}");
-  }
+      .getInputStream();
 
-  private String generateJsonProfile(QualityProfileDto child, int numberOfRules) {
-    return "" +
-      "{" +
-      "  \"key\":\"" + child.getKey() + "\"," +
-      "  \"activeRuleCount\":" + numberOfRules +
-      "}";
+    InheritanceWsResponse result = InheritanceWsResponse.parseFrom(response);
+    assertThat(result.getProfile().getKey()).isEqualTo(profile.getKey());
+    assertThat(result.getProfile().getActiveRuleCount()).isEqualTo(activeRules);
   }
 
   @Test
index fa8c312be2bc054c94f1ebb304da9f012d2353f0..43aec89576f5021724b816170b975f485be4f764 100644 (file)
@@ -71,3 +71,18 @@ message CreateWsResponse {
     }
   }
 }
+
+// WS api/qualityprofiles/inheritance
+message InheritanceWsResponse {
+  optional QualityProfile profile = 1;
+  repeated QualityProfile ancestors = 2;
+  repeated QualityProfile children = 3;
+
+  message QualityProfile {
+    optional string key = 1;
+    optional string name = 2;
+    optional string parent = 3;
+    optional int64 activeRuleCount = 4;
+    optional int64 overridingRuleCount = 5;
+  }
+}