From c4837e97760ab9898ae3fdae630e2ac414a7c5c6 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 8 Apr 2014 16:46:26 +0200 Subject: [PATCH] SONAR-5180 Add missing characteristics in the SQALE model --- .../core/persistence/DatabaseVersion.java | 2 +- .../org/sonar/core/persistence/rows-h2.sql | 2 + .../debt/DebtCharacteristicsXMLImporter.java | 10 ++- .../server/debt/DebtRulesXMLImporter.java | 2 +- .../WEB-INF/db/migrate/522_drop_alerts.rb | 3 +- ...ability_and_sub_characteristic_resource.rb | 68 +++++++++++++++++++ ...date_sub_characteristic_network_use_key.rb | 44 ++++++++++++ .../DebtCharacteristicsXMLImporterTest.java | 12 ++++ .../server/debt/DebtRulesXMLImporterTest.java | 15 ++++ .../convert_network_use_key.xml | 10 +++ .../convert_network_use_key.xml | 23 +++++++ 11 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/db/migrate/523_add_characteristic_reusability_and_sub_characteristic_resource.rb create mode 100644 sonar-server/src/main/webapp/WEB-INF/db/migrate/524_update_sub_characteristic_network_use_key.rb create mode 100644 sonar-server/src/test/resources/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest/convert_network_use_key.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/debt/DebtRulesXMLImporterTest/convert_network_use_key.xml diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java index 7d920dacaf2..f6a6dc41922 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java @@ -33,7 +33,7 @@ import java.util.List; */ public class DatabaseVersion implements BatchComponent, ServerComponent { - public static final int LAST_VERSION = 522; + public static final int LAST_VERSION = 524; public static enum Status { UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index b924803a886..c2427db7198 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -221,6 +221,8 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('519'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('520'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('521'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('522'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('523'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('524'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtCharacteristicsXMLImporter.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtCharacteristicsXMLImporter.java index 3ff96509ce8..0bb070730f7 100644 --- a/sonar-server/src/main/java/org/sonar/server/debt/DebtCharacteristicsXMLImporter.java +++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtCharacteristicsXMLImporter.java @@ -27,6 +27,7 @@ import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; import org.sonar.api.ServerComponent; import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic; +import org.sonar.api.server.rule.RulesDefinition; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -85,7 +86,7 @@ public class DebtCharacteristicsXMLImporter implements ServerComponent { while (cursor.getNext() != null) { String node = cursor.getLocalName(); if (StringUtils.equals(node, CHARACTERISTIC_KEY)) { - characteristic.setKey(cursor.collectDescendantText().trim()); + characteristic.setKey(convertKey(cursor.collectDescendantText().trim())); if (parent == null) { characteristic.setOrder(debtModel.rootCharacteristics().size() + 1); debtModel.addRootCharacteristic(characteristic); @@ -103,4 +104,11 @@ public class DebtCharacteristicsXMLImporter implements ServerComponent { } } + static String convertKey(String key){ + if ("NETWORK_USE_EFFICIENCY".equals(key)) { + return RulesDefinition.SubCharacteristics.NETWORK_USE; + } + return key; + } + } diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java index 66447c840cd..f78ab4adbed 100644 --- a/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java +++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java @@ -99,7 +99,7 @@ public class DebtRulesXMLImporter implements ServerComponent { RuleDebt ruleDebt = processRule(validationMessages, cursor); if (ruleDebt != null && parentKey != null) { if (rootKey != null) { - ruleDebt.setSubCharacteristicKey(parentKey); + ruleDebt.setSubCharacteristicKey(DebtCharacteristicsXMLImporter.convertKey(parentKey)); ruleDebts.add(ruleDebt); } else { validationMessages.addWarningText("Rule '" + ruleDebt.ruleKey() + "' is ignored because it's defined directly under a root characteristic."); diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/522_drop_alerts.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/522_drop_alerts.rb index 6b0403778a2..81fbbf63ee3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/522_drop_alerts.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/522_drop_alerts.rb @@ -20,6 +20,7 @@ # # SonarQube 4.3 +# SONAR-5097 # class DropAlerts < ActiveRecord::Migration @@ -27,4 +28,4 @@ class DropAlerts < ActiveRecord::Migration drop_table(:alerts) end -end \ No newline at end of file +end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/523_add_characteristic_reusability_and_sub_characteristic_resource.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/523_add_characteristic_reusability_and_sub_characteristic_resource.rb new file mode 100644 index 00000000000..f90c34378de --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/523_add_characteristic_reusability_and_sub_characteristic_resource.rb @@ -0,0 +1,68 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 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.3 +# SONAR-5180 +# +class AddCharacteristicReusabilityAndSubCharacteristicResource < ActiveRecord::Migration + + class Characteristic < ActiveRecord::Base + end + + def self.up + # On an empty DB, there are no characteristics, they're all gonna be created after + if Characteristic.all.size > 0 + Characteristic.reset_column_information + create_characteristic_reusability_and_its_sub_characteristics + create_sub_characteristic_resource + end + end + + def self.create_characteristic_reusability_and_its_sub_characteristics + # 'Reusability' is the new characteristic, it has two sub characteristics : 'Modularity' and 'Transportability' + reusability = Characteristic.first(:conditions => ['kee=? AND enabled=?', 'REUSABILITY', true]) + modularity = Characteristic.first(:conditions => ['kee=? AND enabled=?', 'MODULARITY', true]) + transportability = Characteristic.first(:conditions => ['kee=? AND enabled=?', 'TRANSPORTABILITY', true]) + + unless reusability + # Reusability should become the first characteristic + reusability = Characteristic.create(:name => 'Reusability', :kee => 'REUSABILITY', :characteristic_order => 1, :enabled => true) + # So all other characteristics have to moved down + Characteristic.all(:conditions => ['enabled=? AND parent_id IS NULL AND rule_id IS NULL', true]).each do |c| + c.characteristic_order = c.characteristic_order + 1 + c.save! + end + end + + Characteristic.create(:name => 'Modularity', :kee => 'MODULARITY', :parent_id => reusability.id, :enabled => true) unless modularity + Characteristic.create(:name => 'Transportability', :kee => 'TRANSPORTABILITY', :parent_id => reusability.id, :enabled => true) unless transportability + end + + def self.create_sub_characteristic_resource + # New sub characteristic 'Resource' (under characteristic 'Reliability') + resource = Characteristic.first(:conditions => ['kee=? AND enabled=?', 'RESOURCE_RELIABILITY', true]) + unless resource + reliability = Characteristic.first(:conditions => ['kee=? AND enabled=?', 'RELIABILITY', true]) + Characteristic.create(:name => 'Resource', :kee => 'RESOURCE_RELIABILITY', :parent_id => reliability.id, :enabled => true) if reliability + end + end + +end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/524_update_sub_characteristic_network_use_key.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/524_update_sub_characteristic_network_use_key.rb new file mode 100644 index 00000000000..b6bdc8f3183 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/524_update_sub_characteristic_network_use_key.rb @@ -0,0 +1,44 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 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.3 +# SONAR-5180 +# +class UpdateSubCharacteristicNetworkUseKey < ActiveRecord::Migration + + class Characteristic < ActiveRecord::Base + end + + def self.up + # On an empty DB, there are no characteristics, they're all gonna be created after + if Characteristic.all.size > 0 + Characteristic.reset_column_information + + # NETWORK_USE was created with a bad key in the migration 466 + network_use = Characteristic.first(:conditions => ['kee=? AND enabled=?', 'NETWORK_USE_EFFICIENCY', true]) + if network_use + network_use.kee = 'NETWORK_USE' + network_use.save! + end + end + end + +end diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest.java index 7dc0406ca64..89f10f109f7 100644 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest.java +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest.java @@ -83,6 +83,18 @@ public class DebtCharacteristicsXMLImporterTest { assertThat(debtModel.subCharacteristics("EFFICIENCY").get(0).name()).isEqualTo("Memory use"); } + /** + * SONAR-5180 + */ + @Test + public void convert_network_use_key() throws Exception { + String xml = getFileContent("convert_network_use_key.xml"); + + DebtModel debtModel = new DebtCharacteristicsXMLImporter().importXML(xml); + assertThat(debtModel.characteristicByKey("NETWORK_USE_EFFICIENCY")).isNull(); + assertThat(debtModel.characteristicByKey("NETWORK_USE")).isNotNull(); + } + @Test public void fail_on_bad_xml() throws Exception { String xml = getFileContent("fail_on_bad_xml.xml"); diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java index be069aa1b91..3b4b5612dbe 100644 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java @@ -224,6 +224,21 @@ public class DebtRulesXMLImporterTest { assertThat(validationMessages.getErrors()).isNotEmpty(); } + /** + * SONAR-5180 + */ + @Test + public void convert_network_use_key() throws Exception { + // Rule is linked to sub characteristic key NETWORK_USE_EFFICIENCY + String xml = getFileContent("convert_network_use_key.xml"); + + List results = importer.importXML(xml, validationMessages); + assertThat(results).hasSize(1); + + RuleDebt ruleDebt = results.get(0); + assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("NETWORK_USE"); + } + @Test public void fail_on_bad_xml() throws Exception { String xml = getFileContent("fail_on_bad_xml.xml"); diff --git a/sonar-server/src/test/resources/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest/convert_network_use_key.xml b/sonar-server/src/test/resources/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest/convert_network_use_key.xml new file mode 100644 index 00000000000..0ac33c47f82 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/debt/DebtCharacteristicsXMLImporterTest/convert_network_use_key.xml @@ -0,0 +1,10 @@ + + + EFFICIENCY + Efficiency + + NETWORK_USE_EFFICIENCY + Network use + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/debt/DebtRulesXMLImporterTest/convert_network_use_key.xml b/sonar-server/src/test/resources/org/sonar/server/debt/DebtRulesXMLImporterTest/convert_network_use_key.xml new file mode 100644 index 00000000000..d4707ba62a8 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/debt/DebtRulesXMLImporterTest/convert_network_use_key.xml @@ -0,0 +1,23 @@ + + + EFFICIENCY + Efficiency + + NETWORK_USE_EFFICIENCY + Network use + + checkstyle + Regexp + + remediationFactor + 3.0 + h + + + remediationFunction + LINEAR + + + + + -- 2.39.5