From: Teryk Bellahsene Date: Mon, 29 May 2017 08:07:44 +0000 (+0200) Subject: SONAR-9302 Add the buil-in flag to WS api/qualityprofiles/search X-Git-Tag: 6.5-M1~33 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0fb3084e6c609fb9b94cb67ed7c7ac884502aea3;p=sonarqube.git SONAR-9302 Add the buil-in flag to WS api/qualityprofiles/search --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java index 230cb25eaa9..e3134c55607 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java @@ -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(); diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/search-example.json b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/search-example.json index b9b73044acf..479ee088d55 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/search-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/search-example.json @@ -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, diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java index e3bbeccafe7..fd18ebd8f1f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java @@ -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 getParentKey = qp -> qp.hasParentKey() ? qp.getParentKey() : null; + Function getParentName = qp -> qp.hasParentName() ? qp.getParentName() : null; + Function 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 diff --git a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto index 43aec89576f..12938d67cb7 100644 --- a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto +++ b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto @@ -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;