]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9482 Add WS client for api/qualityprofiles/show
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 29 Jun 2017 13:16:16 +0000 (15:16 +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/ShowAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ShowActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ShowRequest.java [new file with mode: 0644]
sonar-ws/src/main/protobuf/ws-qualityprofiles.proto
sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java

index cff1902a7b48a1225426224f3b0bf697ef15d8fd..9a293eeb25dae535cd5a16cb149eb111b9c1864c 100644 (file)
@@ -38,9 +38,10 @@ import org.sonar.db.qualityprofile.QProfileDto;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.rule.index.RuleIndex;
 import org.sonar.server.rule.index.RuleQuery;
-import org.sonarqube.ws.QualityProfiles.ShowWsResponse;
-import org.sonarqube.ws.QualityProfiles.ShowWsResponse.CompareToSonarWay;
-import org.sonarqube.ws.QualityProfiles.ShowWsResponse.QualityProfile;
+import org.sonarqube.ws.QualityProfiles;
+import org.sonarqube.ws.QualityProfiles.ShowResponse;
+import org.sonarqube.ws.QualityProfiles.ShowResponse.CompareToSonarWay;
+import org.sonarqube.ws.QualityProfiles.ShowResponse.QualityProfile;
 
 import static java.util.Collections.singletonList;
 import static org.sonar.api.rule.RuleStatus.DEPRECATED;
@@ -49,6 +50,7 @@ import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
 import static org.sonar.server.ws.WsUtils.checkFound;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SHOW;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE;
 
@@ -71,7 +73,7 @@ public class ShowAction implements QProfileWsAction {
 
   @Override
   public void define(WebService.NewController controller) {
-    NewAction show = controller.createAction("show")
+    NewAction show = controller.createAction(ACTION_SHOW)
       .setSince("6.5")
       .setDescription("Show a quality profile")
       .setResponseExample(getClass().getResource("show-example.json"))
@@ -149,9 +151,9 @@ public class ShowAction implements QProfileWsAction {
       .build();
   }
 
-  private static ShowWsResponse buildResponse(QProfileDto profile, boolean isDefault, Language language, long activeRules, long deprecatedActiveRules, long projects,
-    @Nullable CompareToSonarWay compareToSonarWay) {
-    ShowWsResponse.Builder showResponseBuilder = ShowWsResponse.newBuilder();
+  private static ShowResponse buildResponse(QProfileDto profile, boolean isDefault, Language language, long activeRules, long deprecatedActiveRules, long projects,
+                                            @Nullable CompareToSonarWay compareToSonarWay) {
+    ShowResponse.Builder showResponseBuilder = QualityProfiles.ShowResponse.newBuilder();
     QualityProfile.Builder profileBuilder = QualityProfile.newBuilder()
       .setKey(profile.getKee())
       .setName(profile.getName())
index 4d06ff2ff1b78213f80755817924ec1ff19dea39..5e133f330c912c18a1046ee2e86b14cc81310129 100644 (file)
@@ -43,8 +43,9 @@ import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.WsActionTester;
 import org.sonarqube.ws.MediaTypes;
-import org.sonarqube.ws.QualityProfiles.ShowWsResponse;
-import org.sonarqube.ws.QualityProfiles.ShowWsResponse.CompareToSonarWay;
+import org.sonarqube.ws.QualityProfiles;
+import org.sonarqube.ws.QualityProfiles.ShowResponse;
+import org.sonarqube.ws.QualityProfiles.ShowResponse.CompareToSonarWay;
 
 import static java.util.stream.IntStream.range;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -52,7 +53,7 @@ import static org.sonar.api.rule.RuleStatus.DEPRECATED;
 import static org.sonar.api.utils.DateUtils.parseDateTime;
 import static org.sonar.server.language.LanguageTesting.newLanguage;
 import static org.sonar.test.JsonAssert.assertJson;
-import static org.sonarqube.ws.QualityProfiles.ShowWsResponse.QualityProfile;
+import static org.sonarqube.ws.QualityProfiles.ShowResponse.QualityProfile;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE;
 
@@ -103,7 +104,7 @@ public class ShowActionTest {
   public void profile_info() {
     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey()));
 
-    ShowWsResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
+    QualityProfiles.ShowResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
 
     assertThat(result.getProfile())
       .extracting(QualityProfile::getKey, QualityProfile::getName, QualityProfile::getIsBuiltIn, QualityProfile::getLanguage, QualityProfile::getLanguageName,
@@ -116,7 +117,7 @@ public class ShowActionTest {
     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey()));
     db.qualityProfiles().setAsDefault(profile);
 
-    ShowWsResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
+    QualityProfiles.ShowResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
 
     assertThat(result.getProfile().getIsDefault()).isTrue();
   }
@@ -127,7 +128,7 @@ public class ShowActionTest {
     QProfileDto defaultProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey()));
     db.qualityProfiles().setAsDefault(defaultProfile);
 
-    ShowWsResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
+    ShowResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
 
     assertThat(result.getProfile().getIsDefault()).isFalse();
   }
@@ -141,7 +142,7 @@ public class ShowActionTest {
       .setLastUsed(time)
       .setUserUpdatedAt(time));
 
-    ShowWsResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
+    QualityProfiles.ShowResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
 
     assertThat(result.getProfile().getRulesUpdatedAt()).isEqualTo("2016-12-21T19:10:03+0100");
     assertThat(parseDateTime(result.getProfile().getLastUsed()).getTime()).isEqualTo(time);
@@ -164,7 +165,7 @@ public class ShowActionTest {
       .mapToObj(i -> db.components().insertPrivateProject())
       .forEach(project -> db.qualityProfiles().associateWithProject(project, profile));
 
-    ShowWsResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
+    QualityProfiles.ShowResponse result = call(ws.newRequest().setParam(PARAM_PROFILE, profile.getKee()));
 
     assertThat(result.getProfile())
       .extracting(QualityProfile::getActiveRuleCount, QualityProfile::getActiveDeprecatedRuleCount, QualityProfile::getProjectCount)
@@ -227,7 +228,7 @@ public class ShowActionTest {
       p -> p.setIsBuiltIn(true).setName("Another Sonar way").setLanguage(XOO1.getKey()));
     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey()));
 
-    ShowWsResponse result = call(ws.newRequest()
+    ShowResponse result = call(ws.newRequest()
       .setParam(PARAM_PROFILE, profile.getKee())
       .setParam(PARAM_COMPARE_TO_SONAR_WAY, "true"));
 
@@ -239,7 +240,7 @@ public class ShowActionTest {
     QProfileDto sonarWayProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey()));
     QProfileDto anotherBuiltInProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setIsBuiltIn(true).setLanguage(XOO1.getKey()));
 
