From 12cb58546e204cb3989d2d6f917f8372f70f36a8 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 29 Nov 2018 17:21:38 +0100 Subject: [PATCH] SONAR-11545 Languages should be ordered in documentation of web api --- .../server/component/ws/SearchAction.java | 4 +- .../qualityprofile/ws/CreateAction.java | 4 +- .../qualityprofile/ws/ExportAction.java | 2 +- .../qualityprofile/ws/SearchAction.java | 2 +- .../sonar/server/util/LanguageParamUtils.java | 23 +++----- .../server/util/LanguageParamUtilsTest.java | 52 +++++++++++++++++++ 6 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/util/LanguageParamUtilsTest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java index 8135f4f67dc..f52ed24b31c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java @@ -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); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java index 0d29b328fe2..ca294605682 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/CreateAction.java @@ -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())) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java index a40fd1c0ada..fcf8a8863c3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ExportAction.java @@ -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"); diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java index 6714fb217e5..adee9d6e9ea 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java @@ -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") diff --git a/server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java b/server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java index 827c74abe89..84ccbab325a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java +++ b/server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java @@ -19,13 +19,11 @@ */ 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 getLanguageKeys(Languages languages) { - return Collections2.transform(Arrays.asList(languages.all()), LanguageToKeyFunction.INSTANCE); - } - - private enum LanguageToKeyFunction implements Function { - INSTANCE; - - @Override - public String apply(@Nonnull Language input) { - return input.getKey(); - } + public static List 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 index 00000000000..db9e28f0885 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/util/LanguageParamUtilsTest.java @@ -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]; + } + } +} -- 2.39.5