diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-04-17 16:32:23 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-04-20 11:44:43 +0200 |
commit | 5f49af5dab1f5e76bbe1281855cf52cf369b6c95 (patch) | |
tree | 1881411ff22cdb44572435009805fba7483950d6 /server | |
parent | 350db49873cf3c7ef2ff53ca8ed1e22651fea5cf (diff) | |
download | sonarqube-5f49af5dab1f5e76bbe1281855cf52cf369b6c95.tar.gz sonarqube-5f49af5dab1f5e76bbe1281855cf52cf369b6c95.zip |
SONAR-6298 Add project count in qprofile search WS response
Diffstat (limited to 'server')
5 files changed, 41 insertions, 11 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java index 9560c6b1422..8742cd7bb3b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java @@ -28,6 +28,7 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.NewAction; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.util.NonNullInputFunction; import org.sonar.server.qualityprofile.QProfile; import org.sonar.server.qualityprofile.QProfileLoader; @@ -36,7 +37,11 @@ import org.sonar.server.qualityprofile.QProfileLookup; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.*; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; public class QProfileSearchAction implements BaseQProfileWsAction { @@ -49,8 +54,11 @@ public class QProfileSearchAction implements BaseQProfileWsAction { private static final String FIELD_PARENT_KEY = "parentKey"; private static final String FIELD_PARENT_NAME = "parentName"; private static final String FIELD_ACTIVE_RULE_COUNT = "activeRuleCount"; + private static final String FIELD_PROJECT_COUNT = "projectCount"; + private static final Set<String> ALL_FIELDS = ImmutableSet.of( - FIELD_KEY, FIELD_NAME, FIELD_LANGUAGE, FIELD_LANGUAGE_NAME, FIELD_IS_INHERITED, FIELD_PARENT_KEY, FIELD_PARENT_NAME, FIELD_IS_DEFAULT, FIELD_ACTIVE_RULE_COUNT); + FIELD_KEY, FIELD_NAME, FIELD_LANGUAGE, FIELD_LANGUAGE_NAME, FIELD_IS_INHERITED, FIELD_PARENT_KEY, FIELD_PARENT_NAME, FIELD_IS_DEFAULT, FIELD_ACTIVE_RULE_COUNT, + FIELD_PROJECT_COUNT); private static final String PARAM_LANGUAGE = FIELD_LANGUAGE; private static final String PARAM_FIELDS = "f"; @@ -62,10 +70,13 @@ public class QProfileSearchAction implements BaseQProfileWsAction { private final QProfileLoader profileLoader; - public QProfileSearchAction(Languages languages, QProfileLookup profileLookup, QProfileLoader profileLoader) { + private final QualityProfileDao qualityProfileDao; + + public QProfileSearchAction(Languages languages, QProfileLookup profileLookup, QProfileLoader profileLoader, QualityProfileDao qualityProfileDao) { this.languages = languages; this.profileLookup = profileLookup; this.profileLoader = profileLoader; + this.qualityProfileDao = qualityProfileDao; } @Override @@ -123,6 +134,7 @@ public class QProfileSearchAction implements BaseQProfileWsAction { } }); Map<String, Long> activeRuleCountByKey = profileLoader.countAllActiveRules(); + Map<String, Long> projectCountByKey = qualityProfileDao.countProjectsByProfileKey(); json.name("profiles") @@ -135,10 +147,15 @@ public class QProfileSearchAction implements BaseQProfileWsAction { String key = profile.key(); Long activeRuleCount = activeRuleCountByKey.containsKey(key) ? activeRuleCountByKey.get(key) : 0L; + Long projectCount = projectCountByKey.containsKey(key) ? projectCountByKey.get(key) : 0L; json.beginObject() .prop(FIELD_KEY, nullUnlessNeeded(FIELD_KEY, key, fields)) .prop(FIELD_NAME, nullUnlessNeeded(FIELD_NAME, profile.name(), fields)) .prop(FIELD_ACTIVE_RULE_COUNT, nullUnlessNeeded(FIELD_ACTIVE_RULE_COUNT, activeRuleCount, fields)); + + if (!profile.isDefault()) { + json.prop(FIELD_PROJECT_COUNT, nullUnlessNeeded(FIELD_PROJECT_COUNT, projectCount, fields)); + } writeLanguageFields(json, profile, fields); writeParentFields(json, profile, fields, profilesByKey); // Special case for booleans diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-search.json b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-search.json index 9c796636896..1a0a85588f6 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-search.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/qualityprofile/ws/example-search.json @@ -6,7 +6,8 @@ "language": "cs", "languageName": "C#", "isInherited": false, - "activeRuleCount": 37 + "activeRuleCount": 37, + "isDefault": true }, { "key": "my-bu-profile-java-34567", @@ -16,7 +17,9 @@ "isInherited": true, "parentKey": "my-company-profile-java-23456", "parentName": "My Company Profile", - "activeRuleCount": 72 + "activeRuleCount": 72, + "isDefault": false, + "projectCount": 13 }, { "key": "my-company-profile-java-23456", @@ -33,7 +36,8 @@ "language": "py", "languageName": "Python", "isInherited": false, - "activeRuleCount": 125 + "activeRuleCount": 125, + "isDefault": true } ] } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java index 4d78c497a72..6b93a45b05a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java @@ -31,6 +31,8 @@ import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.DbTester; import org.sonar.core.qualityprofile.db.QualityProfileDao; import org.sonar.core.qualityprofile.db.QualityProfileDto; +import org.sonar.server.component.ComponentTesting; +import org.sonar.server.component.db.ComponentDao; import org.sonar.server.db.DbClient; import org.sonar.server.language.LanguageTesting; import org.sonar.server.qualityprofile.QProfileLoader; @@ -74,7 +76,7 @@ public class QProfileSearchActionTest { mock(RuleActivationActions.class), mock(BulkRuleActivationActions.class), mock(ProjectAssociationActions.class), - new QProfileSearchAction(new Languages(xoo1, xoo2), new QProfileLookup(dbClient), profileLoader))); + new QProfileSearchAction(new Languages(xoo1, xoo2), new QProfileLookup(dbClient), profileLoader, qualityProfileDao))); } @After @@ -95,6 +97,11 @@ public class QProfileSearchActionTest { QualityProfileDto.createFor("my-sonar-way-xoo2-34567").setLanguage(xoo2.getKey()).setName("My Sonar way").setParentKee("sonar-way-xoo2-23456"), QualityProfileDto.createFor("sonar-way-other-666").setLanguage("other").setName("Sonar way").setDefault(true) ); + new ComponentDao(mock(System2.class)).insert(session, + ComponentTesting.newProjectDto("project-uuid1"), + ComponentTesting.newProjectDto("project-uuid2")); + qualityProfileDao.insertProjectProfileAssociation("project-uuid1", "sonar-way-xoo2-23456", session); + qualityProfileDao.insertProjectProfileAssociation("project-uuid2", "sonar-way-xoo2-23456", session); session.commit(); tester.newGetRequest("api/qualityprofiles", "search").execute().assertJson(this.getClass(), "search.json"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index 592033e1a61..653bf535936 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -63,7 +63,7 @@ public class QProfilesWsTest { new QProfileCreateAction(null, null, null, languages, importers), new QProfileImportersAction(importers), new QProfileRestoreBuiltInAction(null), - new QProfileSearchAction(languages, null, null), + new QProfileSearchAction(languages, null, null, null), new QProfileSetDefaultAction(languages, null, null), new QProfileProjectsAction(null), new QProfileBackupAction(null, null, null, languages), @@ -121,7 +121,7 @@ public class QProfilesWsTest { assertThat(search.params()).hasSize(2); assertThat(search.param("language").possibleValues()).containsOnly(xoo1Key, xoo2Key); assertThat(search.param("f").possibleValues()) - .containsOnly("key", "name", "language", "languageName", "isInherited", "parentKey", "parentName", "isDefault", "activeRuleCount"); + .containsOnly("key", "name", "language", "languageName", "isInherited", "parentKey", "parentName", "isDefault", "activeRuleCount", "projectCount"); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest/search.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest/search.json index b800f3a3bce..6c051e7ac96 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest/search.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest/search.json @@ -18,7 +18,8 @@ "isDefault": false, "parentKey": "sonar-way-xoo2-23456", "parentName": "Sonar way", - "activeRuleCount": 33 + "activeRuleCount": 33, + "projectCount": 0 }, { "key": "sonar-way-xoo2-23456", @@ -27,7 +28,8 @@ "languageName": "Xoo2", "isInherited": false, "isDefault": false, - "activeRuleCount": 0 + "activeRuleCount": 0, + "projectCount": 2 } ] }
\ No newline at end of file |