]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11545 Languages should be ordered in documentation of web api
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 29 Nov 2018 16:21:38 +0000 (17:21 +0100)
committerSonarTech <sonartech@sonarsource.com>
Wed, 12 Dec 2018 19:21:03 +0000 (20:21 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java
server/sonar-server/src/test/java/org/sonar/server/util/LanguageParamUtilsTest.java [new file with mode: 0644]

index 8135f4f67dcc690c846a6e4092583b3bd714b2dd..f52ed24b31ccde985ac461a5cee798844ff58cec 100644 (file)
@@ -52,7 +52,7 @@ import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.core.util.stream.MoreCollectors.toHashSet;
 import static org.sonar.server.es.SearchOptions.MAX_LIMIT;
 import static org.sonar.server.util.LanguageParamUtils.getExampleValue;
-import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys;
+import static org.sonar.server.util.LanguageParamUtils.getOrderedLanguageKeys;
 import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
 import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
@@ -107,7 +107,7 @@ public class SearchAction implements ComponentsWsAction {
       .createParam(PARAM_LANGUAGE)
       .setDescription("Language key. If provided, only components for the given language are returned.")
       .setExampleValue(getExampleValue(languages))
-      .setPossibleValues(getLanguageKeys(languages));
+      .setPossibleValues(getOrderedLanguageKeys(languages));
     createQualifiersParameter(action, newQualifierParameterContext(i18n, resourceTypes))
       .setRequired(true);
   }
index 0d29b328fe25e417683f3b404e4a5f51f3302385..ca2946056822c0a7622a3e133888dd510aab8c77 100644 (file)
@@ -43,7 +43,7 @@ import javax.annotation.Nullable;
 import static com.google.common.base.Preconditions.checkArgument;
 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
 import static org.sonar.server.qualityprofile.ws.QProfileWsSupport.createOrganizationParam;
-import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys;
+import static org.sonar.server.util.LanguageParamUtils.getOrderedLanguageKeys;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_CREATE;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
@@ -105,7 +105,7 @@ public class CreateAction implements QProfileWsAction {
       .setRequired(true)
       .setDescription("Quality profile language")
       .setExampleValue("js")
-      .setPossibleValues(getLanguageKeys(languages));
+      .setPossibleValues(getOrderedLanguageKeys(languages));
 
     for (ProfileImporter importer : importers) {
       create.createParam(getBackupParamName(importer.getKey()))
index a40fd1c0ada449e51c54fc27728a0cbce26279ac..fcf8a8863c3e91b26fda72c552ce1eae038899e5 100644 (file)
@@ -94,7 +94,7 @@ public class ExportAction implements QProfileWsAction {
     action.createParam(PARAM_LANGUAGE)
       .setDescription("Quality profile language")
       .setExampleValue(LanguageParamUtils.getExampleValue(languages))
-      .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages));
+      .setPossibleValues(LanguageParamUtils.getOrderedLanguageKeys(languages));
 
     createOrganizationParam(action)
       .setSince("6.4");
index 6714fb217e5d57d5be7f5b00465410958e83c890..adee9d6e9eabededc014d18bebbd2ae6961a186d 100644 (file)
@@ -120,7 +120,7 @@ public class SearchAction implements QProfileWsAction {
     action
       .createParam(PARAM_LANGUAGE)
       .setDescription("Language key. If provided, only profiles for the given language are returned.")
-      .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages));
+      .setPossibleValues(LanguageParamUtils.getOrderedLanguageKeys(languages));
 
     action.createParam(PARAM_QUALITY_PROFILE)
       .setDescription("Quality profile name")
index 827c74abe897117be61d96660f83625ae947396f..84ccbab325a0b087fe2b1098d04346b709bf56be 100644 (file)
  */
 package org.sonar.server.util;
 
-import com.google.common.base.Function;
-import com.google.common.collect.Collections2;
 import java.util.Arrays;
-import java.util.Collection;
-import javax.annotation.Nonnull;
+import java.util.List;
 import org.sonar.api.resources.Language;
 import org.sonar.api.resources.Languages;
+import org.sonar.core.util.stream.MoreCollectors;
 
 public class LanguageParamUtils {
 
@@ -42,16 +40,11 @@ public class LanguageParamUtils {
     }
   }
 
-  public static Collection<String> getLanguageKeys(Languages languages) {
-    return Collections2.transform(Arrays.asList(languages.all()), LanguageToKeyFunction.INSTANCE);
-  }
-
-  private enum LanguageToKeyFunction implements Function<Language, java.lang.String> {
-    INSTANCE;
-
-    @Override
-    public String apply(@Nonnull Language input) {
-      return input.getKey();
-    }
+  public static List<String> getOrderedLanguageKeys(Languages languages) {
+    Language[] all = languages.all();
+    return Arrays.stream(all)
+      .map(Language::getKey)
+      .sorted()
+      .collect(MoreCollectors.toList(all.length));
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/util/LanguageParamUtilsTest.java b/server/sonar-server/src/test/java/org/sonar/server/util/LanguageParamUtilsTest.java
new file mode 100644 (file)
index 0000000..db9e28f
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.util;
+
+import org.junit.Test;
+import org.sonar.api.resources.AbstractLanguage;
+import org.sonar.api.resources.Languages;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class LanguageParamUtilsTest {
+
+  @Test
+  public void getOrderedLanguageKeys() {
+    assertThat(LanguageParamUtils.getOrderedLanguageKeys(new Languages())).isEmpty();
+
+    Languages languages = new Languages(
+      new TestLanguage("java"),
+      new TestLanguage("abap"),
+      new TestLanguage("js"),
+      new TestLanguage("cobol"));
+    assertThat(LanguageParamUtils.getOrderedLanguageKeys(languages)).containsExactly("abap", "cobol", "java", "js");
+  }
+
+  private static class TestLanguage extends AbstractLanguage {
+    TestLanguage(String key) {
+      super(key);
+    }
+
+    @Override
+    public String[] getFileSuffixes() {
+      return new String[0];
+    }
+  }
+}