]> source.dussan.org Git - sonarqube.git/blob
1c7f5b37dc5bf5947501f1d8cb7ea91baccb389a
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.startup;
21
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;
35
36 import static org.fest.assertions.Assertions.assertThat;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.when;
39
40 @RunWith(MockitoJUnitRunner.class)
41 public class CopyRequirementsFromCharacteristicsToRulesTest extends AbstractDaoTestCase {
42
43   @ClassRule
44   public static TestDatabase db = new TestDatabase().schema(CopyRequirementsFromCharacteristicsToRulesTest.class, "schema.sql");
45
46   @Mock
47   ServerUpgradeStatus status;
48
49   @Mock
50   System2 system2;
51
52   @Mock
53   RuleRegistry ruleRegistry;
54
55   CopyRequirementsFromCharacteristicsToRules service;
56
57   @Before
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);
61   }
62
63   @Test
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");
67
68     when(status.isUpgraded()).thenReturn(true);
69     when(status.getInitialDbVersion()).thenReturn(498);
70
71     service.start();
72
73     db.assertDbUnit(getClass(), "copy_requirements_from_characteristics_to_rules_result.xml", "rules");
74     verify(ruleRegistry).reindex();
75   }
76
77   @Test
78   public void remove_requirements_data_from_characteristics() throws Exception {
79     db.prepareDbUnit(getClass(), "remove_requirements_data_from_characteristics.xml");
80
81     when(status.isUpgraded()).thenReturn(true);
82     when(status.getInitialDbVersion()).thenReturn(498);
83
84     service.start();
85
86     db.assertDbUnit(getClass(), "remove_requirements_data_from_characteristics_result.xml", "characteristics");
87     verify(ruleRegistry).reindex();
88   }
89
90   @Test
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");
96
97     assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(0.9, "h")).isEqualTo("1h");
98     assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(1.4, "h")).isEqualTo("1h");
99
100     assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(1.0, null)).isEqualTo("1d");
101     assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(null, "d")).isNull();
102
103     assertThat(CopyRequirementsFromCharacteristicsToRules.convertDuration(0.0, "d")).isNull();
104   }
105
106   @Test
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")
113     )).isTrue();
114
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")
120     )).isFalse();
121
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")
127     )).isFalse();
128
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)
134     )).isFalse();
135   }
136 }