]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4535 Remove a project to a quality profile now uses MyBatis
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 16 Dec 2013 18:04:09 +0000 (19:04 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 16 Dec 2013 18:04:09 +0000 (19:04 +0100)
12 files changed:
sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java
sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java
sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml
sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/delete_project_property-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/delete_project_property.xml [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java

index ab65fdde04143b6ad3fd7492c77fa62002c06516..ed0a9c47b230d6e9559e6b4dcd996640956bcde9 100644 (file)
@@ -46,8 +46,8 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
    * If a resource ID is passed, the search is made on users who have specifically subscribed for the given resource.
    *
    * @param notificationDispatcherKey the key of the notification dispatcher
-   * @param notificationChannelKey the key of the notification channel
-   * @param resourceId the resource id
+   * @param notificationChannelKey    the key of the notification channel
+   * @param resourceId                the resource id
    * @return the list of logins (maybe be empty - obviously)
    */
   public List<String> findUsersForNotification(String notificationDispatcherKey, String notificationChannelKey, @Nullable Long resourceId) {
@@ -129,6 +129,17 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
     }
   }
 
+  public void deleteProjectProperty(String key, Long projectId) {
+    SqlSession session = mybatis.openSession();
+    PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+    try {
+      mapper.deleteProjectProperty(key, projectId);
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
   public void deleteGlobalProperties() {
     SqlSession session = mybatis.openSession();
     PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
index d881040cf84549cb58ba2aec4b5e85f60917ac0c..45aaa71e700fd2bd7a87df51e46944757ba9ad2a 100644 (file)
@@ -43,6 +43,8 @@ public interface PropertiesMapper {
 
   void insert(PropertyDto property);
 
+  void deleteProjectProperty(@Param("key") String key, @Param("rId") Long resourceId);
+
   void deleteGlobalProperty(String key);
 
   void deleteAllProperties(String key);
index 139e05508f43a92dc8f38ba89dc8bea5f58aeb51..fecc4be5e5e7f682f184e6f0e3519fcc4760150a 100644 (file)
     VALUES (#{key}, #{resourceId}, #{userId}, #{value})
   </insert>
 
+  <delete id="deleteProjectProperty" parameterType="map">
+    delete from properties where prop_key=#{key} and resource_id=#{rId} and user_id is null
+  </delete>
+
   <delete id="deleteGlobalProperty" parameterType="string">
     delete from properties where prop_key=#{id} and resource_id is null and user_id is null
   </delete>
index a8d1b3ca651260ff73073e5a7a3c4a1614b55d34..c234424a13cac3a20b50fa347db1eee18e8c371c 100644 (file)
@@ -165,6 +165,15 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
     checkTables("insert", "properties");
   }
 
+  @Test
+  public void delete_project_property() {
+    setupData("delete_project_property");
+
+    dao.deleteProjectProperty("struts.one", 10L);
+
+    checkTables("delete_project_property", "properties");
+  }
+
   @Test
   public void deleteGlobalProperties() {
     setupData("deleteGlobalProperties");
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/delete_project_property-result.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/delete_project_property-result.xml
new file mode 100644 (file)
index 0000000..0045809
--- /dev/null
@@ -0,0 +1,22 @@
+<dataset>
+
+  <!-- global -->
+  <properties id="1" prop_key="global.one" text_value="one" resource_id="[null]" user_id="[null]"/>
+  <properties id="2" prop_key="global.two" text_value="two" resource_id="[null]" user_id="[null]"/>
+
+  <!-- struts -->
+  <!--<properties id="3" prop_key="struts.one" text_value="one" resource_id="10" user_id="[null]"/>-->
+
+  <!-- commons -->
+  <properties id="4" prop_key="commonslang.one" text_value="two" resource_id="11" user_id="[null]"/>
+
+  <!-- user -->
+  <properties id="5" prop_key="user.one" text_value="one" resource_id="[null]" user_id="100"/>
+  <properties id="6" prop_key="user.two" text_value="two" resource_id="10" user_id="100"/>
+
+  <properties id="7" prop_key="commonslang.one" text_value="one" resource_id="12" user_id="[null]"/>
+
+  <projects id="10" kee="org.struts:struts"/>
+  <projects id="11" kee="org.apache:commons-lang"/>
+  <projects id="12" kee="other"/>
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/delete_project_property.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/delete_project_property.xml
new file mode 100644 (file)
index 0000000..27c4803
--- /dev/null
@@ -0,0 +1,22 @@
+<dataset>
+
+  <!-- global -->
+  <properties id="1" prop_key="global.one" text_value="one" resource_id="[null]" user_id="[null]"/>
+  <properties id="2" prop_key="global.two" text_value="two" resource_id="[null]" user_id="[null]"/>
+
+  <!-- struts -->
+  <properties id="3" prop_key="struts.one" text_value="one" resource_id="10" user_id="[null]"/>
+
+  <!-- commons -->
+  <properties id="4" prop_key="commonslang.one" text_value="two" resource_id="11" user_id="[null]"/>
+
+  <!-- user -->
+  <properties id="5" prop_key="user.one" text_value="one" resource_id="[null]" user_id="100"/>
+  <properties id="6" prop_key="user.two" text_value="two" resource_id="10" user_id="100"/>
+
+  <properties id="7" prop_key="commonslang.one" text_value="one" resource_id="12" user_id="[null]"/>
+
+  <projects id="10" kee="org.struts:struts"/>
+  <projects id="11" kee="org.apache:commons-lang"/>
+  <projects id="12" kee="other"/>
+</dataset>
index 4b750c8fc309faa5f496ddeae5f443432ba267a3..c1c60769b4686ebd599584aa8f240e83e4de3c1b 100644 (file)
@@ -146,6 +146,25 @@ public class QProfileOperations implements ServerComponent {
     propertiesDao.setProperty(new PropertyDto().setKey(PROPERTY_PREFIX + qualityProfile.getLanguage()).setValue(qualityProfile.getName()).setResourceId(component.getId()));
   }
 
+  public void removeProject(Integer profileId, Long projectId, UserSession userSession) {
+    checkPermission(userSession);
+    Validation.checkMandatoryParameter(profileId, "profile");
+    QualityProfileDto qualityProfile = findNotNull(profileId);
+    removeProject(qualityProfile.getLanguage(), projectId);
+  }
+
+  public void removeProject(String language, Long projectId, UserSession userSession) {
+    checkPermission(userSession);
+    Validation.checkMandatoryParameter(language, "language");
+    removeProject(language, projectId);
+  }
+
+  private void removeProject(String language, Long projectId) {
+    Validation.checkMandatoryParameter(language, "project");
+    ComponentDto component = (ComponentDto) findNotNull(projectId);
+    propertiesDao.deleteProjectProperty(PROPERTY_PREFIX + language, component.getId());
+  }
+
   private List<RulesProfile> readProfilesFromXml(NewProfileResult result, Map<String, String> xmlProfilesByPlugin) {
     List<RulesProfile> profiles = newArrayList();
     ValidationMessages messages = ValidationMessages.create();
index 200be4d7c92ffc42a0dd358142b2bcabd367450a..d12e3d63239a4f9e429ab50f92a239d0be4ffb78 100644 (file)
@@ -111,12 +111,30 @@ public class QProfiles implements ServerComponent {
     return operations.projects(profileId);
   }
 
+  /**
+   * Used in /project/profile
+   */
+  public QProfile profile(Integer projectId) {
+    throw new UnsupportedOperationException();
+  }
+
+  /**
+   * Used in /project/profile
+   */
+  public List<QProfile> profiles(String language) {
+    throw new UnsupportedOperationException();
+  }
+
   public void addProject(Integer profileId, Long projectId) {
     operations.addProject(profileId, projectId, UserSession.get());
   }
 
-  public void removeProject(Integer profileId, String projectKey) {
-    throw new UnsupportedOperationException();
+  public void removeProject(Integer profileId, Long projectId) {
+    operations.removeProject(profileId, projectId, UserSession.get());
+  }
+
+  public void removeProjectByLanguage(String language, Long projectId) {
+    operations.removeProject(language, projectId, UserSession.get());
   }
 
   public void removeAllProjects(Integer profileId) {
index a07556704175355efafce98f44faadb22f7073ea..f1e6dbdedde13418a9a1b3bf9caf5951d794b652 100644 (file)
@@ -286,19 +286,14 @@ class ProfilesController < ApplicationController
     redirect_to :action => 'projects', :id => profile_id
   end
 
-  # POST /profiles/remove_project?id=<profile id>&project=<project id or key>
+  # POST /profiles/remove_project?id=<profile id>&project=<project id>
   def remove_project
     verify_post_request
-    access_denied unless has_role?(:profileadmin)
-    require_parameters 'id', 'project'
-
-    profile=Profile.find(params[:id])
-    bad_request('Unknown profile') unless profile
-    project=Project.by_key(params[:project])
-    bad_request('Unknown project') unless project
-
-    Profile.reset_default_profile_for_project_id(profile.language, project.id)
-    redirect_to :action => 'projects', :id => profile.id
+    profile_id = params[:id].to_i
+    call_backend do
+      Internal.qprofiles.removeProject(profile_id, params[:project].to_i)
+    end
+    redirect_to :action => 'projects', :id => profile_id
   end
 
   # POST /profiles/remove_projects?id=<profile id>
index 8c3270aeddab545635778d349370294469d45e58..1887eab42b38396b081e13adf034af3f137ddd32 100644 (file)
@@ -78,27 +78,27 @@ class ProjectController < ApplicationController
   # GET /project/profile?id=<project id>
   def profile
     require_parameters :id
-    @project=get_current_project_for_profile(params[:id])
-    @snapshot=@project.last_snapshot
+    @project = get_current_project_for_profile(params[:id])
+    @snapshot = @project.last_snapshot
   end
 
   # POST /project/set_profile?id=<project id>&language=<language>[&profile_id=<profile id>]
   def set_profile
-    require_parameters :id, :language
     verify_post_request
 
-    language=params[:language]
-    project = get_current_project_for_profile(params[:id])
+    language = params[:language]
+    project = params[:id].to_i
+    profile_id = params[:profile_id]
 
-    if params[:profile_id].blank?
-      Profile.reset_default_profile_for_project_id(language, project.id)
-    else
-      profile = Profile.find(params[:profile_id])
-      bad_request('Bad language') if profile.language!=language
-      profile.add_project_id(project.id)
+    call_backend do
+      if profile_id.blank?
+        Internal.qprofiles.removeProjectByLanguage(language, project)
+      else
+        Internal.qprofiles.addProject(profile_id.to_i, project)
+      end
     end
 
-    redirect_to :action => 'profile', :id => project.id
+    redirect_to :action => 'profile', :id => project
   end
 
   def get_current_project_for_profile(project_id)
index 1fa8eb87bddb6cca9915dbaf3b65ac15ce89383c..b4e0b23b3268283cd00c7674aae64984ae131beb 100644 (file)
@@ -317,4 +317,23 @@ public class QProfileOperationsTest {
     }
     verify(propertiesDao, never()).setProperty(any(PropertyDto.class));
   }
+
+  @Test
+  public void remove_project_by_quality_profile() throws Exception {
+    when(dao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
+    when(resourceDao.findById(10L)).thenReturn(new ComponentDto().setId(10L).setKey("org.codehaus.sonar:sonar").setName("SonarQube"));
+
+    operations.removeProject(1, 10L, MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+
+    verify(propertiesDao).deleteProjectProperty("sonar.profile.java", 10L);
+  }
+
+  @Test
+  public void remove_project_by_language() throws Exception {
+    when(resourceDao.findById(10L)).thenReturn(new ComponentDto().setId(10L).setKey("org.codehaus.sonar:sonar").setName("SonarQube"));
+
+    operations.removeProject("java", 10L, MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+
+    verify(propertiesDao).deleteProjectProperty("sonar.profile.java", 10L);
+  }
 }
index 1d6721ca71705c40a8c65d6699901e27bd1b56de..58863d75af3035bc925a326ebf27c832e3359d13 100644 (file)
@@ -35,9 +35,7 @@ import static com.google.common.collect.Maps.newHashMap;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 @RunWith(MockitoJUnitRunner.class)
 public class QProfilesTest {
@@ -139,15 +137,32 @@ public class QProfilesTest {
     verify(operations).projects(1);
   }
 
+  @Test(expected = UnsupportedOperationException.class)
+  public void get_profile_from_project_id() throws Exception {
+    qProfiles.profile(1);
+  }
+
+  @Test(expected = UnsupportedOperationException.class)
+  public void get_profiles_from_language() throws Exception {
+    qProfiles.profiles("java");
+  }
+
   @Test
   public void add_project() throws Exception {
     qProfiles.addProject(1, 10L);
     verify(operations).addProject(eq(1), eq(10L), any(UserSession.class));
   }
 
-  @Test(expected = UnsupportedOperationException.class)
-  public void testRemoveProject() throws Exception {
-    qProfiles.removeProject(null, null);
+  @Test
+  public void remove_project_by_quality_profile_id() throws Exception {
+    qProfiles.removeProject(1, 10L);
+    verify(operations).removeProject(eq(1), eq(10L), any(UserSession.class));
+  }
+
+  @Test
+  public void remove_project_by_language() throws Exception {
+    qProfiles.removeProjectByLanguage("java", 10L);
+    verify(operations).removeProject(eq("java"), eq(10L), any(UserSession.class));
   }
 
   @Test(expected = UnsupportedOperationException.class)