aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-04-17 11:22:38 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-04-17 11:22:38 +0200
commite5c9087d3bb50e74a80b64624d6425dc5cda4641 (patch)
treea4a5a829a10cc84457bed32f0baab5e7f21c3b5e
parent803a3432fd937479db19aff857a9c9357ccca5fe (diff)
downloadsonarqube-e5c9087d3bb50e74a80b64624d6425dc5cda4641.tar.gz
sonarqube-e5c9087d3bb50e74a80b64624d6425dc5cda4641.zip
Fix quality flaws
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileActivity.java6
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndex.java18
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java6
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangeParentAction.java2
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCompareAction.java57
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCopyAction.java4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileInheritanceAction.java42
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest.java21
-rw-r--r--server/sonar-server/src/test/resources/org/sonar/server/qualityprofile/ws/QProfileCopyActionTest/copy_with_parent.json9
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/ws/WebService.java7
10 files changed, 117 insertions, 55 deletions
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<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;
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<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();
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java
index c7010b4f3e5..f7d6557a1c5 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileBackupAction.java
@@ -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;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangeParentAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangeParentAction.java
index 59f28d38c88..17a171d665e 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangeParentAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileChangeParentAction.java
@@ -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)) {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCompareAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCompareAction.java
index d193872a597..abbcedbf59e 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCompareAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileCompareAction.java
@@ -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());
}
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<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();
}
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<WebService.Contex
/**
* Executed once at server startup.
*/
+ @Override
void define(Context context);
}