From 1308b6801babb7d64bec4ffe2e619ea0e4c5cdef Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 27 Jun 2017 18:18:08 +0200 Subject: [PATCH] SONAR-9482 Create api/qualityprofile/show WS returning stub --- .../qualityprofile/ws/QProfilesWsModule.java | 3 +- .../server/qualityprofile/ws/ShowAction.java | 83 +++++++++++++++++ .../qualityprofile/ws/show-example.json | 23 +++++ .../ws/QProfilesWsModuleTest.java | 2 +- .../qualityprofile/ws/ShowActionTest.java | 89 +++++++++++++++++++ .../QualityProfileWsParameters.java | 1 + .../main/protobuf/ws-qualityprofiles.proto | 31 +++++++ 7 files changed, 230 insertions(+), 2 deletions(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ShowAction.java create mode 100644 server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/show-example.json create mode 100644 server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ShowActionTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWsModule.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWsModule.java index 3708ce600ea..f3362d94a61 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWsModule.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWsModule.java @@ -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 index 00000000000..7b4020baf94 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ShowAction.java @@ -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 index 00000000000..60f03aaf168 --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/show-example.json @@ -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 + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java index 60f84b6d05c..1764e6cafcc 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsModuleTest.java @@ -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 index 00000000000..b7f9b98f35d --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ShowActionTest.java @@ -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()); + } +} 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 5ce75182da8..f52962bee5c 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 @@ -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 diff --git a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto index a6f2df7b39a..0dab9b3c06f 100644 --- a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto +++ b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto @@ -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; + } +} -- 2.39.5