}
}
+ public QualityProfileDto selectById(Integer id) {
+ SqlSession session = mybatis.openSession();
+ try {
+ return session.getMapper(QualityProfileMapper.class).selectById(id);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
public QualityProfileDto selectByNameAndLanguage(String name, String language) {
SqlSession session = mybatis.openSession();
try {
@CheckForNull
QualityProfileDto selectByNameAndLanguage(@Param("name") String name, @Param("language") String language);
+ @CheckForNull
+ QualityProfileDto selectById(@Param("id") Integer id);
+
void insert(QualityProfileDto dto);
void update(QualityProfileDto dto);
</where>
</select>
+ <select id="selectById" parameterType="Integer" resultType="QualityProfile">
+ select <include refid="profilesColumns"/>
+ from rules_profiles p
+ <where>
+ and p.id=#{id}
+ </where>
+ </select>
+
<insert id="insert" parameterType="QualityProfile" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
INSERT INTO rules_profiles (name, language, parent_name, version, used_profile)
VALUES (#{name}, #{language}, #{parent}, #{version}, #{used})
assertThat(dto.getVersion()).isEqualTo(1);
assertThat(dto.isUsed()).isFalse();
- assertThat(dao.selectByNameAndLanguage("Sonar way", "unknown"));
+ assertThat(dao.selectByNameAndLanguage("Sonar way", "unknown")).isNull();
+ }
+
+ @Test
+ public void select_by_id() {
+ setupData("shared");
+
+ QualityProfileDto dto = dao.selectById(1);
+ assertThat(dto.getId()).isEqualTo(1);
+ assertThat(dto.getName()).isEqualTo("Sonar way");
+ assertThat(dto.getLanguage()).isEqualTo("java");
+ assertThat(dto.getParent()).isNull();
+ assertThat(dto.getVersion()).isEqualTo(1);
+ assertThat(dto.isUsed()).isFalse();
+
+ assertThat(dao.selectById(555)).isNull();
}
@Test
import org.sonar.core.qualityprofile.db.*;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.platform.PersistentSettings;
import org.sonar.server.user.UserSession;
import org.sonar.server.util.Validation;
+import javax.annotation.CheckForNull;
+
import java.io.StringReader;
import java.util.List;
import java.util.Map;
private final List<ProfileExporter> exporters;
private final List<ProfileImporter> importers;
private final PreviewCache dryRunCache;
+ private final PersistentSettings persistentSettings;
- public QProfileOperations(MyBatis myBatis, QualityProfileDao dao, ActiveRuleDao activeRuleDao, PreviewCache dryRunCache) {
- this(myBatis, dao, activeRuleDao, Lists.<ProfileExporter>newArrayList(), Lists.<ProfileImporter>newArrayList(), dryRunCache);
+ public QProfileOperations(MyBatis myBatis, QualityProfileDao dao, ActiveRuleDao activeRuleDao, PreviewCache dryRunCache, PersistentSettings persistentSettings) {
+ this(myBatis, dao, activeRuleDao, Lists.<ProfileExporter>newArrayList(), Lists.<ProfileImporter>newArrayList(), dryRunCache, persistentSettings);
}
public QProfileOperations(MyBatis myBatis, QualityProfileDao dao, ActiveRuleDao activeRuleDao, List<ProfileExporter> exporters, List<ProfileImporter> importers,
- PreviewCache dryRunCache) {
+ PreviewCache dryRunCache, PersistentSettings persistentSettings) {
this.myBatis = myBatis;
this.dao = dao;
this.activeRuleDao = activeRuleDao;
this.exporters = exporters;
this.importers = importers;
this.dryRunCache = dryRunCache;
+ this.persistentSettings = persistentSettings;
}
public NewProfileResult newProfile(String name, String language, Map<String, String> xmlProfilesByPlugin, UserSession userSession) {
}
result.setProfile(QProfile.from(dto));
sqlSession.commit();
+ dryRunCache.reportGlobalModification();
} finally {
MyBatis.closeQuietly(sqlSession);
- dryRunCache.reportGlobalModification();
}
return result;
}
- public void renameProfile(String name, String language, String newName, UserSession userSession) {
- QualityProfileDto qualityProfile = validateRenameProfile(name, language, newName, userSession);
+ public void renameProfile(Integer profileId, String newName, UserSession userSession) {
+ QualityProfileDto qualityProfile = validateRenameProfile(profileId, newName, userSession);
qualityProfile.setName(newName);
dao.update(qualityProfile);
}
- private QualityProfileDto find(String name, String language) {
- return dao.selectByNameAndLanguage(name, language);
+ public void updateDefaultProfile(Integer id, UserSession userSession) {
+ QualityProfileDto qualityProfile = validateUpdateDefaultProfile(id, userSession);
+ persistentSettings.saveProperty("sonar.profile." + qualityProfile.getLanguage(), qualityProfile.getName());
+ }
+
+ public void updateDefaultProfile(String name, String language, UserSession userSession) {
+ updateDefaultProfile(findNeverNull(name, language).getId(), userSession);
}
private List<RulesProfile> readProfilesFromXml(NewProfileResult result, Map<String, String> xmlProfilesByPlugin) {
}
}
- private QualityProfileDto validateRenameProfile(String name, String language, String newName, UserSession userSession) {
+ private QualityProfileDto validateRenameProfile(Integer profileId, String newName, UserSession userSession) {
checkPermission(userSession);
validateName(newName);
- if (find(newName, language) != null) {
+ QualityProfileDto profileDto = findNeverNull(profileId);
+ if (!profileDto.getName().equals(newName) && find(newName, profileDto.getLanguage()) != null) {
throw BadRequestException.ofL10n("quality_profiles.already_exists");
}
+ return profileDto;
+ }
+
+ private QualityProfileDto validateUpdateDefaultProfile(Integer id, UserSession userSession) {
+ checkPermission(userSession);
+ return findNeverNull(id);
+ }
+ private QualityProfileDto findNeverNull(Integer id) {
+ QualityProfileDto qualityProfile = find(id);
+ if (qualityProfile == null) {
+ throw new NotFoundException("This quality profile does not exists.");
+ }
+ return qualityProfile;
+ }
+
+ private QualityProfileDto findNeverNull(String name, String language) {
QualityProfileDto qualityProfile = find(name, language);
if (qualityProfile == null) {
throw new NotFoundException("This quality profile does not exists.");
return qualityProfile;
}
+ @CheckForNull
+ private QualityProfileDto find(String name, String language) {
+ return dao.selectByNameAndLanguage(name, language);
+ }
+
+ @CheckForNull
+ private QualityProfileDto find(Integer id) {
+ return dao.selectById(id);
+ }
+
private void validateName(String name) {
if (Strings.isNullOrEmpty(name)) {
throw BadRequestException.ofL10n("quality_profiles.please_type_profile_name");
throw new UnsupportedOperationException();
}
- public void renameProfile(String name, String language, String newName) {
- operations.renameProfile(name, language, newName, UserSession.get());
+ public void renameProfile(Integer id, String newName) {
+ operations.renameProfile(id, newName, UserSession.get());
}
- public void updateDefaultProfile() {
- throw new UnsupportedOperationException();
+ public void updateDefaultProfile(Integer id) {
+ operations.updateDefaultProfile(id, UserSession.get());
+ }
+
+ /**
+ * Used by WS
+ */
+ public void updateDefaultProfile(String name, String language) {
+ operations.updateDefaultProfile(name, language, UserSession.get());
}
public void copyProfile() {
render_error(message, 200)
end
+ # Override
def render_server_exception(exception)
render_response(exception.httpCode, exception.getMessage)
end
# Since v.3.3
def set_as_default
verify_post_request
- access_denied unless has_role?(:profileadmin)
- require_parameters :language, :name
-
- profile=Profile.find_by_name_and_language(params[:name], params[:language])
- not_found('Profile not found') unless profile
- profile.set_as_default
+ Internal.qprofiles.updateDefaultProfile(params[:name], params[:language])
render_success
end
if exception.java_kind_of?(Java::OrgSonarServerExceptions::BadRequestException) && !exception.errors.empty?
message += '<br/>' + exception.errors.to_a.map{|error| error.text ? error.text : Api::Utils.message(error.l10nKey, :params => error.l10nParams)}.join('<br/>')
end
- render :text => CGI.escapeHTML(message), :status => exception.httpCode
+ if request.xhr?
+ render :text => CGI.escapeHTML(message), :status => exception.httpCode
+ else
+ flash[:error] = message
+ end
end
def render_native_access_denied(exception)
flash[:warning] = result.warnings.to_a[0...4].join('<br/>')
end
rescue Java::OrgSonarServerExceptions::ServerException => error
- flash[:error] = (error.getMessage ? error.getMessage : Api::Utils.message(error.l10nKey, :params => error.l10nParams.to_a))
- if error.java_kind_of?(Java::OrgSonarServerExceptions::BadRequestException) && !error.errors.empty?
- flash[:error] += '<br/>' + error.errors.to_a.map{|error| error.text ? error.text : Api::Utils.message(error.l10nKey, :params => error.l10nParams)}.join('<br/>')
- end
+ render_server_exception(error)
end
redirect_to :action => 'index'
end
# POST /profiles/set_as_default/<id>
def set_as_default
verify_post_request
- access_denied unless has_role?(:profileadmin)
- require_parameters 'id'
-
- profile = Profile.find(params[:id])
- profile.set_as_default
+ begin
+ Internal.qprofiles.updateDefaultProfile(params[:id].to_i)
+ rescue Java::OrgSonarServerExceptions::ServerException => error
+ render_server_exception(error)
+ end
redirect_to :action => 'index'
end
verify_post_request
verify_ajax_request
- Internal.qprofiles.renameProfile(params[:name], params[:language], params[:new_name])
+ Internal.qprofiles.renameProfile(params[:id].to_i, params[:new_name])
render :text => 'ok', :status => 200
end
Property.value("sonar.profile.#{language}")==name
end
- def set_as_default
- Property.set("sonar.profile.#{language}", name)
- self
- end
-
def active_by_rule_id(rule_id)
active_hash_by_rule_id[rule_id]
end
<form id="rename-profile-form" method="post" action="profiles/rename">
- <input type="hidden" name="name" value="<%= @profile.name -%>"/>
- <input type="hidden" name="language" value="<%= @profile.language -%>"/>
+ <input type="hidden" name="id" value="<%= @profile.id -%>"/>
<fieldset>
<div class="modal-head">
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.platform.PersistentSettings;
import org.sonar.server.user.MockUserSession;
import java.io.Reader;
@Mock
PreviewCache dryRunCache;
+ @Mock
+ PersistentSettings persistentSettings;
+
List<ProfileExporter> exporters = newArrayList();
List<ProfileImporter> importers = newArrayList();
@Before
public void setUp() throws Exception {
when(myBatis.openSession()).thenReturn(session);
- operations = new QProfileOperations(myBatis, dao, activeRuleDao, exporters, importers, dryRunCache);
+ operations = new QProfileOperations(myBatis, dao, activeRuleDao, exporters, importers, dryRunCache, persistentSettings);
}
@Test
@Test
public void rename_profile() throws Exception {
- QualityProfileDto dto = new QualityProfileDto().setName("Default").setLanguage("java");
- when(dao.selectByNameAndLanguage(eq("Default"), anyString())).thenReturn(dto);
- operations.renameProfile("Default", "java", "Default profile", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+ QualityProfileDto dto = new QualityProfileDto().setId(1).setName("Default").setLanguage("java");
+ when(dao.selectById(1)).thenReturn(dto);
+ operations.renameProfile(1, "Default profile", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
ArgumentCaptor<QualityProfileDto> profileArgument = ArgumentCaptor.forClass(QualityProfileDto.class);
verify(dao).update(profileArgument.capture());
@Test
public void fail_to_rename_profile_on_unknown_profile() throws Exception {
try {
- operations.renameProfile("Default", "java", "Default profile", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+ operations.renameProfile(1, "Default profile", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(NotFoundException.class);
@Test
public void fail_to_rename_profile_when_missing_new_name() throws Exception {
try {
- operations.renameProfile("Default", "java", "", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+ operations.renameProfile(1, "", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(BadRequestException.class);
@Test
public void fail_to_rename_profile_if_already_exists() throws Exception {
try {
- when(dao.selectByNameAndLanguage(eq("Default"), anyString())).thenReturn(new QualityProfileDto().setName("Default").setLanguage("java"));
+ when(dao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("Default").setLanguage("java"));
when(dao.selectByNameAndLanguage(eq("New Default"), anyString())).thenReturn(new QualityProfileDto().setName("New Default").setLanguage("java"));
- operations.renameProfile("Default", "java", "New Default", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+ operations.renameProfile(1, "New Default", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(BadRequestException.class);
}
verify(session, never()).commit();
}
+
+ @Test
+ public void update_default_profile() throws Exception {
+ when(dao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
+
+ operations.updateDefaultProfile(1, MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+
+ verify(persistentSettings).saveProperty("sonar.profile.java", "My profile");
+ }
+
+ @Test
+ public void update_default_profile_from_name_and_language() throws Exception {
+ when(dao.selectByNameAndLanguage("My profile", "java")).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
+ when(dao.selectById(1)).thenReturn(new QualityProfileDto().setId(1).setName("My profile").setLanguage("java"));
+
+ operations.updateDefaultProfile("My profile", "java", MockUserSession.create().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN));
+
+ verify(persistentSettings).saveProperty("sonar.profile.java", "My profile");
+ }
}
@Test
public void rename_profile() throws Exception {
- qProfiles.renameProfile("Default", "java", "Default profile");
- verify(operations).renameProfile(eq("Default"), eq("java"), eq("Default profile"), any(UserSession.class));
+ qProfiles.renameProfile(1, "Default profile");
+ verify(operations).renameProfile(eq(1), eq("Default profile"), any(UserSession.class));
}
- @Test(expected = UnsupportedOperationException.class)
- public void testSetDefaultProfile() throws Exception {
- qProfiles.updateDefaultProfile();
+ @Test
+ public void update_default_profile() throws Exception {
+ qProfiles.updateDefaultProfile(1);
+ verify(operations).updateDefaultProfile(eq(1), any(UserSession.class));
+ }
+
+ @Test
+ public void update_default_profile_from_name_and_language() throws Exception {
+ qProfiles.updateDefaultProfile("Default", "java");
+ verify(operations).updateDefaultProfile(eq("Default"), eq("java"), any(UserSession.class));
}
@Test(expected = UnsupportedOperationException.class)