]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6822 Rules search WS is not returning langName
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 19 Oct 2015 16:05:28 +0000 (18:05 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 20 Oct 2015 08:59:38 +0000 (10:59 +0200)
/api/rules/search?f=langName is now returning langName
/api/rules/search?f=lang does not return anymore langName

server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules.json
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules_inheritance.json

index 3e084ef6a27d0c20f7bea7c9fc9aa42b0046d0fc..b4933b6871e0e595419d335f65f35208ae17df7f 100644 (file)
@@ -21,12 +21,6 @@ package org.sonar.server.rule.ws;
 
 import com.google.common.base.Function;
 import com.google.common.collect.Maps;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.rule.RuleKey;
@@ -47,6 +41,14 @@ import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.Common;
 import org.sonarqube.ws.Rules;
 
+import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
 import static com.google.common.collect.FluentIterable.from;
 import static org.sonar.api.utils.DateUtils.formatDateTime;
 
@@ -141,6 +143,7 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> {
     setSeverity(ruleResponse, ruleDoc, fieldsToReturn);
     setInternalKey(ruleResponse, ruleDoc, fieldsToReturn);
     setLanguage(ruleResponse, ruleDoc, fieldsToReturn);
+    setLanguageName(ruleResponse, ruleDoc, fieldsToReturn);
     setIsTemplate(ruleResponse, ruleDoc, fieldsToReturn);
     setTemplateKey(ruleResponse, ruleDoc, fieldsToReturn);
     setDebtRemediationFunctionFields(ruleResponse, ruleDoc, fieldsToReturn);
@@ -314,13 +317,17 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> {
     }
   }
 
-  private void setLanguage(Rules.Rule.Builder ruleResponse, Rule ruleDoc, Set<String> fieldsToReturn) {
+  private static void setLanguage(Rules.Rule.Builder ruleResponse, Rule ruleDoc, Set<String> fieldsToReturn) {
     if (shouldReturnField(fieldsToReturn, RuleNormalizer.RuleField.LANGUAGE) && ruleDoc.language() != null) {
       ruleResponse.setLang(ruleDoc.language());
-      Language language = languages.get(ruleDoc.language());
-      if (language != null) {
-        ruleResponse.setLangName(language.getName());
-      }
+    }
+  }
+
+  private void setLanguageName(Rules.Rule.Builder ruleResponse, Rule ruleDoc, Set<String> fieldsToReturn) {
+    if (shouldReturnField(fieldsToReturn, "langName") && ruleDoc.language() != null) {
+      String languageKey = ruleDoc.language();
+      Language language = languages.get(languageKey);
+      ruleResponse.setLangName(language == null ? languageKey : language.getName());
     }
   }
 
index 3ab6e08f26e752a278bb081702ec967c7d4a28d1..bc2fe0be698ebe4043df532a656207cc6c903794 100644 (file)
@@ -1,9 +1,25 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
 package org.sonar.server.rule.ws;
 
 import com.google.common.collect.ImmutableSet;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.Date;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -34,10 +50,16 @@ import org.sonar.server.tester.ServerTester;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsTester;
 
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
 public class SearchActionMediumTest {
 
   @ClassRule
-  public static ServerTester tester = new ServerTester();
+  public static ServerTester tester = new ServerTester().addXoo();
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
 
@@ -118,6 +140,46 @@ public class SearchActionMediumTest {
     result.assertJson(getClass(), "search_2_rules_fields.json");
   }
 
+  @Test
+  public void return_lang_field() throws Exception {
+    ruleDao.insert(session, RuleTesting.newXooX1());
+    session.commit();
+
+    WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "lang");
+    WsTester.Result result = request.execute();
+
+    result.assertJson("{\"total\":1,\"p\":1,\"ps\":100," +
+      "\"rules\":[{\"key\":\"xoo:x1\",\"lang\":\"xoo\"}]}");
+    assertThat(result.outputAsString()).doesNotContain("\"langName\"");
+    assertThat(result.outputAsString()).doesNotContain("\"name\"");
+  }
+
+  @Test
+  public void return_lang_name_field() throws Exception {
+    ruleDao.insert(session, RuleTesting.newXooX1());
+    session.commit();
+
+    WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "langName");
+    WsTester.Result result = request.execute();
+
+    result.assertJson("{\"total\":1,\"p\":1,\"ps\":100," +
+      "\"rules\":[{\"key\":\"xoo:x1\",\"langName\":\"Xoo\"}]}");
+    assertThat(result.outputAsString()).doesNotContain("\"lang\"");
+    assertThat(result.outputAsString()).doesNotContain("\"name\"");
+  }
+
+  @Test
+  public void return_lang_key_field_when_language_name_is_not_available() throws Exception {
+    ruleDao.insert(session, RuleTesting.newDto(RuleKey.of("other", "rule"))).setLanguage("unknown");
+    session.commit();
+
+    WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD).setParam(WebService.Param.FIELDS, "langName");
+    WsTester.Result result = request.execute();
+
+    result.assertJson("{\"total\":1,\"p\":1,\"ps\":100," +
+      "\"rules\":[{\"key\":\"other:rule\",\"langName\":\"unknown\"}]}");
+  }
+
   @Test
   public void search_debt_rules() throws Exception {
     insertDebtCharacteristics(session);
index adc48218f3e38e97499a70071c543c282a15bf36..016101f2a44890b1263c85a4282ffcee663ff6a9 100644 (file)
     "XOO_P2": {
       "name": "P2",
       "lang": "xoo",
-      "langName": "xoo",
+      "langName": "Xoo",
       "parent": "XOO_P1"
     },
     "XOO_P1": {
       "name": "P1",
       "lang": "xoo",
-      "langName": "xoo"
+      "langName": "Xoo"
     }
   }
 }