]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4923 When renaomg a profile, rename child profile and profile properties
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 17 Jan 2014 11:37:10 +0000 (12:37 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 17 Jan 2014 11:37:10 +0000 (12:37 +0100)
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/java/org/sonar/core/qualityprofile/db/QualityProfileDao.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/updatePropertiesFromKeyAndValueToNewValue-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue.xml [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java
sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileLookupTest.java
sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java

index 27e6a86ad24272b56fe51760a81a09e9e980c9ec..56f5eaaa2e396e565e1c785f2375ad310377511d 100644 (file)
@@ -147,7 +147,7 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
     mapper.deleteProjectProperties(key, value);
   }
 
-    public void deleteProjectProperties(String key, String value) {
+  public void deleteProjectProperties(String key, String value) {
     SqlSession session = mybatis.openSession();
     try {
       deleteProjectProperties(key, value, session);
@@ -229,4 +229,24 @@ public class PropertiesDao implements BatchComponent, ServerComponent {
       }
     }
   }
+
+  /**
+   * Update all properties (global and projects ones) with a given key and value to a new value
+   */
+  public void updateProperties(String key, String oldValue, String newValue) {
+    SqlSession session = mybatis.openSession();
+    try {
+      updateProperties(key, oldValue, newValue, session);
+      session.commit();
+
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public void updateProperties(String key, String oldValue, String newValue, SqlSession session) {
+    PropertiesMapper mapper = session.getMapper(PropertiesMapper.class);
+    mapper.updateProperties(key, oldValue, newValue);
+  }
+
 }
index 71138cb93fa037fe86e99acb6573e7cf7e6d6d7e..83d50d16abc9dd05e1ce28a53c1899fb9419762e 100644 (file)
@@ -54,4 +54,6 @@ public interface PropertiesMapper {
   void deleteGlobalProperties();
 
   void renamePropertyKey(@Param("oldKey") String oldKey, @Param("newKey") String newKey);
+
+  void updateProperties(@Param("key") String key, @Param("oldValue")String oldValue, @Param("newValue") String newValue);
 }
index dc28bc4994c2ea34e4d13ece5ac685d007bda1a5..e2ab8d0e8cc719a43849d20f887157a709abfcaf 100644 (file)
@@ -150,10 +150,14 @@ public class QualityProfileDao implements ServerComponent {
     }
   }
 
-  public List<QualityProfileDto> selectChildren(String name, String language) {
+  public List<QualityProfileDto> selectChildren(String name, String language, SqlSession session) {
+    return session.getMapper(QualityProfileMapper.class).selectChildren(StringUtils.upperCase(name), language);
+  }
+
+    public List<QualityProfileDto> selectChildren(String name, String language) {
     SqlSession session = mybatis.openSession();
     try {
-      return session.getMapper(QualityProfileMapper.class).selectChildren(StringUtils.upperCase(name), language);
+      return selectChildren(StringUtils.upperCase(name), language, session);
     } finally {
       MyBatis.closeQuietly(session);
     }
index d130202688ac48ef9974747db8de9d3ad370cbd1..82c218a70e62a5b26fdd4af8d83fd61e047ee665 100644 (file)
   </delete>
 
   <update id="renamePropertyKey" parameterType="map">
-    update properties set prop_key = #{newKey} where prop_key=#{oldKey}
+    update properties set prop_key=#{newKey} where prop_key=#{oldKey}
+  </update>
+
+  <update id="updateProperties" parameterType="map">
+    update properties set text_value=#{newValue} where text_value=#{oldValue} and prop_key=#{key}
   </update>
 
 </mapper>
index 482e87cc0dd8610e5418fa733ec1c39840257d47..d4ac1dbf7a8898d8f933223b87f2baa4e9303792 100644 (file)
@@ -258,6 +258,15 @@ public class PropertiesDaoTest extends AbstractDaoTestCase {
     dao.renamePropertyKey(null, "foo");
   }
 
+  @Test
+  public void updatePropertiesFromKeyAndValueToNewValue() {
+    setupData("updatePropertiesFromKeyAndValueToNewValue");
+
+    dao.updateProperties("sonar.profile.java", "Sonar Way", "Default");
+
+    checkTable("updatePropertiesFromKeyAndValueToNewValue", "properties", "prop_key", "text_value", "resource_id", "user_id");
+  }
+
   private PropertyDto findById(List<PropertyDto> properties, int id) {
     for (PropertyDto property : properties) {
       if (property.getId() == id) {
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue-result.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue-result.xml
new file mode 100644 (file)
index 0000000..194761a
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <properties id="1" prop_key="sonar.profile.java" text_value="Default" resource_id="[null]" user_id="[null]"/>
+  <properties id="2" prop_key="sonar.profile.java" text_value="Default" resource_id="1" user_id="[null]"/>
+
+  <properties id="3" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="[null]" user_id="[null]"/>
+  <properties id="4" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="2" user_id="[null]"/>
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue.xml
new file mode 100644 (file)
index 0000000..5567744
--- /dev/null
@@ -0,0 +1,9 @@
+<dataset>
+
+  <properties id="1" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="[null]" user_id="[null]"/>
+  <properties id="2" prop_key="sonar.profile.java" text_value="Sonar Way" resource_id="1" user_id="[null]"/>
+
+  <properties id="3" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="[null]" user_id="[null]"/>
+  <properties id="4" prop_key="sonar.profile.js" text_value="Sonar Way" resource_id="2" user_id="[null]"/>
+
+</dataset>
index 798d74e6ed7ed1c9e93254045b9f7ea1280eeb79..a14d88104271f6c5d566905c1c9f5b7c4e7224fb 100644 (file)
@@ -113,7 +113,16 @@ public class QProfileLookup implements ServerComponent {
   }
 
   public List<QProfile> children(QProfile profile) {
-    return toQProfiles(dao.selectChildren(profile.name(), profile.language()));
+    SqlSession session = myBatis.openSession();
+    try {
+      return children(profile, session);
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public List<QProfile> children(QProfile profile, SqlSession session) {
+    return toQProfiles(dao.selectChildren(profile.name(), profile.language(), session));
   }
 
   public List<QProfile> ancestors(QProfile profile) {
index ae5a32d8b07d3dd48dc682f3de37c4925caf16a5..bbe52de7f6bf318f6a201b19f70f6b564b3e6db9 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.server.user.UserSession;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
+import java.util.List;
 import java.util.Map;
 
 public class QProfileOperations implements ServerComponent {
@@ -105,15 +106,22 @@ public class QProfileOperations implements ServerComponent {
     checkPermission(userSession);
     SqlSession session = myBatis.openSession();
     try {
-      QualityProfileDto profile = findNotNull(profileId, session);
+      QualityProfileDto profileDto = findNotNull(profileId, session);
+      String oldName = profileDto.getName();
 
-      // TODO rename children and properties
+      QProfile profile = QProfile.from(profileDto);
+      if (!oldName.equals(newName)) {
+        checkNotAlreadyExists(newName, profile.language(), session);
+      }
+      profileDto.setName(newName);
+      dao.update(profileDto, session);
 
-      if (!profile.getName().equals(newName)) {
-        checkNotAlreadyExists(newName, profile.getLanguage(), session);
+      List<QProfile> children = profileLookup.children(profile, session);
+      for (QProfile child : children) {
+        dao.update(child.setParent(newName).toDto(), session);
       }
-      profile.setName(newName);
-      dao.update(profile, session);
+      propertiesDao.updateProperties(PROFILE_PROPERTY_PREFIX + profile.language(), oldName, newName, session);
+
       session.commit();
     } finally {
       MyBatis.closeQuietly(session);
index 906ddea4156cf2bc2f989beb841974fc4928848d..65106ad8eb59fb70af23d4502002555787b5775f 100644 (file)
@@ -137,7 +137,7 @@ public class QProfileLookupTest {
   @Test
   public void search_children_profiles() throws Exception {
     search.children(new QProfile().setName("Sonar Way").setLanguage("java"));
-    verify(dao).selectChildren("Sonar Way", "java");
+    verify(dao).selectChildren("Sonar Way", "java", session);
   }
 
   @Test
index 9d35648e93dfa7bc29e7bc981695e3dfe764b4b6..a4070be0b4b756db3590b900887b54ca4cdac668 100644 (file)
@@ -47,6 +47,9 @@ import org.sonar.server.rule.RuleRegistry;
 import org.sonar.server.user.MockUserSession;
 import org.sonar.server.user.UserSession;
 
+import java.util.Collections;
+
+import static org.elasticsearch.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.fest.assertions.Fail.fail;
 import static org.mockito.Matchers.any;
@@ -165,16 +168,18 @@ public class QProfileOperationsTest {
 
   @Test
   public void rename_profile() throws Exception {
-    when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java"));
+    when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Old Default").setLanguage("java"));
+    when(profileLookup.children(any(QProfile.class), eq(session))).thenReturn(Collections.<QProfile>emptyList());
 
     operations.renameProfile(1, "Default profile", authorizedUserSession);
 
     ArgumentCaptor<QualityProfileDto> profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class);
     verify(qualityProfileDao).update(profileArgument.capture(), eq(session));
-
+    assertThat(profileArgument.getValue().getId()).isEqualTo(1);
     assertThat(profileArgument.getValue().getName()).isEqualTo("Default profile");
     assertThat(profileArgument.getValue().getLanguage()).isEqualTo("java");
 
+    verify(propertiesDao).updateProperties("sonar.profile.java", "Old Default", "Default profile", session);
     verify(session).commit();
   }
 
@@ -201,6 +206,28 @@ public class QProfileOperationsTest {
     }
   }
 
+
+  @Test
+  public void rename_children_profile() throws Exception {
+    QualityProfileDto profile = new QualityProfileDto().setId(1).setName("Old Default").setLanguage("java");
+    when(qualityProfileDao.selectById(1, session)).thenReturn(profile);
+    when(profileLookup.children(any(QProfile.class), eq(session))).thenReturn(newArrayList(
+      new QProfile().setId(2).setName("Child1").setLanguage("java").setParent("Old Default")
+    ));
+
+    operations.renameProfile(1, "Default profile", authorizedUserSession);
+
+    ArgumentCaptor<QualityProfileDto> profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class);
+    // One call to update current profile and one other for child
+    verify(qualityProfileDao, times(2)).update(profileArgument.capture(), eq(session));
+    assertThat(profileArgument.getAllValues()).hasSize(2);
+    QualityProfileDto child = profileArgument.getAllValues().get(1);
+    assertThat(child.getId()).isEqualTo(2);
+    assertThat(child.getParent()).isEqualTo("Default profile");
+
+    verify(session).commit();
+  }
+
   @Test
   public void update_default_profile() throws Exception {
     when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java"));