]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7737 api/qualityprofiles/restore_built_in throws an error if language does... 1238/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 12 Sep 2016 14:38:34 +0000 (16:38 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 13 Sep 2016 09:50:46 +0000 (11:50 +0200)
13 files changed:
server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java [deleted file]
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/ProjectAssociationActions.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileFinder.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileIdentificationParamUtils.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SetDefaultAction.java
server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInActionTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java
deleted file mode 100644 (file)
index e6e967d..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.component.ws;
-
-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 org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-
-public class LanguageParamUtils {
-
-  private LanguageParamUtils() {
-    // Utility class
-  }
-
-  public static String getExampleValue(Languages languages) {
-    Language[] languageArray = languages.all();
-    if (languageArray.length > 0) {
-      return languageArray[0].getKey();
-    } else {
-      return "";
-    }
-  }
-
-  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();
-    }
-  }
-}
index 19244fa409110056e03f3fca0487d08e3da4b9b4..1e6aa56116b121164542c91f257f211676b0d1f7 100644 (file)
  */
 package org.sonar.server.component.ws;
 
-import static com.google.common.collect.FluentIterable.from;
-import static org.sonar.server.ws.WsParameterBuilder.QualifierParameterContext.newQualifierParameterContext;
-import static org.sonar.server.ws.WsParameterBuilder.createQualifiersParameter;
-import static org.sonar.server.ws.WsUtils.writeProtobuf;
-import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_LANGUAGE;
-import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_QUALIFIERS;
-
 import com.google.common.base.Function;
 import java.util.List;
 import javax.annotation.Nonnull;
@@ -43,10 +36,18 @@ import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentQuery;
 import org.sonar.server.user.UserSession;
+import org.sonar.server.util.LanguageParamUtils;
 import org.sonarqube.ws.WsComponents;
 import org.sonarqube.ws.WsComponents.SearchWsResponse;
 import org.sonarqube.ws.client.component.SearchWsRequest;
 
