From d1efd83084b271eb1ae3d1bc60f744fe8372b1ab Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 12 Sep 2016 16:38:34 +0200 Subject: [PATCH] SONAR-7737 api/qualityprofiles/restore_built_in throws an error if language does not exist --- .../server/component/ws/SearchAction.java | 15 ++++--- .../qualityprofile/ws/CreateAction.java | 3 +- .../qualityprofile/ws/ExportAction.java | 4 +- .../ws/ProjectAssociationActions.java | 4 +- .../qualityprofile/ws/QProfileFinder.java | 2 +- .../ws/QProfileIdentificationParamUtils.java | 2 +- .../ws/RestoreBuiltInAction.java | 11 ++++- .../qualityprofile/ws/SearchAction.java | 2 +- .../qualityprofile/ws/SetDefaultAction.java | 2 +- .../ws => util}/LanguageParamUtils.java | 2 +- .../qualityprofile/ws/QProfilesWsTest.java | 2 +- .../ws/RestoreBuiltInActionTest.java | 44 +++++++++---------- 12 files changed, 50 insertions(+), 43 deletions(-) rename server/sonar-server/src/main/java/org/sonar/server/{component/ws => util}/LanguageParamUtils.java (97%) 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 19244fa4091..1e6aa56116b 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 @@ -19,13 +19,6 @@ */ 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; 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 f9c41bac66f..c0088d45fe4 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 @@ -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 { 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 ac00436cc78..04605c9d18a 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 @@ -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 { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java index 896907eba3e..26eb864f708 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java @@ -20,20 +20,20 @@ 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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileFinder.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileFinder.java index b1c25095c5d..b2bd1b2c7b2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileFinder.java @@ -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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileIdentificationParamUtils.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileIdentificationParamUtils.java index 122b8add805..0a4452c5a6b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileIdentificationParamUtils.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileIdentificationParamUtils.java @@ -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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInAction.java index 4f84cdf0f02..f6873d5ce56 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInAction.java @@ -19,17 +19,23 @@ */ 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); } 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 42eb3d3a0c4..10732fc1dcc 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 @@ -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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SetDefaultAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SetDefaultAction.java index 34ed8959953..3e59b1b017e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SetDefaultAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SetDefaultAction.java @@ -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/component/ws/LanguageParamUtils.java b/server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java similarity index 97% rename from server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java rename to server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java index e6e967dee41..c8feec7f802 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java +++ b/server/sonar-server/src/main/java/org/sonar/server/util/LanguageParamUtils.java @@ -17,7 +17,7 @@ * 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; +package org.sonar.server.util; import com.google.common.base.Function; import com.google.common.collect.Collections2; diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java index 09db9cd1c4d..3ddc714390b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java @@ -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), diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInActionTest.java index 93f19e29ca0..d4cd38c53cd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/RestoreBuiltInActionTest.java @@ -19,40 +19,40 @@ */ 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(); } } -- 2.39.5