+++ /dev/null
-/*
- * 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();
- }
- }
-}
*/
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;
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;
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 {
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 {
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;
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;
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;
*/
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
.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);
}
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;
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;
--- /dev/null
+/*
+ * 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();
+ }
+ }
+}
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),
*/
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();
}
}