+import static com.google.common.collect.FluentIterable.from;
+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;
+import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_LANGUAGE;
+import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_QUALIFIERS;
+
 public class SearchAction implements ComponentsWsAction {
   private final DbClient dbClient;
   private final ResourceTypes resourceTypes;
index f9c41bac66fcf04c0ad99b917ae3e6a702a2a4f7..c0088d45fe48eb00d7c9f6d6b169aad9b0f2d6b7 100644 (file)
@@ -33,13 +33,12 @@ import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.component.ws.LanguageParamUtils;
 import org.sonar.server.qualityprofile.QProfileExporters;
 import org.sonar.server.qualityprofile.QProfileFactory;
 import org.sonar.server.qualityprofile.QProfileName;
 import org.sonar.server.qualityprofile.QProfileResult;
 import org.sonar.server.user.UserSession;
-import org.sonarqube.ws.MediaTypes;
+import org.sonar.server.util.LanguageParamUtils;
 
 public class CreateAction implements QProfileWsAction {
 
index ac00436cc7880dd7b3b977b4a8980763fa004c45..04605c9d18a4903e316d4e27598ad2b3c6434690 100644 (file)
@@ -37,12 +37,12 @@ import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.component.ws.LanguageParamUtils;
 import org.sonar.server.exceptions.NotFoundException;
-import org.sonarqube.ws.MediaTypes;
 import org.sonar.server.qualityprofile.QProfileBackuper;
 import org.sonar.server.qualityprofile.QProfileExporters;
 import org.sonar.server.qualityprofile.QProfileFactory;
+import org.sonar.server.util.LanguageParamUtils;
+import org.sonarqube.ws.MediaTypes;
 
 public class ExportAction implements QProfileWsAction {
 
index 896907eba3eebc18bfd9b88d4ffdbd6db202abdd..26eb864f70856797657885ca4c998ca4f285fe2d 100644 (file)
 package org.sonar.server.qualityprofile.ws;
 
 import com.google.common.base.Preconditions;
-import org.sonar.api.server.ServerSide;
 import org.sonar.api.resources.Languages;
+import org.sonar.api.server.ServerSide;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.server.component.ComponentService;
-import org.sonar.server.component.ws.LanguageParamUtils;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.qualityprofile.QProfile;
 import org.sonar.server.qualityprofile.QProfileLookup;
 import org.sonar.server.qualityprofile.QProfileProjectOperations;
 import org.sonar.server.user.UserSession;
+import org.sonar.server.util.LanguageParamUtils;
 
 import static org.apache.commons.lang.StringUtils.isEmpty;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
index b1c25095c5d43fbbdcd3aab9ec5fdb0ad3329137..b2bd1b2c7b2965e5e05d22452ee0946441657fb7 100644 (file)
@@ -25,8 +25,8 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.component.ws.LanguageParamUtils;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.util.LanguageParamUtils;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.lang.String.format;
index 122b8add805281e416463465a7220568cc3d5fa4..0a4452c5a6bbf0076fae267196f113eb51bdf09c 100644 (file)
@@ -25,9 +25,9 @@ import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.QualityProfileDto;
-import org.sonar.server.component.ws.LanguageParamUtils;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.qualityprofile.QProfileFactory;
+import org.sonar.server.util.LanguageParamUtils;
 
 import static org.apache.commons.lang.StringUtils.isEmpty;
 
index 4f84cdf0f02b49793723837e5312f9bde5f1b00e..f6873d5ce5657ef9bf6507d0419fb0d4221937c8 100644 (file)
  */
 package org.sonar.server.qualityprofile.ws;
 
+import org.sonar.api.resources.Languages;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.server.qualityprofile.QProfileService;
 
+import static org.sonar.server.util.LanguageParamUtils.getExampleValue;
+import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys;
+
 public class RestoreBuiltInAction implements QProfileWsAction {
 
   private final QProfileService service;
+  private final Languages languages;
 
-  public RestoreBuiltInAction(QProfileService service) {
+  public RestoreBuiltInAction(QProfileService service, Languages languages) {
     this.service = service;
+    this.languages = languages;
   }
 
   @Override
@@ -42,7 +48,8 @@ public class RestoreBuiltInAction implements QProfileWsAction {
       .setHandler(this);
     restoreDefault.createParam("language")
       .setDescription("Restore the built-in profiles defined for this language")
-      .setExampleValue("java")
+      .setExampleValue(getExampleValue(languages))
+      .setPossibleValues(getLanguageKeys(languages))
       .setRequired(true);
   }
 
index 42eb3d3a0c4f53a8e8c38319e4497d8ffe4be0b4..10732fc1dccfbf3d0feaa0d91f6e471e4e1b820e 100644 (file)
@@ -27,8 +27,8 @@ import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.NewAction;
-import org.sonar.server.component.ws.LanguageParamUtils;
 import org.sonar.server.qualityprofile.QProfile;
+import org.sonar.server.util.LanguageParamUtils;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse.QualityProfile;
 import org.sonarqube.ws.client.qualityprofile.SearchWsRequest;
index 34ed895995338aff972fef9152ee8f7e891d8466..3e59b1b017e4a2fd4b3c3974ca0e5a8ab5f4f1c6 100644 (file)
@@ -26,12 +26,12 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.NewAction;
 import org.sonar.core.permission.GlobalPermissions;
-import org.sonar.server.component.ws.LanguageParamUtils;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.qualityprofile.QProfile;
 import org.sonar.server.qualityprofile.QProfileFactory;
 import org.sonar.server.qualityprofile.QProfileLookup;
 import org.sonar.server.user.UserSession;
+import org.sonar.server.util.LanguageParamUtils;
 
 import static org.apache.commons.lang.StringUtils.isEmpty;
 
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
new file mode 100644 (file)
index 0000000..c8feec7
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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 com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+import java.util.Arrays;
+import java.util.Collection;
+import javax.annotation.Nonnull;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+
+public class LanguageParamUtils {
+
+  private LanguageParamUtils() {
+    // Utility class
+  }
+
+  public static String getExampleValue(Languages languages) {
+    Language[] languageArray = languages.all();
+    if (languageArray.length > 0) {
+      return languageArray[0].getKey();
+    } else {
+      return "";
+    }
+  }
+
+  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();
+    }
+  }
+}
index 09db9cd1c4dc57ffcefbb6c281f892f3fa945e16..3ddc714390ba90d8b560e859fc490c35af541863 100644 (file)
@@ -63,7 +63,7 @@ public class QProfilesWsTest {
       new ProjectAssociationActions(null, null, null, languages, userSessionRule),
       new CreateAction(null, null, null, languages, importers, userSessionRule),
       new ImportersAction(importers),
-      new RestoreBuiltInAction(null),
+      new RestoreBuiltInAction(null, languages),
       new SearchAction(null, languages),
       new SetDefaultAction(languages, null, null, userSessionRule),
       new ProjectsAction(null, userSessionRule),
index 93f19e29ca02aff2223e62ee8d205491ea79ee0c..d4cd38c53cd308fba4008342841695345a691bc8 100644 (file)
  */
 package org.sonar.server.qualityprofile.ws;
 
-import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.i18n.I18n;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.resources.Languages;
+import org.sonar.server.language.LanguageTesting;
 import org.sonar.server.qualityprofile.QProfileService;
-import org.sonar.server.ws.WsTester;
+import org.sonar.server.ws.TestResponse;
+import org.sonar.server.ws.WsActionTester;
 
+import static org.assertj.core.api.Java6Assertions.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
-@RunWith(MockitoJUnitRunner.class)
 public class RestoreBuiltInActionTest {
 
-  @Mock
-  QProfileService profileService;
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
 
-  @Mock
-  I18n i18n;
+  QProfileService profileService = mock(QProfileService.class);
+  Languages languages = LanguageTesting.newLanguages("xoo");
 
-  WsTester tester;
+  WsActionTester tester = new WsActionTester(new RestoreBuiltInAction(profileService, languages));
 
-  @Before
-  public void setUp() {
-    tester = new WsTester(new QProfilesWs(
-      mock(RuleActivationActions.class),
-      mock(BulkRuleActivationActions.class),
-      mock(ProjectAssociationActions.class),
-      new RestoreBuiltInAction(profileService)));
+  @Test
+  public void return_empty_result_when_no_info_or_warning() {
+    TestResponse response = tester.newRequest().setParam("language", "xoo").execute();
+
+    verify(profileService).restoreBuiltInProfilesForLanguage("xoo");
+    assertThat(response.getStatus()).isEqualTo(204);
   }
 
   @Test
-  public void return_empty_result_when_no_infos_or_warnings() throws Exception {
-    WsTester.TestRequest request = tester.newPostRequest("api/qualityprofiles", "restore_built_in").setParam("language", "java");
-    request.execute().assertNoContent();
+  public void fail_on_unknown_language() throws Exception {
+    expectedException.expect(IllegalArgumentException.class);
+    tester.newRequest().setParam("language", "unknown").execute();
   }
 }