From: Julien Lancelot Date: Mon, 19 Oct 2015 16:05:28 +0000 (+0200) Subject: SONAR-6822 Rules search WS is not returning langName X-Git-Tag: 5.2-RC3~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f08e45093397769d9f15fb000acd54b8a6d03863;p=sonarqube.git SONAR-6822 Rules search WS is not returning langName /api/rules/search?f=langName is now returning langName /api/rules/search?f=lang does not return anymore langName --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java index 3e084ef6a27..b4933b6871e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java @@ -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 { 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 { } } - private void setLanguage(Rules.Rule.Builder ruleResponse, Rule ruleDoc, Set fieldsToReturn) { + private static void setLanguage(Rules.Rule.Builder ruleResponse, Rule ruleDoc, Set 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 fieldsToReturn) { + if (shouldReturnField(fieldsToReturn, "langName") && ruleDoc.language() != null) { + String languageKey = ruleDoc.language(); + Language language = languages.get(languageKey); + ruleResponse.setLangName(language == null ? languageKey : language.getName()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java index 3ab6e08f26e..bc2fe0be698 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/SearchActionMediumTest.java @@ -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); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules.json index 1b7d587e7b0..ec6439cb0a7 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules.json @@ -21,7 +21,7 @@ "XOO_P2": { "name": "P2", "lang": "xoo", - "langName": "xoo" + "langName": "Xoo" } } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules_inheritance.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules_inheritance.json index adc48218f3e..016101f2a44 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules_inheritance.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/SearchActionMediumTest/search_profile_active_rules_inheritance.json @@ -21,13 +21,13 @@ "XOO_P2": { "name": "P2", "lang": "xoo", - "langName": "xoo", + "langName": "Xoo", "parent": "XOO_P1" }, "XOO_P1": { "name": "P1", "lang": "xoo", - "langName": "xoo" + "langName": "Xoo" } } }