aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-03-31 10:59:48 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-04-01 10:49:25 +0200
commitb418299c41982e6b5c2d63ce342a5d550eab64d9 (patch)
treeb101fa3d534fb5b3609291629aa6f4d0211e7e9f
parent67e2278d37142eed525696714b0b585d03daefe0 (diff)
downloadsonarqube-b418299c41982e6b5c2d63ce342a5d550eab64d9.tar.gz
sonarqube-b418299c41982e6b5c2d63ce342a5d550eab64d9.zip
Refactor declaration of language related params in Q profiles WS
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/LanguageParamUtils.java40
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java12
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java10
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultAction.java13
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/language/LanguageTesting.java55
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java16
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultActionTest.java62
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java46
8 files changed, 140 insertions, 114 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/LanguageParamUtils.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/LanguageParamUtils.java
new file mode 100644
index 00000000000..a8ff5ee260f
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/LanguageParamUtils.java
@@ -0,0 +1,40 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.qualityprofile.ws;
+
+import com.google.common.collect.Collections2;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.core.util.NonNullInputFunction;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+class LanguageParamUtils {
+
+ static Collection<String> getLanguageKeys(Languages languages) {
+ return Collections2.transform(Arrays.asList(languages.all()), new NonNullInputFunction<Language, String>() {
+ @Override
+ public String doApply(Language input) {
+ return input.getKey();
+ }
+ });
+ }
+}
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 08e7411cf85..ded3b7f597e 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,13 +20,10 @@
package org.sonar.server.qualityprofile.ws;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Collections2;
import org.sonar.api.ServerComponent;
-import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.*;
import org.sonar.api.server.ws.WebService.NewAction;
-import org.sonar.core.util.NonNullInputFunction;
import org.sonar.server.component.ComponentService;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.qualityprofile.QProfile;
@@ -34,8 +31,6 @@ import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.qualityprofile.QProfileProjectOperations;
import org.sonar.server.user.UserSession;
-import java.util.Arrays;
-
import static org.apache.commons.lang.StringUtils.isEmpty;
public class ProjectAssociationActions implements ServerComponent {
@@ -94,12 +89,7 @@ public class ProjectAssociationActions implements ServerComponent {
.setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate.");
action.createParam(PARAM_LANGUAGE)
.setDescription("A quality profile language. If this parameter is set, profileKey must not be set and profileName must be set to disambiguate.")
- .setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new NonNullInputFunction<Language, String>() {
- @Override
- public String doApply(Language input) {
- return input.getKey();
- }
- }));
+ .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages));
}
private abstract static class AssociationHandler implements RequestHandler {
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java
index fcdfea11a98..cb13f83630f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSearchAction.java
@@ -19,12 +19,9 @@
*/
package org.sonar.server.qualityprofile.ws;
-import com.google.common.base.Function;
-import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import org.apache.commons.lang.builder.CompareToBuilder;
-import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
@@ -169,12 +166,7 @@ public class QProfileSearchAction implements BaseQProfileWsAction {
search.createParam(PARAM_LANGUAGE)
.setDescription("The key of a language supported by the platform. If specified, only profiles for the given language are returned.")
.setExampleValue("js")
- .setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new Function<Language, String>() {
- @Override
- public String apply(Language input) {
- return input.getKey();
- }
- }));
+ .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages));
search.createParam(PARAM_FIELDS)
.setDescription("Use to restrict returned fields.")
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultAction.java
index ec88a6c165c..462b626205a 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultAction.java
@@ -19,10 +19,7 @@
*/
package org.sonar.server.qualityprofile.ws;
-import com.google.common.base.Function;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Collections2;
-import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
@@ -35,8 +32,6 @@ import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.user.UserSession;
-import java.util.Arrays;
-
import static org.apache.commons.lang.StringUtils.isEmpty;
public class QProfileSetDefaultAction implements BaseQProfileWsAction {
@@ -90,18 +85,14 @@ public class QProfileSetDefaultAction implements BaseQProfileWsAction {
NewAction setDefault = controller.createAction("set_default")
.setSince("5.2")
.setDescription("Select the default profile for a given language.")
+ .setPost(true)
.setHandler(this)
.setResponseExample(getClass().getResource("example-search.json"));
setDefault.createParam(PARAM_LANGUAGE)
.setDescription("The key of a language supported by the platform. If specified, profileName must be set to select the default profile for the selected language.")
.setExampleValue("js")
- .setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new Function<Language, String>() {
- @Override
- public String apply(Language input) {
- return input.getKey();
- }
- }));
+ .setPossibleValues(LanguageParamUtils.getLanguageKeys(languages));
setDefault.createParam(PARAM_PROFILE_NAME)
.setDescription("The name of a quality profile. If specified, language must be set. The matching profile will be used as default for the selected language.")
diff --git a/server/sonar-server/src/test/java/org/sonar/server/language/LanguageTesting.java b/server/sonar-server/src/test/java/org/sonar/server/language/LanguageTesting.java
new file mode 100644
index 00000000000..667db85187a
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/language/LanguageTesting.java
@@ -0,0 +1,55 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.language;
+
+import com.google.common.collect.Collections2;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.resources.AbstractLanguage;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.core.util.NonNullInputFunction;
+
+import java.util.Arrays;
+
+public class LanguageTesting {
+
+ public static Language newLanguage(String key, String name, final String... prefixes) {
+ return new AbstractLanguage(key, name) {
+ @Override
+ public String[] getFileSuffixes() {
+ return prefixes;
+ }
+ };
+ }
+
+ public static Language newLanguage(String key) {
+ return newLanguage(key, StringUtils.capitalize(key));
+ }
+
+ public static Languages newLanguages(String... languageKeys) {
+ return new Languages(Collections2.transform(Arrays.asList(languageKeys), new NonNullInputFunction<String, Language>() {
+ @Override
+ protected Language doApply(String languageKey) {
+ return newLanguage(languageKey);
+ }
+
+ }).toArray(new Language[0]));
+ }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java
index 44165ee0516..6252a9d81e0 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSearchActionTest.java
@@ -20,12 +20,10 @@
package org.sonar.server.qualityprofile.ws;
import com.google.common.collect.ImmutableMap;
-import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
-import org.sonar.api.resources.AbstractLanguage;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.utils.System2;
@@ -34,6 +32,7 @@ import org.sonar.core.persistence.DbTester;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.server.db.DbClient;
+import org.sonar.server.language.LanguageTesting;
import org.sonar.server.qualityprofile.QProfileLoader;
import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.ws.WsTester;
@@ -68,8 +67,8 @@ public class QProfileSearchActionTest {
// TODO Replace with actual implementation after removal of DaoV2...
profileLoader = mock(QProfileLoader.class);
- xoo1 = createLanguage("xoo1");
- xoo2 = createLanguage("xoo2");
+ xoo1 = LanguageTesting.newLanguage("xoo1");
+ xoo2 = LanguageTesting.newLanguage("xoo2");
tester = new WsTester(new QProfilesWs(
mock(RuleActivationActions.class),
@@ -128,13 +127,4 @@ public class QProfileSearchActionTest {
tester.newGetRequest("api/qualityprofiles", "search").setParam("language", xoo1.getKey()).execute().assertJson(this.getClass(), "search_xoo1.json");
}
-
- private Language createLanguage(final String key) {
- return new AbstractLanguage(key, StringUtils.capitalize(key)) {
- @Override
- public String[] getFileSuffixes() {
- return new String[] {key};
- }
- };
- }
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultActionTest.java
index 8dac75022ab..fbb13d55de1 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileSetDefaultActionTest.java
@@ -19,17 +19,11 @@
*/
package org.sonar.server.qualityprofile.ws;
-import org.sonar.server.exceptions.ForbiddenException;
-
-import org.apache.commons.lang.StringUtils;
import org.assertj.core.api.Fail;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
-import org.sonar.api.resources.AbstractLanguage;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
import org.sonar.api.utils.System2;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
@@ -37,11 +31,14 @@ import org.sonar.core.persistence.DbTester;
import org.sonar.core.qualityprofile.db.QualityProfileDao;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.server.db.DbClient;
+import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.language.LanguageTesting;
import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.user.MockUserSession;
import org.sonar.server.ws.WsTester;
+
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@@ -54,7 +51,7 @@ public class QProfileSetDefaultActionTest {
private QualityProfileDao qualityProfileDao;
- private Language xoo1, xoo2;
+ private String xoo1Key = "xoo1", xoo2Key = "xoo2";
private WsTester tester;
@@ -68,15 +65,13 @@ public class QProfileSetDefaultActionTest {
dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), qualityProfileDao);
session = dbClient.openSession(false);
- xoo1 = createLanguage("xoo1");
- xoo2 = createLanguage("xoo2");
createProfiles();
tester = new WsTester(new QProfilesWs(
mock(RuleActivationActions.class),
mock(BulkRuleActivationActions.class),
mock(ProjectAssociationActions.class),
- new QProfileSetDefaultAction(new Languages(xoo1, xoo2), new QProfileLookup(dbClient), new QProfileFactory(dbClient))));
+ new QProfileSetDefaultAction(LanguageTesting.newLanguages(xoo1Key, xoo2Key), new QProfileLookup(dbClient), new QProfileFactory(dbClient))));
}
@After
@@ -89,30 +84,30 @@ public class QProfileSetDefaultActionTest {
MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- checkDefaultProfile("xoo1", "sonar-way-xoo1-12345");
- checkDefaultProfile("xoo2", "my-sonar-way-xoo2-34567");
+ checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
+ checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");
tester.newPostRequest("api/qualityprofiles", "set_default").setParam("profileKey", "sonar-way-xoo2-23456").execute().assertNoContent();
- checkDefaultProfile("xoo1", "sonar-way-xoo1-12345");
- checkDefaultProfile("xoo2", "sonar-way-xoo2-23456");
+ checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
+ checkDefaultProfile(xoo2Key, "sonar-way-xoo2-23456");
assertThat(dbClient.qualityProfileDao().getByKey(session, "sonar-way-xoo2-23456").isDefault()).isTrue();
assertThat(dbClient.qualityProfileDao().getByKey(session, "my-sonar-way-xoo2-34567").isDefault()).isFalse();
// One more time!
tester.newPostRequest("api/qualityprofiles", "set_default").setParam("profileKey", "sonar-way-xoo2-23456").execute().assertNoContent();
- checkDefaultProfile("xoo1", "sonar-way-xoo1-12345");
- checkDefaultProfile("xoo2", "sonar-way-xoo2-23456");
+ checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
+ checkDefaultProfile(xoo2Key, "sonar-way-xoo2-23456");
}
@Test
public void set_default_profile_using_language_and_name() throws Exception {
MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- tester.newPostRequest("api/qualityprofiles", "set_default").setParam("language", "xoo2").setParam("profileName", "Sonar way").execute().assertNoContent();
+ tester.newPostRequest("api/qualityprofiles", "set_default").setParam("language", xoo2Key).setParam("profileName", "Sonar way").execute().assertNoContent();
- checkDefaultProfile("xoo1", "sonar-way-xoo1-12345");
- checkDefaultProfile("xoo2", "sonar-way-xoo2-23456");
+ checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
+ checkDefaultProfile(xoo2Key, "sonar-way-xoo2-23456");
}
@Test
@@ -124,8 +119,8 @@ public class QProfileSetDefaultActionTest {
Fail.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
} catch(IllegalArgumentException nfe) {
assertThat(nfe).hasMessage("Quality profile not found: unknown-profile-666");
- checkDefaultProfile("xoo1", "sonar-way-xoo1-12345");
- checkDefaultProfile("xoo2", "my-sonar-way-xoo2-34567");
+ checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
+ checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");
}
}
@@ -135,12 +130,12 @@ public class QProfileSetDefaultActionTest {
MockUserSession.set().setLogin("obiwan").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
try {
- tester.newPostRequest("api/qualityprofiles", "set_default").setParam("language", "xoo2").setParam("profileName", "Unknown").execute();
+ tester.newPostRequest("api/qualityprofiles", "set_default").setParam("language", xoo2Key).setParam("profileName", "Unknown").execute();
Fail.failBecauseExceptionWasNotThrown(NotFoundException.class);
} catch(NotFoundException nfe) {
assertThat(nfe).hasMessage("Unable to find a profile for language 'xoo2' with name 'Unknown'");
- checkDefaultProfile("xoo1", "sonar-way-xoo1-12345");
- checkDefaultProfile("xoo2", "my-sonar-way-xoo2-34567");
+ checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
+ checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");
}
}
@@ -152,16 +147,16 @@ public class QProfileSetDefaultActionTest {
tester.newPostRequest("api/qualityprofiles", "set_default").setParam("profileKey", "sonar-way-xoo2-23456").execute().assertNoContent();
Fail.failBecauseExceptionWasNotThrown(ForbiddenException.class);
} catch(ForbiddenException forbidden) {
- checkDefaultProfile("xoo1", "sonar-way-xoo1-12345");
- checkDefaultProfile("xoo2", "my-sonar-way-xoo2-34567");
+ checkDefaultProfile(xoo1Key, "sonar-way-xoo1-12345");
+ checkDefaultProfile(xoo2Key, "my-sonar-way-xoo2-34567");
}
}
private void createProfiles() {
qualityProfileDao.insert(session,
- QualityProfileDto.createFor("sonar-way-xoo1-12345").setLanguage(xoo1.getKey()).setName("Sonar way").setDefault(true),
- QualityProfileDto.createFor("sonar-way-xoo2-23456").setLanguage(xoo2.getKey()).setName("Sonar way"),
- QualityProfileDto.createFor("my-sonar-way-xoo2-34567").setLanguage(xoo2.getKey()).setName("My Sonar way").setParentKee("sonar-way-xoo2-23456").setDefault(true)
+ QualityProfileDto.createFor("sonar-way-xoo1-12345").setLanguage(xoo1Key).setName("Sonar way").setDefault(true),
+ QualityProfileDto.createFor("sonar-way-xoo2-23456").setLanguage(xoo2Key).setName("Sonar way"),
+ QualityProfileDto.createFor("my-sonar-way-xoo2-34567").setLanguage(xoo2Key).setName("My Sonar way").setParentKee("sonar-way-xoo2-23456").setDefault(true)
);
session.commit();
}
@@ -169,13 +164,4 @@ public class QProfileSetDefaultActionTest {
private void checkDefaultProfile(String language, String key) throws Exception {
assertThat(dbClient.qualityProfileDao().getDefaultProfile(language).getKey()).isEqualTo(key);
}
-
- private Language createLanguage(final String key) {
- return new AbstractLanguage(key, StringUtils.capitalize(key)) {
- @Override
- public String[] getFileSuffixes() {
- return new String[] {key};
- }
- };
- }
}
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 c2eb08cab48..e48291f8221 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
@@ -20,14 +20,12 @@
package org.sonar.server.qualityprofile.ws;
-import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.i18n.I18n;
-import org.sonar.api.resources.AbstractLanguage;
-import org.sonar.api.resources.Language;
import org.sonar.api.resources.Languages;
import org.sonar.api.server.ws.WebService;
+import org.sonar.server.language.LanguageTesting;
import org.sonar.server.qualityprofile.QProfileService;
import org.sonar.server.rule.RuleService;
import org.sonar.server.ws.WsTester;
@@ -39,38 +37,23 @@ public class QProfilesWsTest {
WebService.Controller controller;
- Language xoo1, xoo2;
+ String xoo1Key = "xoo1", xoo2Key = "xoo2";
@Before
public void setUp() {
- Language xoo1 = new AbstractLanguage("xoo1", "Xoo1") {
- @Override
- public String[] getFileSuffixes() {
- return new String[] {"xoo1"};
- }
- };
- Language xoo2 = new AbstractLanguage("xoo2", "Xoo2") {
- @Override
- public String[] getFileSuffixes() {
- return new String[] {"xoo2"};
- }
- };
-
QProfileService profileService = mock(QProfileService.class);
RuleService ruleService = mock(RuleService.class);
I18n i18n = mock(I18n.class);
- xoo1 = createLanguage("xoo1");
- xoo2 = createLanguage("xoo2");
- Languages languages = new Languages(xoo1, xoo2);
+ Languages languages = LanguageTesting.newLanguages(xoo1Key, xoo2Key);
controller = new WsTester(new QProfilesWs(
new RuleActivationActions(profileService),
new BulkRuleActivationActions(profileService, ruleService, i18n),
new ProjectAssociationActions(null, null, null, languages),
- new QProfileRestoreBuiltInAction(
- mock(QProfileService.class)),
- new QProfileSearchAction(new Languages(xoo1, xoo2), null, null)
+ new QProfileRestoreBuiltInAction(null),
+ new QProfileSearchAction(languages, null, null),
+ new QProfileSetDefaultAction(languages, null, null)
)).controller(QProfilesWs.API_ENDPOINT);
}
@@ -79,7 +62,7 @@ public class QProfilesWsTest {
assertThat(controller).isNotNull();
assertThat(controller.path()).isEqualTo(QProfilesWs.API_ENDPOINT);
assertThat(controller.description()).isNotEmpty();
- assertThat(controller.actions()).hasSize(8);
+ assertThat(controller.actions()).hasSize(9);
}
@Test
@@ -96,7 +79,7 @@ public class QProfilesWsTest {
assertThat(search).isNotNull();
assertThat(search.isPost()).isFalse();
assertThat(search.params()).hasSize(2);
- assertThat(search.param("language").possibleValues()).containsOnly("xoo1", "xoo2");
+ assertThat(search.param("language").possibleValues()).containsOnly(xoo1Key, xoo2Key);
assertThat(search.param("f").possibleValues())
.containsOnly("key", "name", "language", "languageName", "isInherited", "parentKey", "parentName", "isDefault", "activeRuleCount");
}
@@ -133,13 +116,12 @@ public class QProfilesWsTest {
assertThat(removeProject.params()).hasSize(5);
}
- private Language createLanguage(final String key) {
- return new AbstractLanguage(key, StringUtils.capitalize(key)) {
- @Override
- public String[] getFileSuffixes() {
- return new String[] {key};
- }
- };
+ @Test
+ public void define_set_default_action() throws Exception {
+ WebService.Action setDefault = controller.action("set_default");
+ assertThat(setDefault).isNotNull();
+ assertThat(setDefault.isPost()).isTrue();
+ assertThat(setDefault.params()).hasSize(3);
}
public void define_bulk_activate_rule_action() throws Exception {