aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
authorStas Vilchik <vilchiks@gmail.com>2014-03-19 13:38:43 +0600
committerStas Vilchik <vilchiks@gmail.com>2014-03-19 13:38:43 +0600
commit3c3c1e3e2c091285017986c11f075f5a6d87a643 (patch)
treedda80d8d899e4679082616251dc22eaff7584dd1 /sonar-server
parent660591f1137d2dcbd13e9e29a1965bf3dd1febb1 (diff)
parent398dfaff33c9a2c90c2bbbfa0f7f41a867896f8d (diff)
downloadsonarqube-3c3c1e3e2c091285017986c11f075f5a6d87a643.tar.gz
sonarqube-3c3c1e3e2c091285017986c11f075f5a6d87a643.zip
Merge branch 'master' into grunt
Conflicts: sonar-server/src/main/webapp/javascripts/quality-gate/views/quality-gate-detail-condition-view.js sonar-server/src/main/webapp/stylesheets/quality-gates.css
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java116
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java6
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/characteristic.rb12
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_condition_template.hbs.erb12
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_projects_template.hbs.erb6
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/db/migrate/499_delete_inclusions_properties.rb35
-rw-r--r--sonar-server/src/main/webapp/coffee/quality-gate/views/quality-gate-detail-condition-view.coffee3
-rw-r--r--sonar-server/src/main/webapp/javascripts/quality-gate/views/quality-gate-detail-condition-view.js146
-rw-r--r--sonar-server/src/main/webapp/less/quality-gates.less10
-rw-r--r--sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java245
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java12
11 files changed, 376 insertions, 227 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
index a5a32807add..367f3bd1052 100644
--- a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
+++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelService.java
@@ -22,13 +22,21 @@ package org.sonar.server.debt;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
+import org.apache.ibatis.session.SqlSession;
import org.sonar.api.server.debt.DebtCharacteristic;
import org.sonar.api.server.debt.DebtModel;
import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.MyBatis;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.UserSession;
+import org.sonar.server.util.Validation;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
@@ -37,12 +45,15 @@ import static com.google.common.collect.Lists.newArrayList;
/**
* Used through ruby code <pre>Internal.debt</pre>
+ * Also used by SQALE plugin.
*/
public class DebtModelService implements DebtModel {
+ private final MyBatis mybatis;
private final CharacteristicDao dao;
- public DebtModelService(CharacteristicDao dao) {
+ public DebtModelService(MyBatis mybatis, CharacteristicDao dao) {
+ this.mybatis = mybatis;
this.dao = dao;
}
@@ -60,6 +71,109 @@ public class DebtModelService implements DebtModel {
return dto != null ? toCharacteristic(dto) : null;
}
+ public DebtCharacteristic create(String name, @Nullable Integer parentId) {
+ checkPermission();
+
+ SqlSession session = mybatis.openSession();
+ try {
+ checkNotAlreadyExists(name, session);
+
+ CharacteristicDto newCharacteristic = new CharacteristicDto()
+ .setKey(name.toUpperCase().replace(" ", "_"))
+ .setName(name)
+ .setEnabled(true);
+
+ // New sub characteristic
+ if (parentId != null) {
+ CharacteristicDto parent = findCharacteristic(parentId, session);
+ if (parent.getParentId() != null) {
+ throw new BadRequestException("A sub characteristic can not have a sub characteristic as parent.");
+ }
+ newCharacteristic.setParentId(parent.getId());
+ } else {
+ // New root characteristic
+ newCharacteristic.setOrder(dao.selectMaxCharacteristicOrder(session) + 1);
+ }
+ dao.insert(newCharacteristic, session);
+ session.commit();
+ return toCharacteristic(newCharacteristic);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public DebtCharacteristic rename(int characteristicId, String newName) {
+ checkPermission();
+
+ SqlSession session = mybatis.openSession();
+ try {
+ checkNotAlreadyExists(newName, session);
+
+ CharacteristicDto dto = findCharacteristic(characteristicId, session);
+ if (!dto.getName().equals(newName)) {
+ dto.setName(newName);
+ dao.update(dto, session);
+ session.commit();
+ }
+ return toCharacteristic(dto);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public DebtCharacteristic moveUp(int characteristicId) {
+ return move(characteristicId, true);
+ }
+
+ public DebtCharacteristic moveDown(int characteristicId) {
+ return move(characteristicId, false);
+ }
+
+ private DebtCharacteristic move(int characteristicId, boolean moveUpOrDown) {
+ checkPermission();
+
+ SqlSession session = mybatis.openSession();
+ try {
+ CharacteristicDto dto = findCharacteristic(characteristicId, session);
+ int currentOrder = dto.getOrder();
+ CharacteristicDto dtoToSwitchOrderWith = moveUpOrDown ? dao.selectPrevious(currentOrder, session) : dao.selectNext(currentOrder, session);
+
+ // Do nothing when characteristic is already to the new location
+ if (dtoToSwitchOrderWith == null) {
+ return toCharacteristic(dto);
+ }
+ int nextOrder = dtoToSwitchOrderWith.getOrder();
+ dtoToSwitchOrderWith.setOrder(currentOrder);
+ dao.update(dtoToSwitchOrderWith, session);
+
+ dto.setOrder(nextOrder);
+ dao.update(dto, session);
+
+ session.commit();
+ return toCharacteristic(dto);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ private CharacteristicDto findCharacteristic(Integer id, SqlSession session) {
+ CharacteristicDto dto = dao.selectById(id, session);
+ if (dto == null) {
+ throw new NotFoundException(String.format("Characteristic with id %s does not exists.", id));
+ }
+ return dto;
+ }
+
+ private void checkNotAlreadyExists(String name, SqlSession session) {
+ if (dao.selectByName(name, session) != null) {
+ throw BadRequestException.ofL10n(Validation.IS_ALREADY_USED_MESSAGE, name);
+ }
+ }
+
+ private void checkPermission() {
+ UserSession.get().checkGlobalPermission(GlobalPermissions.SYSTEM_ADMIN);
+ }
+
private static List<DebtCharacteristic> toCharacteristics(Collection<CharacteristicDto> dtos) {
return newArrayList(Iterables.transform(dtos, new Function<CharacteristicDto, DebtCharacteristic>() {
@Override
diff --git a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
index 10ab51f61a5..5f51aa76eb8 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
@@ -139,11 +139,11 @@ public class QualityGates {
public void delete(long idToDelete) {
checkPermission(UserSession.get());
QualityGateDto qGate = getNonNullQgate(idToDelete);
- if (isDefault(qGate)) {
- throw new BadRequestException("Impossible to delete default quality gate.");
- }
SqlSession session = myBatis.openSession();
try {
+ if (isDefault(qGate)) {
+ propertiesDao.deleteGlobalProperty(SONAR_QUALITYGATE_PROPERTY, session);
+ }
propertiesDao.deleteProjectProperties(SONAR_QUALITYGATE_PROPERTY, Long.toString(idToDelete), session);
dao.delete(qGate, session);
session.commit();
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/characteristic.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/characteristic.rb
index e04b5154720..2633ee39b42 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/models/characteristic.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/models/characteristic.rb
@@ -30,10 +30,12 @@ class Characteristic < ActiveRecord::Base
MINUTE = "mn"
belongs_to :parent, :class_name => 'Characteristic', :foreign_key => 'parent_id'
+
+ # Needed for Views Plugin. Remove it when the plugin will not used it anymore
belongs_to :rule
- validates_uniqueness_of :name, :scope => [:enabled], :case_sensitive => false, :if => Proc.new { |c| c.rule_id.nil? && c.enabled }
- validates_length_of :name, :in => 1..NAME_MAX_SIZE, :allow_blank => false, :if => Proc.new { |c| c.rule_id.nil? }
+ validates_uniqueness_of :name, :scope => [:enabled], :case_sensitive => false, :if => Proc.new { |c| c.enabled }
+ validates_length_of :name, :in => 1..NAME_MAX_SIZE, :allow_blank => false
def root?
parent_id.nil?
@@ -48,11 +50,7 @@ class Characteristic < ActiveRecord::Base
end
def name(rule_name_if_empty=false)
- result=read_attribute(:name)
- if (result.nil? && rule_name_if_empty && rule_id)
- result=rule.name
- end
- result
+ read_attribute(:name)
end
end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_condition_template.hbs.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_condition_template.hbs.erb
index 6023e5b4357..7ca50016040 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_condition_template.hbs.erb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_condition_template.hbs.erb
@@ -5,11 +5,13 @@
<td width="10%" nowrap>
{{#if canEdit}}
<select name="period">
- <option value="0">{{t 'value'}}</option>
- {{#each periods}}<option value="{{key}}">{{text}}</option>{{/each}}
+ {{#unless isDiffMetric}}<option value="0">{{t 'value'}}</option>{{/unless}}
+ {{#each periods}}<option value="{{key}}">&Delta; {{text}}</option>{{/each}}
</select>
{{else}}
- {{periodText}}
+ {{#if periodText}}&Delta; {{periodText}}
+ {{else}}{{t 'value'}}
+ {{/if}}
{{/if}}
</td>
<td width="10%" nowrap>
@@ -23,7 +25,7 @@
{{t 'quality_gates.operator' op}}
{{/if}}
</td>
- <td width="15%">
+ <td width="15%" nowrap="nowrap">
<i class="icon-alert-warn" title="{{t 'alerts.warning_tooltip'}}"></i>
{{#if canEdit}}
<input name="warning" class="measure-input" data-type="{{metric.type}}" type="text">
@@ -31,7 +33,7 @@
{{warning}}
{{/if}}
</td>
- <td width="15%">
+ <td width="15%" nowrap="nowrap">
<i class="icon-alert-error" title="{{t 'alerts.error_tooltip'}}"></i>
{{#if canEdit}}
<input name="error" class="measure-input" data-type="{{metric.type}}" type="text">
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_projects_template.hbs.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_projects_template.hbs.erb
index 22487657e3b..7ea0b397156 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_projects_template.hbs.erb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/views/quality_gates/templates/_quality_gate_detail_projects_template.hbs.erb
@@ -2,11 +2,13 @@
<div class="quality-gate-section-name">{{t 'quality_gates.projects'}}</div>
{{#if default}}
+ <p class="quality-gate-default-message">
{{#if canEdit}}
- <p>{{t 'quality_gates.projects_for_default.edit'}}</p>
+ {{t 'quality_gates.projects_for_default.edit'}}
{{else}}
- <p>{{t 'quality_gates.projects_for_default'}}</p>
+ {{t 'quality_gates.projects_for_default'}}
{{/if}}
+ </p>
{{else}}
<div id="select-list-projects"></div>
{{/if}}
diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/499_delete_inclusions_properties.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/499_delete_inclusions_properties.rb
deleted file mode 100644
index 84a8bfb9899..00000000000
--- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/499_delete_inclusions_properties.rb
+++ /dev/null
@@ -1,35 +0,0 @@
-#
-# SonarQube, open source software quality management tool.
-# Copyright (C) 2008-2013 SonarSource
-# mailto:contact AT sonarsource DOT com
-#
-# SonarQube is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 3 of the License, or (at your option) any later version.
-#
-# SonarQube is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public License
-# along with this program; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-
-#
-# SonarQube 4.2
-# SONAR-5143
-#
-class DeleteInclusionsProperties < ActiveRecord::Migration
-
- class Property < ActiveRecord::Base
- end
-
- def self.up
- Property.delete_all("prop_key = 'sonar.inclusions'")
- Property.delete_all("prop_key = 'sonar.test.inclusions'")
- end
-
-end
diff --git a/sonar-server/src/main/webapp/coffee/quality-gate/views/quality-gate-detail-condition-view.coffee b/sonar-server/src/main/webapp/coffee/quality-gate/views/quality-gate-detail-condition-view.coffee
index da1b8d46f04..26f23dbc32e 100644
--- a/sonar-server/src/main/webapp/coffee/quality-gate/views/quality-gate-detail-condition-view.coffee
+++ b/sonar-server/src/main/webapp/coffee/quality-gate/views/quality-gate-detail-condition-view.coffee
@@ -42,6 +42,7 @@ define [
metricKey = @model.get('metric')
metric = _.findWhere @options.app.metrics, key: metricKey
@model.set { metric: metric }, { silent: true }
+ @model.set { isDiffMetric: metric.key.indexOf('new_') == 0 }, { silent: true }
onRender: ->
@@ -105,7 +106,7 @@ define [
serializeData: ->
- period = _.findWhere(@options.app.periods, key: '' + this.model.get('period'))
+ period = _.findWhere(@options.app.periods, key: this.model.get('period'))
_.extend super,
canEdit: @options.app.canEdit
periods: @options.app.periods
diff --git a/sonar-server/src/main/webapp/javascripts/quality-gate/views/quality-gate-detail-condition-view.js b/sonar-server/src/main/webapp/javascripts/quality-gate/views/quality-gate-detail-condition-view.js
deleted file mode 100644
index d69b084c3e4..00000000000
--- a/sonar-server/src/main/webapp/javascripts/quality-gate/views/quality-gate-detail-condition-view.js
+++ /dev/null
@@ -1,146 +0,0 @@
-(function() {
- var __hasProp = {}.hasOwnProperty,
- __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
-
- define(['backbone.marionette', 'handlebars'], function(Marionette, Handlebars) {
- var QualityGateDetailConditionView;
- return QualityGateDetailConditionView = (function(_super) {
- __extends(QualityGateDetailConditionView, _super);
-
- function QualityGateDetailConditionView() {
- return QualityGateDetailConditionView.__super__.constructor.apply(this, arguments);
- }
-
- QualityGateDetailConditionView.prototype.tagName = 'tr';
-
- QualityGateDetailConditionView.prototype.template = Handlebars.compile(jQuery('#quality-gate-detail-condition-template').html());
-
- QualityGateDetailConditionView.prototype.spinner = '<i class="spinner"></i>';
-
- QualityGateDetailConditionView.prototype.modelEvents = {
- 'change:id': 'render'
- };
-
- QualityGateDetailConditionView.prototype.ui = {
- periodSelect: '[name=period]',
- operatorSelect: '[name=operator]',
- warningInput: '[name=warning]',
- errorInput: '[name=error]',
- actionsBox: '.quality-gate-condition-actions',
- updateButton: '.update-condition'
- };
-
- QualityGateDetailConditionView.prototype.events = {
- 'click @ui.updateButton': 'saveCondition',
- 'click .delete-condition': 'deleteCondition',
- 'click .add-condition': 'saveCondition',
- 'click .cancel-add-condition': 'cancelAddCondition',
- 'keyup :input': 'enableUpdate',
- 'change :input': 'enableUpdate'
- };
-
- QualityGateDetailConditionView.prototype.initialize = function() {
- return this.populateMetric();
- };
-
- QualityGateDetailConditionView.prototype.populateMetric = function() {
- var metric, metricKey;
- metricKey = this.model.get('metric');
- metric = _.findWhere(this.options.app.metrics, {
- key: metricKey
- });
- return this.model.set({
- metric: metric
- }, {
- silent: true
- });
- };
-
- QualityGateDetailConditionView.prototype.onRender = function() {
- this.ui.periodSelect.val(this.model.get('period') || '0');
- this.ui.operatorSelect.val(this.model.get('op'));
- this.ui.warningInput.val(this.model.get('warning'));
- this.ui.errorInput.val(this.model.get('error'));
- this.ui.periodSelect.select2({
- allowClear: false,
- minimumResultsForSearch: 999,
- width: '200px'
- });
- this.ui.operatorSelect.select2({
- allowClear: false,
- minimumResultsForSearch: 999,
- width: '150px'
- });
- if (this.model.isNew()) {
- return this.ui.periodSelect.select2('open');
- }
- };
-
- QualityGateDetailConditionView.prototype.showSpinner = function() {
- jQuery(this.spinner).prependTo(this.ui.actionsBox);
- return this.ui.actionsBox.find(':not(.spinner)').hide();
- };
-
- QualityGateDetailConditionView.prototype.hideSpinner = function() {
- this.ui.actionsBox.find('.spinner').remove();
- return this.ui.actionsBox.find(':not(.spinner)').show();
- };
-
- QualityGateDetailConditionView.prototype.saveCondition = function() {
- this.showSpinner();
- this.model.set({
- period: this.ui.periodSelect.val(),
- op: this.ui.operatorSelect.val(),
- warning: this.ui.warningInput.val(),
- error: this.ui.errorInput.val()
- });
- return this.model.save().always((function(_this) {
- return function() {
- _this.ui.updateButton.prop('disabled', true);
- return _this.hideSpinner();
- };
- })(this)).done((function(_this) {
- return function() {
- return _this.options.collectionView.updateConditions();
- };
- })(this));
- };
-
- QualityGateDetailConditionView.prototype.deleteCondition = function() {
- if (confirm(t('are_you_sure'))) {
- this.showSpinner();
- return this.model["delete"]().done((function(_this) {
- return function() {
- _this.options.collectionView.updateConditions();
- return _this.close();
- };
- })(this));
- }
- };
-
- QualityGateDetailConditionView.prototype.cancelAddCondition = function() {
- return this.close();
- };
-
- QualityGateDetailConditionView.prototype.enableUpdate = function() {
- return this.ui.updateButton.prop('disabled', false);
- };
-
- QualityGateDetailConditionView.prototype.serializeData = function() {
- var period;
- period = _.findWhere(this.options.app.periods, {
- key: '' + this.model.get('period')
- });
- return _.extend(QualityGateDetailConditionView.__super__.serializeData.apply(this, arguments), {
- canEdit: this.options.app.canEdit,
- periods: this.options.app.periods,
- periodText: period != null ? period.text : void 0
- });
- };
-
- return QualityGateDetailConditionView;
-
- })(Marionette.ItemView);
- });
-
-}).call(this);
diff --git a/sonar-server/src/main/webapp/less/quality-gates.less b/sonar-server/src/main/webapp/less/quality-gates.less
index a0293d5e1b3..4d07ee8f986 100644
--- a/sonar-server/src/main/webapp/less/quality-gates.less
+++ b/sonar-server/src/main/webapp/less/quality-gates.less
@@ -66,7 +66,7 @@
&.empty {
cursor: default;
}
-
+
.line {
padding-top: 2px;
padding-bottom: 2px;
@@ -76,7 +76,7 @@
text-transform: lowercase;
}
}
- }
+ }
}
@@ -108,3 +108,9 @@
.quality-gate-condition-actions {
position: relative;
}
+
+.quality-gate-default-message {
+ padding: 6px 5px;
+ border: 1px solid #ddd;
+ background-color: #efefef;
+}
diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java
index 8a59e05b387..61a58305daf 100644
--- a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelServiceTest.java
@@ -19,18 +19,30 @@
*/
package org.sonar.server.debt;
+import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
+import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
import org.sonar.api.server.debt.DebtCharacteristic;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.core.persistence.MyBatis;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.MockUserSession;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.when;
+import static org.fest.assertions.Fail.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class DebtModelServiceTest {
@@ -38,51 +50,240 @@ public class DebtModelServiceTest {
@Mock
CharacteristicDao dao;
+ @Mock
+ MyBatis mybatis;
+
+ @Mock
+ SqlSession session;
+
+ CharacteristicDto rootCharacteristicDto = new CharacteristicDto()
+ .setId(1)
+ .setKey("MEMORY_EFFICIENCY")
+ .setName("Memory use")
+ .setOrder(2);
+
+ CharacteristicDto characteristicDto = new CharacteristicDto()
+ .setId(2)
+ .setKey("EFFICIENCY")
+ .setName("Efficiency")
+ .setParentId(1);
+
+ int currentId;
+
DebtModelService service;
@Before
public void setUp() throws Exception {
- service = new DebtModelService(dao);
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+
+ currentId = 10;
+ // Associate an id when inserting an object to simulate the db id generator
+ doAnswer(new Answer() {
+ public Object answer(InvocationOnMock invocation) {
+ Object[] args = invocation.getArguments();
+ CharacteristicDto dto = (CharacteristicDto) args[0];
+ dto.setId(++currentId);
+ return null;
+ }
+ }).when(dao).insert(any(CharacteristicDto.class), any(SqlSession.class));
+
+ when(mybatis.openSession()).thenReturn(session);
+ service = new DebtModelService(mybatis, dao);
}
@Test
public void find_root_characteristics() {
- CharacteristicDto dto = new CharacteristicDto()
- .setId(1)
- .setKey("MEMORY_EFFICIENCY")
- .setName("Memory use");
- when(dao.selectEnabledRootCharacteristics()).thenReturn(newArrayList(dto));
+ when(dao.selectEnabledRootCharacteristics()).thenReturn(newArrayList(rootCharacteristicDto));
assertThat(service.rootCharacteristics()).hasSize(1);
}
@Test
public void find_characteristics() {
- CharacteristicDto dto = new CharacteristicDto()
- .setId(1)
- .setKey("MEMORY_EFFICIENCY")
- .setName("Memory use");
- when(dao.selectEnabledCharacteristics()).thenReturn(newArrayList(dto));
+ when(dao.selectEnabledCharacteristics()).thenReturn(newArrayList(rootCharacteristicDto));
assertThat(service.characteristics()).hasSize(1);
}
@Test
public void find_characteristic_by_id() {
- CharacteristicDto dto = new CharacteristicDto()
- .setId(1)
- .setKey("MEMORY_EFFICIENCY")
- .setName("Memory use")
- .setParentId(2)
- .setOrder(1);
- when(dao.selectById(1)).thenReturn(dto);
+ when(dao.selectById(1)).thenReturn(rootCharacteristicDto);
DebtCharacteristic characteristic = service.characteristicById(1);
assertThat(characteristic.id()).isEqualTo(1);
assertThat(characteristic.key()).isEqualTo("MEMORY_EFFICIENCY");
assertThat(characteristic.name()).isEqualTo("Memory use");
- assertThat(characteristic.parentId()).isEqualTo(2);
- assertThat(characteristic.order()).isEqualTo(1);
+ assertThat(characteristic.order()).isEqualTo(2);
+ assertThat(characteristic.parentId()).isNull();
+
+ assertThat(service.characteristicById(111)).isNull();
+ }
+
+ @Test
+ public void create_sub_characteristic() {
+ when(dao.selectById(1, session)).thenReturn(rootCharacteristicDto);
+
+ DebtCharacteristic result = service.create("Compilation name", 1);
+
+ assertThat(result.id()).isEqualTo(currentId);
+ assertThat(result.key()).isEqualTo("COMPILATION_NAME");
+ assertThat(result.name()).isEqualTo("Compilation name");
+ assertThat(result.parentId()).isEqualTo(1);
+ }
+
+ @Test
+ public void fail_to_create_sub_characteristic_when_parent_id_is_not_a_root_characteristic() {
+ when(dao.selectById(1, session)).thenReturn(characteristicDto);
+
+ try {
+ service.create("Compilation", 1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("A sub characteristic can not have a sub characteristic as parent.");
+ }
+ }
+
+ @Test
+ public void fail_to_create_sub_characteristic_when_parent_does_not_exists() {
+ when(dao.selectById(1, session)).thenReturn(null);
+
+ try {
+ service.create("Compilation", 1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("Characteristic with id 1 does not exists.");
+ }
+ }
+
+ @Test
+ public void fail_to_create_sub_characteristic_when_name_already_used() {
+ when(dao.selectByName("Compilation", session)).thenReturn(new CharacteristicDto());
+ when(dao.selectById(1, session)).thenReturn(rootCharacteristicDto);
+
+ try {
+ service.create("Compilation", 1);
+ fail();
+ } catch (BadRequestException e) {
+ assertThat(e.l10nKey()).isEqualTo("errors.is_already_used");
+ assertThat(e.l10nParams().iterator().next()).isEqualTo("Compilation");
+ }
+ }
+
+ @Test
+ public void fail_to_create_sub_characteristic_when_wrong_permission() {
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.DASHBOARD_SHARING);
+
+ try {
+ service.create("Compilation", 1);
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(ForbiddenException.class);
+ }
+ }
+
+ @Test
+ public void create_characteristic() {
+ when(dao.selectMaxCharacteristicOrder(session)).thenReturn(2);
+
+ DebtCharacteristic result = service.create("Portability", null);
+
+ assertThat(result.id()).isEqualTo(currentId);
+ assertThat(result.key()).isEqualTo("PORTABILITY");
+ assertThat(result.name()).isEqualTo("Portability");
+ assertThat(result.order()).isEqualTo(3);
+ }
+
+ @Test
+ public void create_first_characteristic() {
+ when(dao.selectMaxCharacteristicOrder(session)).thenReturn(0);
+
+ DebtCharacteristic result = service.create("Portability", null);
+
+ assertThat(result.id()).isEqualTo(currentId);
+ assertThat(result.key()).isEqualTo("PORTABILITY");
+ assertThat(result.name()).isEqualTo("Portability");
+ assertThat(result.order()).isEqualTo(1);
+ }
+
+ @Test
+ public void rename_characteristic() {
+ when(dao.selectById(10, session)).thenReturn(characteristicDto);
+
+ DebtCharacteristic result = service.rename(10, "New Efficiency");
+
+ assertThat(result.key()).isEqualTo("EFFICIENCY");
+ assertThat(result.name()).isEqualTo("New Efficiency");
+ }
+
+ @Test
+ public void not_rename_characteristic_when_renaming_with_same_name() {
+ when(dao.selectById(10, session)).thenReturn(characteristicDto);
+
+ service.rename(10, "Efficiency");
+
+ verify(dao, never()).update(any(CharacteristicDto.class), eq(session));
+ }
+
+ @Test
+ public void fail_to_rename_unknown_characteristic() {
+ when(dao.selectById(10, session)).thenReturn(null);
+
+ try {
+ service.rename(10, "New Efficiency");
+ fail();
+ } catch (Exception e) {
+ assertThat(e).isInstanceOf(NotFoundException.class).hasMessage("Characteristic with id 10 does not exists.");
+ }
+ }
+
+ @Test
+ public void move_up() {
+ when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setOrder(2));
+ when(dao.selectPrevious(2, session)).thenReturn(new CharacteristicDto().setId(2).setOrder(1));
+
+ DebtCharacteristic result = service.moveUp(10);
+
+ ArgumentCaptor<CharacteristicDto> argument = ArgumentCaptor.forClass(CharacteristicDto.class);
+ verify(dao, times(2)).update(argument.capture(), eq(session));
+
+ assertThat(result.order()).isEqualTo(1);
+ assertThat(argument.getAllValues().get(0).getOrder()).isEqualTo(2);
+ assertThat(argument.getAllValues().get(1).getOrder()).isEqualTo(1);
+ }
+
+ @Test
+ public void do_nothing_when_move_up_and_already_on_top() {
+ CharacteristicDto dto = new CharacteristicDto().setId(10).setOrder(1);
+ when(dao.selectById(10, session)).thenReturn(dto);
+ when(dao.selectPrevious(1, session)).thenReturn(null);
+
+ service.moveUp(10);
+
+ verify(dao, never()).update(any(CharacteristicDto.class), eq(session));
+ }
+
+ @Test
+ public void move_down() {
+ when(dao.selectById(10, session)).thenReturn(new CharacteristicDto().setId(10).setOrder(2));
+ when(dao.selectNext(2, session)).thenReturn(new CharacteristicDto().setId(2).setOrder(3));
+
+ DebtCharacteristic result = service.moveDown(10);
+
+ ArgumentCaptor<CharacteristicDto> argument = ArgumentCaptor.forClass(CharacteristicDto.class);
+ verify(dao, times(2)).update(argument.capture(), eq(session));
+
+ assertThat(result.order()).isEqualTo(3);
+ assertThat(argument.getAllValues().get(0).getOrder()).isEqualTo(2);
+ assertThat(argument.getAllValues().get(1).getOrder()).isEqualTo(3);
+ }
+
+ @Test
+ public void do_nothing_when_move_down_and_already_on_bottom() {
+ CharacteristicDto dto = new CharacteristicDto().setId(10).setOrder(5);
+ when(dao.selectById(10, session)).thenReturn(dto);
+ when(dao.selectNext(5, session)).thenReturn(null);
+
+ service.moveDown(10);
- assertThat(service.characteristicById(10)).isNull();
+ verify(dao, never()).update(any(CharacteristicDto.class), eq(session));
}
}
diff --git a/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java b/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java
index 55ef7f025a5..31fb5d55716 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java
@@ -257,14 +257,20 @@ public class QualityGatesTest {
verify(dao).delete(toDelete, session);
}
- @Test(expected = BadRequestException.class)
- public void should_not_delete_qgate_if_default() throws Exception {
+ @Test
+ public void should_delete_qgate_even_if_default() throws Exception {
long idToDelete = 42L;
String name = "To Delete";
QualityGateDto toDelete = new QualityGateDto().setId(idToDelete).setName(name);
when(dao.selectById(idToDelete)).thenReturn(toDelete);
- when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue(Long.toString(idToDelete)));
+ when(propertiesDao.selectGlobalProperty("sonar.qualitygate")).thenReturn(new PropertyDto().setValue("42"));
+ SqlSession session = mock(SqlSession.class);
+ when(myBatis.openSession()).thenReturn(session);
qGates.delete(idToDelete);
+ verify(dao).selectById(idToDelete);
+ verify(propertiesDao).deleteGlobalProperty("sonar.qualitygate", session);
+ verify(propertiesDao).deleteProjectProperties("sonar.qualitygate", "42", session);
+ verify(dao).delete(toDelete, session);
}
@Test