From: Teryk Bellahsene Date: Thu, 29 Jun 2017 13:16:16 +0000 (+0200) Subject: SONAR-9482 Add WS client for api/qualityprofiles/show X-Git-Tag: 6.5-M2~16 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=82e6624f67a8cb93aea2a2517db879801b8e9346;p=sonarqube.git SONAR-9482 Add WS client for api/qualityprofiles/show --- 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 index cff1902a7b4..9a293eeb25d 100644 --- 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 @@ -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()) 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 index 4d06ff2ff1b..5e133f330c9 100644 --- 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 @@ -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); } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java index f52962bee5c..96b2d45c204 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java @@ -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"; diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java index 46e327963ee..431f310be56 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java @@ -20,9 +20,11 @@ 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 index 00000000000..2cbcfe95dcd --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ShowRequest.java @@ -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; + } +} diff --git a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto index 0f1ddd6cb0b..3e19aaf63a7 100644 --- a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto +++ b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto @@ -101,7 +101,7 @@ message CopyWsResponse { } // WS api/qualityprofiles/show -message ShowWsResponse { +message ShowResponse { optional QualityProfile profile = 1; optional CompareToSonarWay compareToSonarWay = 2; diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java index 316d97d77b3..1ea8262b646 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java @@ -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()