-    ShowWsResponse result = call(ws.newRequest()
+    QualityProfiles.ShowResponse result = call(ws.newRequest()
       .setParam(PARAM_PROFILE, anotherBuiltInProfile.getKee())
       .setParam(PARAM_COMPARE_TO_SONAR_WAY, "true"));
 
@@ -251,7 +252,7 @@ public class ShowActionTest {
     QProfileDto sonarWayProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setIsBuiltIn(false).setName("Sonar way").setLanguage(XOO1.getKey()));
     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey()));
 
-    ShowWsResponse result = call(ws.newRequest()
+    QualityProfiles.ShowResponse result = call(ws.newRequest()
       .setParam(PARAM_PROFILE, profile.getKee())
       .setParam(PARAM_COMPARE_TO_SONAR_WAY, "true"));
 
@@ -263,7 +264,7 @@ public class ShowActionTest {
     QProfileDto sonarWayProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setIsBuiltIn(true).setName("Sonar way").setLanguage(XOO1.getKey()));
     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(XOO1.getKey()));
 
-    ShowWsResponse result = call(ws.newRequest()
+    QualityProfiles.ShowResponse result = call(ws.newRequest()
       .setParam(PARAM_PROFILE, profile.getKee())
       .setParam(PARAM_COMPARE_TO_SONAR_WAY, "false"));
 
@@ -349,8 +350,8 @@ public class ShowActionTest {
     assertJson(result).ignoreFields("rulesUpdatedAt", "lastUsed", "userUpdatedAt").isSimilarTo(ws.getDef().responseExampleAsString());
   }
 
-  private ShowWsResponse call(TestRequest request) {
+  private ShowResponse call(TestRequest request) {
     TestRequest wsRequest = request.setMediaType(MediaTypes.PROTOBUF);
-    return wsRequest.executeProtobuf(ShowWsResponse.class);
+    return wsRequest.executeProtobuf(QualityProfiles.ShowResponse.class);
   }
 }
index f52962bee5c6efc3b28d83f2622b1d6a22e0dffe..96b2d45c2043f6537ed4b0c691e3e52d8b49696f 100644 (file)
@@ -39,6 +39,7 @@ public class QualityProfileWsParameters {
   public static final String ACTION_REMOVE_PROJECT = "remove_project";
   public static final String ACTION_RESTORE = "restore";
   public static final String ACTION_SEARCH = "search";
+  public static final String ACTION_SHOW = "show";
   public static final String ACTION_SET_DEFAULT = "set_default";
   public static final String PARAM_DEFAULTS = "defaults";
 
index 46e327963ee1bab190d0acd8074d79107224f2a6..431f310be56c02267f87ed0b1d54ed6a635704db 100644 (file)
 package org.sonarqube.ws.client.qualityprofile;
 
 import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.QualityProfiles;
 import org.sonarqube.ws.QualityProfiles.CopyWsResponse;
 import org.sonarqube.ws.QualityProfiles.CreateWsResponse;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
+import org.sonarqube.ws.QualityProfiles.ShowResponse;
 import org.sonarqube.ws.client.BaseService;
 import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.PostRequest;
@@ -39,7 +41,9 @@ import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_RESTORE;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SET_DEFAULT;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SHOW;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.CONTROLLER_QUALITY_PROFILES;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_FROM_KEY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
@@ -100,6 +104,14 @@ public class QualityProfilesService extends BaseService {
       SearchWsResponse.parser());
   }
 
+  public QualityProfiles.ShowResponse show(ShowRequest request) {
+    return call(
+      new GetRequest(path(ACTION_SHOW))
+        .setParam(PARAM_PROFILE, request.getProfile())
+        .setParam(PARAM_COMPARE_TO_SONAR_WAY, request.getCompareToSonarWay()),
+      ShowResponse.parser());
+  }
+
   public void addProject(AddProjectRequest request) {
     call(new PostRequest(path(ACTION_ADD_PROJECT))
       .setParam(PARAM_LANGUAGE, request.getLanguage())
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ShowRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ShowRequest.java
new file mode 100644 (file)
index 0000000..2cbcfe9
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * 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.sonarqube.ws.client.qualityprofile;
+
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class ShowRequest {
+  private String profile;
+  private Boolean compareToSonarWay;
+
+  @CheckForNull
+  public String getProfile() {
+    return profile;
+  }
+
+  public ShowRequest setProfile(@Nullable String profile) {
+    this.profile = profile;
+    return this;
+  }
+
+  @CheckForNull
+  public Boolean getCompareToSonarWay() {
+    return compareToSonarWay;
+  }
+
+  public ShowRequest setCompareToSonarWay(@Nullable Boolean compareToSonarWay) {
+    this.compareToSonarWay = compareToSonarWay;
+    return this;
+  }
+}
index 0f1ddd6cb0b1bbc7e39ded6a4661dda684f7238e..3e19aaf63a7c2481abdd31688c902721734558f6 100644 (file)
@@ -101,7 +101,7 @@ message CopyWsResponse {
 }
 
 // WS api/qualityprofiles/show
-message ShowWsResponse {
+message ShowResponse {
   optional QualityProfile profile = 1;
   optional CompareToSonarWay compareToSonarWay = 2;
 
index 316d97d77b36ca669f988bdf2c37e71190b93d3f..1ea8262b646b5526efd44d46e5ce4a471c407883 100644 (file)
@@ -22,7 +22,8 @@ package org.sonarqube.ws.client.qualityprofile;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonarqube.ws.Common.Severity;
-import org.sonarqube.ws.QualityProfiles;
+import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
+import org.sonarqube.ws.QualityProfiles.ShowResponse;
 import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.PostRequest;
 import org.sonarqube.ws.client.ServiceTester;
@@ -30,6 +31,7 @@ import org.sonarqube.ws.client.WsConnector;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_COMPARE_TO_SONAR_WAY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_FROM_KEY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
@@ -59,7 +61,7 @@ public class QualityProfilesServiceTest {
       .setProfileName("profile"));
     GetRequest getRequest = serviceTester.getGetRequest();
 
-    assertThat(serviceTester.getGetParser()).isSameAs(QualityProfiles.SearchWsResponse.parser());
+    assertThat(serviceTester.getGetParser()).isSameAs(SearchWsResponse.parser());
     serviceTester.assertThat(getRequest)
       .hasPath("search")
       .hasParam(PARAM_DEFAULTS, true)
@@ -69,6 +71,21 @@ public class QualityProfilesServiceTest {
       .andNoOtherParam();
   }
 
+  @Test
+  public void show() {
+    underTest.show(new ShowRequest()
+      .setProfile("profile")
+      .setCompareToSonarWay(true));
+    GetRequest getRequest = serviceTester.getGetRequest();
+
+    assertThat(serviceTester.getGetParser()).isSameAs(ShowResponse.parser());
+    serviceTester.assertThat(getRequest)
+      .hasPath("show")
+      .hasParam(PARAM_PROFILE, "profile")
+      .hasParam(PARAM_COMPARE_TO_SONAR_WAY, true)
+      .andNoOtherParam();
+  }
+
   @Test
   public void add_project() throws Exception {
     underTest.addProject(AddProjectRequest.builder()