]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9302 Add the buil-in flag to WS api/qualityprofiles/search
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 29 May 2017 08:07:44 +0000 (10:07 +0200)
committerEric Hartmann <hartmann.eric@gmail.com>
Wed, 14 Jun 2017 13:43:12 +0000 (15:43 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java
server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/search-example.json
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java
sonar-ws/src/main/protobuf/ws-qualityprofiles.proto

index 230cb25eaa9b21548a0b1a3e5fe6f3bda316fbd5..e3134c55607b6f0397030fda6dafdc12058b45ec 100644 (file)
@@ -44,6 +44,7 @@ import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
 import static java.lang.String.format;
 import static java.util.function.Function.identity;
 import static org.sonar.api.utils.DateUtils.formatDateTime;
+import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
 import static org.sonar.server.ws.WsUtils.checkRequest;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
@@ -194,22 +195,12 @@ public class SearchAction implements QProfileWsAction {
       QualityProfile.Builder profileBuilder = response.addProfilesBuilder();
 
       String profileKey = profile.getKey();
-      if (profile.getOrganizationUuid() != null) {
-        profileBuilder.setOrganization(data.getOrganization().getKey());
-      }
+      setNullable(profile.getOrganizationUuid(), o -> profileBuilder.setOrganization(data.getOrganization().getKey()));
       profileBuilder.setKey(profileKey);
-      if (profile.getName() != null) {
-        profileBuilder.setName(profile.getName());
-      }
-      if (profile.getRulesUpdatedAt() != null) {
-        profileBuilder.setRulesUpdatedAt(profile.getRulesUpdatedAt());
-      }
-      if (profile.getLastUsed() != null) {
-        profileBuilder.setLastUsed(formatDateTime(profile.getLastUsed()));
-      }
-      if (profile.getUserUpdatedAt() != null) {
-        profileBuilder.setUserUpdatedAt(formatDateTime(profile.getUserUpdatedAt()));
-      }
+      setNullable(profile.getName(), profileBuilder::setName);
+      setNullable(profile.getRulesUpdatedAt(), profileBuilder::setRulesUpdatedAt);
+      setNullable(profile.getLastUsed(), last -> profileBuilder.setLastUsed(formatDateTime(last)));
+      setNullable(profile.getUserUpdatedAt(), userUpdatedAt -> profileBuilder.setUserUpdatedAt(formatDateTime(userUpdatedAt)));
       profileBuilder.setActiveRuleCount(data.getActiveRuleCount(profileKey));
       profileBuilder.setActiveDeprecatedRuleCount(data.getActiveDeprecatedRuleCount(profileKey));
       if (!profile.isDefault()) {
@@ -220,6 +211,7 @@ public class SearchAction implements QProfileWsAction {
       writeParentFields(profileBuilder, profile, profilesByKey);
       profileBuilder.setIsInherited(profile.getParentKee() != null);
       profileBuilder.setIsDefault(profile.isDefault());
+      profileBuilder.setIsBuiltIn(profile.isBuiltIn());
     }
 
     return response.build();
index b9b73044acfe26c7193a55514698b248905a43a5..479ee088d553fddb15c36f8ac5a767b8720a2130 100644 (file)
@@ -6,6 +6,7 @@
       "language": "cs",
       "languageName": "C#",
       "isInherited": false,
+      "isBuiltIn": true,
       "activeRuleCount": 37,
       "activeDeprecatedRuleCount": 0,
       "isDefault": true,
@@ -18,6 +19,7 @@
       "language": "java",
       "languageName": "Java",
       "isInherited": true,
+      "isBuiltIn": false,
       "parentKey": "my-company-profile-java-23456",
       "parentName": "My Company Profile",
       "activeRuleCount": 72,
@@ -35,6 +37,7 @@
       "languageName": "Java",
       "isInherited": false,
       "isDefault": true,
+      "isBuiltIn": false,
       "activeRuleCount": 42,
       "activeDeprecatedRuleCount": 3,
       "ruleUpdatedAt": "2016-12-22T19:10:03+0100",
@@ -46,6 +49,7 @@
       "language": "py",
       "languageName": "Python",
       "isInherited": false,
+      "isBuiltIn": true,
       "activeRuleCount": 125,
       "activeDeprecatedRuleCount": 0,
       "isDefault": true,
index e3bbeccafe78d129cee8c3a838b1773b33f79ed2..fd18ebd8f1fafd2c136d46680786ca0d6adc1105 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.server.qualityprofile.ws;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.function.Function;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -58,6 +59,7 @@ import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
 import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
 import static org.sonar.api.utils.DateUtils.parseDateTime;
 import static org.sonar.db.qualityprofile.QualityProfileTesting.newQualityProfileDto;
 import static org.sonar.test.JsonAssert.assertJson;
@@ -98,8 +100,7 @@ public class SearchActionTest {
       new SearchDataLoader(
         languages,
         new QProfileLookup(dbClient),
-        dbClient
-      ),
+        dbClient),
       languages,
       dbClient,
       qProfileWsSupport);
@@ -131,12 +132,14 @@ public class SearchActionTest {
       .setOrganizationUuid(organization.getUuid())
       .setLanguage(xoo1.getKey())
       .setName("Sonar way")
+      .setIsBuiltIn(false)
       .setDefault(true);
     QualityProfileDto parentProfile = QualityProfileDto
       .createFor("sonar-way-xoo2-23456")
       .setOrganizationUuid(organization.getUuid())
       .setLanguage(xoo2.getKey())
-      .setName("Sonar way");
+      .setName("Sonar way")
+      .setIsBuiltIn(true);
     QualityProfileDto childProfile = QualityProfileDto
       .createFor("my-sonar-way-xoo2-34567")
       .setOrganizationUuid(organization.getUuid())
@@ -145,7 +148,8 @@ public class SearchActionTest {
       .setParentKee(parentProfile.getKey());
     QualityProfileDto profileOnUnknownLang = QualityProfileDto.createFor("sonar-way-other-666")
       .setOrganizationUuid(organization.getUuid())
-      .setLanguage("other").setName("Sonar way")
+      .setLanguage("other")
+      .setName("Sonar way")
       .setDefault(true);
     qualityProfileDao.insert(dbSession, defaultProfile, parentProfile, childProfile, profileOnUnknownLang);
 
@@ -154,49 +158,20 @@ public class SearchActionTest {
     db.qualityProfiles().associateProjectWithQualityProfile(project1, parentProfile);
     db.qualityProfiles().associateProjectWithQualityProfile(project2, parentProfile);
 
-    String result = ws.newRequest().execute().getInput();
+    SearchWsResponse result = ws.newRequest().executeProtobuf(SearchWsResponse.class);
 
-    assertJson(result).isSimilarTo("{" +
-      "  \"profiles\": [" +
-      "    {" +
-      "      \"key\": \"sonar-way-xoo1-12345\"," +
-      "      \"name\": \"Sonar way\"," +
-      "      \"language\": \"xoo1\"," +
-      "      \"languageName\": \"Xoo1\"," +
-      "      \"isInherited\": false," +
-      "      \"isDefault\": true," +
-      "      \"activeRuleCount\": 0," +
-      "      \"activeDeprecatedRuleCount\": 0," +
-      "      \"organization\": \"" + organization.getKey() + "\"" +
-      "    }," +
-      "    {" +
-      "      \"key\": \"my-sonar-way-xoo2-34567\"," +
-      "      \"name\": \"My Sonar way\"," +
-      "      \"language\": \"xoo2\"," +
-      "      \"languageName\": \"Xoo2\"," +
-      "      \"isInherited\": true," +
-      "      \"isDefault\": false," +
-      "      \"parentKey\": \"sonar-way-xoo2-23456\"," +
-      "      \"parentName\": \"Sonar way\"," +
-      "      \"activeRuleCount\": 0," +
-      "      \"activeDeprecatedRuleCount\": 0," +
-      "      \"projectCount\": 0," +
-      "      \"organization\": \"" + organization.getKey() + "\"" +
-      "    }," +
-      "    {" +
-      "      \"key\": \"sonar-way-xoo2-23456\"," +
-      "      \"name\": \"Sonar way\"," +
-      "      \"language\": \"xoo2\"," +
-      "      \"languageName\": \"Xoo2\"," +
-      "      \"isInherited\": false," +
-      "      \"isDefault\": false," +
-      "      \"activeRuleCount\": 0," +
-      "      \"activeDeprecatedRuleCount\": 0," +
-      "      \"projectCount\": 2," +
-      "      \"organization\": \"" + organization.getKey() + "\"" +
-      "    }" +
-      "  ]" +
-      "}");
+    Function<QualityProfile, String> getParentKey = qp -> qp.hasParentKey() ? qp.getParentKey() : null;
+    Function<QualityProfile, String> getParentName = qp -> qp.hasParentName() ? qp.getParentName() : null;
+    Function<QualityProfile, Long> getProjectCount = qp -> qp.hasProjectCount() ? qp.getProjectCount() : null;
+    assertThat(result.getProfilesList())
+      .extracting(QualityProfile::getKey, QualityProfile::getName, QualityProfile::getLanguage, QualityProfile::getLanguageName,
+        QualityProfile::getIsInherited, QualityProfile::getIsDefault, QualityProfile::getIsBuiltIn,
+        QualityProfile::getActiveRuleCount, QualityProfile::getActiveDeprecatedRuleCount, getProjectCount,
+        QualityProfile::getOrganization, getParentKey, getParentName)
+      .containsExactlyInAnyOrder(
+        tuple("sonar-way-xoo1-12345", "Sonar way", "xoo1", "Xoo1", false, true, false, 0L, 0L, null, organization.getKey(), null, null),
+        tuple("my-sonar-way-xoo2-34567", "My Sonar way", "xoo2", "Xoo2", true, false, false, 0L, 0L, 0L, organization.getKey(), "sonar-way-xoo2-23456", "Sonar way"),
+        tuple("sonar-way-xoo2-23456", "Sonar way", "xoo2", "Xoo2", false, false, true, 0L, 0L, 2L, organization.getKey(), null, null));
   }
 
   @Test
index 43aec89576f5021724b816170b975f485be4f764..12938d67cb79ddb94c07e229ed1be12ee3e8f9f9 100644 (file)
@@ -44,6 +44,7 @@ message SearchWsResponse {
     optional string lastUsed = 13;
     optional string userUpdatedAt = 14;
     optional string organization = 15;
+    optional bool isBuiltIn = 16;
   }
 }
 
@@ -61,6 +62,7 @@ message CreateWsResponse {
     optional Infos infos = 7;
     optional Warnings warnings = 8;
     optional string organization = 9;
+    optional bool isBuiltIn = 10;
 
     message Infos {
       repeated string infos = 1;