]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9482 Create api/qualityprofile/show WS returning stub
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 27 Jun 2017 16:18:08 +0000 (18:18 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 4 Jul 2017 14:29:36 +0000 (16:29 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWsModule.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ShowAction.java [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/show-example.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ShowActionTest.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
sonar-ws/src/main/protobuf/ws-qualityprofiles.proto

index 3708ce600eadb09ac7e95ce1ac456943507cb23c..f3362d94a6187226ea59c43705d18345c8a26740 100644 (file)
@@ -50,7 +50,8 @@ public class QProfilesWsModule extends Module {
       ActivateRuleAction.class,
       DeactivateRuleAction.class,
       SearchAction.class,
-      SetDefaultAction.class
+      SetDefaultAction.class,
+      ShowAction.class
       );
   }
 }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ShowAction.java
new file mode 100644 (file)
index 0000000..7b4020b
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.qualityprofile.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.server.ws.WebService.NewAction;
+import org.sonarqube.ws.QualityProfiles.ShowWsResponse;
+import org.sonarqube.ws.QualityProfiles.ShowWsResponse.CompareToSonarWay;
+
+import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE;
+
+public class ShowAction implements QProfileWsAction {
+
+  @Override
+  public void define(WebService.NewController controller) {
+    NewAction show = controller.createAction("show")
+      .setSince("6.5")
+      .setDescription("Show a quality profile")
+      .setResponseExample(getClass().getResource("show-example.json"))
+      .setInternal(true)
+      .setHandler(this);
+
+    show.createParam(PARAM_PROFILE)
+      .setDescription("Quality profile key")
+      .setExampleValue(UUID_EXAMPLE_01)
+      .setRequired(true);
+
+    show.createParam(PARAM_COMPARE_TO_SONAR_WAY)
+      .setDescription("Add the number of missing rules from the related Sonar way profile in the response")
+      .setInternal(true)
+      .setBooleanPossibleValues();
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    ShowWsResponse showWsResponse = ShowWsResponse.newBuilder()
+      .setProfile(ShowWsResponse.QualityProfile.newBuilder()
+        .setKey("AU-TpxcA-iU5OvuD2FL3")
+        .setName("My Company Profile")
+        .setLanguage("cs")
+        .setLanguageName("C#")
+        .setIsInherited(true)
+        .setIsBuiltIn(false)
+        .setIsDefault(false)
+        .setParentKey("AU-TpxcA-iU5OvuD2FL1")
+        .setParentName("Parent Company Profile")
+        .setActiveRuleCount(10)
+        .setActiveDeprecatedRuleCount(0)
+        .setProjectCount(7)
+        .setRuleUpdatedAt("2016-12-22T19:10:03+0100")
+        .setLastUsed("2016-12-01T19:10:03+0100"))
+      .setCompareToSonarWay(CompareToSonarWay.newBuilder()
+        .setProfile("iU5OvuD2FLz")
+        .setProfileName("Sonar way")
+        .setMissingRuleCount(4)
+        .build())
+      .build();
+    writeProtobuf(showWsResponse, request, response);
+  }
+
+}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/show-example.json b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/show-example.json
new file mode 100644 (file)
index 0000000..60f03aa
--- /dev/null
@@ -0,0 +1,23 @@
+{
+  "profile": {
+    "key": "AU-TpxcA-iU5OvuD2FL3",
+    "name": "My Company Profile",
+    "language": "cs",
+    "languageName": "C#",
+    "isInherited": true,
+    "isBuiltIn": false,
+    "isDefault": false,
+    "parentKey": "AU-TpxcA-iU5OvuD2FL1",
+    "parentName": "Parent Company Profile",
+    "activeRuleCount": 10,
+    "activeDeprecatedRuleCount": 0,
+    "projectCount": 7,
+    "ruleUpdatedAt": "2016-12-22T19:10:03+0100",
+    "lastUsed": "2016-12-01T19:10:03+0100"
+  },
+  "compareToSonarWay": {
+    "profile": "iU5OvuD2FLz",
+    "profileName": "Sonar way",
+    "missingRuleCount": 4
+  }
+}
index 60f84b6d05cd81947b77dcec47e707cc33f0f460..1764e6cafcce5bfc9dc41e189b433234ad1e175a 100644 (file)
@@ -29,6 +29,6 @@ public class QProfilesWsModuleTest {
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new QProfilesWsModule().configure(container);
-    assertThat(container.size()).isEqualTo(26 + 2);
+    assertThat(container.size()).isEqualTo(27 + 2);
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ShowActionTest.java
new file mode 100644 (file)
index 0000000..b7f9b98
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.qualityprofile.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.resources.Language;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.qualityprofile.QProfileDto;
+import org.sonar.server.ws.WsActionTester;
+
+import static java.util.stream.IntStream.range;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.language.LanguageTesting.newLanguage;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class ShowActionTest {
+
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private WsActionTester ws = new WsActionTester(new ShowAction());
+
+  @Test
+  public void test_definition() {
+    WebService.Action action = ws.getDef();
+    assertThat(action.key()).isEqualTo("show");
+    assertThat(action.responseExampleAsString()).isNotEmpty();
+    assertThat(action.isPost()).isFalse();
+    assertThat(action.since()).isEqualTo("6.5");
+
+    WebService.Param profile = action.param("profile");
+    assertThat(profile).isNotNull();
+    assertThat(profile.isRequired()).isTrue();
+    assertThat(profile.isInternal()).isFalse();
+    assertThat(profile.description()).isNotEmpty();
+
+    WebService.Param compareToSonarWay = action.param("compareToSonarWay");
+    assertThat(compareToSonarWay).isNotNull();
+    assertThat(compareToSonarWay.isRequired()).isFalse();
+    assertThat(compareToSonarWay.isInternal()).isTrue();
+    assertThat(compareToSonarWay.description()).isNotEmpty();
+    assertThat(compareToSonarWay.possibleValues()).contains("true", "false");
+  }
+
+  @Test
+  public void test_example() {
+    Language cs = newLanguage("cs", "C#");
+    QProfileDto parentProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setKee("AU-TpxcA-iU5OvuD2FL1").setName("Parent Company Profile"));
+    QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p
+      .setKee("AU-TpxcA-iU5OvuD2FL3")
+      .setName("My Company Profile")
+      .setLanguage(cs.getKey())
+      .setIsBuiltIn(false)
+      .setRulesUpdatedAt("2016-12-22T19:10:03+0100")
+      .setParentKee(parentProfile.getKee()));
+    // Active rules
+    range(0, 10)
+      .mapToObj(i -> db.rules().insertRule(r -> r.setLanguage(cs.getKey())).getDefinition())
+      .forEach(r -> db.qualityProfiles().activateRule(profile, r));
+    // Projects
+    range(0, 7)
+      .mapToObj(i -> db.components().insertPrivateProject())
+      .forEach(project -> db.qualityProfiles().associateWithProject(project, profile));
+
+    String result = ws.newRequest().execute().getInput();
+
+    assertJson(result).isSimilarTo(ws.getDef().responseExampleAsString());
+  }
+}
index 5ce75182da84babdfe54cf5904d92a84e7226143..f52962bee5c6efc3b28d83f2622b1d6a22e0dffe 100644 (file)
@@ -63,6 +63,7 @@ public class QualityProfileWsParameters {
   public static final String PARAM_TARGET_SEVERITY = "targetSeverity";
   public static final String PARAM_TO = "to";
   public static final String PARAM_TO_NAME = "toName";
+  public static final String PARAM_COMPARE_TO_SONAR_WAY = "compareToSonarWay";
 
   private QualityProfileWsParameters() {
     // Only static stuff
index a6f2df7b39a7884ca664f9c1ae4e191c63f23492..0dab9b3c06f35c326c4f33ba3b27a3d5989d4c2f 100644 (file)
@@ -99,3 +99,34 @@ message CopyWsResponse {
   optional bool isInherited = 6;
   optional string parentKey = 7;
 }
+
+// WS api/qualityprofiles/show
+message ShowWsResponse {
+  optional QualityProfile profile = 1;
+  optional CompareToSonarWay compareToSonarWay = 2;
+
+  message QualityProfile {
+    optional string key = 1;
+    optional string name = 2;
+    optional string language = 3;
+    optional string languageName = 4;
+    optional bool isInherited = 5;
+    optional string parentKey = 6;
+    optional string parentName = 7;
+    optional bool isDefault = 8;
+    optional int64 activeRuleCount = 9;
+    optional int64 activeDeprecatedRuleCount = 12;
+    optional int64 projectCount = 10;
+    optional string ruleUpdatedAt = 11;
+    optional string lastUsed = 13;
+    optional string userUpdatedAt = 14;
+    optional string organization = 15;
+    optional bool isBuiltIn = 16;
+  }
+
+  message CompareToSonarWay {
+    optional string profile = 1;
+    optional string profileName = 2;
+    optional int64 missingRuleCount = 3;
+  }
+}