From: Jean-Baptiste Lievremont Date: Fri, 17 Apr 2015 09:22:38 +0000 (+0200) Subject: Fix quality flaws X-Git-Tag: 5.2-RC1~2239 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e5c9087d3bb50e74a80b64624d6425dc5cda4641;p=sonarqube.git Fix quality flaws --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java index acf03166504..d77c7b2bd42 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java @@ -89,9 +89,9 @@ public class QProfileActivity extends ActivityDoc { public Map parameters() { Map params = Maps.newHashMap(); - for (String key : fields.keySet()) { - if (key.startsWith("param_")) { - params.put(key.replace("param_", ""), (String) fields.get(key)); + for (Map.Entry param : fields.entrySet()) { + if (param.getKey().startsWith("param_")) { + params.put(param.getKey().replace("param_", ""), (String) param.getValue()); } } return params; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java index 1cc6fee6c32..ff5892613e6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java @@ -37,12 +37,22 @@ import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.rule.index.RuleNormalizer; -import org.sonar.server.search.*; - -import java.util.*; +import org.sonar.server.search.BaseIndex; +import org.sonar.server.search.FacetValue; +import org.sonar.server.search.IndexDefinition; +import org.sonar.server.search.IndexField; +import org.sonar.server.search.SearchClient; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; public class ActiveRuleIndex extends BaseIndex { + public static final String COUNT_ACTIVE_RULES = "countActiveRules"; + public ActiveRuleIndex(ActiveRuleNormalizer normalizer, SearchClient node) { super(IndexDefinition.ACTIVE_RULE, normalizer, node); } @@ -171,7 +181,7 @@ public class ActiveRuleIndex extends BaseIndex rulesByKey) { json.beginObject(); - json.name("left").beginObject(); + json.name(ATTRIBUTE_LEFT).beginObject(); writeProfile(json, result.left()); json.endObject(); - json.name("right").beginObject(); + json.name(ATTRIBUTE_RIGHT).beginObject(); writeProfile(json, result.right()); json.endObject(); - json.name("inLeft"); + json.name(ATTRIBUTE_IN_LEFT); writeRules(json, result.inLeft(), rulesByKey); - json.name("inRight"); + json.name(ATTRIBUTE_IN_RIGHT); writeRules(json, result.inRight(), rulesByKey); - json.name("modified"); + json.name(ATTRIBUTE_MODIFIED); writeDifferences(json, result.modified(), rulesByKey); - json.name("same"); + json.name(ATTRIBUTE_SAME); writeRules(json, result.same(), rulesByKey); json.endObject().close(); } private void writeProfile(JsonWriter json, QualityProfileDto profile) { - json.prop("key", profile.getKey()) - .prop("name", profile.getName()); + json.prop(ATTRIBUTE_KEY, profile.getKey()) + .prop(ATTRIBUTE_NAME, profile.getName()); } private void writeRules(JsonWriter json, Map activeRules, Map rulesByKey) { @@ -138,7 +153,7 @@ public class QProfileCompareAction implements BaseQProfileWsAction { json.beginObject(); writeRule(json, key, rulesByKey); - json.prop("severity", value.severity()); + json.prop(ATTRIBUTE_SEVERITY, value.severity()); json.endObject(); } json.endArray(); @@ -146,18 +161,18 @@ public class QProfileCompareAction implements BaseQProfileWsAction { private void writeRule(JsonWriter json, RuleKey key, Map rulesByKey) { String repositoryKey = key.repository(); - json.prop("key", key.toString()) - .prop("name", rulesByKey.get(key).name()) - .prop("pluginKey", repositoryKey); + json.prop(ATTRIBUTE_KEY, key.toString()) + .prop(ATTRIBUTE_NAME, rulesByKey.get(key).name()) + .prop(ATTRIBUTE_PLUGIN_KEY, repositoryKey); Repository repo = ruleRepositories.repository(repositoryKey); if (repo != null) { String languageKey = repo.getLanguage(); Language language = languages.get(languageKey); - json.prop("pluginName", repo.getName()); - json.prop("languageKey", languageKey); - json.prop("languageName", language == null ? null : language.getName()); + json.prop(ATTRIBUTE_PLUGIN_NAME, repo.getName()); + json.prop(ATTRIBUTE_LANGUAGE_KEY, languageKey); + json.prop(ATTRIBUTE_LANGUAGE_NAME, language == null ? null : language.getName()); } } @@ -169,9 +184,9 @@ public class QProfileCompareAction implements BaseQProfileWsAction { json.beginObject(); writeRule(json, key, rulesByKey); - json.name("left").beginObject(); - json.prop("severity", value.leftSeverity()); - json.name("params").beginObject(); + json.name(ATTRIBUTE_LEFT).beginObject(); + json.prop(ATTRIBUTE_SEVERITY, value.leftSeverity()); + json.name(ATTRIBUTE_PARAMS).beginObject(); for (Entry> valueDiff : value.paramDifference().entriesDiffering().entrySet()) { json.prop(valueDiff.getKey(), valueDiff.getValue().leftValue()); } @@ -183,9 +198,9 @@ public class QProfileCompareAction implements BaseQProfileWsAction { // left json.endObject(); - json.name("right").beginObject(); - json.prop("severity", value.rightSeverity()); - json.name("params").beginObject(); + json.name(ATTRIBUTE_RIGHT).beginObject(); + json.prop(ATTRIBUTE_SEVERITY, value.rightSeverity()); + json.name(ATTRIBUTE_PARAMS).beginObject(); for (Entry> valueDiff : value.paramDifference().entriesDiffering().entrySet()) { json.prop(valueDiff.getKey(), valueDiff.getValue().rightValue()); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java index 7281dad8939..79ad6630ad8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java @@ -73,6 +73,7 @@ public class QProfileCopyAction implements BaseQProfileWsAction { String languageKey = copiedProfile.getLanguage(); Language language = languages.get(copiedProfile.getLanguage()); + String parentKey = copiedProfile.getParentKee(); response.newJsonWriter() .beginObject() .prop("key", copiedProfile.getKey()) @@ -80,7 +81,8 @@ public class QProfileCopyAction implements BaseQProfileWsAction { .prop("language", languageKey) .prop("languageName", language == null ? null : language.getName()) .prop("isDefault", copiedProfile.isDefault()) - .prop("isInherited", copiedProfile.getParentKee() != null) + .prop("isInherited", parentKey != null) + .prop("parentKey", parentKey) .endObject().close(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileInheritanceAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileInheritanceAction.java index 57ec4fdae77..ed3c13c5fee 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileInheritanceAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileInheritanceAction.java @@ -34,8 +34,12 @@ import org.sonar.server.qualityprofile.QProfile; import org.sonar.server.qualityprofile.QProfileFactory; import org.sonar.server.qualityprofile.QProfileLoader; import org.sonar.server.qualityprofile.QProfileLookup; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex; +import org.sonar.server.qualityprofile.index.ActiveRuleNormalizer; import org.sonar.server.search.FacetValue; +import javax.annotation.Nullable; + import java.util.List; import java.util.Map; @@ -101,24 +105,15 @@ public class QProfileInheritanceAction implements BaseQProfileWsAction { private void writeProfile(JsonWriter json, QualityProfileDto profile, Map> profileStats) { String profileKey = profile.getKey(); - json.name("profile").beginObject() - .prop("key", profileKey) - .prop("name", profile.getName()) - .prop("parent", profile.getParentKee()); - writeStats(json, profileKey, profileStats); - json.endObject(); + json.name("profile"); + writeProfileAttributes(json, profileKey, profile.getName(), profile.getParentKee(), profileStats); } private void writeAncestors(JsonWriter json, List ancestors, Map> profileStats) { json.name("ancestors").beginArray(); for (QProfile ancestor : ancestors) { String ancestorKey = ancestor.key(); - json.beginObject() - .prop("key", ancestorKey) - .prop("name", ancestor.name()) - .prop("parent", ancestor.parent()); - writeStats(json, ancestorKey, profileStats); - json.endObject(); + writeProfileAttributes(json, ancestorKey, ancestor.name(), ancestor.parent(), profileStats); } json.endArray(); } @@ -127,15 +122,20 @@ public class QProfileInheritanceAction implements BaseQProfileWsAction { json.name("children").beginArray(); for (QualityProfileDto child : children) { String childKey = child.getKey(); - json.beginObject() - .prop("key", childKey) - .prop("name", child.getName()); - writeStats(json, childKey, profileStats); - json.endObject(); + writeProfileAttributes(json, childKey, child.getName(), null, profileStats); } json.endArray(); } + private void writeProfileAttributes(JsonWriter json, String key, String name, @Nullable String parentKey, Map> profileStats) { + json.beginObject(); + json.prop("key", key) + .prop("name", name) + .prop("parent", parentKey); + writeStats(json, key, profileStats); + json.endObject(); + } + private void writeStats(JsonWriter json, String profileKey, Map> profileStats) { if (profileStats.containsKey(profileKey)) { Multimap ancestorStats = profileStats.get(profileKey); @@ -148,16 +148,16 @@ public class QProfileInheritanceAction implements BaseQProfileWsAction { private Long getActiveRuleCount(Multimap profileStats) { Long result = null; - if (profileStats.containsKey("countActiveRules")) { - result = profileStats.get("countActiveRules").iterator().next().getValue(); + if (profileStats.containsKey(ActiveRuleIndex.COUNT_ACTIVE_RULES)) { + result = profileStats.get(ActiveRuleIndex.COUNT_ACTIVE_RULES).iterator().next().getValue(); } return result; } private Long getOverridingRuleCount(Multimap profileStats) { Long result = null; - if (profileStats.containsKey("inheritance")) { - for (FacetValue value : profileStats.get("inheritance")) { + if (profileStats.containsKey(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())) { + for (FacetValue value : profileStats.get(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field())) { if ("OVERRIDES".equals(value.getKey())) { result = value.getValue(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java index 872bc690f23..0b2d31a3e58 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java @@ -74,6 +74,27 @@ public class QProfileCopyActionTest { verify(qProfileCopier).copyToName(fromProfileKey, toName); } + @Test + public void copy_with_parent() throws Exception { + MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); + + String fromProfileKey = "xoo-sonar-way-23456"; + String toName = "Other Sonar Way"; + + when(qProfileCopier.copyToName(fromProfileKey, toName)).thenReturn( + QualityProfileDto.createFor("xoo-other-sonar-way-12345") + .setName(toName) + .setLanguage("xoo") + .setParentKee("xoo-parent-profile-01324")); + + tester.newPostRequest("api/qualityprofiles", "copy") + .setParam("fromKey", fromProfileKey) + .setParam("toName", toName) + .execute().assertJson(getClass(), "copy_with_parent.json"); + + verify(qProfileCopier).copyToName(fromProfileKey, toName); + } + @Test(expected = IllegalArgumentException.class) public void fail_on_missing_key() throws Exception { MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_with_parent.json b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_with_parent.json new file mode 100644 index 00000000000..9fefdca7912 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_with_parent.json @@ -0,0 +1,9 @@ +{ + "key": "xoo-other-sonar-way-12345", + "name": "Other Sonar Way", + "language": "xoo", + "languageName": "Xoo", + "isDefault": false, + "isInherited": true, + "parentKey": "xoo-parent-profile-01324" +} \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java index 20e7c97fae4..af429ac9b69 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java @@ -35,7 +35,11 @@ import javax.annotation.concurrent.Immutable; import java.io.IOException; import java.net.URL; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * Defines a web service. Note that contrary to the deprecated {@link org.sonar.api.web.Webservice} @@ -657,6 +661,7 @@ public interface WebService extends ServerExtension, Definable