]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 17 Apr 2015 09:22:38 +0000 (11:22 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 17 Apr 2015 09:22:38 +0000 (11:22 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangeParentAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCompareAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileInheritanceAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_with_parent.json [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java

index acf03166504ee0c360b7b0a17a040b405ece7e71..d77c7b2bd422bbf90c69bcec4ca8f63f92e078d6 100644 (file)
@@ -89,9 +89,9 @@ public class QProfileActivity extends ActivityDoc {
 
   public Map<String, String> parameters() {
     Map<String, String> params = Maps.newHashMap();
-    for (String key : fields.keySet()) {
-      if (key.startsWith("param_")) {
-        params.put(key.replace("param_", ""), (String) fields.get(key));
+    for (Map.Entry<String, Object> param : fields.entrySet()) {
+      if (param.getKey().startsWith("param_")) {
+        params.put(param.getKey().replace("param_", ""), (String) param.getValue());
       }
     }
     return params;
index 1cc6fee6c3201d4ddae5d9d247a6b3f76446e070..ff5892613e674a125778d955859009540627334c 100644 (file)
@@ -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<ActiveRule, ActiveRuleDto, ActiveRuleKey> {
 
+  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<ActiveRule, ActiveRuleDto, Active
           .field(ActiveRuleNormalizer.ActiveRuleField.INHERITANCE.field()))
         .subAggregation(AggregationBuilders.terms(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field())
           .field(ActiveRuleNormalizer.ActiveRuleField.SEVERITY.field()))
-        .subAggregation(AggregationBuilders.count("countActiveRules")))
+        .subAggregation(AggregationBuilders.count(COUNT_ACTIVE_RULES)))
       .setSize(0)
       .setTypes(this.getIndexType());
     SearchResponse response = request.get();
index c7010b4f3e57896a8320603aaae7b66ab940e537..f7d6557a1c51f70a10aca1fce1a167cc01d2bb77 100644 (file)
@@ -22,8 +22,10 @@ package org.sonar.server.qualityprofile.ws;
 import com.google.common.base.Charsets;
 import org.apache.commons.io.IOUtils;
 import org.sonar.api.resources.Languages;
-import org.sonar.api.server.ws.*;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.Response.Stream;
+import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.server.db.DbClient;
@@ -34,8 +36,6 @@ import java.io.OutputStreamWriter;
 
 public class QProfileBackupAction implements BaseQProfileWsAction {
 
-  private static final String PARAM_KEY = "key";
-
   private final QProfileBackuper backuper;
 
   private final DbClient dbClient;
index 59f28d38c8863eed58fe14206dc60a640f423ef8..17a171d665e373f1f97ac62087a3a82abf518a16 100644 (file)
@@ -98,7 +98,7 @@ public class QProfileChangeParentAction implements BaseQProfileWsAction {
     String parentKey = request.param(PARAM_PARENT_KEY);
 
     Preconditions.checkArgument(
-      (isEmpty(parentName) || isEmpty(parentKey)), "parentKey and parentName cannot be used simultaneously");
+      isEmpty(parentName) || isEmpty(parentKey), "parentKey and parentName cannot be used simultaneously");
 
     if (isEmpty(parentKey)) {
       if (!isEmpty(parentName)) {
index d193872a5970de0d27754dda58a07c7fd30891ed..abbcedbf59e9002b6970d1e2be185d4cd7d58043 100644 (file)
@@ -46,6 +46,21 @@ import java.util.Map.Entry;
 
 public class QProfileCompareAction implements BaseQProfileWsAction {
 
+  private static final String ATTRIBUTE_LEFT = "left";
+  private static final String ATTRIBUTE_RIGHT = "right";
+  private static final String ATTRIBUTE_IN_LEFT = "inLeft";
+  private static final String ATTRIBUTE_IN_RIGHT = "inRight";
+  private static final String ATTRIBUTE_MODIFIED = "modified";
+  private static final String ATTRIBUTE_SAME = "same";
+  private static final String ATTRIBUTE_KEY = "key";
+  private static final String ATTRIBUTE_NAME = "name";
+  private static final String ATTRIBUTE_SEVERITY = "severity";
+  private static final String ATTRIBUTE_PLUGIN_KEY = "pluginKey";
+  private static final String ATTRIBUTE_PLUGIN_NAME = "pluginName";
+  private static final String ATTRIBUTE_LANGUAGE_KEY = "languageKey";
+  private static final String ATTRIBUTE_LANGUAGE_NAME = "languageName";
+  private static final String ATTRIBUTE_PARAMS = "params";
+
   private static final String PARAM_LEFT_KEY = "leftKey";
   private static final String PARAM_RIGHT_KEY = "rightKey";
 
@@ -102,32 +117,32 @@ public class QProfileCompareAction implements BaseQProfileWsAction {
   private void writeResult(JsonWriter json, QProfileComparisonResult result, Map<RuleKey, Rule> 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<RuleKey, ActiveRule> activeRules, Map<RuleKey, Rule> 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<RuleKey, Rule> 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<String, ValueDifference<String>> 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<String, ValueDifference<String>> valueDiff : value.paramDifference().entriesDiffering().entrySet()) {
         json.prop(valueDiff.getKey(), valueDiff.getValue().rightValue());
       }
index 7281dad8939e00a62246242fd93df8dc39c2551a..79ad6630ad83ab5c77695bcee416c866ecdfaa50 100644 (file)
@@ -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();
   }
 }
index 57ec4fdae774887376cc41a2ccf8016ea24866de..ed3c13c5fee12c2759da1aab88fe9a91139fa1c7 100644 (file)
@@ -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<String, Multimap<String, FacetValue>> 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<QProfile> ancestors, Map<String, Multimap<String, FacetValue>> 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<String, Multimap<String, FacetValue>> 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<String, Multimap<String, FacetValue>> profileStats) {
     if (profileStats.containsKey(profileKey)) {
       Multimap<String, FacetValue> ancestorStats = profileStats.get(profileKey);
@@ -148,16 +148,16 @@ public class QProfileInheritanceAction implements BaseQProfileWsAction {
 
   private Long getActiveRuleCount(Multimap<String, FacetValue> 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<String, FacetValue> 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();
         }
index 872bc690f235007f761f70d54ca8f1f8a3c9b465..0b2d31a3e5870aa74374c7e85798a95f87bdf2dd 100644 (file)
@@ -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 (file)
index 0000000..9fefdca
--- /dev/null
@@ -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
index 20e7c97fae45e086d3a45adab7c8316ee0af14bd..af429ac9b69f1a741ced7da044c51a609bc8fb0f 100644 (file)
@@ -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<WebService.Contex
   /**
    * Executed once at server startup.
    */
+  @Override
   void define(Context context);
 
 }