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;
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;
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);
}
}
- 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());
}
}
+/*
+ * 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;
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);
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);