aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-03-25 14:22:56 +0100
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2015-03-26 14:51:08 +0100
commitc361f824ba490fe69138205c56e2679800497405 (patch)
treeae10841b8c98da19be87470717b047313ccf433f
parent2bc5c0c7689068f5403d474901d264458afa036a (diff)
downloadsonarqube-c361f824ba490fe69138205c56e2679800497405.tar.gz
sonarqube-c361f824ba490fe69138205c56e2679800497405.zip
SONAR-5489 Add web services to manage project/profile association
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java1
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java24
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java12
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java147
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java6
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java16
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java14
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRestoreBuiltInActionTest.java3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java141
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsTest.java41
11 files changed, 368 insertions, 39 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
index 50644f9683d..7bd9c24ddbf 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
@@ -383,6 +383,7 @@ class ServerComponents {
pico.addSingleton(ProfilesWs.class);
pico.addSingleton(RuleActivationActions.class);
pico.addSingleton(BulkRuleActivationActions.class);
+ pico.addSingleton(ProjectAssociationActions.class);
pico.addSingleton(RuleActivator.class);
pico.addSingleton(QProfileLoader.class);
pico.addSingleton(QProfileExporters.class);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java
index a2d71d79753..897d6264815 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java
@@ -43,31 +43,31 @@ public class QProfileProjectOperations implements ServerComponent {
this.db = db;
}
- public void addProject(int profileId, long projectId, UserSession userSession) {
+ public void addProject(String profileKey, String projectUuid, UserSession userSession) {
DbSession session = db.openSession(false);
try {
- addProject(profileId, projectId, userSession, session);
+ addProject(profileKey, projectUuid, userSession, session);
session.commit();
} finally {
MyBatis.closeQuietly(session);
}
}
- void addProject(int profileId, long projectId, UserSession userSession, DbSession session) {
- ComponentDto project = db.componentDao().getById(projectId, session);
+ void addProject(String profileKey, String projectUuid, UserSession userSession, DbSession session) {
+ ComponentDto project = db.componentDao().getByUuid(session, projectUuid);
checkPermission(userSession, project.key());
- QualityProfileDto qualityProfile = findNotNull(profileId, session);
+ QualityProfileDto qualityProfile = findNotNull(profileKey, session);
db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
session.commit();
}
- public void removeProject(int profileId, long projectId, UserSession userSession) {
+ public void removeProject(String profileKey, String projectUuid, UserSession userSession) {
DbSession session = db.openSession(false);
try {
- ComponentDto project = db.componentDao().getById(projectId, session);
+ ComponentDto project = db.componentDao().getByUuid(session, projectUuid);
checkPermission(userSession, project.key());
- QualityProfileDto qualityProfile = findNotNull(profileId, session);
+ QualityProfileDto qualityProfile = findNotNull(profileKey, session);
db.qualityProfileDao().deleteProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
session.commit();
@@ -92,11 +92,11 @@ public class QProfileProjectOperations implements ServerComponent {
}
}
- public void removeAllProjects(int profileId, UserSession userSession) {
+ public void removeAllProjects(String profileKey, UserSession userSession) {
checkPermission(userSession);
DbSession session = db.openSession(false);
try {
- QualityProfileDto qualityProfile = findNotNull(profileId, session);
+ QualityProfileDto qualityProfile = findNotNull(profileKey, session);
db.qualityProfileDao().deleteAllProjectProfileAssociation(qualityProfile.getKey(), session);
session.commit();
} finally {
@@ -104,8 +104,8 @@ public class QProfileProjectOperations implements ServerComponent {
}
}
- private QualityProfileDto findNotNull(int id, DbSession session) {
- QualityProfileDto qualityProfile = db.qualityProfileDao().getById(id, session);
+ private QualityProfileDto findNotNull(String key, DbSession session) {
+ QualityProfileDto qualityProfile = db.qualityProfileDao().getByKey(session, key);
QProfileValidations.checkProfileIsNotNull(qualityProfile);
return qualityProfile;
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
index b3559277ad1..084b3bef83a 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
@@ -101,20 +101,20 @@ public class QProfiles implements ServerComponent {
return projectLookup.findProfileByProjectAndLanguage(projectId, language);
}
- public void addProject(int profileId, long projectId) {
- projectOperations.addProject(profileId, projectId, UserSession.get());
+ public void addProject(String profileKey, String projectUuid) {
+ projectOperations.addProject(profileKey, projectUuid, UserSession.get());
}
- public void removeProject(int profileId, long projectId) {
- projectOperations.removeProject(profileId, projectId, UserSession.get());
+ public void removeProject(String profileKey, String projectUuid) {
+ projectOperations.removeProject(profileKey, projectUuid, UserSession.get());
}
public void removeProjectByLanguage(String language, long projectId) {
projectOperations.removeProject(language, projectId, UserSession.get());
}
- public void removeAllProjects(int profileId) {
- projectOperations.removeAllProjects(profileId, UserSession.get());
+ public void removeAllProjects(String profileKey) {
+ projectOperations.removeAllProjects(profileKey, UserSession.get());
}
private void checkProfileNameParam(String name) {
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
new file mode 100644
index 00000000000..2760559bd38
--- /dev/null
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java
@@ -0,0 +1,147 @@
+/*
+ * 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.base.Function;
+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.server.component.ComponentService;
+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 java.util.Arrays;
+
+public class ProjectAssociationActions implements ServerComponent {
+
+ private static final String PARAM_LANGUAGE = "language";
+ private static final String PARAM_PROFILE_NAME = "profileName";
+ private static final String PARAM_PROFILE_KEY = "profileKey";
+ private static final String PARAM_PROJECT_KEY = "projectKey";
+ private static final String PARAM_PROJECT_UUID = "projectUuid";
+
+ private final QProfileProjectOperations profileProjectOperations;
+ private final QProfileLookup profileLookup;
+ private final ComponentService componentService;
+ private final Languages languages;
+
+ public ProjectAssociationActions(QProfileProjectOperations profileProjectOperations, QProfileLookup profileLookup, ComponentService componentService, Languages languages) {
+ this.profileProjectOperations = profileProjectOperations;
+ this.profileLookup = profileLookup;
+ this.componentService = componentService;
+ this.languages = languages;
+ }
+
+ void define(WebService.NewController controller) {
+ NewAction addProject = controller.createAction("add_project")
+ .setSince("5.2")
+ .setDescription("Associate a project with a quality profile")
+ .setHandler(new AssociationHandler(profileLookup, componentService) {
+ @Override
+ protected void changeAssociation(String profileKey, String projectUuid, UserSession userSession) {
+ profileProjectOperations.addProject(profileKey, projectUuid, userSession);
+ }
+ });
+ setCommonAttributes(addProject);
+
+ NewAction removeProject = controller.createAction("remove_project")
+ .setSince("5.2")
+ .setDescription("Remove a project's association with a quality profile")
+ .setHandler(new AssociationHandler(profileLookup, componentService) {
+ @Override
+ protected void changeAssociation(String profileKey, String projectUuid, UserSession userSession) {
+ profileProjectOperations.removeProject(profileKey, projectUuid, userSession);
+ }
+ });
+ setCommonAttributes(removeProject);
+ }
+
+ private void setCommonAttributes(NewAction action) {
+ action.setPost(true);
+ action.createParam(PARAM_PROJECT_UUID)
+ .setDescription("A project UUID. Either this parameter, or projectKey must be set");
+ action.createParam(PARAM_PROJECT_KEY)
+ .setDescription("A project key. Either this parameter, or projectUuid must be set");
+ action.createParam(PARAM_PROFILE_KEY)
+ .setDescription("A quality profile key. Either this parameter, or a combination of profileName + language must be set");
+ action.createParam(PARAM_PROFILE_NAME)
+ .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 language must be set to disambiguate")
+ .setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new Function<Language, String>() {
+ @Override
+ public String apply(Language input) {
+ return input.getKey();
+ }
+ }));
+ }
+
+ private static abstract class AssociationHandler implements RequestHandler {
+
+ private final QProfileLookup profileLookup;
+ private final ComponentService componentService;
+
+ public AssociationHandler(QProfileLookup profileLookup, ComponentService componentService) {
+ this.profileLookup = profileLookup;
+ this.componentService = componentService;
+ }
+
+ protected abstract void changeAssociation(String profileKey, String projectUuid, UserSession userSession);
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ String language = request.param(PARAM_LANGUAGE);
+ String profileName = request.param(PARAM_PROFILE_NAME);
+ String profileKey = request.param(PARAM_PROFILE_KEY);
+ String projectKey = request.param(PARAM_PROJECT_KEY);
+ String projectUuid = request.param(PARAM_PROJECT_UUID);
+
+ Preconditions.checkArgument(
+ (language != null && profileName != null) ^ profileKey != null, "Either profileKey or profileName + language must be set");
+ Preconditions.checkArgument(projectKey != null ^ projectUuid != null, "Either projectKey or projectUuid must be set");
+
+ if(profileKey == null) {
+ profileKey = getProfileKeyFromLanguageAndName(language, profileName);
+ }
+
+ if (projectUuid == null) {
+ projectUuid = componentService.getByKey(projectKey).uuid();
+ }
+
+ changeAssociation(profileKey, projectUuid, UserSession.get());
+ response.noContent();
+ }
+
+ private String getProfileKeyFromLanguageAndName(String language, String profileName) {
+ QProfile profile = profileLookup.profile(profileName, language);
+ if (profile == null) {
+ throw new NotFoundException(String.format("Unable to find a profile for language '%s' with name '%s'", language, profileName));
+ }
+ return profile.key();
+ }
+ }
+}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java
index 132f84c9144..750660f0f18 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/QProfilesWs.java
@@ -28,13 +28,16 @@ public class QProfilesWs implements WebService {
private final QProfileRestoreBuiltInAction resetAction;
private final RuleActivationActions ruleActivationActions;
private final BulkRuleActivationActions bulkRuleActivationActions;
+ private final ProjectAssociationActions projectAssociationActions;
public QProfilesWs(QProfileRestoreBuiltInAction resetAction,
RuleActivationActions ruleActivationActions,
- BulkRuleActivationActions bulkRuleActivationActions) {
+ BulkRuleActivationActions bulkRuleActivationActions,
+ ProjectAssociationActions projectAssociationActions) {
this.resetAction = resetAction;
this.ruleActivationActions = ruleActivationActions;
this.bulkRuleActivationActions = bulkRuleActivationActions;
+ this.projectAssociationActions = projectAssociationActions;
}
@Override
@@ -46,6 +49,7 @@ public class QProfilesWs implements WebService {
resetAction.define(controller);
ruleActivationActions.define(controller);
bulkRuleActivationActions.define(controller);
+ projectAssociationActions.define(controller);
controller.done();
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
index 4e31be74ebd..d9e2c1af4d5 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileFactoryMediumTest.java
@@ -324,7 +324,7 @@ public class QProfileFactoryMediumTest {
dbSession.clearCache();
assertThat(factory.getByProjectAndLanguage("org.codehaus.sonar:sonar", "xoo")).isNull();
- tester.get(QProfileProjectOperations.class).addProject(profileDto.getId(), project.getId(),
+ tester.get(QProfileProjectOperations.class).addProject(profileDto.getKey(), project.uuid(),
MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN), dbSession);
dbSession.commit();
dbSession.clearCache();
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java
index ddf0da41a08..161d2757def 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileProjectOperationsMediumTest.java
@@ -86,30 +86,30 @@ public class QProfileProjectOperationsMediumTest {
@Test
public void add_project() throws Exception {
- projectOperations.addProject(profile.getId(), project.getId(), authorizedProfileAdminUserSession);
+ projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
}
@Test
public void add_project_with_only_project_admin_permission() throws Exception {
- projectOperations.addProject(profile.getId(), project.getId(), authorizedProjectAdminUserSession);
+ projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProjectAdminUserSession);
assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
}
@Test
public void remove_project_from_project_id() throws Exception {
- projectOperations.addProject(profile.getId(), project.getId(), authorizedProfileAdminUserSession);
+ projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
- projectOperations.removeProject(profile.getId(), project.getId(), authorizedProfileAdminUserSession);
+ projectOperations.removeProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNull();
}
@Test
public void remove_project_from_language() throws Exception {
- projectOperations.addProject(profile.getId(), project.getId(), authorizedProfileAdminUserSession);
+ projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
projectOperations.removeProject(profile.getLanguage(), project.getId(), authorizedProfileAdminUserSession);
@@ -147,11 +147,11 @@ public class QProfileProjectOperationsMediumTest {
dbSession.commit();
- projectOperations.addProject(profile.getId(), project1.getId(), userSession);
- projectOperations.addProject(profile.getId(), project2.getId(), userSession);
+ projectOperations.addProject(profile.getKey(), project1.uuid(), userSession);
+ projectOperations.addProject(profile.getKey(), project2.uuid(), userSession);
assertThat(tester.get(QProfileProjectLookup.class).projects(profile.getId())).hasSize(2);
- projectOperations.removeAllProjects(profile.getId(), userSession);
+ projectOperations.removeAllProjects(profile.getKey(), userSession);
assertThat(tester.get(QProfileProjectLookup.class).projects(profile.getId())).isEmpty();
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java
index 59ecedf9321..a313b0f104c 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java
@@ -116,14 +116,14 @@ public class QProfilesTest {
@Test
public void add_project() throws Exception {
- qProfiles.addProject(1, 10L);
- verify(projectOperations).addProject(eq(1), eq(10L), any(UserSession.class));
+ qProfiles.addProject("sonar-way-java", "ABCD");
+ verify(projectOperations).addProject(eq("sonar-way-java"), eq("ABCD"), any(UserSession.class));
}
@Test
- public void remove_project_by_quality_profile_id() throws Exception {
- qProfiles.removeProject(1, 10L);
- verify(projectOperations).removeProject(eq(1), eq(10L), any(UserSession.class));
+ public void remove_project_by_quality_profile_key() throws Exception {
+ qProfiles.removeProject("sonar-way-java", "ABCD");
+ verify(projectOperations).removeProject(eq("sonar-way-java"), eq("ABCD"), any(UserSession.class));
}
@Test
@@ -134,7 +134,7 @@ public class QProfilesTest {
@Test
public void remove_all_projects() throws Exception {
- qProfiles.removeAllProjects(1);
- verify(projectOperations).removeAllProjects(eq(1), any(UserSession.class));
+ qProfiles.removeAllProjects("sonar-way-java");
+ verify(projectOperations).removeAllProjects(eq("sonar-way-java"), any(UserSession.class));
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRestoreBuiltInActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRestoreBuiltInActionTest.java
index cf660c36572..42af5b83a08 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRestoreBuiltInActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfileRestoreBuiltInActionTest.java
@@ -50,7 +50,8 @@ public class QProfileRestoreBuiltInActionTest {
tester = new WsTester(new QProfilesWs(
new QProfileRestoreBuiltInAction(this.profileService),
new RuleActivationActions(profileService),
- new BulkRuleActivationActions(profileService, ruleService, i18n)));
+ new BulkRuleActivationActions(profileService, ruleService, i18n),
+ mock(ProjectAssociationActions.class)));
}
@Test
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java
index 7c3371c2f64..d2d83e3e5ad 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java
@@ -28,6 +28,7 @@ import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.ws.WebService;
+import org.sonar.core.component.ComponentDto;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.qualityprofile.db.ActiveRuleDto;
@@ -36,6 +37,8 @@ import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.qualityprofile.QProfileFactory;
import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
@@ -88,7 +91,7 @@ public class QProfilesWsMediumTest {
WebService.Controller controller = context.controller(QProfilesWs.API_ENDPOINT);
assertThat(controller).isNotNull();
- assertThat(controller.actions()).hasSize(5);
+ assertThat(controller.actions()).hasSize(7);
assertThat(controller.action(BulkRuleActivationActions.BULK_ACTIVATE_ACTION)).isNotNull();
assertThat(controller.action(BulkRuleActivationActions.BULK_DEACTIVATE_ACTION)).isNotNull();
assertThat(controller.action(RuleActivationActions.ACTIVATE_ACTION)).isNotNull();
@@ -396,6 +399,142 @@ public class QProfilesWsMediumTest {
assertThat(db.activeRuleDao().getByKey(session, active2.getKey()).getSeverityString()).isNotEqualTo("MINOR");
}
+ @Test
+ public void add_project_with_key_and_uuid() throws Exception {
+ ComponentDto project = new ComponentDto()
+ .setId(1L)
+ .setUuid("ABCD")
+ .setKey("org.codehaus.sonar:sonar")
+ .setName("SonarQube")
+ .setLongName("SonarQube")
+ .setQualifier("TRK")
+ .setScope("TRK")
+ .setEnabled(true);
+ db.componentDao().insert(session, project);
+ QualityProfileDto profile = QProfileTesting.newXooP1();
+ db.qualityProfileDao().insert(session, profile);
+
+ session.commit();
+
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid())
+ .execute().assertNoContent();
+ assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
+ }
+
+ @Test
+ public void add_project_with_name_language_and_key() throws Exception {
+ ComponentDto project = new ComponentDto()
+ .setId(1L)
+ .setUuid("ABCD")
+ .setKey("org.codehaus.sonar:sonar")
+ .setName("SonarQube")
+ .setLongName("SonarQube")
+ .setQualifier("TRK")
+ .setScope("TRK")
+ .setEnabled(true);
+ db.componentDao().insert(session, project);
+ QualityProfileDto profile = QProfileTesting.newXooP1();
+ db.qualityProfileDao().insert(session, profile);
+
+ session.commit();
+
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("language", "xoo").setParam("profileName", profile.getName()).setParam("projectKey", project.getKey())
+ .execute().assertNoContent();
+ assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void add_project_missing_language() throws Exception {
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("profileName", "polop").setParam("projectKey", "palap")
+ .execute();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void add_project_missing_name() throws Exception {
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("language", "xoo").setParam("projectKey", "palap")
+ .execute();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void add_project_too_many_profile_parameters() throws Exception {
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("profileKey", "plouf").setParam("language", "xoo").setParam("profileName", "polop").setParam("projectUuid", "palap")
+ .execute();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void add_project_missing_project() throws Exception {
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("profileKey", "plouf")
+ .execute();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void add_project_too_many_project_parameters() throws Exception {
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("profileKey", "plouf").setParam("projectUuid", "polop").setParam("projectKey", "palap")
+ .execute();
+ }
+
+ @Test(expected = NotFoundException.class)
+ public void add_project_unknown_profile() throws Exception {
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+ .setParam("projectUuid", "plouf").setParam("profileName", "polop").setParam("language", "xoo")
+ .execute();
+ }
+
+ @Test
+ public void remove_project_with_key_and_uuid() throws Exception {
+ ComponentDto project = new ComponentDto()
+ .setId(1L)
+ .setUuid("ABCD")
+ .setKey("org.codehaus.sonar:sonar")
+ .setName("SonarQube")
+ .setLongName("SonarQube")
+ .setQualifier("TRK")
+ .setScope("TRK")
+ .setEnabled(true);
+ db.componentDao().insert(session, project);
+ QualityProfileDto profile = QProfileTesting.newXooP1();
+ db.qualityProfileDao().insert(session, profile);
+ db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), profile.getKee(), session);
+
+ session.commit();
+
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "remove_project")
+ .setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid())
+ .execute().assertNoContent();
+ assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo")).isNull();
+ }
+
+ @Test
+ public void remove_project_with_name_language_and_key() throws Exception {
+ ComponentDto project = new ComponentDto()
+ .setId(1L)
+ .setUuid("ABCD")
+ .setKey("org.codehaus.sonar:sonar")
+ .setName("SonarQube")
+ .setLongName("SonarQube")
+ .setQualifier("TRK")
+ .setScope("TRK")
+ .setEnabled(true);
+ db.componentDao().insert(session, project);
+ QualityProfileDto profile = QProfileTesting.newXooP1();
+ db.qualityProfileDao().insert(session, profile);
+ db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), profile.getKee(), session);
+
+ session.commit();
+
+ wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "remove_project")
+ .setParam("language", "xoo").setParam("profileName", profile.getName()).setParam("projectKey", project.getKey())
+ .execute().assertNoContent();
+ assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo")).isNull();
+ }
+
private QualityProfileDto createProfile(String lang) {
QualityProfileDto profile = QProfileTesting.newDto(new QProfileName(lang, "P" + lang), "p" + lang);
db.qualityProfileDao().insert(session, profile);
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 9b30116c775..90052ec371f 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,9 +20,13 @@
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.qualityprofile.QProfileService;
import org.sonar.server.rule.RuleService;
@@ -35,15 +39,23 @@ public class QProfilesWsTest {
WebService.Controller controller;
+ Language xoo1, xoo2;
+
@Before
public void setUp() {
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);
+
controller = new WsTester(new QProfilesWs(new QProfileRestoreBuiltInAction(
mock(QProfileService.class)),
new RuleActivationActions(profileService),
- new BulkRuleActivationActions(profileService, ruleService, i18n)
+ new BulkRuleActivationActions(profileService, ruleService, i18n),
+ new ProjectAssociationActions(null, null, null, languages)
)).controller(QProfilesWs.API_ENDPOINT);
}
@@ -52,7 +64,7 @@ public class QProfilesWsTest {
assertThat(controller).isNotNull();
assertThat(controller.path()).isEqualTo(QProfilesWs.API_ENDPOINT);
assertThat(controller.description()).isNotEmpty();
- assertThat(controller.actions()).hasSize(5);
+ assertThat(controller.actions()).hasSize(7);
}
@Test
@@ -79,4 +91,29 @@ public class QProfilesWsTest {
assertThat(restoreProfiles.params()).hasSize(2);
}
+ @Test
+ public void define_add_project_action() throws Exception {
+ WebService.Action addProject = controller.action("add_project");
+ assertThat(addProject).isNotNull();
+ assertThat(addProject.isPost()).isTrue();
+ assertThat(addProject.params()).hasSize(5);
+ }
+
+ @Test
+ public void define_remove_project_action() throws Exception {
+ WebService.Action removeProject = controller.action("remove_project");
+ assertThat(removeProject).isNotNull();
+ assertThat(removeProject.isPost()).isTrue();
+ 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};
+ }
+ };
+ }
+
}