From 959cb697cf777b944557cf7d03b02eeab282d634 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 11 Jun 2014 18:53:57 +0200 Subject: [PATCH] SONAR-5392 When migrating to SonarQube 4.3.X with a incompatible plugin, requirements are not copied to rules --- .../core/template/LoadedTemplateDto.java | 1 + .../technicaldebt/db/CharacteristicMapper.xml | 20 +-- ...equirementsFromCharacteristicsToRules.java | 87 ++++++----- ...rementsFromCharacteristicsToRulesTest.java | 31 ++-- ...s_from_characteristics_to_rules_result.xml | 2 + .../do_nothing_when_already_executed.xml | 137 ++++++++++++++++++ ...o_nothing_when_already_executed_result.xml | 134 +++++++++++++++++ 7 files changed, 347 insertions(+), 65 deletions(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed.xml create mode 100644 sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed_result.xml diff --git a/sonar-core/src/main/java/org/sonar/core/template/LoadedTemplateDto.java b/sonar-core/src/main/java/org/sonar/core/template/LoadedTemplateDto.java index 99cb9ba5447..d0a52bc1c66 100644 --- a/sonar-core/src/main/java/org/sonar/core/template/LoadedTemplateDto.java +++ b/sonar-core/src/main/java/org/sonar/core/template/LoadedTemplateDto.java @@ -28,6 +28,7 @@ public final class LoadedTemplateDto { public static final String QUALITY_PROFILE_TYPE = "QUALITY_PROFILE"; public static final String PERMISSION_TEMPLATE_TYPE = "PERM_TEMPLATE"; public static final String QUALITY_GATE_TYPE = "QUALITY_GATE"; + public static final String ONE_SHOT_TASK_TYPE = "ONE_SHOT_TASK"; private Long id; private String key; diff --git a/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml b/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml index ba18caf7e8a..5857ba78a87 100644 --- a/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/technicaldebt/db/CharacteristicMapper.xml @@ -108,16 +108,16 @@ diff --git a/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java b/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java index 02024698f0d..10ddce7b90f 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRules.java @@ -29,15 +29,14 @@ import org.apache.commons.lang.builder.EqualsBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; -import org.sonar.api.platform.ServerUpgradeStatus; import org.sonar.api.rule.RuleStatus; -import org.sonar.api.rules.Rule; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.utils.Duration; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.core.rule.RuleDto; import org.sonar.core.technicaldebt.db.CharacteristicMapper; +import org.sonar.core.template.LoadedTemplateDto; import org.sonar.server.db.DbClient; import org.sonar.server.rule.RegisterRules; @@ -60,29 +59,31 @@ public class CopyRequirementsFromCharacteristicsToRules implements ServerCompone private static final Logger LOGGER = LoggerFactory.getLogger(CopyRequirementsFromCharacteristicsToRules.class); + private static final String TEMPLATE_KEY = "CopyRequirementsFromCharacteristicsToRules"; + private final DbClient dbClient; - private final ServerUpgradeStatus status; /** * @param registerRules used only to be started after init of rules */ - public CopyRequirementsFromCharacteristicsToRules(DbClient dbClient, ServerUpgradeStatus status, RegisterRules registerRules) { + public CopyRequirementsFromCharacteristicsToRules(DbClient dbClient, RegisterRules registerRules) { this.dbClient = dbClient; - this.status = status; } public void start() { - if (status.isUpgraded() && status.getInitialDbVersion() <= 520) { - doExecute(); - } + doExecute(); } private void doExecute() { - LOGGER.info("Copying requirement from characteristics to rules"); - copyRequirementsFromCharacteristicsToRules(); + if (dbClient.loadedTemplateDao().countByTypeAndKey(LoadedTemplateDto.ONE_SHOT_TASK_TYPE, TEMPLATE_KEY) == 0) { + LOGGER.info("Copying requirement from characteristics to rules"); + copyRequirementsFromCharacteristicsToRules(); + + LOGGER.info("Deleting requirements from characteristics"); + removeRequirementsDataFromCharacteristics(); - LOGGER.info("Deleting requirements data"); - removeRequirementsDataFromCharacteristics(); + dbClient.loadedTemplateDao().insert(new LoadedTemplateDto(TEMPLATE_KEY, LoadedTemplateDto.ONE_SHOT_TASK_TYPE)); + } } private void copyRequirementsFromCharacteristicsToRules() { @@ -90,20 +91,30 @@ public class CopyRequirementsFromCharacteristicsToRules implements ServerCompone try { List> requirementDtos = dbSession.getMapper(CharacteristicMapper.class).selectDeprecatedRequirements(); - final Multimap requirementsByRuleId = ArrayListMultimap.create(); - for (Map map : requirementDtos) { - RequirementDto requirementDto = new RequirementDto(map); - requirementsByRuleId.put(requirementDto.getRuleId(), requirementDto); - } + if (requirementDtos.isEmpty()) { + LOGGER.info("No requirement will be copied", requirementDtos); + + } else { + int requirementCopied = 0; - List rules = dbClient.ruleDao().findAll(dbSession); - for (RuleDto rule : rules) { - Collection requirementsForRule = requirementsByRuleId.get(rule.getId()); - if (!requirementsForRule.isEmpty()) { - convert(rule, requirementsForRule, dbSession); + final Multimap requirementsByRuleId = ArrayListMultimap.create(); + for (Map map : requirementDtos) { + RequirementDto requirementDto = new RequirementDto(map); + requirementsByRuleId.put(requirementDto.getRuleId(), requirementDto); } + + List rules = dbClient.ruleDao().findAll(dbSession); + for (RuleDto rule : rules) { + Collection requirementsForRule = requirementsByRuleId.get(rule.getId()); + if (!requirementsForRule.isEmpty()) { + convert(rule, requirementsForRule, dbSession); + requirementCopied++; + } + } + dbSession.commit(); + + LOGGER.info("{} requirements have been found, {} have be copied", requirementDtos.size(), requirementCopied); } - dbSession.commit(); } finally { MyBatis.closeQuietly(dbSession); } @@ -174,10 +185,10 @@ public class CopyRequirementsFromCharacteristicsToRules implements ServerCompone @CheckForNull @VisibleForTesting - static String convertDuration(@Nullable Double oldValue, @Nullable String oldUnit) { - if (oldValue != null && oldValue > 0) { + static String convertDuration(@Nullable Number oldValue, @Nullable String oldUnit) { + if (oldValue != null && oldValue.doubleValue() > 0) { // As value is stored in double, we have to round it in order to have an integer (for instance, if it was 1.6, we'll use 2) - return Integer.toString((int) Math.round(oldValue)) + convertUnit(oldUnit); + return Integer.toString((int) Math.round(oldValue.doubleValue())) + convertUnit(oldUnit); } return null; } @@ -220,55 +231,53 @@ public class CopyRequirementsFromCharacteristicsToRules implements ServerCompone * Do not use Integer because Oracle returns BigDecimal */ public Number getId() { - return (Number) map.get("ID"); + return (Number) map.get("id"); } /** * Do not use Integer because Oracle returns BigDecimal */ public Number getParentId() { - return (Number) map.get("PARENTID"); + return (Number) map.get("parentId"); } /** * Do not use Integer because Oracle returns BigDecimal */ public Number getRuleId() { - return (Number) map.get("RULEID"); + return (Number) map.get("ruleId"); } public String getFunction() { - return (String) map.get("FUNCTIONKEY"); + return (String) map.get("functionKey"); } @CheckForNull - public Double getCoefficientValue() { - return (Double) map.get("COEFFICIENTVALUE"); + public Number getCoefficientValue() { + return (Number) map.get("coefficientValue"); } @CheckForNull public String getCoefficientUnit() { - return (String) map.get("COEFFICIENTUNIT"); + return (String) map.get("coefficientUnit"); } @CheckForNull - public Double getOffsetValue() { - return (Double) map.get("OFFSETVALUE"); + public Number getOffsetValue() { + return (Number) map.get("offsetValue"); } @CheckForNull public String getOffsetUnit() { - return (String) map.get("OFFSETUNIT"); + return (String) map.get("offsetUnit"); } public boolean isEnabled() { - return (Boolean) map.get("ENABLED"); + return (Boolean) map.get("enabled"); } - - } } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest.java b/sonar-server/src/test/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest.java index 9c7cfbeeaa6..ac7dcd807dc 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest.java @@ -25,11 +25,11 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.sonar.api.platform.ServerUpgradeStatus; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.System2; import org.sonar.core.persistence.TestDatabase; import org.sonar.core.rule.RuleDto; +import org.sonar.core.template.LoadedTemplateDao; import org.sonar.server.db.DbClient; import org.sonar.server.rule.db.RuleDao; @@ -42,31 +42,27 @@ public class CopyRequirementsFromCharacteristicsToRulesTest { @ClassRule public static TestDatabase db = new TestDatabase(); - @Mock - ServerUpgradeStatus status; - @Mock System2 system2; + DbClient dbClient; + CopyRequirementsFromCharacteristicsToRules service; @Before public void setUp() throws Exception { when(system2.now()).thenReturn(DateUtils.parseDate("2014-03-13").getTime()); - DbClient dbClient = new DbClient(db.database(), db.myBatis(), new RuleDao(system2)); - service = new CopyRequirementsFromCharacteristicsToRules(dbClient, status, null); + dbClient = new DbClient(db.database(), db.myBatis(), new RuleDao(system2), new LoadedTemplateDao(db.myBatis())); + service = new CopyRequirementsFromCharacteristicsToRules(dbClient, null); } @Test public void copy_requirements_from_characteristics_to_rules() throws Exception { db.prepareDbUnit(getClass(), "copy_requirements_from_characteristics_to_rules.xml"); - when(status.isUpgraded()).thenReturn(true); - when(status.getInitialDbVersion()).thenReturn(498); - service.start(); - db.assertDbUnit(getClass(), "copy_requirements_from_characteristics_to_rules_result.xml", "rules"); + db.assertDbUnit(getClass(), "copy_requirements_from_characteristics_to_rules_result.xml", "rules", "loaded_templates"); } /** @@ -76,9 +72,6 @@ public class CopyRequirementsFromCharacteristicsToRulesTest { public void convert_constant_issue_with_coeff_to_constant_issue_with_offset() throws Exception { db.prepareDbUnit(getClass(), "convert_constant_issue_with_coeff_to_constant_issue_with_offset.xml"); - when(status.isUpgraded()).thenReturn(true); - when(status.getInitialDbVersion()).thenReturn(498); - service.start(); db.assertDbUnit(getClass(), "convert_constant_issue_with_coeff_to_constant_issue_with_offset_result.xml", "rules"); @@ -88,14 +81,20 @@ public class CopyRequirementsFromCharacteristicsToRulesTest { public void remove_requirements_data_from_characteristics() throws Exception { db.prepareDbUnit(getClass(), "remove_requirements_data_from_characteristics.xml"); - when(status.isUpgraded()).thenReturn(true); - when(status.getInitialDbVersion()).thenReturn(498); - service.start(); db.assertDbUnit(getClass(), "remove_requirements_data_from_characteristics_result.xml", "characteristics"); } + @Test + public void do_nothing_when_already_executed() throws Exception { + db.prepareDbUnit(getClass(), "do_nothing_when_already_executed.xml"); + + service.start(); + + db.assertDbUnit(getClass(), "do_nothing_when_already_executed_result.xml", "rules"); + } + @Test public void convert_duration() throws Exception { assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(1.0, "h")).isEqualTo("1h"); diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/copy_requirements_from_characteristics_to_rules_result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/copy_requirements_from_characteristics_to_rules_result.xml index 9fe876218ab..bc8c7c6d925 100644 --- a/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/copy_requirements_from_characteristics_to_rules_result.xml +++ b/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/copy_requirements_from_characteristics_to_rules_result.xml @@ -73,4 +73,6 @@ remediation_offset="[null]" default_remediation_offset="[null]" updated_at="2014-03-13" NOTE_CREATED_AT="[null]" NOTE_DATA="[null]" NOTE_UPDATED_AT="[null]" NOTE_USER_LOGIN="[null]" PARENT_ID="[null]" PLUGIN_CONFIG_KEY="[null]" PRIORITY="[null]" cardinality="[null]" created_at="[null]" language="[null]" effort_to_fix_description="[null]"/> + + diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed.xml b/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed.xml new file mode 100644 index 00000000000..ec71824d7b8 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed.xml @@ -0,0 +1,137 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed_result.xml b/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed_result.xml new file mode 100644 index 00000000000..594b202c4a4 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/startup/CopyRequirementsFromCharacteristicsToRulesTest/do_nothing_when_already_executed_result.xml @@ -0,0 +1,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.39.5