diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-17 16:36:37 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-17 16:37:15 +0100 |
commit | 5ce267adea56343875d1db5b43cbb3b3ea744798 (patch) | |
tree | 52be228db41cc80cc64c3d6e134f036905b6c8ef /sonar-server | |
parent | 347e09b4ff016135db4f6d45a541f6f1331f5089 (diff) | |
download | sonarqube-5ce267adea56343875d1db5b43cbb3b3ea744798.tar.gz sonarqube-5ce267adea56343875d1db5b43cbb3b3ea744798.zip |
SONAR-4923 Copy profile now use Java facade and index new active rules in ES
Diffstat (limited to 'sonar-server')
8 files changed, 88 insertions, 30 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java index 2f539fa73ff..ec346d02146 100644 --- a/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java +++ b/sonar-server/src/main/java/org/sonar/server/configuration/ProfilesManager.java @@ -44,7 +44,7 @@ public class ProfilesManager extends BaseDao { this.dryRunCache = dryRunCache; } - public void copyProfile(int profileId, String newProfileName) { + public int copyProfile(int profileId, String newProfileName) { RulesProfile profile = getSession().getSingleResult(RulesProfile.class, "id", profileId); RulesProfile toImport = (RulesProfile) profile.clone(); toImport.setName(newProfileName); @@ -52,6 +52,7 @@ public class ProfilesManager extends BaseDao { pb.importProfile(rulesDao, toImport); getSession().commit(); dryRunCache.reportGlobalModification(); + return toImport.getId(); } public void deleteAllProfiles() { 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 b1298afc259..81203c16f1e 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 @@ -72,7 +72,7 @@ public class QProfileOperations implements ServerComponent { public QProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin, UserSession userSession) { SqlSession session = myBatis.openSession(); try { - QProfile profile = newProfile(name, language, userSession, session); + QProfile profile = newProfile(name, language, true, userSession, session); QProfileResult result = new QProfileResult(); result.setProfile(profile); @@ -89,19 +89,19 @@ public class QProfileOperations implements ServerComponent { } public QProfile newProfile(String name, String language, boolean failIfAlreadyExists, UserSession userSession, SqlSession session) { + return newProfile(name, language, null, failIfAlreadyExists, userSession, session); + } + + public QProfile newProfile(String name, String language, String parent, boolean failIfAlreadyExists, UserSession userSession, SqlSession session) { checkPermission(userSession); if (failIfAlreadyExists) { checkNotAlreadyExists(name, language, session); } - QualityProfileDto dto = new QualityProfileDto().setName(name).setLanguage(language).setVersion(1).setUsed(false); + QualityProfileDto dto = new QualityProfileDto().setName(name).setLanguage(language).setParent(parent).setVersion(1).setUsed(false); dao.insert(dto, session); return QProfile.from(dto); } - private QProfile newProfile(String name, String language, UserSession userSession, SqlSession session) { - return newProfile(name, language, true, userSession, session); - } - public void renameProfile(int profileId, String newName, UserSession userSession) { checkPermission(userSession); SqlSession session = myBatis.openSession(); @@ -191,6 +191,19 @@ public class QProfileOperations implements ServerComponent { } } + public void copyProfile(int profileId, String copyProfileName, UserSession userSession) { + checkPermission(userSession); + SqlSession session = myBatis.openSession(); + try { + QualityProfileDto profileDto = findNotNull(profileId, session); + checkNotAlreadyExists(copyProfileName, profileDto.getLanguage(), session); + int copyProfileId = profilesManager.copyProfile(profileId, copyProfileName); + ruleRegistry.bulkIndexProfile(copyProfileId, session); + } finally { + MyBatis.closeQuietly(session); + } + } + @VisibleForTesting boolean isCycle(QualityProfileDto childProfile, @Nullable QualityProfileDto parentProfile, SqlSession session) { QualityProfileDto currentParent = parentProfile; diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java index def76196c9a..40957f4aa4b 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfiles.java @@ -132,6 +132,11 @@ public class QProfiles implements ServerComponent { operations.setDefaultProfile(profileId, UserSession.get()); } + public void copyProfile(int profileId, String copyProfileName) { + validateProfileName(copyProfileName); + operations.copyProfile(profileId, copyProfileName, UserSession.get()); + } + @CheckForNull public QProfile parent(QProfile profile) { return profileLookup.parent(profile); diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb index 645b76f16bf..6ef35144d0b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/profiles_controller.rb @@ -87,34 +87,30 @@ class ProfilesController < ApplicationController # GET /profiles/copy_form/<profile id> - # TODO use QProfiles facade instead def copy_form - access_denied unless has_role?(:profileadmin) require_parameters 'id' - @profile = Profile.find(params[:id]) + + profile_id = params[:id].to_i + call_backend do + @profile = Internal.quality_profiles.profile(profile_id) + end + not_found('Profile not found') unless @profile + render :partial => 'profiles/copy_form' end # POST /profiles/copy/<id>?name=<name of new profile> - # TODO use QProfiles facade instead def copy verify_post_request verify_ajax_request - access_denied unless has_role?(:profileadmin) require_parameters 'id' - @profile = Profile.find(params[:id]) + profile_id = params[:id].to_i name = params['name'] - - target_profile=Profile.new(:name => name, :language => @profile.language) - if target_profile.valid? - java_facade.copyProfile(@profile.id, name) + call_backend do + @profile = Internal.quality_profiles.copyProfile(profile_id, name) flash[:notice]= message('quality_profiles.profile_x_not_activated', :params => name) render :text => 'ok', :status => 200 - else - @errors = [] - target_profile.errors.each{|attr,msg| @errors<<msg} - render :partial => 'profiles/copy_form', :status => 400 end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb index 4c59eb28330..ab8a79f4247 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_copy_form.html.erb @@ -5,13 +5,7 @@ <h2> <%= message('quality_profiles.copy_x_title', :params => [h @profile.name]) -%></h2> </div> <div class="modal-body"> - <% if @errors - @errors.each do |error| - %> - <p class="error"><%= h error -%></p> - <% end - end - %> + <div class="modal-error"/> <div class="modal-field"> <label for="name"><%= message 'quality_profiles.copy_new_name' -%> <em class="mandatory">*</em></label> <input id="copy-name" name="name" type="text" size="50" maxlength="100" autofocus="autofocus"/> @@ -25,4 +19,4 @@ </form> <script> $j("#copy-profile-form").modalForm(); -</script>
\ No newline at end of file +</script> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb index 0948c6a5800..42063d7f3c9 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/profiles/_restore_form.html.erb @@ -6,7 +6,6 @@ </div> <div class="modal-body"> - <div class="modal-field"> <label for="name"><%= message('backup') -%> <em class="mandatory">*</em></label> <%= file_field_tag 'backup' %> 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 80d848afd97..52c10699b01 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 @@ -384,4 +384,48 @@ public class QProfileOperationsTest { verifyZeroInteractions(ruleRegistry); } + @Test + public void copy_profile() throws Exception { + when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java")); + when(profilesManager.copyProfile(1, "Copy Default")).thenReturn(2); + + operations.copyProfile(1, "Copy Default", authorizedUserSession); + + verify(profilesManager).copyProfile(1, "Copy Default"); + verify(ruleRegistry).bulkIndexProfile(2, session); + } + + @Test + public void fail_to_copy_profile_on_unknown_profile() throws Exception { + when(qualityProfileDao.selectById(1, session)).thenReturn(null); + when(profilesManager.copyProfile(1, "Copy Default")).thenReturn(2); + + try { + operations.copyProfile(1, "Copy Default", authorizedUserSession); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(NotFoundException.class); + } + + verifyZeroInteractions(profilesManager); + verifyZeroInteractions(ruleRegistry); + } + + @Test + public void fail_to_copy_profile_if_name_already_exists() throws Exception { + when(qualityProfileDao.selectById(1, session)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java")); + when(qualityProfileDao.selectByNameAndLanguage(anyString(), anyString(), eq(session))).thenReturn(new QualityProfileDto()); + when(profilesManager.copyProfile(1, "Copy Default")).thenReturn(2); + + try { + operations.copyProfile(1, "Copy Default", authorizedUserSession); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(BadRequestException.class); + } + + verifyZeroInteractions(profilesManager); + verifyZeroInteractions(ruleRegistry); + } + } diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java index 6b9f53551d6..1719e49200b 100644 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfilesTest.java @@ -222,6 +222,12 @@ public class QProfilesTest { } @Test + public void copy_profile() throws Exception { + qProfiles.copyProfile(1, "Copy Profile"); + verify(profileOperations).copyProfile(eq(1), eq("Copy Profile"), any(UserSession.class)); + } + + @Test public void update_parent_profile() throws Exception { qProfiles.updateParentProfile(1, 2); verify(profileOperations).updateParentProfile(eq(1), eq(2), any(UserSession.class)); |