2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * SonarQube is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.startup;
22 import org.junit.Before;
23 import org.junit.ClassRule;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.sonar.api.platform.ServerUpgradeStatus;
29 import org.sonar.api.utils.DateUtils;
30 import org.sonar.api.utils.System2;
31 import org.sonar.core.persistence.AbstractDaoTestCase;
32 import org.sonar.core.persistence.TestDatabase;
33 import org.sonar.core.technicaldebt.db.RequirementDao;
34 import org.sonar.server.rule.RuleRegistry;
36 import static org.fest.assertions.Assertions.assertThat;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
40 @RunWith(MockitoJUnitRunner.class)
41 public class CopyRequirementsFromCharacteristicsToRulesTest extends AbstractDaoTestCase {
44 public static TestDatabase db = new TestDatabase().schema(CopyRequirementsFromCharacteristicsToRulesTest.class, "schema.sql");
47 ServerUpgradeStatus status;
53 RuleRegistry ruleRegistry;
55 CopyRequirementsFromCharacteristicsToRules service;
58 public void setUp() throws Exception {
59 when(system2.now()).thenReturn(DateUtils.parseDateTime("2014-03-13T19:10:03+0100").getTime());
60 service = new CopyRequirementsFromCharacteristicsToRules(db.database(), new RequirementDao(getMyBatis()), ruleRegistry, status, system2);
64 public void copy_requirements_from_characteristics_to_rules() throws Exception {
65 setupData("requirements");
66 db.prepareDbUnit(getClass(), "copy_requirements_from_characteristics_to_rules.xml");
68 when(status.isUpgraded()).thenReturn(true);
69 when(status.getInitialDbVersion()).thenReturn(498);
73 db.assertDbUnit(getClass(), "copy_requirements_from_characteristics_to_rules_result.xml", "rules");
74 verify(ruleRegistry).reindex();
78 public void remove_requirements_data_from_characteristics() throws Exception {
79 db.prepareDbUnit(getClass(), "remove_requirements_data_from_characteristics.xml");
81 when(status.isUpgraded()).thenReturn(true);
82 when(status.getInitialDbVersion()).thenReturn(498);
86 db.assertDbUnit(getClass(), "remove_requirements_data_from_characteristics_result.xml", "characteristics");
87 verify(ruleRegistry).reindex();
91 public void convert_duration() throws Exception {
92 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(1.0, "h")).isEqualTo("1h");
93 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(15.0, "d")).isEqualTo("15d");
94 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(5.0, "min")).isEqualTo("5min");
95 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(5.0, "mn")).isEqualTo("5min");
97 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(0.9, "h")).isEqualTo("1h");
98 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(1.4, "h")).isEqualTo("1h");
100 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(1.0, null)).isEqualTo("1d");
101 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(null, "d")).isNull();
103 assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(0.0, "d")).isNull();
107 public void is_debt_default_values_same_as_overridden_values() throws Exception {
108 assertThat(CopyRequirementsFromCharacteristicsToRules.isDebtDefaultValuesSameAsOverriddenValues(new CopyRequirementsFromCharacteristicsToRules.RuleRow()
109 .setDefaultCharacteristicId(1).setCharacteristicId(1)
110 .setDefaultFunction("LINEAR_OFFSET").setFunction("LINEAR_OFFSET")
111 .setDefaultCoefficient("5h").setCoefficient("5h")
112 .setDefaultOffset("10min").setOffset("10min")
115 assertThat(CopyRequirementsFromCharacteristicsToRules.isDebtDefaultValuesSameAsOverriddenValues(new CopyRequirementsFromCharacteristicsToRules.RuleRow()
116 .setDefaultCharacteristicId(1).setCharacteristicId(2)
117 .setDefaultFunction("LINEAR_OFFSET").setFunction("LINEAR_OFFSET")
118 .setDefaultCoefficient("5h").setCoefficient("5h")
119 .setDefaultOffset("10min").setOffset("10min")
122 assertThat(CopyRequirementsFromCharacteristicsToRules.isDebtDefaultValuesSameAsOverriddenValues(new CopyRequirementsFromCharacteristicsToRules.RuleRow()
123 .setDefaultCharacteristicId(1).setCharacteristicId(1)
124 .setDefaultFunction("LINEAR_OFFSET").setFunction("LINEAR_OFFSET")
125 .setDefaultCoefficient("5h").setCoefficient("4h")
126 .setDefaultOffset("10min").setOffset("5min")
129 assertThat(CopyRequirementsFromCharacteristicsToRules.isDebtDefaultValuesSameAsOverriddenValues(new CopyRequirementsFromCharacteristicsToRules.RuleRow()
130 .setDefaultCharacteristicId(1).setCharacteristicId(1)
131 .setDefaultFunction("CONSTANT_ISSUE").setFunction("LINEAR")
132 .setDefaultCoefficient(null).setCoefficient("5h")
133 .setDefaultOffset("10min").setOffset(null)