From f43bc836fbd3263a3bdefe403d18844ca9e32e66 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 17 Jan 2014 12:37:10 +0100 Subject: [PATCH] SONAR-4923 When renaomg a profile, rename child profile and profile properties --- .../sonar/core/properties/PropertiesDao.java | 22 ++++++++++++- .../core/properties/PropertiesMapper.java | 2 ++ .../qualityprofile/db/QualityProfileDao.java | 8 +++-- .../core/properties/PropertiesMapper.xml | 6 +++- .../core/properties/PropertiesDaoTest.java | 9 ++++++ ...ertiesFromKeyAndValueToNewValue-result.xml | 9 ++++++ ...atePropertiesFromKeyAndValueToNewValue.xml | 9 ++++++ .../server/qualityprofile/QProfileLookup.java | 11 ++++++- .../qualityprofile/QProfileOperations.java | 20 ++++++++---- .../qualityprofile/QProfileLookupTest.java | 2 +- .../QProfileOperationsTest.java | 31 +++++++++++++++++-- 11 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue.xml diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java index 27e6a86ad24..56f5eaaa2e3 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -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); + } + } diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java index 71138cb93fa..83d50d16abc 100644 --- a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java @@ -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); } diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java index dc28bc4994c..e2ab8d0e8cc 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java @@ -150,10 +150,14 @@ public class QualityProfileDao implements ServerComponent { } } - public List selectChildren(String name, String language) { + public List selectChildren(String name, String language, SqlSession session) { + return session.getMapper(QualityProfileMapper.class).selectChildren(StringUtils.upperCase(name), language); + } + + public List 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); } diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml index d130202688a..82c218a70e6 100644 --- a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml @@ -100,7 +100,11 @@ - update properties set prop_key = #{newKey} where prop_key=#{oldKey} + update properties set prop_key=#{newKey} where prop_key=#{oldKey} + + + + update properties set text_value=#{newValue} where text_value=#{oldValue} and prop_key=#{key} diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java index 482e87cc0dd..d4ac1dbf7a8 100644 --- a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java @@ -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 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 index 00000000000..194761a6b2d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue-result.xml @@ -0,0 +1,9 @@ + + + + + + + + + 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 index 00000000000..5567744e146 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/updatePropertiesFromKeyAndValueToNewValue.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java index 798d74e6ed7..a14d8810427 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileLookup.java @@ -113,7 +113,16 @@ public class QProfileLookup implements ServerComponent { } public List 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 children(QProfile profile, SqlSession session) { + return toQProfiles(dao.selectChildren(profile.name(), profile.language(), session)); } public List ancestors(QProfile profile) { diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java index ae5a32d8b07..bbe52de7f6b 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java @@ -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 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); diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileLookupTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileLookupTest.java index 906ddea4156..65106ad8eb5 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileLookupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileLookupTest.java @@ -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 diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java index 9d35648e93d..a4070be0b4b 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileOperationsTest.java @@ -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.emptyList()); operations.renameProfile(1, "Default profile", authorizedUserSession); ArgumentCaptor 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 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")); -- 2.39.5